mythix-orm 1.8.3 → 1.11.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.
@@ -81,6 +81,7 @@ 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 finalizeQuery(operation: string, query: QueryEngine, options: GenericObject): Promise<QueryEngine>
84
85
  public registerModel<T = ModelClass>(Model: T, options?: GenericObject): T;
85
86
  public registerModels(models: Models | Array<ModelClass>, options?: GenericObject): Models | undefined;
86
87
  public getContextValue(key: any, defaultValue?: any): any;
@@ -238,7 +238,7 @@ class ConnectionBase extends EventEmitter {
238
238
  get: () => {
239
239
  return this.constructor.dialect;
240
240
  },
241
- set: () => {
241
+ set: () => {
242
242
  },
243
243
  },
244
244
  '_models': {
@@ -346,25 +346,6 @@ class ConnectionBase extends EventEmitter {
346
346
  /// are also valid return values (in which case no order will be
347
347
  /// applied to the given operation).
348
348
  getDefaultOrder(Model, options) {
349
- let order = Model.defaultOrder(options);
350
- if (!order)
351
- return;
352
-
353
- order = Nife.arrayFlatten(Nife.toArray(order)).filter((value) => {
354
- if (!value)
355
- return false;
356
-
357
- if (!Nife.instanceOf(value, 'string'))
358
- return false;
359
-
360
- return true;
361
- });
362
-
363
- if (Nife.isEmpty(order))
364
- return;
365
-
366
- let modelName = Model.getModelName();
367
- return order.map((value) => ((value.indexOf(':') < 0) ? `${modelName}:${value}` : value));
368
349
  }
369
350
 
370
351
  /// This method is called (and often provided)
@@ -396,6 +377,9 @@ class ConnectionBase extends EventEmitter {
396
377
  if (options.isSubQuery) {
397
378
  let subQueryOperator = options.subQueryOperator;
398
379
  if (subQueryOperator === 'EXISTS' || subQueryOperator === 'NOT EXISTS')
380
+ return false;
381
+
382
+ if (subQueryOperator === 'ANY' || subQueryOperator === 'ALL')
399
383
  return true;
400
384
 
401
385
  return 'PROJECTION_ONLY';
@@ -489,6 +473,73 @@ class ConnectionBase extends EventEmitter {
489
473
  return queryEngine;
490
474
  }
491
475
 
476
+ async finalizeQuery(crudOperation, queryEngine, options) {
477
+ if (!QueryEngine.isQuery(queryEngine))
478
+ return queryEngine;
479
+
480
+ const finalizeQueryForModel = async (query, parent, contextKey, depth) => {
481
+ let operations = query.getOperationStack();
482
+
483
+ // Has query already been finalized?
484
+ let lastOperation = operations[operations.length - 1];
485
+ if (lastOperation && Object.prototype.hasOwnProperty.call(lastOperation, '_queryFinalized') && lastOperation._queryFinalized)
486
+ return query;
487
+
488
+ let newQuery = query.clone();
489
+ let alreadyVisitedModels = new Set();
490
+
491
+ for (let i = 0, il = operations.length; i < il; i++) {
492
+ let operation = operations[i];
493
+ if (!Object.prototype.hasOwnProperty.call(operation, 'modelName'))
494
+ continue;
495
+
496
+ let modelName = operation.modelName;
497
+ if (alreadyVisitedModels.has(modelName))
498
+ continue;
499
+
500
+ alreadyVisitedModels.add(modelName);
501
+
502
+ let Model = operation.Model;
503
+ if (typeof Model.finalizeQuery !== 'function')
504
+ continue;
505
+
506
+ newQuery = await Model.finalizeQuery({
507
+ type: crudOperation,
508
+ query: newQuery,
509
+ queryDepth: depth,
510
+ operationIndex: i,
511
+ connection: this,
512
+ Model,
513
+ modelName,
514
+ operation,
515
+ operations,
516
+ parent,
517
+ contextKey,
518
+ options,
519
+ });
520
+
521
+ if (parent && contextKey)
522
+ parent[contextKey] = newQuery;
523
+ }
524
+
525
+ // Mark as finalized
526
+ newQuery.getOperationContext()._queryFinalized = true;
527
+
528
+ return newQuery;
529
+ };
530
+
531
+ let promises = [];
532
+
533
+ queryEngine.walk((query, parent, contextKey, depth) => {
534
+ promises.push(finalizeQueryForModel(query, parent, contextKey, depth));
535
+ });
536
+
537
+ if (promises.length > 0)
538
+ await Promise.all(promises);
539
+
540
+ return await finalizeQueryForModel(queryEngine, null, null, 0);
541
+ }
542
+
492
543
  /// Register the provided model class.
493
544
  ///
494
545
  /// This will register the provided model class with this
@@ -1,6 +1,7 @@
1
1
  'use strict';
2
2
 
3
3
  const ModelUtils = require('../../utils/model-utils');
4
+ const Field = require('../../field');
4
5
 
5
6
  class LiteralBase {
6
7
  static _isMythixLiteral = true;
@@ -31,6 +32,13 @@ class LiteralBase {
31
32
  return false;
32
33
  }
33
34
 
35
+ isLiteral(value) {
36
+ if (arguments.length === 0)
37
+ return true;
38
+
39
+ return this.constructor.isLiteral(value);
40
+ }
41
+
34
42
  static isLiteralType(value) {
35
43
  return (this.isLiteral(value) && value.constructor && value.constructor.name === this.name);
36
44
  }
@@ -80,8 +88,19 @@ class LiteralBase {
80
88
  return definition;
81
89
 
82
90
  let field = connection.getField(definition.fieldNames[0], definition.modelName);
83
- if (!field)
91
+ if (!field) {
92
+ if (definition.fieldNames[0] && definition.modelName) {
93
+ let Model = connection.getModel(definition.modelName);
94
+ if (Model) {
95
+ return new Field({
96
+ fieldName: definition.fieldNames[0],
97
+ Model,
98
+ });
99
+ }
100
+ }
101
+
84
102
  throw new Error(`${this.constructor.name}::definitionToField: Unable to locate field "${definition.modelName}"."${definition.fieldNames[0]}".`);
103
+ }
85
104
 
86
105
  return field;
87
106
  }
@@ -69,7 +69,6 @@ export declare interface FieldOrderInfo {
69
69
  declare class QueryGeneratorBase {
70
70
  public constructor(connection);
71
71
  public stackAssign(obj: GenericObject, ...args: Array<GenericObject>): GenericObject;
72
- public getOptionsCache(options: GenericObject, keyPath: string, initialValue: any): any;
73
72
  public setOptionsCache(options: GenericObject, keyPath: string, value: any): void;
74
73
  public escape(field: Field, value: any, options?: GenericObject): string;
75
74
  public escapeID(value: LiteralBase | string, options?: GenericObject): string;
@@ -77,11 +76,7 @@ declare class QueryGeneratorBase {
77
76
  public getEscapedColumnName(Model: ModelClass | null | undefined, field: Field, options?: GetEscapedColumnNameOptions): string;
78
77
  public getEscapedTableName(modelOrField: ModelClass | Field, options?: GetEscapedTableNameNameOptions): string;
79
78
  public getEscapedProjectionName(Model: ModelClass | null | undefined, field: Field, options?: GetEscapedProjectionNameOptions): string;
80
- public getEscapedModelFields(Model: ModelClass, options?: GetEscapedModelFieldsOptions): { [ key: string ]: string };
81
- public getAllModelsUsedInQuery(queryEngine: QueryEngine, options?: GenericObject): Array<ModelClass>;
82
- public getProjectionRequiredFields(queryEngine: QueryEngine, options?: GenericObject): Map<string, ProjectedFieldInfo>;
83
- public sortedProjectedFields(projectedFields: Array<LiteralBase | string>, options?: GenericObject): Array<LiteralBase | string>;
84
- public getProjectionFromQueryEngine(queryEngine: QueryEngine, options?: GenericObject): Array<LiteralBase | string | ProjectedFieldInfo>;
79
+ public getEscapedModelFields(Model: ModelClass, options?: GetEscapedModelFieldsOptions): { [key: string]: string };
85
80
  public isFieldIdentifier(value: string): boolean;
86
81
  public getProjectedFields(queryEngine: QueryEngine, options?: GenericObject, asMap?: false | undefined): Array<string>;
87
82
  public getProjectedFields(queryEngine: QueryEngine, options?: GenericObject, asMap?: true): Map<string, string>;
@@ -96,15 +91,6 @@ declare class QueryGeneratorBase {
96
91
  public getFieldDirectionSpecifier(order: LiteralBase): LiteralBase;
97
92
  public getFieldDirectionSpecifier(order: string | Field): FieldDirectionInfo;
98
93
 
99
- public getOrderLimitOffset(
100
- queryEngine: QueryEngine,
101
- options?: GenericObject,
102
- ): {
103
- limit: number | undefined,
104
- offset: number | undefined,
105
- order: Array<FieldOrderInfo>,
106
- };
107
-
108
94
  public getQuerySliceFromQueryPart(queryPart: GenericObject): Array<GenericObject>;
109
95
  public _averageLiteralToString(literal: AverageLiteral, options?: GenericObject): string;
110
96
  public _countLiteralToString(literal: CountLiteral, options?: GenericObject): string;