mythix-orm 1.8.0 → 1.8.1

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.
@@ -81,8 +81,8 @@ declare class ConnectionBase extends EventEmitter {
81
81
  public getOptions(): ConnectionBaseOptions;
82
82
  public isStarted(): boolean;
83
83
  public toQueryEngine(queryEngineLike: any): QueryEngine | undefined;
84
- public registerModel<T = ModelClass>(Model: T): T;
85
- public registerModels(models: Models | Array<ModelClass>): Models | undefined;
84
+ public registerModel<T = ModelClass>(Model: T, options?: GenericObject): T;
85
+ public registerModels(models: Models | Array<ModelClass>, options?: GenericObject): Models | undefined;
86
86
  public getContextValue(key: any, defaultValue?: any): any;
87
87
  public setContextValue(key: any, value: any): any;
88
88
  public buildConnectionContext(connection?: ConnectionBase): Map<any, any>;
@@ -236,15 +236,15 @@ class ConnectionBase extends EventEmitter {
236
236
  return queryEngine;
237
237
  }
238
238
 
239
- registerModel(_Model) {
240
- let Model = _Model.bindConnection(this);
239
+ registerModel(_Model, options) {
240
+ let Model = _Model.bindConnection(this, options);
241
241
 
242
242
  this._models[Model.getModelName()] = Model;
243
243
 
244
244
  return Model;
245
245
  }
246
246
 
247
- registerModels(models) {
247
+ registerModels(models, options) {
248
248
  if (!models)
249
249
  return;
250
250
 
@@ -254,7 +254,7 @@ class ConnectionBase extends EventEmitter {
254
254
  let key = keys[i];
255
255
  let Model = models[key];
256
256
 
257
- this.registerModel(Model);
257
+ this.registerModel(Model, options);
258
258
  }
259
259
 
260
260
  return this._models;
@@ -330,8 +330,8 @@ class ConnectionBase extends EventEmitter {
330
330
  }
331
331
 
332
332
  async createContext(callback, _connection, thisArg) {
333
- let context = this.buildConnectionContext(_connection);
334
333
  let connection = _connection || this;
334
+ let context = this.buildConnectionContext(connection);
335
335
 
336
336
  return await Utils.runInContext(context, async () => {
337
337
  return await callback.call(thisArg, connection);
package/lib/model.js CHANGED
@@ -458,6 +458,14 @@ class Model {
458
458
  return this.constructor._getConnection();
459
459
  }
460
460
 
461
+ static getConnection(_connection) {
462
+ let connection = this._getConnection(_connection);
463
+ if (!connection)
464
+ throw new Error(`${this.getModelName()}::getConnection: No connection bound to model. You need to provide a connection for this operation.`);
465
+
466
+ return connection;
467
+ }
468
+
461
469
  /// Get the underlying connection bound to
462
470
  /// the model's class. Connection binding is
463
471
  /// optional, so this method can also be provided
@@ -470,6 +478,15 @@ class Model {
470
478
  /// you wish to provide your own model specific
471
479
  /// connection.
472
480
  ///
481
+ /// This method will also search for a `modelInstance.connection`,
482
+ /// and return that connection if found.
483
+ ///
484
+ /// Note:
485
+ /// `static getConnection` is simply a proxy for
486
+ /// `static _getConnection`, whereas this instance
487
+ /// method will also attempt to find a connection
488
+ /// on the model instance itself.
489
+ ///
473
490
  /// Return: <see>Connection</see>
474
491
  ///
475
492
  /// Arguments:
@@ -479,14 +496,6 @@ class Model {
479
496
  /// Otherwise, if not provided, or a falsy value,
480
497
  /// then attempt to fallback to the bound connection,
481
498
  /// if any is available.
482
- static getConnection(_connection) {
483
- let connection = this._getConnection(_connection);
484
- if (!connection)
485
- throw new Error(`${this.getModelName()}::getConnection: No connection bound to model. You need to provide a connection for this operation.`);
486
-
487
- return connection;
488
- }
489
-
490
499
  getConnection(_connection) {
491
500
  if (_connection)
492
501
  return _connection;
@@ -514,14 +523,14 @@ class Model {
514
523
  /// connection?: <see>Connection</see>
515
524
  /// The connection instance to bind to
516
525
  /// this model class.
517
- static bindConnection(connection) {
526
+ static bindConnection(connection, options) {
518
527
  let ModelClass = this;
519
528
 
520
529
  // Ensure that the model fields
521
530
  // are constructed properly
522
531
  ModelClass.getFields();
523
532
 
524
- let connectionOptions = connection.getOptions();
533
+ let connectionOptions = { ...(connection.getOptions() || {}), ...(options || {}) };
525
534
  if (connectionOptions && connectionOptions.bindModels === false) {
526
535
  // let modelName = ModelClass.getModelName();
527
536
 
@@ -544,7 +553,7 @@ class Model {
544
553
  enumerable: false,
545
554
  configurable: true,
546
555
  get: () => {
547
- return ModelClass.getQueryEngine(connection);
556
+ return ModelClass.getQueryEngine(ModelClass._getConnection() || connection);
548
557
  },
549
558
  set: () => {},
550
559
  };
@@ -1,3 +1,4 @@
1
+ export declare function getContextStore(): any;
1
2
  export declare function getContextValue(key: any, defaultValue: any): any;
2
3
  export declare function setContextValue(key: any, value: any): void;
3
4
  export declare function runInContext(context: any, callback: Function): Promise<any>;
@@ -12,6 +12,10 @@ try {
12
12
  };
13
13
  }
14
14
 
15
+ function getContextStore() {
16
+ return globalAsyncStore.getStore();
17
+ }
18
+
15
19
  function getContextValue(key, defaultValue) {
16
20
  let store = globalAsyncStore.getStore();
17
21
  while (store) {
@@ -52,4 +56,5 @@ module.exports = {
52
56
  getContextValue,
53
57
  setContextValue,
54
58
  runInContext,
59
+ getContextStore,
55
60
  };
@@ -34,6 +34,7 @@ const {
34
34
  } = QueryUtils;
35
35
 
36
36
  const {
37
+ getContextStore,
37
38
  getContextValue,
38
39
  setContextValue,
39
40
  runInContext,
@@ -71,6 +72,7 @@ module.exports = {
71
72
  generateQueryFromFilter,
72
73
 
73
74
  // AsyncStore
75
+ getContextStore,
74
76
  getContextValue,
75
77
  setContextValue,
76
78
  runInContext,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "mythix-orm",
3
- "version": "1.8.0",
3
+ "version": "1.8.1",
4
4
  "description": "ORM for Mythix framework",
5
5
  "main": "lib/index",
6
6
  "type": "commonjs",