@nattyjs/orm 0.0.1-beta.5 → 0.0.1-beta.50

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.mjs CHANGED
@@ -1,4 +1,5 @@
1
1
  import * as mssql from 'mssql';
2
+ import * as pg from 'pg';
2
3
  import { isObject, List } from '@nattyjs/common';
3
4
  import { entityContainer } from '@nattyjs/core';
4
5
 
@@ -42,6 +43,12 @@ var QueryAction = /* @__PURE__ */ ((QueryAction2) => {
42
43
  return QueryAction2;
43
44
  })(QueryAction || {});
44
45
 
46
+ var DatabaseServerTechnology = /* @__PURE__ */ ((DatabaseServerTechnology2) => {
47
+ DatabaseServerTechnology2[DatabaseServerTechnology2["MSSQL"] = 0] = "MSSQL";
48
+ DatabaseServerTechnology2[DatabaseServerTechnology2["PGSQL"] = 1] = "PGSQL";
49
+ return DatabaseServerTechnology2;
50
+ })(DatabaseServerTechnology || {});
51
+
45
52
  function getComparisonOperator(operator) {
46
53
  let operatorText = "";
47
54
  switch (operator) {
@@ -71,23 +78,27 @@ function getLogicalOperator(operator) {
71
78
  function getDeleteQueryText(config) {
72
79
  const tableName = config.tableName;
73
80
  const params = new Array();
74
- const whereClause = getWhereClause$1(config.whereClause, params);
81
+ const whereClause = getWhereClause$1(config.whereClause, params, config.databaseServerTechnology);
75
82
  return {
76
83
  query: `delete from ${tableName} where ${whereClause}`,
77
84
  parameters: params
78
85
  };
79
86
  }
80
- function getWhereClause$1(whereClause, params) {
87
+ function getWhereClause$1(whereClause, params, dbTechnology) {
88
+ const isMsSql = dbTechnology == DatabaseServerTechnology.MSSQL;
89
+ let paramCharacter = isMsSql ? "@" : "$";
81
90
  let sqlQuery = "";
82
91
  const paramKeys = {};
83
92
  let increment = 1;
93
+ let index = 1;
84
94
  for (var i = 0; i < whereClause.length; i++) {
85
95
  const expression = whereClause[i];
86
96
  let paramKey = expression[0];
87
97
  if (paramKeys[paramKey])
88
98
  paramKey = `${paramKey}${increment}`;
89
99
  paramKeys[paramKey] = paramKey;
90
- sqlQuery += `${expression[0]} ${getComparisonOperator(expression[1])} @${paramKey} `;
100
+ sqlQuery += `${expression[0]} ${getComparisonOperator(expression[1])} ${paramCharacter}${isMsSql ? paramKey : index} `;
101
+ index++;
91
102
  params.push({
92
103
  name: paramKey,
93
104
  value: expression[2]
@@ -110,16 +121,28 @@ class Utils {
110
121
  }
111
122
 
112
123
  function getInsertQueryText(config) {
124
+ const isMsSql = DatabaseServerTechnology.MSSQL == config.databaseServerTechnology;
125
+ const isPgSql = DatabaseServerTechnology.PGSQL == config.databaseServerTechnology;
113
126
  const tableName = config.tableName;
114
127
  const queryInfo = getColumnNameAndParams$1(config);
115
128
  const columnNames = queryInfo.columnNames.join(",");
116
- const parameters = queryInfo.columnNames.map((name) => `@${name}`).join(",");
129
+ const parameters = queryInfo.columnNames.map((name, index) => {
130
+ if (isMsSql)
131
+ return `@${name}`;
132
+ else if (isPgSql)
133
+ return `$${index + 1}`;
134
+ }).join(",");
117
135
  const primaryKey = config.primaryKeys[0];
118
136
  let output = "";
119
- if (primaryKey)
120
- output = `OUTPUT INSERTED.${primaryKey}`;
137
+ let returningOutput = "";
138
+ if (primaryKey) {
139
+ if (DatabaseServerTechnology.MSSQL == config.databaseServerTechnology)
140
+ output = `OUTPUT INSERTED.${primaryKey}`;
141
+ else if (DatabaseServerTechnology.PGSQL == config.databaseServerTechnology)
142
+ returningOutput = `RETURNING ${primaryKey}`;
143
+ }
121
144
  return {
122
- query: `insert into ${tableName} (${columnNames}) ${output} values (${parameters})`,
145
+ query: `insert into ${tableName} (${columnNames}) ${output} values (${parameters}) ${returningOutput}`,
123
146
  parameters: queryInfo.params,
124
147
  relationProps: queryInfo.relationProps
125
148
  };
@@ -129,9 +152,10 @@ function getColumnNameAndParams$1(config) {
129
152
  const params = new Array();
130
153
  const relationProps = /* @__PURE__ */ new Map();
131
154
  if (config.entity) {
132
- for (const key of Object.keys(config.entity)) {
133
- if (config.properties[key.toLowerCase()]) {
134
- const paramValue = config.entity[key];
155
+ for (const key of Object.keys(config.properties)) {
156
+ const propName = config.properties[key];
157
+ if (config.entity[propName] !== void 0) {
158
+ const paramValue = config.entity[propName];
135
159
  if (!(Utils.isObject(paramValue) || Utils.isArray(paramValue))) {
136
160
  columnNames.push(key);
137
161
  params.push({
@@ -151,23 +175,27 @@ function getSelectQueryText(config) {
151
175
  const tableName = config.tableName;
152
176
  const columnNames = config.columns.length == 0 ? "*" : config.columns.join(",");
153
177
  const params = new Array();
154
- const whereClause = getWhereClause(config.whereClause, params);
178
+ const whereClause = getWhereClause(config.whereClause, params, config.databaseServerTechnology);
155
179
  return {
156
180
  query: `select ${columnNames} from ${tableName} ${whereClause ? `where ${whereClause}` : ""}`,
157
181
  parameters: params
158
182
  };
159
183
  }
160
- function getWhereClause(whereClause, params) {
184
+ function getWhereClause(whereClause, params, dbTechnology) {
185
+ const isMsSql = dbTechnology == DatabaseServerTechnology.MSSQL;
186
+ let paramCharacter = isMsSql ? "@" : "$";
161
187
  let sqlQuery = "";
162
188
  const paramKeys = {};
163
189
  let increment = 1;
190
+ let index = 1;
164
191
  for (var i = 0; i < whereClause.length; i++) {
165
192
  const expression = whereClause[i];
166
193
  let paramKey = expression[0];
167
194
  if (paramKeys[paramKey])
168
195
  paramKey = `${paramKey}${increment}`;
169
196
  paramKeys[paramKey] = paramKey;
170
- sqlQuery += `${expression[0]} ${getComparisonOperator(expression[1])} @${paramKey} `;
197
+ sqlQuery += `${expression[0]} ${getComparisonOperator(expression[1])} ${paramCharacter}${isMsSql ? paramKey : index} `;
198
+ index++;
171
199
  params.push({
172
200
  name: paramKey,
173
201
  value: expression[2]
@@ -190,20 +218,29 @@ function getUpdateQueryText(config) {
190
218
  };
191
219
  }
192
220
  function getColumnNameAndParams(config) {
221
+ const isMsSql = config.databaseServerTechnology == DatabaseServerTechnology.MSSQL;
193
222
  const sets = [];
194
223
  const whereClause = [];
195
224
  const params = new Array();
196
225
  if (config.entity) {
197
- for (const [key, value] of Object.entries(config.entity)) {
198
- if (!(Utils.isObject(value) || Utils.isArray(value))) {
199
- if (config.primaryKeys.indexOf(key) === -1)
200
- sets.push(`${key}=@${key}`);
201
- else
202
- whereClause.push(`${key}=@${key}`);
203
- params.push({
204
- name: `${key}`,
205
- value
206
- });
226
+ let index = 1;
227
+ let parameterCharacter = isMsSql ? "@" : "$";
228
+ for (const prop of Object.keys(config.properties)) {
229
+ const key = config.properties[prop];
230
+ const value = config.entity[key];
231
+ if (value !== void 0) {
232
+ const paramProp = isMsSql ? key : index;
233
+ if (!(Utils.isObject(value) || Utils.isArray(value))) {
234
+ if (config.primaryKeys.indexOf(key) === -1)
235
+ sets.push(`${key}=${parameterCharacter}${paramProp}`);
236
+ else
237
+ whereClause.push(`${key}=${parameterCharacter}${paramProp}`);
238
+ index++;
239
+ params.push({
240
+ name: `${key}`,
241
+ value
242
+ });
243
+ }
207
244
  }
208
245
  }
209
246
  }
@@ -233,6 +270,281 @@ class DbConnection {
233
270
  constructor(config) {
234
271
  this.config = config;
235
272
  this.mssql = mssql;
273
+ this.pg = pg;
274
+ }
275
+ }
276
+
277
+ const connectionCacheContainer = new class {
278
+ constructor() {
279
+ this.connectionPoolState = {};
280
+ }
281
+ getCacheKey(config) {
282
+ return `${config.server}-${config.database}`;
283
+ }
284
+ async getConnectionPool(mssql, config) {
285
+ return await new Promise((resolve, error) => {
286
+ const cacheKey = this.getCacheKey(config);
287
+ if (this.connectionPoolState[cacheKey])
288
+ return resolve(this.connectionPoolState[cacheKey]);
289
+ const pool = new mssql.ConnectionPool(config);
290
+ const errorHandler = (error2) => {
291
+ };
292
+ pool.on("error", errorHandler);
293
+ const connection = pool.connect((err) => {
294
+ if (err)
295
+ return error(err);
296
+ this.connectionPoolState[cacheKey] = connection;
297
+ resolve(connection);
298
+ });
299
+ });
300
+ }
301
+ async getPgSqlConnectionPool(sql, 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 sql.Pool(config);
307
+ this.connectionPoolState[cacheKey] = pool;
308
+ resolve(pool);
309
+ });
310
+ }
311
+ }();
312
+
313
+ class PgSqlDbTransaction {
314
+ constructor() {
315
+ this.transactionConnection = null;
316
+ this.isSelfTransaction = true;
317
+ this.dbContexts = null;
318
+ }
319
+ useTransaction(client) {
320
+ this.isSelfTransaction = false;
321
+ this.transactionConnection = client;
322
+ }
323
+ async begin(pool, isolationLevel, dbContexts) {
324
+ if (!this.transactionConnection) {
325
+ this.transactionConnection = await pool.connect();
326
+ this.isSelfTransaction = true;
327
+ const isoSql = this.convertToIsolationLevel(isolationLevel);
328
+ await this.transactionConnection.query(`BEGIN ${isoSql}`);
329
+ }
330
+ this.assignTransaction(dbContexts);
331
+ }
332
+ assignTransaction(dbContexts) {
333
+ if (dbContexts)
334
+ for (const dbContext of dbContexts) {
335
+ dbContext.connection.useTransaction(this.transactionConnection);
336
+ }
337
+ this.dbContexts = dbContexts;
338
+ }
339
+ async commit() {
340
+ if (this.transactionConnection && this.isSelfTransaction) {
341
+ try {
342
+ await this.transactionConnection.query("COMMIT");
343
+ } catch (err) {
344
+ throw err;
345
+ } finally {
346
+ this.transactionRelease();
347
+ this.transactionConnection.release();
348
+ this.transactionConnection = null;
349
+ }
350
+ }
351
+ }
352
+ async rollback() {
353
+ if (this.transactionConnection && this.isSelfTransaction) {
354
+ try {
355
+ await this.transactionConnection.query("ROLLBACK");
356
+ } catch (err) {
357
+ throw err;
358
+ } finally {
359
+ this.transactionRelease();
360
+ this.transactionConnection.release();
361
+ this.transactionConnection = null;
362
+ }
363
+ }
364
+ }
365
+ transactionRelease() {
366
+ if (this.dbContexts) {
367
+ for (const dbContext of this.dbContexts) {
368
+ dbContext.connection.releaseTransaction();
369
+ }
370
+ this.dbContexts = null;
371
+ }
372
+ }
373
+ releaseTransaction() {
374
+ this.transactionConnection = null;
375
+ this.isSelfTransaction = true;
376
+ }
377
+ convertToIsolationLevel(isolation) {
378
+ switch (isolation?.toUpperCase()) {
379
+ case "READ UNCOMMITTED":
380
+ return "ISOLATION LEVEL READ UNCOMMITTED";
381
+ case "REPEATABLE READ":
382
+ return "ISOLATION LEVEL REPEATABLE READ";
383
+ case "SERIALIZABLE":
384
+ return "ISOLATION LEVEL SERIALIZABLE";
385
+ case "READ COMMITTED":
386
+ default:
387
+ return "ISOLATION LEVEL READ COMMITTED";
388
+ }
389
+ }
390
+ }
391
+
392
+ class PgSqlConnection extends DbConnection {
393
+ constructor(config) {
394
+ super(config);
395
+ this.baseTransaction = new PgSqlDbTransaction();
396
+ }
397
+ get transaction() {
398
+ return this.baseTransaction;
399
+ }
400
+ useTransaction(connection) {
401
+ this.baseTransaction.useTransaction(connection);
402
+ }
403
+ useConnection(connection) {
404
+ this.connection = connection;
405
+ }
406
+ async executeQuery(query) {
407
+ try {
408
+ query.databaseServerTechnology = DatabaseServerTechnology.PGSQL;
409
+ const sqlQueryText = getSqlTextAndParams(query);
410
+ const result = await this.query(sqlQueryText);
411
+ let records = new Array();
412
+ const outputInfo = this.getOutput(result, query);
413
+ await this.runNestedObjectQueries(outputInfo, sqlQueryText.relationProps, query);
414
+ if (result["rows"]) {
415
+ records = result.rows;
416
+ }
417
+ return records;
418
+ } catch (ex) {
419
+ throw ex;
420
+ }
421
+ }
422
+ async runNestedObjectQueries(outputInfo, relationProps, config) {
423
+ if (outputInfo) {
424
+ switch (config.queryAction) {
425
+ case QueryAction.add:
426
+ for (const propName of relationProps.keys()) {
427
+ const entries = relationProps.get(propName);
428
+ const queryConfig = config.propsDbConfig.get(propName);
429
+ if (Array.isArray(entries) && queryConfig) {
430
+ for (const entity of entries) {
431
+ if (!entity[queryConfig.foreignKey])
432
+ entity[queryConfig.foreignKey] = outputInfo.value;
433
+ await this.runQuery([{ ...queryConfig, entity }], config.queryAction);
434
+ }
435
+ }
436
+ }
437
+ break;
438
+ }
439
+ }
440
+ }
441
+ async executeFunc(funcName, params) {
442
+ let records = new Array();
443
+ if (isObject(params)) {
444
+ const queryInfo = { parameters: [] };
445
+ let query = `select * from ${funcName}(`;
446
+ let index = 1;
447
+ for (const [key, value] of Object.entries(params)) {
448
+ query += index == 1 ? `$${index}` : `,$${index}`;
449
+ index++;
450
+ queryInfo.parameters.push({ name: key, value });
451
+ }
452
+ query += ")";
453
+ queryInfo.query = query;
454
+ const result = await this.query(queryInfo);
455
+ if (result["rows"]) {
456
+ records = result.rows;
457
+ }
458
+ return records;
459
+ }
460
+ return records;
461
+ }
462
+ async executeSp(spName, params) {
463
+ let records = new Array();
464
+ if (isObject(params)) {
465
+ const queryInfo = { parameters: [] };
466
+ let query = `CALL ${spName}(`;
467
+ let index = 1;
468
+ for (const [key, value] of Object.entries(params)) {
469
+ query += index == 1 ? `$${index}` : `,$${index}`;
470
+ index++;
471
+ queryInfo.parameters.push({ name: key, value });
472
+ }
473
+ query += ")";
474
+ queryInfo.query = query;
475
+ const result = await this.query(queryInfo);
476
+ if (result["rows"]) {
477
+ records = result.rows;
478
+ }
479
+ return records;
480
+ }
481
+ return records;
482
+ }
483
+ getOutput(result, config) {
484
+ switch (config.queryAction) {
485
+ case QueryAction.add:
486
+ const primaryKey = config.primaryKeys[0];
487
+ const output = result.rows[0][primaryKey];
488
+ if (output)
489
+ config.entity[primaryKey] = output;
490
+ return { key: primaryKey, value: output };
491
+ }
492
+ }
493
+ async addEntries(dbEntities) {
494
+ await this.runQuery(dbEntities, QueryAction.add);
495
+ }
496
+ async updateEntries(dbEntities) {
497
+ await this.runQuery(dbEntities, QueryAction.update);
498
+ }
499
+ async deleteEntries(dbEntities) {
500
+ await this.runQuery(dbEntities, QueryAction.delete);
501
+ }
502
+ async runQuery(dbEntities, queryAction) {
503
+ for (const dbEntity of dbEntities) {
504
+ const executeQuery = {
505
+ ...dbEntity,
506
+ queryAction
507
+ };
508
+ await this.executeQuery(executeQuery);
509
+ }
510
+ }
511
+ async query(queryInfo) {
512
+ const pool = await this.getConnectionPool();
513
+ const request = pool;
514
+ const values = queryInfo.parameters.map((t) => t.value);
515
+ const resolveQuery = (resolve, errorResolver) => (error, raw) => {
516
+ if (error)
517
+ errorResolver(error);
518
+ resolve(raw);
519
+ };
520
+ const catchFn = (ex) => {
521
+ throw ex;
522
+ };
523
+ if (queryInfo.query) {
524
+ const raw = await new Promise((resolve, error) => {
525
+ request.query(queryInfo.query, values, resolveQuery(resolve, error));
526
+ }).catch(catchFn);
527
+ return raw;
528
+ }
529
+ }
530
+ async beginTransaction(isolationLevel, dbContexts) {
531
+ const connection = await this.getConnectionPool();
532
+ await this.baseTransaction.begin(connection, isolationLevel, dbContexts);
533
+ }
534
+ async commitTransaction() {
535
+ await this.baseTransaction.commit();
536
+ }
537
+ async rollbackTransaction() {
538
+ await this.baseTransaction.rollback();
539
+ }
540
+ releaseTransaction() {
541
+ this.baseTransaction.releaseTransaction();
542
+ }
543
+ async getConnectionPool() {
544
+ return this.createPool();
545
+ }
546
+ async createPool() {
547
+ return await connectionCacheContainer.getPgSqlConnectionPool(this.pg, this.config);
236
548
  }
237
549
  }
238
550
 
@@ -344,6 +656,7 @@ class SqlConnection extends DbConnection {
344
656
  }
345
657
  async executeQuery(query) {
346
658
  try {
659
+ query.databaseServerTechnology = DatabaseServerTechnology.MSSQL;
347
660
  const sqlQueryText = getSqlTextAndParams(query);
348
661
  const result = await this.query(sqlQueryText);
349
662
  let records = new Array();
@@ -354,6 +667,7 @@ class SqlConnection extends DbConnection {
354
667
  }
355
668
  return records;
356
669
  } catch (ex) {
670
+ throw ex;
357
671
  }
358
672
  }
359
673
  async runNestedObjectQueries(outputInfo, relationProps, config) {
@@ -375,6 +689,27 @@ class SqlConnection extends DbConnection {
375
689
  }
376
690
  }
377
691
  }
692
+ async executeFunc(funcName, params) {
693
+ let records = new Array();
694
+ if (isObject(params)) {
695
+ const queryInfo = { parameters: [] };
696
+ let query = `select * from ${funcName}(`;
697
+ let index = 1;
698
+ for (const [key, value] of Object.entries(params)) {
699
+ query += index == 1 ? `@${key}` : `,@${key}`;
700
+ index++;
701
+ queryInfo.parameters.push({ name: key, value });
702
+ }
703
+ query += ")";
704
+ queryInfo.query = query;
705
+ const result = await this.query(queryInfo);
706
+ if (result["recordset"]) {
707
+ records = result.recordset;
708
+ }
709
+ return records;
710
+ }
711
+ return records;
712
+ }
378
713
  async executeSp(spName, params) {
379
714
  let records = new Array();
380
715
  if (isObject(params)) {
@@ -427,20 +762,23 @@ class SqlConnection extends DbConnection {
427
762
  else
428
763
  request.input(param.name, param.value);
429
764
  }
430
- const resolveQuery = (resolve) => (error, raw) => {
765
+ const resolveQuery = (resolve, errorResolver) => (error, raw) => {
431
766
  if (error)
432
- throw error;
767
+ errorResolver(error);
433
768
  resolve(raw);
434
769
  };
770
+ const catchFn = (ex) => {
771
+ throw ex;
772
+ };
435
773
  if (queryInfo.query) {
436
774
  const raw = await new Promise((resolve, error) => {
437
- request.query(queryInfo.query, resolveQuery(resolve));
438
- });
775
+ request.query(queryInfo.query, resolveQuery(resolve, error));
776
+ }).catch(catchFn);
439
777
  return raw;
440
778
  } else if (queryInfo.sp) {
441
779
  const raw = await new Promise((resolve, error) => {
442
- request.execute(queryInfo.sp, resolveQuery(resolve));
443
- });
780
+ request.execute(queryInfo.sp, resolveQuery(resolve, error));
781
+ }).catch(catchFn);
444
782
  return raw;
445
783
  }
446
784
  }
@@ -461,19 +799,7 @@ class SqlConnection extends DbConnection {
461
799
  return this.createPool();
462
800
  }
463
801
  async createPool() {
464
- return await new Promise((resolve, error) => {
465
- if (this.baseTransaction.transactionConnection)
466
- return resolve(this.baseTransaction.transactionConnection);
467
- const pool = new this.mssql.ConnectionPool(this.config);
468
- const errorHandler = (error2) => {
469
- };
470
- pool.on("error", errorHandler);
471
- const connection = pool.connect((err) => {
472
- if (err)
473
- return error(err);
474
- resolve(connection);
475
- });
476
- });
802
+ return await connectionCacheContainer.getConnectionPool(this.mssql, this.config);
477
803
  }
478
804
  }
479
805
 
@@ -481,6 +807,9 @@ class DbConnectionContext {
481
807
  useSqlServer(options) {
482
808
  this.connection = new SqlConnection(options);
483
809
  }
810
+ usePgSqlServer(options) {
811
+ this.connection = new PgSqlConnection(options);
812
+ }
484
813
  }
485
814
 
486
815
  class DbSet {
@@ -501,27 +830,36 @@ class DbSet {
501
830
  }
502
831
  async add(entity) {
503
832
  const properties = this.getProperties();
504
- this.changeTracker.addItem({
505
- tableName: this.getTableName(this.type.name),
506
- entity,
507
- primaryKeys: this.getPrimaryKeys(this.type.name),
508
- propsDbConfig: this.getPropsDbConfig(entity),
509
- properties
510
- }, EntityState.added);
833
+ this.changeTracker.addItem(
834
+ {
835
+ tableName: this.getTableName(this.type.name),
836
+ entity,
837
+ primaryKeys: this.getPrimaryKeys(this.type.name),
838
+ propsDbConfig: this.getPropsDbConfig(entity),
839
+ properties
840
+ },
841
+ EntityState.added
842
+ );
511
843
  }
512
844
  async update(entity) {
513
- this.changeTracker.addItem({
514
- tableName: this.getTableName(this.type.name),
515
- entity,
516
- primaryKeys: this.getPrimaryKeys(this.type.name),
517
- propsDbConfig: this.getPropsDbConfig(entity)
518
- }, EntityState.updated);
845
+ this.changeTracker.addItem(
846
+ {
847
+ tableName: this.getTableName(this.type.name),
848
+ entity,
849
+ primaryKeys: this.getPrimaryKeys(this.type.name),
850
+ propsDbConfig: this.getPropsDbConfig(entity)
851
+ },
852
+ EntityState.updated
853
+ );
519
854
  }
520
855
  async delete(expression) {
521
- this.changeTracker.addItem({
522
- tableName: this.getTableName(this.type.name),
523
- whereClause: expression
524
- }, EntityState.deleted);
856
+ this.changeTracker.addItem(
857
+ {
858
+ tableName: this.getTableName(this.type.name),
859
+ whereClause: expression
860
+ },
861
+ EntityState.deleted
862
+ );
525
863
  }
526
864
  async toList() {
527
865
  return await this.execute();
@@ -543,22 +881,30 @@ class DbSet {
543
881
  const props = entityContainer.getProperties(name || this.type.name);
544
882
  const jProps = {};
545
883
  if (props)
546
- props.forEach((property) => jProps[property.name.toLowerCase()] = property.name);
884
+ props.forEach(
885
+ (property) => jProps[property.name.toLowerCase()] = property.name
886
+ );
547
887
  return jProps;
548
888
  }
549
889
  getPropsDbConfig(entity) {
550
890
  const propsDbConfig = entityContainer.getPropsDbConfig(this.type.name);
891
+ const properties = entityContainer.getProperties(this.type.name);
551
892
  const props = /* @__PURE__ */ new Map();
552
- for (const propName of Object.keys(entity)) {
553
- const dbFieldConfig = propsDbConfig.get(propName);
554
- if (dbFieldConfig && (dbFieldConfig.oneToMany || dbFieldConfig.oneToOne)) {
555
- const fieldRelationConfig = dbFieldConfig.oneToMany || dbFieldConfig.oneToOne;
556
- props.set(propName, {
557
- tableName: this.getTableName(fieldRelationConfig.model.name),
558
- primaryKeys: this.getPrimaryKeys(fieldRelationConfig.model.name),
559
- foreignKey: fieldRelationConfig.foreignKey,
560
- properties: this.getProperties(fieldRelationConfig.model.name)
561
- });
893
+ if (properties && Array.isArray(properties)) {
894
+ for (const property of properties) {
895
+ if (entity[property.name]) {
896
+ const propName = property.name;
897
+ const dbFieldConfig = propsDbConfig.get(propName);
898
+ if (dbFieldConfig && (dbFieldConfig.oneToMany || dbFieldConfig.oneToOne)) {
899
+ const fieldRelationConfig = dbFieldConfig.oneToMany || dbFieldConfig.oneToOne;
900
+ props.set(propName, {
901
+ tableName: this.getTableName(fieldRelationConfig.model.name),
902
+ primaryKeys: this.getPrimaryKeys(fieldRelationConfig.model.name),
903
+ foreignKey: fieldRelationConfig.foreignKey,
904
+ properties: this.getProperties(fieldRelationConfig.model.name)
905
+ });
906
+ }
907
+ }
562
908
  }
563
909
  }
564
910
  return props;
@@ -587,6 +933,25 @@ class StoreProc {
587
933
  }
588
934
  }
589
935
 
936
+ class DbFunc {
937
+ constructor(funcName, type, queryConnection) {
938
+ this.funcName = funcName;
939
+ this.type = type;
940
+ this.queryConnection = queryConnection;
941
+ }
942
+ async execute(params) {
943
+ const items = await this.queryConnection.executeFunc(this.funcName, params);
944
+ return new List(items, this.type, this.properties);
945
+ }
946
+ get properties() {
947
+ const props = entityContainer.getProperties(this.type.name);
948
+ const jProps = {};
949
+ if (props)
950
+ props.forEach((property) => jProps[property.name.toLowerCase()] = property.name);
951
+ return jProps;
952
+ }
953
+ }
954
+
590
955
  class DbContext {
591
956
  get connection() {
592
957
  return this.dbConnectionInfo.connection;
@@ -614,6 +979,11 @@ class DbContext {
614
979
  for (const [key, value] of Object.entries(sps))
615
980
  this[key] = new StoreProc(value.spName, value.model(), this.queryConnection);
616
981
  }
982
+ onDbFuncRegistering(funcs) {
983
+ if (funcs)
984
+ for (const [key, value] of Object.entries(funcs))
985
+ this[key] = new DbFunc(value.funcName, value.model(), this.queryConnection);
986
+ }
617
987
  async saveChanges() {
618
988
  return new Promise(async (resolve, error) => {
619
989
  try {
@@ -633,4 +1003,4 @@ class DbContext {
633
1003
  }
634
1004
  }
635
1005
 
636
- export { DbContext, DbSet, StoreProc };
1006
+ export { DbConnectionContext, DbContext, DbFunc, DbSet, StoreProc };