@memberjunction/data-context-server 2.43.0 → 2.45.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +3 -3
- package/readme.md +231 -1
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@memberjunction/data-context-server",
|
|
3
|
-
"version": "2.
|
|
3
|
+
"version": "2.45.0",
|
|
4
4
|
"description": "This library provides a server-side implementation of the DataContextItem class from @memberjunction/data-context that can handle the server-side only use case of loading data into a context using raw SQL statements.",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"types": "dist/index.d.ts",
|
|
@@ -19,8 +19,8 @@
|
|
|
19
19
|
"typescript": "^5.4.5"
|
|
20
20
|
},
|
|
21
21
|
"dependencies": {
|
|
22
|
-
"@memberjunction/data-context": "2.
|
|
23
|
-
"@memberjunction/global": "2.
|
|
22
|
+
"@memberjunction/data-context": "2.45.0",
|
|
23
|
+
"@memberjunction/global": "2.45.0",
|
|
24
24
|
"typeorm": "^0.3.20"
|
|
25
25
|
}
|
|
26
26
|
}
|
package/readme.md
CHANGED
|
@@ -1,3 +1,233 @@
|
|
|
1
1
|
# @memberjunction/data-context-server
|
|
2
2
|
|
|
3
|
-
This library provides a server-side implementation of the DataContextItem class from `@memberjunction/data-context` that can handle the server-side only use case of loading data into a context using raw SQL statements.
|
|
3
|
+
This library provides a server-side implementation of the `DataContextItem` class from `@memberjunction/data-context` that can handle the server-side only use case of loading data into a context using raw SQL statements.
|
|
4
|
+
|
|
5
|
+
## Overview
|
|
6
|
+
|
|
7
|
+
The `@memberjunction/data-context-server` package extends the base `DataContextItem` class to provide server-side functionality for executing SQL queries directly against a database. This is particularly useful when you need to load data contexts that include custom SQL statements, which cannot be executed on the client side.
|
|
8
|
+
|
|
9
|
+
## Installation
|
|
10
|
+
|
|
11
|
+
```bash
|
|
12
|
+
npm install @memberjunction/data-context-server
|
|
13
|
+
```
|
|
14
|
+
|
|
15
|
+
## Purpose and Functionality
|
|
16
|
+
|
|
17
|
+
This package serves a critical role in the MemberJunction ecosystem by:
|
|
18
|
+
|
|
19
|
+
1. **Enabling SQL Execution**: Provides the ability to execute raw SQL statements through data contexts on the server side
|
|
20
|
+
2. **TypeORM Integration**: Uses TypeORM's DataSource for database operations
|
|
21
|
+
3. **Automatic Registration**: Registers itself with higher priority (2) to override the base implementation when running on the server
|
|
22
|
+
4. **Tree-Shaking Prevention**: Includes a utility function to ensure the class isn't removed during build optimization
|
|
23
|
+
|
|
24
|
+
## Usage
|
|
25
|
+
|
|
26
|
+
### Basic Setup
|
|
27
|
+
|
|
28
|
+
First, ensure the server-side implementation is loaded to prevent tree-shaking:
|
|
29
|
+
|
|
30
|
+
```typescript
|
|
31
|
+
import { LoadDataContextItemsServer } from '@memberjunction/data-context-server';
|
|
32
|
+
|
|
33
|
+
// Call this once in your server initialization code
|
|
34
|
+
LoadDataContextItemsServer();
|
|
35
|
+
```
|
|
36
|
+
|
|
37
|
+
### Loading Data Contexts with SQL Items
|
|
38
|
+
|
|
39
|
+
```typescript
|
|
40
|
+
import { DataContext } from '@memberjunction/data-context';
|
|
41
|
+
import { DataSource } from 'typeorm';
|
|
42
|
+
import { UserInfo } from '@memberjunction/core';
|
|
43
|
+
|
|
44
|
+
// Assume you have a TypeORM DataSource configured
|
|
45
|
+
const dataSource: DataSource = /* your configured data source */;
|
|
46
|
+
|
|
47
|
+
// Load a data context that includes SQL-type items
|
|
48
|
+
const context = new DataContext();
|
|
49
|
+
const user = /* current user context */;
|
|
50
|
+
|
|
51
|
+
// Load metadata and data in one operation
|
|
52
|
+
const success = await context.Load(
|
|
53
|
+
dataContextId,
|
|
54
|
+
dataSource, // Pass the TypeORM DataSource
|
|
55
|
+
false, // forceRefresh
|
|
56
|
+
false, // loadRelatedDataOnSingleRecords
|
|
57
|
+
0, // maxRecordsPerRelationship
|
|
58
|
+
user // contextUser
|
|
59
|
+
);
|
|
60
|
+
|
|
61
|
+
if (success) {
|
|
62
|
+
// Access the loaded data
|
|
63
|
+
context.Items.forEach(item => {
|
|
64
|
+
if (item.Type === 'sql' && item.DataLoaded) {
|
|
65
|
+
console.log(`SQL Item Data:`, item.Data);
|
|
66
|
+
}
|
|
67
|
+
});
|
|
68
|
+
}
|
|
69
|
+
```
|
|
70
|
+
|
|
71
|
+
### Creating SQL Data Context Items
|
|
72
|
+
|
|
73
|
+
```typescript
|
|
74
|
+
import { DataContext } from '@memberjunction/data-context';
|
|
75
|
+
|
|
76
|
+
const context = new DataContext();
|
|
77
|
+
const sqlItem = context.AddDataContextItem();
|
|
78
|
+
|
|
79
|
+
// Configure as SQL type
|
|
80
|
+
sqlItem.Type = 'sql';
|
|
81
|
+
sqlItem.SQL = 'SELECT * FROM Customers WHERE Country = @country';
|
|
82
|
+
sqlItem.RecordName = 'US Customers';
|
|
83
|
+
sqlItem.AdditionalDescription = 'All customers from the United States';
|
|
84
|
+
|
|
85
|
+
// Load the data
|
|
86
|
+
const dataSource = /* your TypeORM DataSource */;
|
|
87
|
+
const loaded = await sqlItem.LoadData(dataSource);
|
|
88
|
+
|
|
89
|
+
if (loaded) {
|
|
90
|
+
console.log('SQL Results:', sqlItem.Data);
|
|
91
|
+
} else {
|
|
92
|
+
console.error('Loading failed:', sqlItem.DataLoadingError);
|
|
93
|
+
}
|
|
94
|
+
```
|
|
95
|
+
|
|
96
|
+
## API Documentation
|
|
97
|
+
|
|
98
|
+
### DataContextItemServer Class
|
|
99
|
+
|
|
100
|
+
The `DataContextItemServer` class extends `DataContextItem` and overrides the `LoadFromSQL` method.
|
|
101
|
+
|
|
102
|
+
#### Protected Methods
|
|
103
|
+
|
|
104
|
+
##### `LoadFromSQL(dataSource: any, contextUser?: UserInfo): Promise<boolean>`
|
|
105
|
+
|
|
106
|
+
Executes a SQL statement and loads the results into the DataContextItem.
|
|
107
|
+
|
|
108
|
+
**Parameters:**
|
|
109
|
+
- `dataSource` (any): The TypeORM DataSource object used to execute queries
|
|
110
|
+
- `contextUser` (UserInfo, optional): The user context for the operation
|
|
111
|
+
|
|
112
|
+
**Returns:**
|
|
113
|
+
- `Promise<boolean>`: Returns `true` if successful, `false` if an error occurs
|
|
114
|
+
|
|
115
|
+
**Error Handling:**
|
|
116
|
+
- Catches and logs any SQL execution errors
|
|
117
|
+
- Sets the `DataLoadingError` property with error details
|
|
118
|
+
- Returns `false` on failure
|
|
119
|
+
|
|
120
|
+
### Utility Functions
|
|
121
|
+
|
|
122
|
+
#### `LoadDataContextItemsServer(): void`
|
|
123
|
+
|
|
124
|
+
Prevents tree-shaking from removing the `DataContextItemServer` class during build optimization.
|
|
125
|
+
|
|
126
|
+
**Usage:**
|
|
127
|
+
Call this function after importing the package in your server application to ensure the class registration takes effect.
|
|
128
|
+
|
|
129
|
+
## Integration with MemberJunction Packages
|
|
130
|
+
|
|
131
|
+
This package integrates seamlessly with:
|
|
132
|
+
|
|
133
|
+
- **@memberjunction/data-context**: Provides the base `DataContextItem` class and `DataContext` functionality
|
|
134
|
+
- **@memberjunction/global**: Uses the class registration system for runtime polymorphism
|
|
135
|
+
- **@memberjunction/core**: Leverages logging utilities and user context
|
|
136
|
+
- **typeorm**: Utilizes TypeORM's DataSource for database operations
|
|
137
|
+
|
|
138
|
+
## Dependencies
|
|
139
|
+
|
|
140
|
+
```json
|
|
141
|
+
{
|
|
142
|
+
"@memberjunction/data-context": "2.43.0",
|
|
143
|
+
"@memberjunction/global": "2.43.0",
|
|
144
|
+
"typeorm": "^0.3.20"
|
|
145
|
+
}
|
|
146
|
+
```
|
|
147
|
+
|
|
148
|
+
## Configuration
|
|
149
|
+
|
|
150
|
+
No special configuration is required. The package automatically registers itself with the MemberJunction class factory system when imported.
|
|
151
|
+
|
|
152
|
+
## Build and Development
|
|
153
|
+
|
|
154
|
+
### Scripts
|
|
155
|
+
|
|
156
|
+
- `npm run build`: Compiles TypeScript to JavaScript
|
|
157
|
+
- `npm start`: Runs the TypeScript code directly using ts-node-dev
|
|
158
|
+
|
|
159
|
+
### TypeScript Configuration
|
|
160
|
+
|
|
161
|
+
The package uses a standard TypeScript configuration that compiles to ES modules and includes type definitions.
|
|
162
|
+
|
|
163
|
+
## Important Notes
|
|
164
|
+
|
|
165
|
+
1. **Server-Side Only**: This package is designed for server-side use only. Client-side applications should not include this package.
|
|
166
|
+
|
|
167
|
+
2. **Class Registration Priority**: The `DataContextItemServer` class registers with priority 2, ensuring it overrides the base implementation when present.
|
|
168
|
+
|
|
169
|
+
3. **Error Handling**: SQL execution errors are caught and stored in the `DataLoadingError` property rather than throwing exceptions.
|
|
170
|
+
|
|
171
|
+
4. **Data Loading**: The `LoadFromSQL` method stores query results directly in the `Data` property as an array of objects.
|
|
172
|
+
|
|
173
|
+
## Example: Complete Server Application
|
|
174
|
+
|
|
175
|
+
```typescript
|
|
176
|
+
import { DataContext } from '@memberjunction/data-context';
|
|
177
|
+
import { LoadDataContextItemsServer } from '@memberjunction/data-context-server';
|
|
178
|
+
import { createConnection, DataSource } from 'typeorm';
|
|
179
|
+
import { Metadata } from '@memberjunction/core';
|
|
180
|
+
|
|
181
|
+
// Initialize the server-side data context support
|
|
182
|
+
LoadDataContextItemsServer();
|
|
183
|
+
|
|
184
|
+
// Configure your database connection
|
|
185
|
+
const dataSource = new DataSource({
|
|
186
|
+
type: 'mssql',
|
|
187
|
+
host: 'localhost',
|
|
188
|
+
username: 'your_username',
|
|
189
|
+
password: 'your_password',
|
|
190
|
+
database: 'your_database',
|
|
191
|
+
// ... other TypeORM configuration
|
|
192
|
+
});
|
|
193
|
+
|
|
194
|
+
async function loadDataContextWithSQL() {
|
|
195
|
+
await dataSource.initialize();
|
|
196
|
+
|
|
197
|
+
const context = new DataContext();
|
|
198
|
+
const user = await Metadata.Provider.GetCurrentUser();
|
|
199
|
+
|
|
200
|
+
// Create a SQL-based data context item
|
|
201
|
+
const item = context.AddDataContextItem();
|
|
202
|
+
item.Type = 'sql';
|
|
203
|
+
item.SQL = `
|
|
204
|
+
SELECT
|
|
205
|
+
c.CustomerID,
|
|
206
|
+
c.CompanyName,
|
|
207
|
+
COUNT(o.OrderID) as OrderCount,
|
|
208
|
+
SUM(od.Quantity * od.UnitPrice) as TotalRevenue
|
|
209
|
+
FROM Customers c
|
|
210
|
+
LEFT JOIN Orders o ON c.CustomerID = o.CustomerID
|
|
211
|
+
LEFT JOIN [Order Details] od ON o.OrderID = od.OrderID
|
|
212
|
+
GROUP BY c.CustomerID, c.CompanyName
|
|
213
|
+
ORDER BY TotalRevenue DESC
|
|
214
|
+
`;
|
|
215
|
+
item.RecordName = 'Customer Revenue Summary';
|
|
216
|
+
|
|
217
|
+
// Load the data
|
|
218
|
+
const success = await item.LoadData(dataSource, false, false, 0, user);
|
|
219
|
+
|
|
220
|
+
if (success) {
|
|
221
|
+
console.log('Customer revenue data:', item.Data);
|
|
222
|
+
|
|
223
|
+
// Save the context if needed
|
|
224
|
+
await context.SaveItems(user, true); // true to persist the data
|
|
225
|
+
}
|
|
226
|
+
|
|
227
|
+
await dataSource.destroy();
|
|
228
|
+
}
|
|
229
|
+
```
|
|
230
|
+
|
|
231
|
+
## License
|
|
232
|
+
|
|
233
|
+
This package is part of the MemberJunction framework and follows the same licensing terms.
|