@nattyjs/orm 0.0.1-beta.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/README.md +11 -0
- package/dist/index.cjs +654 -0
- package/dist/index.d.ts +122 -0
- package/dist/index.mjs +636 -0
- package/package.json +26 -0
package/README.md
ADDED
package/dist/index.cjs
ADDED
|
@@ -0,0 +1,654 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
const mssql = require('mssql');
|
|
4
|
+
const common = require('@nattyjs/common');
|
|
5
|
+
const core = require('@nattyjs/core');
|
|
6
|
+
|
|
7
|
+
function _interopNamespaceCompat(e) {
|
|
8
|
+
if (e && typeof e === 'object' && 'default' in e) return e;
|
|
9
|
+
const n = Object.create(null);
|
|
10
|
+
if (e) {
|
|
11
|
+
for (const k in e) {
|
|
12
|
+
n[k] = e[k];
|
|
13
|
+
}
|
|
14
|
+
}
|
|
15
|
+
n.default = e;
|
|
16
|
+
return n;
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
const mssql__namespace = /*#__PURE__*/_interopNamespaceCompat(mssql);
|
|
20
|
+
|
|
21
|
+
var EntityState = /* @__PURE__ */ ((EntityState2) => {
|
|
22
|
+
EntityState2[EntityState2["added"] = 0] = "added";
|
|
23
|
+
EntityState2[EntityState2["updated"] = 1] = "updated";
|
|
24
|
+
EntityState2[EntityState2["deleted"] = 2] = "deleted";
|
|
25
|
+
return EntityState2;
|
|
26
|
+
})(EntityState || {});
|
|
27
|
+
|
|
28
|
+
class ChangeTracker {
|
|
29
|
+
constructor() {
|
|
30
|
+
this.entries = /* @__PURE__ */ new Map();
|
|
31
|
+
}
|
|
32
|
+
get addedEntries() {
|
|
33
|
+
return this.entries.get(EntityState.added) || [];
|
|
34
|
+
}
|
|
35
|
+
get updatedEntries() {
|
|
36
|
+
return this.entries.get(EntityState.updated) || [];
|
|
37
|
+
}
|
|
38
|
+
get deletedEntries() {
|
|
39
|
+
return this.entries.get(EntityState.deleted) || [];
|
|
40
|
+
}
|
|
41
|
+
clear() {
|
|
42
|
+
this.entries.clear();
|
|
43
|
+
}
|
|
44
|
+
addItem(entity, entityState) {
|
|
45
|
+
let arraySet = this.entries.get(entityState);
|
|
46
|
+
if (!arraySet)
|
|
47
|
+
arraySet = new Array();
|
|
48
|
+
arraySet.push(entity);
|
|
49
|
+
this.entries.set(entityState, arraySet);
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
var QueryAction = /* @__PURE__ */ ((QueryAction2) => {
|
|
54
|
+
QueryAction2[QueryAction2["select"] = 0] = "select";
|
|
55
|
+
QueryAction2[QueryAction2["add"] = 1] = "add";
|
|
56
|
+
QueryAction2[QueryAction2["update"] = 2] = "update";
|
|
57
|
+
QueryAction2[QueryAction2["delete"] = 3] = "delete";
|
|
58
|
+
return QueryAction2;
|
|
59
|
+
})(QueryAction || {});
|
|
60
|
+
|
|
61
|
+
function getComparisonOperator(operator) {
|
|
62
|
+
let operatorText = "";
|
|
63
|
+
switch (operator) {
|
|
64
|
+
case "==":
|
|
65
|
+
operatorText = "=";
|
|
66
|
+
break;
|
|
67
|
+
default:
|
|
68
|
+
operatorText = operator;
|
|
69
|
+
break;
|
|
70
|
+
}
|
|
71
|
+
return operatorText;
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
function getLogicalOperator(operator) {
|
|
75
|
+
let operatorText = "";
|
|
76
|
+
switch (operator) {
|
|
77
|
+
case "&&":
|
|
78
|
+
operatorText = "AND";
|
|
79
|
+
break;
|
|
80
|
+
case "||":
|
|
81
|
+
operatorText = "OR";
|
|
82
|
+
break;
|
|
83
|
+
}
|
|
84
|
+
return operatorText;
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
function getDeleteQueryText(config) {
|
|
88
|
+
const tableName = config.tableName;
|
|
89
|
+
const params = new Array();
|
|
90
|
+
const whereClause = getWhereClause$1(config.whereClause, params);
|
|
91
|
+
return {
|
|
92
|
+
query: `delete from ${tableName} where ${whereClause}`,
|
|
93
|
+
parameters: params
|
|
94
|
+
};
|
|
95
|
+
}
|
|
96
|
+
function getWhereClause$1(whereClause, params) {
|
|
97
|
+
let sqlQuery = "";
|
|
98
|
+
const paramKeys = {};
|
|
99
|
+
let increment = 1;
|
|
100
|
+
for (var i = 0; i < whereClause.length; i++) {
|
|
101
|
+
const expression = whereClause[i];
|
|
102
|
+
let paramKey = expression[0];
|
|
103
|
+
if (paramKeys[paramKey])
|
|
104
|
+
paramKey = `${paramKey}${increment}`;
|
|
105
|
+
paramKeys[paramKey] = paramKey;
|
|
106
|
+
sqlQuery += `${expression[0]} ${getComparisonOperator(expression[1])} @${paramKey} `;
|
|
107
|
+
params.push({
|
|
108
|
+
name: paramKey,
|
|
109
|
+
value: expression[2]
|
|
110
|
+
});
|
|
111
|
+
i = i + 1;
|
|
112
|
+
if (whereClause[i]) {
|
|
113
|
+
sqlQuery += ` ${getLogicalOperator(whereClause[i])} `;
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
return sqlQuery;
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
class Utils {
|
|
120
|
+
static isObject(value) {
|
|
121
|
+
return Object.prototype.toString.call(value) === "[object Object]";
|
|
122
|
+
}
|
|
123
|
+
static isArray(value) {
|
|
124
|
+
return Array.isArray(value);
|
|
125
|
+
}
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
function getInsertQueryText(config) {
|
|
129
|
+
const tableName = config.tableName;
|
|
130
|
+
const queryInfo = getColumnNameAndParams$1(config);
|
|
131
|
+
const columnNames = queryInfo.columnNames.join(",");
|
|
132
|
+
const parameters = queryInfo.columnNames.map((name) => `@${name}`).join(",");
|
|
133
|
+
const primaryKey = config.primaryKeys[0];
|
|
134
|
+
let output = "";
|
|
135
|
+
if (primaryKey)
|
|
136
|
+
output = `OUTPUT INSERTED.${primaryKey}`;
|
|
137
|
+
return {
|
|
138
|
+
query: `insert into ${tableName} (${columnNames}) ${output} values (${parameters})`,
|
|
139
|
+
parameters: queryInfo.params,
|
|
140
|
+
relationProps: queryInfo.relationProps
|
|
141
|
+
};
|
|
142
|
+
}
|
|
143
|
+
function getColumnNameAndParams$1(config) {
|
|
144
|
+
const columnNames = [];
|
|
145
|
+
const params = new Array();
|
|
146
|
+
const relationProps = /* @__PURE__ */ new Map();
|
|
147
|
+
if (config.entity) {
|
|
148
|
+
for (const key of Object.keys(config.entity)) {
|
|
149
|
+
if (config.properties[key.toLowerCase()]) {
|
|
150
|
+
const paramValue = config.entity[key];
|
|
151
|
+
if (!(Utils.isObject(paramValue) || Utils.isArray(paramValue))) {
|
|
152
|
+
columnNames.push(key);
|
|
153
|
+
params.push({
|
|
154
|
+
name: `${key}`,
|
|
155
|
+
value: paramValue
|
|
156
|
+
});
|
|
157
|
+
} else {
|
|
158
|
+
relationProps.set(key, paramValue);
|
|
159
|
+
}
|
|
160
|
+
}
|
|
161
|
+
}
|
|
162
|
+
}
|
|
163
|
+
return { columnNames, params, relationProps };
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
function getSelectQueryText(config) {
|
|
167
|
+
const tableName = config.tableName;
|
|
168
|
+
const columnNames = config.columns.length == 0 ? "*" : config.columns.join(",");
|
|
169
|
+
const params = new Array();
|
|
170
|
+
const whereClause = getWhereClause(config.whereClause, params);
|
|
171
|
+
return {
|
|
172
|
+
query: `select ${columnNames} from ${tableName} ${whereClause ? `where ${whereClause}` : ""}`,
|
|
173
|
+
parameters: params
|
|
174
|
+
};
|
|
175
|
+
}
|
|
176
|
+
function getWhereClause(whereClause, params) {
|
|
177
|
+
let sqlQuery = "";
|
|
178
|
+
const paramKeys = {};
|
|
179
|
+
let increment = 1;
|
|
180
|
+
for (var i = 0; i < whereClause.length; i++) {
|
|
181
|
+
const expression = whereClause[i];
|
|
182
|
+
let paramKey = expression[0];
|
|
183
|
+
if (paramKeys[paramKey])
|
|
184
|
+
paramKey = `${paramKey}${increment}`;
|
|
185
|
+
paramKeys[paramKey] = paramKey;
|
|
186
|
+
sqlQuery += `${expression[0]} ${getComparisonOperator(expression[1])} @${paramKey} `;
|
|
187
|
+
params.push({
|
|
188
|
+
name: paramKey,
|
|
189
|
+
value: expression[2]
|
|
190
|
+
});
|
|
191
|
+
i = i + 1;
|
|
192
|
+
if (whereClause[i]) {
|
|
193
|
+
sqlQuery += ` ${getLogicalOperator(whereClause[i])} `;
|
|
194
|
+
}
|
|
195
|
+
}
|
|
196
|
+
return sqlQuery;
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
function getUpdateQueryText(config) {
|
|
200
|
+
const tableName = config.tableName;
|
|
201
|
+
const queryInfo = getColumnNameAndParams(config);
|
|
202
|
+
const sets = queryInfo.sets.join(",");
|
|
203
|
+
return {
|
|
204
|
+
query: `update ${tableName} set ${sets} where ${queryInfo.whereClause.join(` AND `)}`,
|
|
205
|
+
parameters: queryInfo.params
|
|
206
|
+
};
|
|
207
|
+
}
|
|
208
|
+
function getColumnNameAndParams(config) {
|
|
209
|
+
const sets = [];
|
|
210
|
+
const whereClause = [];
|
|
211
|
+
const params = new Array();
|
|
212
|
+
if (config.entity) {
|
|
213
|
+
for (const [key, value] of Object.entries(config.entity)) {
|
|
214
|
+
if (!(Utils.isObject(value) || Utils.isArray(value))) {
|
|
215
|
+
if (config.primaryKeys.indexOf(key) === -1)
|
|
216
|
+
sets.push(`${key}=@${key}`);
|
|
217
|
+
else
|
|
218
|
+
whereClause.push(`${key}=@${key}`);
|
|
219
|
+
params.push({
|
|
220
|
+
name: `${key}`,
|
|
221
|
+
value
|
|
222
|
+
});
|
|
223
|
+
}
|
|
224
|
+
}
|
|
225
|
+
}
|
|
226
|
+
return { sets, params, whereClause };
|
|
227
|
+
}
|
|
228
|
+
|
|
229
|
+
function getSqlTextAndParams(config) {
|
|
230
|
+
let queryInfo = void 0;
|
|
231
|
+
switch (config.queryAction) {
|
|
232
|
+
case QueryAction.select:
|
|
233
|
+
queryInfo = getSelectQueryText(config);
|
|
234
|
+
break;
|
|
235
|
+
case QueryAction.add:
|
|
236
|
+
queryInfo = getInsertQueryText(config);
|
|
237
|
+
break;
|
|
238
|
+
case QueryAction.update:
|
|
239
|
+
queryInfo = getUpdateQueryText(config);
|
|
240
|
+
break;
|
|
241
|
+
case QueryAction.delete:
|
|
242
|
+
queryInfo = getDeleteQueryText(config);
|
|
243
|
+
break;
|
|
244
|
+
}
|
|
245
|
+
return queryInfo;
|
|
246
|
+
}
|
|
247
|
+
|
|
248
|
+
class DbConnection {
|
|
249
|
+
constructor(config) {
|
|
250
|
+
this.config = config;
|
|
251
|
+
this.mssql = mssql__namespace;
|
|
252
|
+
}
|
|
253
|
+
}
|
|
254
|
+
|
|
255
|
+
class SqlDbTransaction {
|
|
256
|
+
constructor() {
|
|
257
|
+
this.isSelfTransaction = true;
|
|
258
|
+
}
|
|
259
|
+
useTransaction(connection) {
|
|
260
|
+
this.isSelfTransaction = false;
|
|
261
|
+
this.transactionConnection = connection;
|
|
262
|
+
}
|
|
263
|
+
async begin(pool, isolationLevel, dbContexts) {
|
|
264
|
+
return await new Promise(async (resolve, error) => {
|
|
265
|
+
const transactionCallback = (err) => {
|
|
266
|
+
if (err) {
|
|
267
|
+
return error(err);
|
|
268
|
+
}
|
|
269
|
+
resolve();
|
|
270
|
+
};
|
|
271
|
+
if (!this.transactionConnection) {
|
|
272
|
+
this.transactionConnection = pool.transaction();
|
|
273
|
+
if (isolationLevel) {
|
|
274
|
+
this.transactionConnection.begin(
|
|
275
|
+
this.convertToIsolationLevel(isolationLevel),
|
|
276
|
+
transactionCallback
|
|
277
|
+
);
|
|
278
|
+
} else {
|
|
279
|
+
this.transactionConnection.begin(transactionCallback);
|
|
280
|
+
}
|
|
281
|
+
} else {
|
|
282
|
+
resolve();
|
|
283
|
+
}
|
|
284
|
+
this.assignTransaction(dbContexts);
|
|
285
|
+
});
|
|
286
|
+
}
|
|
287
|
+
assignTransaction(dbContexts) {
|
|
288
|
+
if (dbContexts)
|
|
289
|
+
for (const dbContext of dbContexts)
|
|
290
|
+
dbContext.connection.useTransaction(this.transactionConnection);
|
|
291
|
+
this.dbContexts = dbContexts;
|
|
292
|
+
}
|
|
293
|
+
async commit() {
|
|
294
|
+
return new Promise((resolve, error) => {
|
|
295
|
+
if (this.transactionConnection && this.isSelfTransaction) {
|
|
296
|
+
this.transactionConnection.commit(async (err) => {
|
|
297
|
+
if (err)
|
|
298
|
+
return error(err);
|
|
299
|
+
this.transactionConnection = null;
|
|
300
|
+
this.transactionRelease();
|
|
301
|
+
resolve();
|
|
302
|
+
});
|
|
303
|
+
} else {
|
|
304
|
+
resolve();
|
|
305
|
+
}
|
|
306
|
+
});
|
|
307
|
+
}
|
|
308
|
+
transactionRelease() {
|
|
309
|
+
if (this.dbContexts)
|
|
310
|
+
for (const dbContext of this.dbContexts)
|
|
311
|
+
dbContext.connection.releaseTransaction();
|
|
312
|
+
this.dbContexts = null;
|
|
313
|
+
}
|
|
314
|
+
releaseTransaction() {
|
|
315
|
+
this.transactionConnection = null;
|
|
316
|
+
this.isSelfTransaction = true;
|
|
317
|
+
}
|
|
318
|
+
rollback() {
|
|
319
|
+
if (this.transactionConnection && this.isSelfTransaction) {
|
|
320
|
+
return new Promise((resolve, error) => {
|
|
321
|
+
this.transactionConnection.rollback(async (err) => {
|
|
322
|
+
if (err)
|
|
323
|
+
return error(err);
|
|
324
|
+
this.transactionConnection = null;
|
|
325
|
+
this.transactionRelease();
|
|
326
|
+
resolve();
|
|
327
|
+
});
|
|
328
|
+
});
|
|
329
|
+
}
|
|
330
|
+
}
|
|
331
|
+
convertToIsolationLevel(isolation) {
|
|
332
|
+
const ISOLATION_LEVEL = mssql__namespace.ISOLATION_LEVEL;
|
|
333
|
+
switch (isolation) {
|
|
334
|
+
case "READ UNCOMMITTED":
|
|
335
|
+
return ISOLATION_LEVEL.READ_UNCOMMITTED;
|
|
336
|
+
case "REPEATABLE READ":
|
|
337
|
+
return ISOLATION_LEVEL.REPEATABLE_READ;
|
|
338
|
+
case "SERIALIZABLE":
|
|
339
|
+
return ISOLATION_LEVEL.SERIALIZABLE;
|
|
340
|
+
case "READ COMMITTED":
|
|
341
|
+
default:
|
|
342
|
+
return ISOLATION_LEVEL.READ_COMMITTED;
|
|
343
|
+
}
|
|
344
|
+
}
|
|
345
|
+
}
|
|
346
|
+
|
|
347
|
+
class SqlConnection extends DbConnection {
|
|
348
|
+
constructor(config) {
|
|
349
|
+
super(config);
|
|
350
|
+
this.baseTransaction = new SqlDbTransaction();
|
|
351
|
+
}
|
|
352
|
+
get transaction() {
|
|
353
|
+
return this.baseTransaction;
|
|
354
|
+
}
|
|
355
|
+
useTransaction(connection) {
|
|
356
|
+
this.baseTransaction.useTransaction(connection);
|
|
357
|
+
}
|
|
358
|
+
useConnection(connection) {
|
|
359
|
+
this.connection = connection;
|
|
360
|
+
}
|
|
361
|
+
async executeQuery(query) {
|
|
362
|
+
try {
|
|
363
|
+
const sqlQueryText = getSqlTextAndParams(query);
|
|
364
|
+
const result = await this.query(sqlQueryText);
|
|
365
|
+
let records = new Array();
|
|
366
|
+
const outputInfo = this.getOutput(result, query);
|
|
367
|
+
await this.runNestedObjectQueries(outputInfo, sqlQueryText.relationProps, query);
|
|
368
|
+
if (result["recordset"]) {
|
|
369
|
+
records = result.recordset;
|
|
370
|
+
}
|
|
371
|
+
return records;
|
|
372
|
+
} catch (ex) {
|
|
373
|
+
}
|
|
374
|
+
}
|
|
375
|
+
async runNestedObjectQueries(outputInfo, relationProps, config) {
|
|
376
|
+
if (outputInfo) {
|
|
377
|
+
switch (config.queryAction) {
|
|
378
|
+
case QueryAction.add:
|
|
379
|
+
for (const propName of relationProps.keys()) {
|
|
380
|
+
const entries = relationProps.get(propName);
|
|
381
|
+
const queryConfig = config.propsDbConfig.get(propName);
|
|
382
|
+
if (Array.isArray(entries) && queryConfig) {
|
|
383
|
+
for (const entity of entries) {
|
|
384
|
+
if (!entity[queryConfig.foreignKey])
|
|
385
|
+
entity[queryConfig.foreignKey] = outputInfo.value;
|
|
386
|
+
await this.runQuery([{ ...queryConfig, entity }], config.queryAction);
|
|
387
|
+
}
|
|
388
|
+
}
|
|
389
|
+
}
|
|
390
|
+
break;
|
|
391
|
+
}
|
|
392
|
+
}
|
|
393
|
+
}
|
|
394
|
+
async executeSp(spName, params) {
|
|
395
|
+
let records = new Array();
|
|
396
|
+
if (common.isObject(params)) {
|
|
397
|
+
const queryInfo = { parameters: [] };
|
|
398
|
+
for (const [key, value] of Object.entries(params))
|
|
399
|
+
queryInfo.parameters.push({ name: key, value });
|
|
400
|
+
queryInfo.sp = spName;
|
|
401
|
+
const result = await this.query(queryInfo);
|
|
402
|
+
if (result["recordset"]) {
|
|
403
|
+
records = result.recordset;
|
|
404
|
+
}
|
|
405
|
+
return records;
|
|
406
|
+
}
|
|
407
|
+
return records;
|
|
408
|
+
}
|
|
409
|
+
getOutput(result, config) {
|
|
410
|
+
switch (config.queryAction) {
|
|
411
|
+
case QueryAction.add:
|
|
412
|
+
const primaryKey = config.primaryKeys[0];
|
|
413
|
+
const output = result.recordset[0][primaryKey];
|
|
414
|
+
if (output)
|
|
415
|
+
config.entity[primaryKey] = output;
|
|
416
|
+
return { key: primaryKey, value: output };
|
|
417
|
+
}
|
|
418
|
+
}
|
|
419
|
+
async addEntries(dbEntities) {
|
|
420
|
+
await this.runQuery(dbEntities, QueryAction.add);
|
|
421
|
+
}
|
|
422
|
+
async updateEntries(dbEntities) {
|
|
423
|
+
await this.runQuery(dbEntities, QueryAction.update);
|
|
424
|
+
}
|
|
425
|
+
async deleteEntries(dbEntities) {
|
|
426
|
+
await this.runQuery(dbEntities, QueryAction.delete);
|
|
427
|
+
}
|
|
428
|
+
async runQuery(dbEntities, queryAction) {
|
|
429
|
+
for (const dbEntity of dbEntities) {
|
|
430
|
+
const executeQuery = {
|
|
431
|
+
...dbEntity,
|
|
432
|
+
queryAction
|
|
433
|
+
};
|
|
434
|
+
await this.executeQuery(executeQuery);
|
|
435
|
+
}
|
|
436
|
+
}
|
|
437
|
+
async query(queryInfo) {
|
|
438
|
+
const pool = await this.getConnectionPool();
|
|
439
|
+
const request = new this.mssql.Request(pool);
|
|
440
|
+
for (const param of queryInfo.parameters) {
|
|
441
|
+
if (param.type !== void 0)
|
|
442
|
+
request.input(param.name, param.type, param.value);
|
|
443
|
+
else
|
|
444
|
+
request.input(param.name, param.value);
|
|
445
|
+
}
|
|
446
|
+
const resolveQuery = (resolve) => (error, raw) => {
|
|
447
|
+
if (error)
|
|
448
|
+
throw error;
|
|
449
|
+
resolve(raw);
|
|
450
|
+
};
|
|
451
|
+
if (queryInfo.query) {
|
|
452
|
+
const raw = await new Promise((resolve, error) => {
|
|
453
|
+
request.query(queryInfo.query, resolveQuery(resolve));
|
|
454
|
+
});
|
|
455
|
+
return raw;
|
|
456
|
+
} else if (queryInfo.sp) {
|
|
457
|
+
const raw = await new Promise((resolve, error) => {
|
|
458
|
+
request.execute(queryInfo.sp, resolveQuery(resolve));
|
|
459
|
+
});
|
|
460
|
+
return raw;
|
|
461
|
+
}
|
|
462
|
+
}
|
|
463
|
+
async beginTransaction(isolationLevel, dbContexts) {
|
|
464
|
+
const connection = await this.getConnectionPool();
|
|
465
|
+
await this.baseTransaction.begin(connection, isolationLevel, dbContexts);
|
|
466
|
+
}
|
|
467
|
+
async commitTransaction() {
|
|
468
|
+
await this.baseTransaction.commit();
|
|
469
|
+
}
|
|
470
|
+
async rollbackTransaction() {
|
|
471
|
+
await this.baseTransaction.rollback();
|
|
472
|
+
}
|
|
473
|
+
releaseTransaction() {
|
|
474
|
+
this.baseTransaction.releaseTransaction();
|
|
475
|
+
}
|
|
476
|
+
async getConnectionPool() {
|
|
477
|
+
return this.createPool();
|
|
478
|
+
}
|
|
479
|
+
async createPool() {
|
|
480
|
+
return await new Promise((resolve, error) => {
|
|
481
|
+
if (this.baseTransaction.transactionConnection)
|
|
482
|
+
return resolve(this.baseTransaction.transactionConnection);
|
|
483
|
+
const pool = new this.mssql.ConnectionPool(this.config);
|
|
484
|
+
const errorHandler = (error2) => {
|
|
485
|
+
};
|
|
486
|
+
pool.on("error", errorHandler);
|
|
487
|
+
const connection = pool.connect((err) => {
|
|
488
|
+
if (err)
|
|
489
|
+
return error(err);
|
|
490
|
+
resolve(connection);
|
|
491
|
+
});
|
|
492
|
+
});
|
|
493
|
+
}
|
|
494
|
+
}
|
|
495
|
+
|
|
496
|
+
class DbConnectionContext {
|
|
497
|
+
useSqlServer(options) {
|
|
498
|
+
this.connection = new SqlConnection(options);
|
|
499
|
+
}
|
|
500
|
+
}
|
|
501
|
+
|
|
502
|
+
class DbSet {
|
|
503
|
+
constructor(type, queryConnection, changeTracker) {
|
|
504
|
+
this.type = type;
|
|
505
|
+
this.queryConnection = queryConnection;
|
|
506
|
+
this.changeTracker = changeTracker;
|
|
507
|
+
this.columns = new Array();
|
|
508
|
+
this.whereClause = [];
|
|
509
|
+
}
|
|
510
|
+
filter(expression) {
|
|
511
|
+
this.whereClause = expression;
|
|
512
|
+
return this;
|
|
513
|
+
}
|
|
514
|
+
async firstOrDefault(expression) {
|
|
515
|
+
this.whereClause = expression;
|
|
516
|
+
return (await this.execute()).firstOrDefault();
|
|
517
|
+
}
|
|
518
|
+
async add(entity) {
|
|
519
|
+
const properties = this.getProperties();
|
|
520
|
+
this.changeTracker.addItem({
|
|
521
|
+
tableName: this.getTableName(this.type.name),
|
|
522
|
+
entity,
|
|
523
|
+
primaryKeys: this.getPrimaryKeys(this.type.name),
|
|
524
|
+
propsDbConfig: this.getPropsDbConfig(entity),
|
|
525
|
+
properties
|
|
526
|
+
}, EntityState.added);
|
|
527
|
+
}
|
|
528
|
+
async update(entity) {
|
|
529
|
+
this.changeTracker.addItem({
|
|
530
|
+
tableName: this.getTableName(this.type.name),
|
|
531
|
+
entity,
|
|
532
|
+
primaryKeys: this.getPrimaryKeys(this.type.name),
|
|
533
|
+
propsDbConfig: this.getPropsDbConfig(entity)
|
|
534
|
+
}, EntityState.updated);
|
|
535
|
+
}
|
|
536
|
+
async delete(expression) {
|
|
537
|
+
this.changeTracker.addItem({
|
|
538
|
+
tableName: this.getTableName(this.type.name),
|
|
539
|
+
whereClause: expression
|
|
540
|
+
}, EntityState.deleted);
|
|
541
|
+
}
|
|
542
|
+
async toList() {
|
|
543
|
+
return await this.execute();
|
|
544
|
+
}
|
|
545
|
+
async execute() {
|
|
546
|
+
const items = await this.queryConnection.executeQuery({
|
|
547
|
+
queryAction: QueryAction.select,
|
|
548
|
+
tableName: this.getTableName(this.type.name),
|
|
549
|
+
columns: this.columns,
|
|
550
|
+
whereClause: this.whereClause
|
|
551
|
+
});
|
|
552
|
+
return new common.List(items, this.type, this.getProperties());
|
|
553
|
+
}
|
|
554
|
+
getTableName(name) {
|
|
555
|
+
const modelConfig = core.entityContainer.getModelConfig(name);
|
|
556
|
+
return modelConfig ? modelConfig.tableName || this.type.name : this.type.name;
|
|
557
|
+
}
|
|
558
|
+
getProperties(name) {
|
|
559
|
+
const props = core.entityContainer.getProperties(name || this.type.name);
|
|
560
|
+
const jProps = {};
|
|
561
|
+
if (props)
|
|
562
|
+
props.forEach((property) => jProps[property.name.toLowerCase()] = property.name);
|
|
563
|
+
return jProps;
|
|
564
|
+
}
|
|
565
|
+
getPropsDbConfig(entity) {
|
|
566
|
+
const propsDbConfig = core.entityContainer.getPropsDbConfig(this.type.name);
|
|
567
|
+
const props = /* @__PURE__ */ new Map();
|
|
568
|
+
for (const propName of Object.keys(entity)) {
|
|
569
|
+
const dbFieldConfig = propsDbConfig.get(propName);
|
|
570
|
+
if (dbFieldConfig && (dbFieldConfig.oneToMany || dbFieldConfig.oneToOne)) {
|
|
571
|
+
const fieldRelationConfig = dbFieldConfig.oneToMany || dbFieldConfig.oneToOne;
|
|
572
|
+
props.set(propName, {
|
|
573
|
+
tableName: this.getTableName(fieldRelationConfig.model.name),
|
|
574
|
+
primaryKeys: this.getPrimaryKeys(fieldRelationConfig.model.name),
|
|
575
|
+
foreignKey: fieldRelationConfig.foreignKey,
|
|
576
|
+
properties: this.getProperties(fieldRelationConfig.model.name)
|
|
577
|
+
});
|
|
578
|
+
}
|
|
579
|
+
}
|
|
580
|
+
return props;
|
|
581
|
+
}
|
|
582
|
+
getPrimaryKeys(name) {
|
|
583
|
+
return core.entityContainer.getPrimaryKeys(name);
|
|
584
|
+
}
|
|
585
|
+
}
|
|
586
|
+
|
|
587
|
+
class StoreProc {
|
|
588
|
+
constructor(spName, type, queryConnection) {
|
|
589
|
+
this.spName = spName;
|
|
590
|
+
this.type = type;
|
|
591
|
+
this.queryConnection = queryConnection;
|
|
592
|
+
}
|
|
593
|
+
async execute(params) {
|
|
594
|
+
const items = await this.queryConnection.executeSp(this.spName, params);
|
|
595
|
+
return new common.List(items, this.type, this.properties);
|
|
596
|
+
}
|
|
597
|
+
get properties() {
|
|
598
|
+
const props = core.entityContainer.getProperties(this.type.name);
|
|
599
|
+
const jProps = {};
|
|
600
|
+
if (props)
|
|
601
|
+
props.forEach((property) => jProps[property.name.toLowerCase()] = property.name);
|
|
602
|
+
return jProps;
|
|
603
|
+
}
|
|
604
|
+
}
|
|
605
|
+
|
|
606
|
+
class DbContext {
|
|
607
|
+
get connection() {
|
|
608
|
+
return this.dbConnectionInfo.connection;
|
|
609
|
+
}
|
|
610
|
+
get internalConnection() {
|
|
611
|
+
return this.dbConnectionInfo.connection;
|
|
612
|
+
}
|
|
613
|
+
get queryConnection() {
|
|
614
|
+
return this.dbConnectionInfo.connection;
|
|
615
|
+
}
|
|
616
|
+
constructor() {
|
|
617
|
+
this.dbConnectionInfo = new DbConnectionContext();
|
|
618
|
+
this.changeTracker = new ChangeTracker();
|
|
619
|
+
this.onConfiguring(this.dbConnectionInfo);
|
|
620
|
+
this.onDbSetRegistering();
|
|
621
|
+
this.onSpRegistering();
|
|
622
|
+
}
|
|
623
|
+
onDbSetRegistering(dbSets) {
|
|
624
|
+
if (dbSets)
|
|
625
|
+
for (const [key, value] of Object.entries(dbSets))
|
|
626
|
+
this[key] = new DbSet(value(), this.queryConnection, this.changeTracker);
|
|
627
|
+
}
|
|
628
|
+
onSpRegistering(sps) {
|
|
629
|
+
if (sps)
|
|
630
|
+
for (const [key, value] of Object.entries(sps))
|
|
631
|
+
this[key] = new StoreProc(value.spName, value.model(), this.queryConnection);
|
|
632
|
+
}
|
|
633
|
+
async saveChanges() {
|
|
634
|
+
return new Promise(async (resolve, error) => {
|
|
635
|
+
try {
|
|
636
|
+
await this.internalConnection.beginTransaction();
|
|
637
|
+
await this.internalConnection.addEntries(this.changeTracker.addedEntries);
|
|
638
|
+
await this.internalConnection.updateEntries(this.changeTracker.updatedEntries);
|
|
639
|
+
await this.internalConnection.deleteEntries(this.changeTracker.deletedEntries);
|
|
640
|
+
await this.internalConnection.commitTransaction();
|
|
641
|
+
this.changeTracker.clear();
|
|
642
|
+
resolve(void 0);
|
|
643
|
+
} catch (ex) {
|
|
644
|
+
this.changeTracker.clear();
|
|
645
|
+
await this.internalConnection.rollbackTransaction();
|
|
646
|
+
error(ex);
|
|
647
|
+
}
|
|
648
|
+
});
|
|
649
|
+
}
|
|
650
|
+
}
|
|
651
|
+
|
|
652
|
+
exports.DbContext = DbContext;
|
|
653
|
+
exports.DbSet = DbSet;
|
|
654
|
+
exports.StoreProc = StoreProc;
|