@nattyjs/orm 0.0.1-beta.5 → 0.0.1-beta.51
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/dist/index.cjs +443 -70
- package/dist/index.d.ts +26 -1
- package/dist/index.mjs +441 -71
- package/package.json +6 -5
package/dist/index.cjs
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
|
|
3
3
|
const mssql = require('mssql');
|
|
4
|
+
const pg = require('pg');
|
|
4
5
|
const common = require('@nattyjs/common');
|
|
5
6
|
const core = require('@nattyjs/core');
|
|
6
7
|
|
|
@@ -17,6 +18,7 @@ function _interopNamespaceCompat(e) {
|
|
|
17
18
|
}
|
|
18
19
|
|
|
19
20
|
const mssql__namespace = /*#__PURE__*/_interopNamespaceCompat(mssql);
|
|
21
|
+
const pg__namespace = /*#__PURE__*/_interopNamespaceCompat(pg);
|
|
20
22
|
|
|
21
23
|
var EntityState = /* @__PURE__ */ ((EntityState2) => {
|
|
22
24
|
EntityState2[EntityState2["added"] = 0] = "added";
|
|
@@ -58,6 +60,12 @@ var QueryAction = /* @__PURE__ */ ((QueryAction2) => {
|
|
|
58
60
|
return QueryAction2;
|
|
59
61
|
})(QueryAction || {});
|
|
60
62
|
|
|
63
|
+
var DatabaseServerTechnology = /* @__PURE__ */ ((DatabaseServerTechnology2) => {
|
|
64
|
+
DatabaseServerTechnology2[DatabaseServerTechnology2["MSSQL"] = 0] = "MSSQL";
|
|
65
|
+
DatabaseServerTechnology2[DatabaseServerTechnology2["PGSQL"] = 1] = "PGSQL";
|
|
66
|
+
return DatabaseServerTechnology2;
|
|
67
|
+
})(DatabaseServerTechnology || {});
|
|
68
|
+
|
|
61
69
|
function getComparisonOperator(operator) {
|
|
62
70
|
let operatorText = "";
|
|
63
71
|
switch (operator) {
|
|
@@ -87,23 +95,27 @@ function getLogicalOperator(operator) {
|
|
|
87
95
|
function getDeleteQueryText(config) {
|
|
88
96
|
const tableName = config.tableName;
|
|
89
97
|
const params = new Array();
|
|
90
|
-
const whereClause = getWhereClause$1(config.whereClause, params);
|
|
98
|
+
const whereClause = getWhereClause$1(config.whereClause, params, config.databaseServerTechnology);
|
|
91
99
|
return {
|
|
92
100
|
query: `delete from ${tableName} where ${whereClause}`,
|
|
93
101
|
parameters: params
|
|
94
102
|
};
|
|
95
103
|
}
|
|
96
|
-
function getWhereClause$1(whereClause, params) {
|
|
104
|
+
function getWhereClause$1(whereClause, params, dbTechnology) {
|
|
105
|
+
const isMsSql = dbTechnology == DatabaseServerTechnology.MSSQL;
|
|
106
|
+
let paramCharacter = isMsSql ? "@" : "$";
|
|
97
107
|
let sqlQuery = "";
|
|
98
108
|
const paramKeys = {};
|
|
99
109
|
let increment = 1;
|
|
110
|
+
let index = 1;
|
|
100
111
|
for (var i = 0; i < whereClause.length; i++) {
|
|
101
112
|
const expression = whereClause[i];
|
|
102
113
|
let paramKey = expression[0];
|
|
103
114
|
if (paramKeys[paramKey])
|
|
104
115
|
paramKey = `${paramKey}${increment}`;
|
|
105
116
|
paramKeys[paramKey] = paramKey;
|
|
106
|
-
sqlQuery += `${expression[0]} ${getComparisonOperator(expression[1])}
|
|
117
|
+
sqlQuery += `${expression[0]} ${getComparisonOperator(expression[1])} ${paramCharacter}${isMsSql ? paramKey : index} `;
|
|
118
|
+
index++;
|
|
107
119
|
params.push({
|
|
108
120
|
name: paramKey,
|
|
109
121
|
value: expression[2]
|
|
@@ -126,16 +138,28 @@ class Utils {
|
|
|
126
138
|
}
|
|
127
139
|
|
|
128
140
|
function getInsertQueryText(config) {
|
|
141
|
+
const isMsSql = DatabaseServerTechnology.MSSQL == config.databaseServerTechnology;
|
|
142
|
+
const isPgSql = DatabaseServerTechnology.PGSQL == config.databaseServerTechnology;
|
|
129
143
|
const tableName = config.tableName;
|
|
130
144
|
const queryInfo = getColumnNameAndParams$1(config);
|
|
131
145
|
const columnNames = queryInfo.columnNames.join(",");
|
|
132
|
-
const parameters = queryInfo.columnNames.map((name) =>
|
|
146
|
+
const parameters = queryInfo.columnNames.map((name, index) => {
|
|
147
|
+
if (isMsSql)
|
|
148
|
+
return `@${name}`;
|
|
149
|
+
else if (isPgSql)
|
|
150
|
+
return `$${index + 1}`;
|
|
151
|
+
}).join(",");
|
|
133
152
|
const primaryKey = config.primaryKeys[0];
|
|
134
153
|
let output = "";
|
|
135
|
-
|
|
136
|
-
|
|
154
|
+
let returningOutput = "";
|
|
155
|
+
if (primaryKey) {
|
|
156
|
+
if (DatabaseServerTechnology.MSSQL == config.databaseServerTechnology)
|
|
157
|
+
output = `OUTPUT INSERTED.${primaryKey}`;
|
|
158
|
+
else if (DatabaseServerTechnology.PGSQL == config.databaseServerTechnology)
|
|
159
|
+
returningOutput = `RETURNING ${primaryKey}`;
|
|
160
|
+
}
|
|
137
161
|
return {
|
|
138
|
-
query: `insert into ${tableName} (${columnNames}) ${output} values (${parameters})`,
|
|
162
|
+
query: `insert into ${tableName} (${columnNames}) ${output} values (${parameters}) ${returningOutput}`,
|
|
139
163
|
parameters: queryInfo.params,
|
|
140
164
|
relationProps: queryInfo.relationProps
|
|
141
165
|
};
|
|
@@ -145,9 +169,10 @@ function getColumnNameAndParams$1(config) {
|
|
|
145
169
|
const params = new Array();
|
|
146
170
|
const relationProps = /* @__PURE__ */ new Map();
|
|
147
171
|
if (config.entity) {
|
|
148
|
-
for (const key of Object.keys(config.
|
|
149
|
-
|
|
150
|
-
|
|
172
|
+
for (const key of Object.keys(config.properties)) {
|
|
173
|
+
const propName = config.properties[key];
|
|
174
|
+
if (config.entity[propName] !== void 0) {
|
|
175
|
+
const paramValue = config.entity[propName];
|
|
151
176
|
if (!(Utils.isObject(paramValue) || Utils.isArray(paramValue))) {
|
|
152
177
|
columnNames.push(key);
|
|
153
178
|
params.push({
|
|
@@ -167,23 +192,27 @@ function getSelectQueryText(config) {
|
|
|
167
192
|
const tableName = config.tableName;
|
|
168
193
|
const columnNames = config.columns.length == 0 ? "*" : config.columns.join(",");
|
|
169
194
|
const params = new Array();
|
|
170
|
-
const whereClause = getWhereClause(config.whereClause, params);
|
|
195
|
+
const whereClause = getWhereClause(config.whereClause, params, config.databaseServerTechnology);
|
|
171
196
|
return {
|
|
172
197
|
query: `select ${columnNames} from ${tableName} ${whereClause ? `where ${whereClause}` : ""}`,
|
|
173
198
|
parameters: params
|
|
174
199
|
};
|
|
175
200
|
}
|
|
176
|
-
function getWhereClause(whereClause, params) {
|
|
201
|
+
function getWhereClause(whereClause, params, dbTechnology) {
|
|
202
|
+
const isMsSql = dbTechnology == DatabaseServerTechnology.MSSQL;
|
|
203
|
+
let paramCharacter = isMsSql ? "@" : "$";
|
|
177
204
|
let sqlQuery = "";
|
|
178
205
|
const paramKeys = {};
|
|
179
206
|
let increment = 1;
|
|
207
|
+
let index = 1;
|
|
180
208
|
for (var i = 0; i < whereClause.length; i++) {
|
|
181
209
|
const expression = whereClause[i];
|
|
182
210
|
let paramKey = expression[0];
|
|
183
211
|
if (paramKeys[paramKey])
|
|
184
212
|
paramKey = `${paramKey}${increment}`;
|
|
185
213
|
paramKeys[paramKey] = paramKey;
|
|
186
|
-
sqlQuery += `${expression[0]} ${getComparisonOperator(expression[1])}
|
|
214
|
+
sqlQuery += `${expression[0]} ${getComparisonOperator(expression[1])} ${paramCharacter}${isMsSql ? paramKey : index} `;
|
|
215
|
+
index++;
|
|
187
216
|
params.push({
|
|
188
217
|
name: paramKey,
|
|
189
218
|
value: expression[2]
|
|
@@ -206,20 +235,29 @@ function getUpdateQueryText(config) {
|
|
|
206
235
|
};
|
|
207
236
|
}
|
|
208
237
|
function getColumnNameAndParams(config) {
|
|
238
|
+
const isMsSql = config.databaseServerTechnology == DatabaseServerTechnology.MSSQL;
|
|
209
239
|
const sets = [];
|
|
210
240
|
const whereClause = [];
|
|
211
241
|
const params = new Array();
|
|
212
242
|
if (config.entity) {
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
243
|
+
let index = 1;
|
|
244
|
+
let parameterCharacter = isMsSql ? "@" : "$";
|
|
245
|
+
for (const prop of Object.keys(config.properties)) {
|
|
246
|
+
const key = config.properties[prop];
|
|
247
|
+
const value = config.entity[key];
|
|
248
|
+
if (value !== void 0) {
|
|
249
|
+
const paramProp = isMsSql ? key : index;
|
|
250
|
+
if (!(Utils.isObject(value) || Utils.isArray(value))) {
|
|
251
|
+
if (config.primaryKeys.indexOf(key) === -1)
|
|
252
|
+
sets.push(`${key}=${parameterCharacter}${paramProp}`);
|
|
253
|
+
else
|
|
254
|
+
whereClause.push(`${key}=${parameterCharacter}${paramProp}`);
|
|
255
|
+
index++;
|
|
256
|
+
params.push({
|
|
257
|
+
name: `${key}`,
|
|
258
|
+
value
|
|
259
|
+
});
|
|
260
|
+
}
|
|
223
261
|
}
|
|
224
262
|
}
|
|
225
263
|
}
|
|
@@ -249,6 +287,281 @@ class DbConnection {
|
|
|
249
287
|
constructor(config) {
|
|
250
288
|
this.config = config;
|
|
251
289
|
this.mssql = mssql__namespace;
|
|
290
|
+
this.pg = pg__namespace;
|
|
291
|
+
}
|
|
292
|
+
}
|
|
293
|
+
|
|
294
|
+
const connectionCacheContainer = new class {
|
|
295
|
+
constructor() {
|
|
296
|
+
this.connectionPoolState = {};
|
|
297
|
+
}
|
|
298
|
+
getCacheKey(config) {
|
|
299
|
+
return `${config.server}-${config.database}`;
|
|
300
|
+
}
|
|
301
|
+
async getConnectionPool(mssql, config) {
|
|
302
|
+
return await new Promise((resolve, error) => {
|
|
303
|
+
const cacheKey = this.getCacheKey(config);
|
|
304
|
+
if (this.connectionPoolState[cacheKey])
|
|
305
|
+
return resolve(this.connectionPoolState[cacheKey]);
|
|
306
|
+
const pool = new mssql.ConnectionPool(config);
|
|
307
|
+
const errorHandler = (error2) => {
|
|
308
|
+
};
|
|
309
|
+
pool.on("error", errorHandler);
|
|
310
|
+
const connection = pool.connect((err) => {
|
|
311
|
+
if (err)
|
|
312
|
+
return error(err);
|
|
313
|
+
this.connectionPoolState[cacheKey] = connection;
|
|
314
|
+
resolve(connection);
|
|
315
|
+
});
|
|
316
|
+
});
|
|
317
|
+
}
|
|
318
|
+
async getPgSqlConnectionPool(sql, config) {
|
|
319
|
+
return await new Promise((resolve, error) => {
|
|
320
|
+
const cacheKey = this.getCacheKey(config);
|
|
321
|
+
if (this.connectionPoolState[cacheKey])
|
|
322
|
+
return resolve(this.connectionPoolState[cacheKey]);
|
|
323
|
+
const pool = new sql.Pool(config);
|
|
324
|
+
this.connectionPoolState[cacheKey] = pool;
|
|
325
|
+
resolve(pool);
|
|
326
|
+
});
|
|
327
|
+
}
|
|
328
|
+
}();
|
|
329
|
+
|
|
330
|
+
class PgSqlDbTransaction {
|
|
331
|
+
constructor() {
|
|
332
|
+
this.transactionConnection = null;
|
|
333
|
+
this.isSelfTransaction = true;
|
|
334
|
+
this.dbContexts = null;
|
|
335
|
+
}
|
|
336
|
+
useTransaction(client) {
|
|
337
|
+
this.isSelfTransaction = false;
|
|
338
|
+
this.transactionConnection = client;
|
|
339
|
+
}
|
|
340
|
+
async begin(pool, isolationLevel, dbContexts) {
|
|
341
|
+
if (!this.transactionConnection) {
|
|
342
|
+
this.transactionConnection = await pool.connect();
|
|
343
|
+
this.isSelfTransaction = true;
|
|
344
|
+
const isoSql = this.convertToIsolationLevel(isolationLevel);
|
|
345
|
+
await this.transactionConnection.query(`BEGIN ${isoSql}`);
|
|
346
|
+
}
|
|
347
|
+
this.assignTransaction(dbContexts);
|
|
348
|
+
}
|
|
349
|
+
assignTransaction(dbContexts) {
|
|
350
|
+
if (dbContexts)
|
|
351
|
+
for (const dbContext of dbContexts) {
|
|
352
|
+
dbContext.connection.useTransaction(this.transactionConnection);
|
|
353
|
+
}
|
|
354
|
+
this.dbContexts = dbContexts;
|
|
355
|
+
}
|
|
356
|
+
async commit() {
|
|
357
|
+
if (this.transactionConnection && this.isSelfTransaction) {
|
|
358
|
+
try {
|
|
359
|
+
await this.transactionConnection.query("COMMIT");
|
|
360
|
+
} catch (err) {
|
|
361
|
+
throw err;
|
|
362
|
+
} finally {
|
|
363
|
+
this.transactionRelease();
|
|
364
|
+
this.transactionConnection.release();
|
|
365
|
+
this.transactionConnection = null;
|
|
366
|
+
}
|
|
367
|
+
}
|
|
368
|
+
}
|
|
369
|
+
async rollback() {
|
|
370
|
+
if (this.transactionConnection && this.isSelfTransaction) {
|
|
371
|
+
try {
|
|
372
|
+
await this.transactionConnection.query("ROLLBACK");
|
|
373
|
+
} catch (err) {
|
|
374
|
+
throw err;
|
|
375
|
+
} finally {
|
|
376
|
+
this.transactionRelease();
|
|
377
|
+
this.transactionConnection.release();
|
|
378
|
+
this.transactionConnection = null;
|
|
379
|
+
}
|
|
380
|
+
}
|
|
381
|
+
}
|
|
382
|
+
transactionRelease() {
|
|
383
|
+
if (this.dbContexts) {
|
|
384
|
+
for (const dbContext of this.dbContexts) {
|
|
385
|
+
dbContext.connection.releaseTransaction();
|
|
386
|
+
}
|
|
387
|
+
this.dbContexts = null;
|
|
388
|
+
}
|
|
389
|
+
}
|
|
390
|
+
releaseTransaction() {
|
|
391
|
+
this.transactionConnection = null;
|
|
392
|
+
this.isSelfTransaction = true;
|
|
393
|
+
}
|
|
394
|
+
convertToIsolationLevel(isolation) {
|
|
395
|
+
switch (isolation?.toUpperCase()) {
|
|
396
|
+
case "READ UNCOMMITTED":
|
|
397
|
+
return "ISOLATION LEVEL READ UNCOMMITTED";
|
|
398
|
+
case "REPEATABLE READ":
|
|
399
|
+
return "ISOLATION LEVEL REPEATABLE READ";
|
|
400
|
+
case "SERIALIZABLE":
|
|
401
|
+
return "ISOLATION LEVEL SERIALIZABLE";
|
|
402
|
+
case "READ COMMITTED":
|
|
403
|
+
default:
|
|
404
|
+
return "ISOLATION LEVEL READ COMMITTED";
|
|
405
|
+
}
|
|
406
|
+
}
|
|
407
|
+
}
|
|
408
|
+
|
|
409
|
+
class PgSqlConnection extends DbConnection {
|
|
410
|
+
constructor(config) {
|
|
411
|
+
super(config);
|
|
412
|
+
this.baseTransaction = new PgSqlDbTransaction();
|
|
413
|
+
}
|
|
414
|
+
get transaction() {
|
|
415
|
+
return this.baseTransaction;
|
|
416
|
+
}
|
|
417
|
+
useTransaction(connection) {
|
|
418
|
+
this.baseTransaction.useTransaction(connection);
|
|
419
|
+
}
|
|
420
|
+
useConnection(connection) {
|
|
421
|
+
this.connection = connection;
|
|
422
|
+
}
|
|
423
|
+
async executeQuery(query) {
|
|
424
|
+
try {
|
|
425
|
+
query.databaseServerTechnology = DatabaseServerTechnology.PGSQL;
|
|
426
|
+
const sqlQueryText = getSqlTextAndParams(query);
|
|
427
|
+
const result = await this.query(sqlQueryText);
|
|
428
|
+
let records = new Array();
|
|
429
|
+
const outputInfo = this.getOutput(result, query);
|
|
430
|
+
await this.runNestedObjectQueries(outputInfo, sqlQueryText.relationProps, query);
|
|
431
|
+
if (result["rows"]) {
|
|
432
|
+
records = result.rows;
|
|
433
|
+
}
|
|
434
|
+
return records;
|
|
435
|
+
} catch (ex) {
|
|
436
|
+
throw ex;
|
|
437
|
+
}
|
|
438
|
+
}
|
|
439
|
+
async runNestedObjectQueries(outputInfo, relationProps, config) {
|
|
440
|
+
if (outputInfo) {
|
|
441
|
+
switch (config.queryAction) {
|
|
442
|
+
case QueryAction.add:
|
|
443
|
+
for (const propName of relationProps.keys()) {
|
|
444
|
+
const entries = relationProps.get(propName);
|
|
445
|
+
const queryConfig = config.propsDbConfig.get(propName);
|
|
446
|
+
if (Array.isArray(entries) && queryConfig) {
|
|
447
|
+
for (const entity of entries) {
|
|
448
|
+
if (!entity[queryConfig.foreignKey])
|
|
449
|
+
entity[queryConfig.foreignKey] = outputInfo.value;
|
|
450
|
+
await this.runQuery([{ ...queryConfig, entity }], config.queryAction);
|
|
451
|
+
}
|
|
452
|
+
}
|
|
453
|
+
}
|
|
454
|
+
break;
|
|
455
|
+
}
|
|
456
|
+
}
|
|
457
|
+
}
|
|
458
|
+
async executeFunc(funcName, params) {
|
|
459
|
+
let records = new Array();
|
|
460
|
+
if (common.isObject(params)) {
|
|
461
|
+
const queryInfo = { parameters: [] };
|
|
462
|
+
let query = `select * from ${funcName}(`;
|
|
463
|
+
let index = 1;
|
|
464
|
+
for (const [key, value] of Object.entries(params)) {
|
|
465
|
+
query += index == 1 ? `$${index}` : `,$${index}`;
|
|
466
|
+
index++;
|
|
467
|
+
queryInfo.parameters.push({ name: key, value });
|
|
468
|
+
}
|
|
469
|
+
query += ")";
|
|
470
|
+
queryInfo.query = query;
|
|
471
|
+
const result = await this.query(queryInfo);
|
|
472
|
+
if (result["rows"]) {
|
|
473
|
+
records = result.rows;
|
|
474
|
+
}
|
|
475
|
+
return records;
|
|
476
|
+
}
|
|
477
|
+
return records;
|
|
478
|
+
}
|
|
479
|
+
async executeSp(spName, params) {
|
|
480
|
+
let records = new Array();
|
|
481
|
+
if (common.isObject(params)) {
|
|
482
|
+
const queryInfo = { parameters: [] };
|
|
483
|
+
let query = `CALL ${spName}(`;
|
|
484
|
+
let index = 1;
|
|
485
|
+
for (const [key, value] of Object.entries(params)) {
|
|
486
|
+
query += index == 1 ? `$${index}` : `,$${index}`;
|
|
487
|
+
index++;
|
|
488
|
+
queryInfo.parameters.push({ name: key, value });
|
|
489
|
+
}
|
|
490
|
+
query += ")";
|
|
491
|
+
queryInfo.query = query;
|
|
492
|
+
const result = await this.query(queryInfo);
|
|
493
|
+
if (result["rows"]) {
|
|
494
|
+
records = result.rows;
|
|
495
|
+
}
|
|
496
|
+
return records;
|
|
497
|
+
}
|
|
498
|
+
return records;
|
|
499
|
+
}
|
|
500
|
+
getOutput(result, config) {
|
|
501
|
+
switch (config.queryAction) {
|
|
502
|
+
case QueryAction.add:
|
|
503
|
+
const primaryKey = config.primaryKeys[0];
|
|
504
|
+
const output = result.rows[0][primaryKey];
|
|
505
|
+
if (output)
|
|
506
|
+
config.entity[primaryKey] = output;
|
|
507
|
+
return { key: primaryKey, value: output };
|
|
508
|
+
}
|
|
509
|
+
}
|
|
510
|
+
async addEntries(dbEntities) {
|
|
511
|
+
await this.runQuery(dbEntities, QueryAction.add);
|
|
512
|
+
}
|
|
513
|
+
async updateEntries(dbEntities) {
|
|
514
|
+
await this.runQuery(dbEntities, QueryAction.update);
|
|
515
|
+
}
|
|
516
|
+
async deleteEntries(dbEntities) {
|
|
517
|
+
await this.runQuery(dbEntities, QueryAction.delete);
|
|
518
|
+
}
|
|
519
|
+
async runQuery(dbEntities, queryAction) {
|
|
520
|
+
for (const dbEntity of dbEntities) {
|
|
521
|
+
const executeQuery = {
|
|
522
|
+
...dbEntity,
|
|
523
|
+
queryAction
|
|
524
|
+
};
|
|
525
|
+
await this.executeQuery(executeQuery);
|
|
526
|
+
}
|
|
527
|
+
}
|
|
528
|
+
async query(queryInfo) {
|
|
529
|
+
const pool = await this.getConnectionPool();
|
|
530
|
+
const request = pool;
|
|
531
|
+
const values = queryInfo.parameters.map((t) => t.value);
|
|
532
|
+
const resolveQuery = (resolve, errorResolver) => (error, raw) => {
|
|
533
|
+
if (error)
|
|
534
|
+
errorResolver(error);
|
|
535
|
+
resolve(raw);
|
|
536
|
+
};
|
|
537
|
+
const catchFn = (ex) => {
|
|
538
|
+
throw ex;
|
|
539
|
+
};
|
|
540
|
+
if (queryInfo.query) {
|
|
541
|
+
const raw = await new Promise((resolve, error) => {
|
|
542
|
+
request.query(queryInfo.query, values, resolveQuery(resolve, error));
|
|
543
|
+
}).catch(catchFn);
|
|
544
|
+
return raw;
|
|
545
|
+
}
|
|
546
|
+
}
|
|
547
|
+
async beginTransaction(isolationLevel, dbContexts) {
|
|
548
|
+
const connection = await this.getConnectionPool();
|
|
549
|
+
await this.baseTransaction.begin(connection, isolationLevel, dbContexts);
|
|
550
|
+
}
|
|
551
|
+
async commitTransaction() {
|
|
552
|
+
await this.baseTransaction.commit();
|
|
553
|
+
}
|
|
554
|
+
async rollbackTransaction() {
|
|
555
|
+
await this.baseTransaction.rollback();
|
|
556
|
+
}
|
|
557
|
+
releaseTransaction() {
|
|
558
|
+
this.baseTransaction.releaseTransaction();
|
|
559
|
+
}
|
|
560
|
+
async getConnectionPool() {
|
|
561
|
+
return this.createPool();
|
|
562
|
+
}
|
|
563
|
+
async createPool() {
|
|
564
|
+
return await connectionCacheContainer.getPgSqlConnectionPool(this.pg, this.config);
|
|
252
565
|
}
|
|
253
566
|
}
|
|
254
567
|
|
|
@@ -360,6 +673,7 @@ class SqlConnection extends DbConnection {
|
|
|
360
673
|
}
|
|
361
674
|
async executeQuery(query) {
|
|
362
675
|
try {
|
|
676
|
+
query.databaseServerTechnology = DatabaseServerTechnology.MSSQL;
|
|
363
677
|
const sqlQueryText = getSqlTextAndParams(query);
|
|
364
678
|
const result = await this.query(sqlQueryText);
|
|
365
679
|
let records = new Array();
|
|
@@ -370,6 +684,7 @@ class SqlConnection extends DbConnection {
|
|
|
370
684
|
}
|
|
371
685
|
return records;
|
|
372
686
|
} catch (ex) {
|
|
687
|
+
throw ex;
|
|
373
688
|
}
|
|
374
689
|
}
|
|
375
690
|
async runNestedObjectQueries(outputInfo, relationProps, config) {
|
|
@@ -391,6 +706,27 @@ class SqlConnection extends DbConnection {
|
|
|
391
706
|
}
|
|
392
707
|
}
|
|
393
708
|
}
|
|
709
|
+
async executeFunc(funcName, params) {
|
|
710
|
+
let records = new Array();
|
|
711
|
+
if (common.isObject(params)) {
|
|
712
|
+
const queryInfo = { parameters: [] };
|
|
713
|
+
let query = `select * from ${funcName}(`;
|
|
714
|
+
let index = 1;
|
|
715
|
+
for (const [key, value] of Object.entries(params)) {
|
|
716
|
+
query += index == 1 ? `@${key}` : `,@${key}`;
|
|
717
|
+
index++;
|
|
718
|
+
queryInfo.parameters.push({ name: key, value });
|
|
719
|
+
}
|
|
720
|
+
query += ")";
|
|
721
|
+
queryInfo.query = query;
|
|
722
|
+
const result = await this.query(queryInfo);
|
|
723
|
+
if (result["recordset"]) {
|
|
724
|
+
records = result.recordset;
|
|
725
|
+
}
|
|
726
|
+
return records;
|
|
727
|
+
}
|
|
728
|
+
return records;
|
|
729
|
+
}
|
|
394
730
|
async executeSp(spName, params) {
|
|
395
731
|
let records = new Array();
|
|
396
732
|
if (common.isObject(params)) {
|
|
@@ -443,20 +779,23 @@ class SqlConnection extends DbConnection {
|
|
|
443
779
|
else
|
|
444
780
|
request.input(param.name, param.value);
|
|
445
781
|
}
|
|
446
|
-
const resolveQuery = (resolve) => (error, raw) => {
|
|
782
|
+
const resolveQuery = (resolve, errorResolver) => (error, raw) => {
|
|
447
783
|
if (error)
|
|
448
|
-
|
|
784
|
+
errorResolver(error);
|
|
449
785
|
resolve(raw);
|
|
450
786
|
};
|
|
787
|
+
const catchFn = (ex) => {
|
|
788
|
+
throw ex;
|
|
789
|
+
};
|
|
451
790
|
if (queryInfo.query) {
|
|
452
791
|
const raw = await new Promise((resolve, error) => {
|
|
453
|
-
request.query(queryInfo.query, resolveQuery(resolve));
|
|
454
|
-
});
|
|
792
|
+
request.query(queryInfo.query, resolveQuery(resolve, error));
|
|
793
|
+
}).catch(catchFn);
|
|
455
794
|
return raw;
|
|
456
795
|
} else if (queryInfo.sp) {
|
|
457
796
|
const raw = await new Promise((resolve, error) => {
|
|
458
|
-
request.execute(queryInfo.sp, resolveQuery(resolve));
|
|
459
|
-
});
|
|
797
|
+
request.execute(queryInfo.sp, resolveQuery(resolve, error));
|
|
798
|
+
}).catch(catchFn);
|
|
460
799
|
return raw;
|
|
461
800
|
}
|
|
462
801
|
}
|
|
@@ -477,19 +816,7 @@ class SqlConnection extends DbConnection {
|
|
|
477
816
|
return this.createPool();
|
|
478
817
|
}
|
|
479
818
|
async createPool() {
|
|
480
|
-
return await
|
|
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
|
-
});
|
|
819
|
+
return await connectionCacheContainer.getConnectionPool(this.mssql, this.config);
|
|
493
820
|
}
|
|
494
821
|
}
|
|
495
822
|
|
|
@@ -497,6 +824,9 @@ class DbConnectionContext {
|
|
|
497
824
|
useSqlServer(options) {
|
|
498
825
|
this.connection = new SqlConnection(options);
|
|
499
826
|
}
|
|
827
|
+
usePgSqlServer(options) {
|
|
828
|
+
this.connection = new PgSqlConnection(options);
|
|
829
|
+
}
|
|
500
830
|
}
|
|
501
831
|
|
|
502
832
|
class DbSet {
|
|
@@ -517,27 +847,36 @@ class DbSet {
|
|
|
517
847
|
}
|
|
518
848
|
async add(entity) {
|
|
519
849
|
const properties = this.getProperties();
|
|
520
|
-
this.changeTracker.addItem(
|
|
521
|
-
|
|
522
|
-
|
|
523
|
-
|
|
524
|
-
|
|
525
|
-
|
|
526
|
-
|
|
850
|
+
this.changeTracker.addItem(
|
|
851
|
+
{
|
|
852
|
+
tableName: this.getTableName(this.type.name),
|
|
853
|
+
entity,
|
|
854
|
+
primaryKeys: this.getPrimaryKeys(this.type.name),
|
|
855
|
+
propsDbConfig: this.getPropsDbConfig(entity),
|
|
856
|
+
properties
|
|
857
|
+
},
|
|
858
|
+
EntityState.added
|
|
859
|
+
);
|
|
527
860
|
}
|
|
528
861
|
async update(entity) {
|
|
529
|
-
this.changeTracker.addItem(
|
|
530
|
-
|
|
531
|
-
|
|
532
|
-
|
|
533
|
-
|
|
534
|
-
|
|
862
|
+
this.changeTracker.addItem(
|
|
863
|
+
{
|
|
864
|
+
tableName: this.getTableName(this.type.name),
|
|
865
|
+
entity,
|
|
866
|
+
primaryKeys: this.getPrimaryKeys(this.type.name),
|
|
867
|
+
propsDbConfig: this.getPropsDbConfig(entity)
|
|
868
|
+
},
|
|
869
|
+
EntityState.updated
|
|
870
|
+
);
|
|
535
871
|
}
|
|
536
872
|
async delete(expression) {
|
|
537
|
-
this.changeTracker.addItem(
|
|
538
|
-
|
|
539
|
-
|
|
540
|
-
|
|
873
|
+
this.changeTracker.addItem(
|
|
874
|
+
{
|
|
875
|
+
tableName: this.getTableName(this.type.name),
|
|
876
|
+
whereClause: expression
|
|
877
|
+
},
|
|
878
|
+
EntityState.deleted
|
|
879
|
+
);
|
|
541
880
|
}
|
|
542
881
|
async toList() {
|
|
543
882
|
return await this.execute();
|
|
@@ -559,22 +898,30 @@ class DbSet {
|
|
|
559
898
|
const props = core.entityContainer.getProperties(name || this.type.name);
|
|
560
899
|
const jProps = {};
|
|
561
900
|
if (props)
|
|
562
|
-
props.forEach(
|
|
901
|
+
props.forEach(
|
|
902
|
+
(property) => jProps[property.name.toLowerCase()] = property.name
|
|
903
|
+
);
|
|
563
904
|
return jProps;
|
|
564
905
|
}
|
|
565
906
|
getPropsDbConfig(entity) {
|
|
566
907
|
const propsDbConfig = core.entityContainer.getPropsDbConfig(this.type.name);
|
|
908
|
+
const properties = core.entityContainer.getProperties(this.type.name);
|
|
567
909
|
const props = /* @__PURE__ */ new Map();
|
|
568
|
-
|
|
569
|
-
const
|
|
570
|
-
|
|
571
|
-
|
|
572
|
-
|
|
573
|
-
|
|
574
|
-
|
|
575
|
-
|
|
576
|
-
|
|
577
|
-
|
|
910
|
+
if (properties && Array.isArray(properties)) {
|
|
911
|
+
for (const property of properties) {
|
|
912
|
+
if (entity[property.name]) {
|
|
913
|
+
const propName = property.name;
|
|
914
|
+
const dbFieldConfig = propsDbConfig.get(propName);
|
|
915
|
+
if (dbFieldConfig && (dbFieldConfig.oneToMany || dbFieldConfig.oneToOne)) {
|
|
916
|
+
const fieldRelationConfig = dbFieldConfig.oneToMany || dbFieldConfig.oneToOne;
|
|
917
|
+
props.set(propName, {
|
|
918
|
+
tableName: this.getTableName(fieldRelationConfig.model.name),
|
|
919
|
+
primaryKeys: this.getPrimaryKeys(fieldRelationConfig.model.name),
|
|
920
|
+
foreignKey: fieldRelationConfig.foreignKey,
|
|
921
|
+
properties: this.getProperties(fieldRelationConfig.model.name)
|
|
922
|
+
});
|
|
923
|
+
}
|
|
924
|
+
}
|
|
578
925
|
}
|
|
579
926
|
}
|
|
580
927
|
return props;
|
|
@@ -603,6 +950,25 @@ class StoreProc {
|
|
|
603
950
|
}
|
|
604
951
|
}
|
|
605
952
|
|
|
953
|
+
class DbFunc {
|
|
954
|
+
constructor(funcName, type, queryConnection) {
|
|
955
|
+
this.funcName = funcName;
|
|
956
|
+
this.type = type;
|
|
957
|
+
this.queryConnection = queryConnection;
|
|
958
|
+
}
|
|
959
|
+
async execute(params) {
|
|
960
|
+
const items = await this.queryConnection.executeFunc(this.funcName, params);
|
|
961
|
+
return new common.List(items, this.type, this.properties);
|
|
962
|
+
}
|
|
963
|
+
get properties() {
|
|
964
|
+
const props = core.entityContainer.getProperties(this.type.name);
|
|
965
|
+
const jProps = {};
|
|
966
|
+
if (props)
|
|
967
|
+
props.forEach((property) => jProps[property.name.toLowerCase()] = property.name);
|
|
968
|
+
return jProps;
|
|
969
|
+
}
|
|
970
|
+
}
|
|
971
|
+
|
|
606
972
|
class DbContext {
|
|
607
973
|
get connection() {
|
|
608
974
|
return this.dbConnectionInfo.connection;
|
|
@@ -630,6 +996,11 @@ class DbContext {
|
|
|
630
996
|
for (const [key, value] of Object.entries(sps))
|
|
631
997
|
this[key] = new StoreProc(value.spName, value.model(), this.queryConnection);
|
|
632
998
|
}
|
|
999
|
+
onDbFuncRegistering(funcs) {
|
|
1000
|
+
if (funcs)
|
|
1001
|
+
for (const [key, value] of Object.entries(funcs))
|
|
1002
|
+
this[key] = new DbFunc(value.funcName, value.model(), this.queryConnection);
|
|
1003
|
+
}
|
|
633
1004
|
async saveChanges() {
|
|
634
1005
|
return new Promise(async (resolve, error) => {
|
|
635
1006
|
try {
|
|
@@ -649,6 +1020,8 @@ class DbContext {
|
|
|
649
1020
|
}
|
|
650
1021
|
}
|
|
651
1022
|
|
|
1023
|
+
exports.DbConnectionContext = DbConnectionContext;
|
|
652
1024
|
exports.DbContext = DbContext;
|
|
1025
|
+
exports.DbFunc = DbFunc;
|
|
653
1026
|
exports.DbSet = DbSet;
|
|
654
1027
|
exports.StoreProc = StoreProc;
|