@nattyjs/orm 0.0.1-beta.3 → 0.0.1-beta.30

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 CHANGED
@@ -344,6 +344,32 @@ class SqlDbTransaction {
344
344
  }
345
345
  }
346
346
 
347
+ const connectionCacheContainer = new class {
348
+ constructor() {
349
+ this.connectionPoolState = {};
350
+ }
351
+ getCacheKey(config) {
352
+ return `${config.server}-${config.database}`;
353
+ }
354
+ async getConnectionPool(mssql, config) {
355
+ return await new Promise((resolve, error) => {
356
+ const cacheKey = this.getCacheKey(config);
357
+ if (this.connectionPoolState[cacheKey])
358
+ return resolve(this.connectionPoolState[cacheKey]);
359
+ const pool = new mssql.ConnectionPool(config);
360
+ const errorHandler = (error2) => {
361
+ };
362
+ pool.on("error", errorHandler);
363
+ const connection = pool.connect((err) => {
364
+ if (err)
365
+ return error(err);
366
+ this.connectionPoolState[cacheKey] = connection;
367
+ resolve(connection);
368
+ });
369
+ });
370
+ }
371
+ }();
372
+
347
373
  class SqlConnection extends DbConnection {
348
374
  constructor(config) {
349
375
  super(config);
@@ -370,6 +396,7 @@ class SqlConnection extends DbConnection {
370
396
  }
371
397
  return records;
372
398
  } catch (ex) {
399
+ throw ex;
373
400
  }
374
401
  }
375
402
  async runNestedObjectQueries(outputInfo, relationProps, config) {
@@ -443,20 +470,23 @@ class SqlConnection extends DbConnection {
443
470
  else
444
471
  request.input(param.name, param.value);
445
472
  }
446
- const resolveQuery = (resolve) => (error, raw) => {
473
+ const resolveQuery = (resolve, errorResolver) => (error, raw) => {
447
474
  if (error)
448
- throw error;
475
+ errorResolver(error);
449
476
  resolve(raw);
450
477
  };
478
+ const catchFn = (ex) => {
479
+ throw ex;
480
+ };
451
481
  if (queryInfo.query) {
452
482
  const raw = await new Promise((resolve, error) => {
453
- request.query(queryInfo.query, resolveQuery(resolve));
454
- });
483
+ request.query(queryInfo.query, resolveQuery(resolve, error));
484
+ }).catch(catchFn);
455
485
  return raw;
456
486
  } else if (queryInfo.sp) {
457
487
  const raw = await new Promise((resolve, error) => {
458
- request.execute(queryInfo.sp, resolveQuery(resolve));
459
- });
488
+ request.execute(queryInfo.sp, resolveQuery(resolve, error));
489
+ }).catch(catchFn);
460
490
  return raw;
461
491
  }
462
492
  }
@@ -477,19 +507,7 @@ class SqlConnection extends DbConnection {
477
507
  return this.createPool();
478
508
  }
479
509
  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
- });
510
+ return await connectionCacheContainer.getConnectionPool(this.mssql, this.config);
493
511
  }
494
512
  }
495
513
 
@@ -649,6 +667,7 @@ class DbContext {
649
667
  }
650
668
  }
651
669
 
670
+ exports.DbConnectionContext = DbConnectionContext;
652
671
  exports.DbContext = DbContext;
653
672
  exports.DbSet = DbSet;
654
673
  exports.StoreProc = StoreProc;
package/dist/index.d.ts CHANGED
@@ -119,4 +119,4 @@ declare class StoreProc<SpName, Entity, SpParamterModel> {
119
119
  private get properties();
120
120
  }
121
121
 
122
- export { DbContext, DbSet, StoreProc };
122
+ export { DbConnectionContext, DbContext, DbSet, StoreProc };
package/dist/index.mjs CHANGED
@@ -328,6 +328,32 @@ class SqlDbTransaction {
328
328
  }
329
329
  }
330
330
 
331
+ const connectionCacheContainer = new class {
332
+ constructor() {
333
+ this.connectionPoolState = {};
334
+ }
335
+ getCacheKey(config) {
336
+ return `${config.server}-${config.database}`;
337
+ }
338
+ async getConnectionPool(mssql, config) {
339
+ return await new Promise((resolve, error) => {
340
+ const cacheKey = this.getCacheKey(config);
341
+ if (this.connectionPoolState[cacheKey])
342
+ return resolve(this.connectionPoolState[cacheKey]);
343
+ const pool = new mssql.ConnectionPool(config);
344
+ const errorHandler = (error2) => {
345
+ };
346
+ pool.on("error", errorHandler);
347
+ const connection = pool.connect((err) => {
348
+ if (err)
349
+ return error(err);
350
+ this.connectionPoolState[cacheKey] = connection;
351
+ resolve(connection);
352
+ });
353
+ });
354
+ }
355
+ }();
356
+
331
357
  class SqlConnection extends DbConnection {
332
358
  constructor(config) {
333
359
  super(config);
@@ -354,6 +380,7 @@ class SqlConnection extends DbConnection {
354
380
  }
355
381
  return records;
356
382
  } catch (ex) {
383
+ throw ex;
357
384
  }
358
385
  }
359
386
  async runNestedObjectQueries(outputInfo, relationProps, config) {
@@ -427,20 +454,23 @@ class SqlConnection extends DbConnection {
427
454
  else
428
455
  request.input(param.name, param.value);
429
456
  }
430
- const resolveQuery = (resolve) => (error, raw) => {
457
+ const resolveQuery = (resolve, errorResolver) => (error, raw) => {
431
458
  if (error)
432
- throw error;
459
+ errorResolver(error);
433
460
  resolve(raw);
434
461
  };
462
+ const catchFn = (ex) => {
463
+ throw ex;
464
+ };
435
465
  if (queryInfo.query) {
436
466
  const raw = await new Promise((resolve, error) => {
437
- request.query(queryInfo.query, resolveQuery(resolve));
438
- });
467
+ request.query(queryInfo.query, resolveQuery(resolve, error));
468
+ }).catch(catchFn);
439
469
  return raw;
440
470
  } else if (queryInfo.sp) {
441
471
  const raw = await new Promise((resolve, error) => {
442
- request.execute(queryInfo.sp, resolveQuery(resolve));
443
- });
472
+ request.execute(queryInfo.sp, resolveQuery(resolve, error));
473
+ }).catch(catchFn);
444
474
  return raw;
445
475
  }
446
476
  }
@@ -461,19 +491,7 @@ class SqlConnection extends DbConnection {
461
491
  return this.createPool();
462
492
  }
463
493
  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
- });
494
+ return await connectionCacheContainer.getConnectionPool(this.mssql, this.config);
477
495
  }
478
496
  }
479
497
 
@@ -633,4 +651,4 @@ class DbContext {
633
651
  }
634
652
  }
635
653
 
636
- export { DbContext, DbSet, StoreProc };
654
+ export { DbConnectionContext, DbContext, DbSet, StoreProc };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@nattyjs/orm",
3
- "version": "0.0.1-beta.3",
3
+ "version": "0.0.1-beta.30",
4
4
  "description": "",
5
5
  "module": "./dist/index.mjs",
6
6
  "main": "./dist/index.cjs",
@@ -18,9 +18,9 @@
18
18
  },
19
19
  "dependencies": {
20
20
  "mssql": "^9.1.1",
21
- "@nattyjs/core": "0.0.1-beta.3",
22
- "@nattyjs/common": "0.0.1-beta.3",
23
- "@nattyjs/entity": "0.0.1-beta.3",
24
- "@nattyjs/types": "0.0.1-beta.3"
21
+ "@nattyjs/core": "0.0.1-beta.30",
22
+ "@nattyjs/common": "0.0.1-beta.30",
23
+ "@nattyjs/entity": "0.0.1-beta.30",
24
+ "@nattyjs/types": "0.0.1-beta.30"
25
25
  }
26
26
  }