mythix-orm 1.5.5 → 1.6.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.
- package/lib/connection/connection-base.d.ts +180 -0
- package/lib/connection/connection-base.js +272 -16
- package/lib/connection/index.d.ts +5 -0
- package/lib/connection/literals/average-literal.d.ts +5 -0
- package/lib/connection/literals/count-literal.d.ts +5 -0
- package/lib/connection/literals/distinct-literal.d.ts +5 -0
- package/lib/connection/literals/field-literal.d.ts +5 -0
- package/lib/connection/literals/field-literal.js +14 -0
- package/lib/connection/literals/index.d.ts +10 -0
- package/lib/connection/literals/index.js +15 -13
- package/lib/connection/literals/literal-base.d.ts +22 -0
- package/lib/connection/literals/literal-base.js +2 -2
- package/lib/connection/literals/literal-field-base.d.ts +17 -0
- package/lib/connection/literals/literal-field-base.js +4 -4
- package/lib/connection/literals/literal.d.ts +5 -0
- package/lib/connection/literals/max-literal.d.ts +5 -0
- package/lib/connection/literals/min-literal.d.ts +5 -0
- package/lib/connection/literals/sum-literal.d.ts +5 -0
- package/lib/connection/query-generator-base.d.ts +121 -0
- package/lib/connection/query-generator-base.js +32 -26
- package/lib/field.d.ts +61 -0
- package/lib/field.js +9 -0
- package/lib/index.d.ts +7 -0
- package/lib/index.js +7 -1
- package/lib/interfaces/common.ts +3 -0
- package/lib/model.d.ts +205 -0
- package/lib/model.js +136 -32
- package/lib/proxy-class/proxy-class.js +6 -6
- package/lib/query-engine/field-scope.js +1 -0
- package/lib/query-engine/index.d.ts +1 -0
- package/lib/query-engine/model-scope.js +2 -2
- package/lib/query-engine/query-engine-base.js +18 -2
- package/lib/query-engine/query-engine.d.ts +144 -0
- package/lib/query-engine/query-engine.js +4 -0
- package/lib/types/concrete/bigint-type.d.ts +16 -0
- package/lib/types/concrete/bigint-type.js +8 -5
- package/lib/types/concrete/blob-type.d.ts +11 -0
- package/lib/types/concrete/blob-type.js +5 -4
- package/lib/types/concrete/boolean-type.d.ts +11 -0
- package/lib/types/concrete/boolean-type.js +4 -4
- package/lib/types/concrete/char-type.d.ts +11 -0
- package/lib/types/concrete/char-type.js +4 -4
- package/lib/types/concrete/date-type.d.ts +16 -0
- package/lib/types/concrete/date-type.js +9 -4
- package/lib/types/concrete/datetime-type.d.ts +16 -0
- package/lib/types/concrete/datetime-type.js +9 -4
- package/lib/types/concrete/foreign-key-type.d.ts +32 -0
- package/lib/types/concrete/foreign-key-type.js +274 -10
- package/lib/types/concrete/index.d.ts +19 -0
- package/lib/types/concrete/index.js +2 -0
- package/lib/types/concrete/integer-type.d.ts +16 -0
- package/lib/types/concrete/integer-type.js +6 -4
- package/lib/types/concrete/numeric-type.d.ts +11 -0
- package/lib/types/concrete/numeric-type.js +6 -4
- package/lib/types/concrete/real-type.d.ts +11 -0
- package/lib/types/concrete/real-type.js +111 -4
- package/lib/types/concrete/serialized-type.d.ts +25 -0
- package/lib/types/concrete/serialized-type.js +287 -5
- package/lib/types/concrete/string-type.d.ts +11 -0
- package/lib/types/concrete/string-type.js +0 -4
- package/lib/types/concrete/text-type.d.ts +11 -0
- package/lib/types/concrete/text-type.js +0 -4
- package/lib/types/concrete/uuid-base.d.ts +13 -0
- package/lib/types/concrete/uuid-base.js +4 -1
- package/lib/types/concrete/uuid-v1-type.d.ts +31 -0
- package/lib/types/concrete/uuid-v1-type.js +0 -4
- package/lib/types/concrete/uuid-v3-type.d.ts +27 -0
- package/lib/types/concrete/uuid-v3-type.js +0 -4
- package/lib/types/concrete/uuid-v4-type.d.ts +27 -0
- package/lib/types/concrete/uuid-v4-type.js +0 -4
- package/lib/types/concrete/uuid-v5-type.d.ts +27 -0
- package/lib/types/concrete/uuid-v5-type.js +0 -4
- package/lib/types/concrete/xid-type.d.ts +17 -0
- package/lib/types/concrete/xid-type.js +0 -4
- package/lib/types/helpers/default-helpers.d.ts +61 -0
- package/lib/types/helpers/index.d.ts +1 -0
- package/lib/types/index.d.ts +6 -0
- package/lib/types/index.js +2 -0
- package/lib/types/type.d.ts +89 -0
- package/lib/types/type.js +57 -25
- package/lib/types/virtual/index.d.ts +4 -0
- package/lib/types/virtual/model-type.d.ts +15 -0
- package/lib/types/virtual/model-type.js +2 -2
- package/lib/types/virtual/models-type.d.ts +15 -0
- package/lib/types/virtual/models-type.js +5 -5
- package/lib/types/virtual/relational-type-base.d.ts +46 -0
- package/lib/types/virtual/relational-type-base.js +12 -20
- package/lib/utils/index.d.ts +6 -0
- package/lib/utils/index.js +1 -4
- package/lib/utils/misc-utils.d.ts +4 -0
- package/lib/utils/misc-utils.js +0 -35
- package/lib/utils/model-utils.d.ts +90 -0
- package/lib/utils/model-utils.js +4 -4
- package/lib/utils/query-utils.d.ts +11 -0
- package/package.json +3 -2
- package/.biblorc.js +0 -29
- package/docs/Associations.md +0 -539
- package/docs/Certifications.md +0 -22
- package/docs/Home.md +0 -54
- package/docs/Query Engine.md +0 -428
|
@@ -0,0 +1,180 @@
|
|
|
1
|
+
import { EventEmitter } from 'events';
|
|
2
|
+
import { Moment } from 'moment';
|
|
3
|
+
import { Literals } from '.';
|
|
4
|
+
import Field from '../field';
|
|
5
|
+
import { GenericObject } from '../interfaces/common';
|
|
6
|
+
import { IterateFieldsCallback, ModelClass, Models, Model, HookContext, DirtyChanges } from '../model';
|
|
7
|
+
import { QueryEngine, QueryEngineClass } from '../query-engine/query-engine';
|
|
8
|
+
import { BigIntType, BlobType, BooleanType, CharType, DateTimeType, DateType, IntegerType, NumericType, RealType, StringType, TextType, Type, UUIDV1Type, UUIDV3Type, UUIDV4Type, UUIDV5Type, XIDType } from '../types';
|
|
9
|
+
import { DefaultValueContext } from '../types/helpers';
|
|
10
|
+
import { FullyQualifiedFieldDefinition } from '../utils/model-utils';
|
|
11
|
+
import { AverageLiteral, CountLiteral, DistinctLiteral, FieldLiteral, MaxLiteral, MinLiteral, SumLiteral } from './literals';
|
|
12
|
+
import LiteralBase from './literals/literal-base';
|
|
13
|
+
import QueryGeneratorBase from './query-generator-base';
|
|
14
|
+
|
|
15
|
+
export declare interface ConnectionBaseOptions {
|
|
16
|
+
QueryEngine: QueryEngineClass;
|
|
17
|
+
queryGenerator: QueryGeneratorBase;
|
|
18
|
+
models: Models | Array<ModelClass>;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
export declare type ModelCache = Map<ModelClass, Map<string, any>>;
|
|
22
|
+
|
|
23
|
+
export declare interface PreparedModels {
|
|
24
|
+
models: Array<Model>;
|
|
25
|
+
dirtyFields: Array<Field>;
|
|
26
|
+
dirtyModels: Array<Model>;
|
|
27
|
+
_mythixPreparedModels: boolean;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
export declare interface LockModeOptions {
|
|
31
|
+
modelName?: string;
|
|
32
|
+
lock?: boolean;
|
|
33
|
+
read?: boolean;
|
|
34
|
+
write?: boolean;
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
export declare interface LockMode {
|
|
38
|
+
modelName?: string;
|
|
39
|
+
lock: boolean;
|
|
40
|
+
read: boolean;
|
|
41
|
+
write: boolean;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
export declare interface QueryResults {
|
|
45
|
+
rows: Array<any>;
|
|
46
|
+
columns: Array<string>;
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
export declare interface DirtyFieldHelperContext {
|
|
50
|
+
options: GenericObject;
|
|
51
|
+
fieldData: GenericObject;
|
|
52
|
+
dirtyFieldData: GenericObject;
|
|
53
|
+
dirtyFields: DirtyChanges;
|
|
54
|
+
field: Field;
|
|
55
|
+
fieldName: string;
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
declare class ConnectionBase extends EventEmitter {
|
|
59
|
+
declare public static Literals: typeof Literals;
|
|
60
|
+
declare public static dialect: string;
|
|
61
|
+
declare public static _isMythixConnection: boolean;
|
|
62
|
+
|
|
63
|
+
public static isConnectionClass(value: any): boolean;
|
|
64
|
+
public static isConnection(value: any): boolean;
|
|
65
|
+
public static getLiteralClassByName(name: string): typeof LiteralBase;
|
|
66
|
+
public static Literal(name: string, ...args: Array<any>): LiteralBase;
|
|
67
|
+
|
|
68
|
+
declare public dialect: string;
|
|
69
|
+
declare protected _models: Models;
|
|
70
|
+
declare protected _options: ConnectionBaseOptions;
|
|
71
|
+
declare protected _modelCache: ModelCache;
|
|
72
|
+
declare protected queryGenerator: any; // TODO: Needs proper type
|
|
73
|
+
|
|
74
|
+
public constructor(options?: ConnectionBaseOptions);
|
|
75
|
+
public getLockMode(options: string | LockModeOptions): LockMode;
|
|
76
|
+
public getDefaultOrder(Model: ModelClass, options?: GenericObject): Array<string>;
|
|
77
|
+
public isLimitSupportedInContext(options?: GenericObject): boolean;
|
|
78
|
+
public isOrderSupportedInContext(options?: GenericObject): boolean | string;
|
|
79
|
+
public _getFromModelCache(Model: ModelClass, key: string, defaultValue?: any): any;
|
|
80
|
+
public _setToModelCache<T>(Model: ModelClass, key: string, value: T): T;
|
|
81
|
+
public getOptions(): ConnectionBaseOptions;
|
|
82
|
+
public isStarted(): boolean;
|
|
83
|
+
public toQueryEngine(queryEngineLike: any): QueryEngine | undefined;
|
|
84
|
+
public registerModel<T = ModelClass>(Model: T): T;
|
|
85
|
+
public registerModels(models: Models | Array<ModelClass>): Models | undefined;
|
|
86
|
+
public findModelField(finder: IterateFieldsCallback): Array<Field>;
|
|
87
|
+
public parseQualifiedName(fullyQualifiedName: string): FullyQualifiedFieldDefinition;
|
|
88
|
+
public getModels(): Models;
|
|
89
|
+
public getModel(modelName: string): ModelClass | undefined;
|
|
90
|
+
public getField(fieldName: string, modelName?: string): Field | undefined;
|
|
91
|
+
public getQueryEngineClass(): QueryEngineClass;
|
|
92
|
+
public getQueryGenerator(): QueryGeneratorBase;
|
|
93
|
+
public setQueryGenerator(queryGenerator: QueryGeneratorBase): void;
|
|
94
|
+
public _escape(value: any): string;
|
|
95
|
+
public escape(field: Field, value: any, options?: GenericObject): string;
|
|
96
|
+
public _escapeID(value: string): string;
|
|
97
|
+
public escapeID(value: LiteralBase | string, options?: GenericObject): string;
|
|
98
|
+
public _averageLiteralToString(literal: AverageLiteral, options?: GenericObject): string;
|
|
99
|
+
public _countLiteralToString(literal: CountLiteral, options?: GenericObject): string;
|
|
100
|
+
public _distinctLiteralToString(literal: DistinctLiteral, options?: GenericObject): string;
|
|
101
|
+
public _fieldLiteralToString(literal: FieldLiteral, options?: GenericObject): string;
|
|
102
|
+
public _maxLiteralToString(literal: MaxLiteral, options?: GenericObject): string;
|
|
103
|
+
public _minLiteralToString(literal: MinLiteral, options?: GenericObject): string;
|
|
104
|
+
public _sumLiteralToString(literal: SumLiteral, options?: GenericObject): string;
|
|
105
|
+
public literalToString(literal: LiteralBase, options?: GenericObject): string;
|
|
106
|
+
public _bigintTypeToString(type: BigIntType): string;
|
|
107
|
+
public _blobTypeToString(type: BlobType): string;
|
|
108
|
+
public _booleanTypeToString(type: BooleanType): string;
|
|
109
|
+
public _charTypeToString(type: CharType): string;
|
|
110
|
+
public _dateTypeToString(type: DateType): string;
|
|
111
|
+
public _datetimeTypeToString(type: DateTimeType): string;
|
|
112
|
+
public _numericTypeToString(type: NumericType): string;
|
|
113
|
+
public _realTypeToString(type: RealType): string;
|
|
114
|
+
public _integerTypeToString(type: IntegerType);
|
|
115
|
+
public _stringTypeToString(type: StringType): string
|
|
116
|
+
public _textTypeToString(type: TextType): string;
|
|
117
|
+
public _uuidV1TypeToString(type: UUIDV1Type): string;
|
|
118
|
+
public _uuidV3TypeToString(type: UUIDV3Type): string;
|
|
119
|
+
public _uuidV4TypeToString(type: UUIDV4Type): string;
|
|
120
|
+
public _uuidV5TypeToString(type: UUIDV5Type): string;
|
|
121
|
+
public _xidTypeToString(type: XIDType): string;
|
|
122
|
+
public typeToString(type: Type, options?: GenericObject): string;
|
|
123
|
+
public convertDateToDBTime(value: Date | Moment): Date;
|
|
124
|
+
public ensureAllModelsAreInstances(Model: ModelClass, models: Array<Model | GenericObject> | PreparedModels, options?: GenericObject): Array<Model>;
|
|
125
|
+
public prepareAllModelsForOperation(Model: ModelClass, models: Array<Model | GenericObject> | PreparedModels, options?: GenericObject): PreparedModels;
|
|
126
|
+
public splitModelAndSubModels(Model: ModelClass, primaryModel: Model, relationMap?: Map<string, Set<Model>>): Map<string, Set<Model>>;
|
|
127
|
+
public prepareAllModelsAndSubModelsForOperation(Model: ModelClass, models: Array<Model>, options?: GenericObject): Map<string, Array<Model>>;
|
|
128
|
+
|
|
129
|
+
public bulkModelOperation(
|
|
130
|
+
Model: ModelClass,
|
|
131
|
+
models: Array<Model | GenericObject> | PreparedModels,
|
|
132
|
+
options: GenericObject | null | undefined,
|
|
133
|
+
beforeCallback: ((Model: ModelClass, batchModelInstances: Array<Model>, options: GenericObject, queryGenerator: QueryGeneratorBase) => Promise<void>) | null | undefined,
|
|
134
|
+
callback: (Model: ModelClass, preparedModels: PreparedModels, options: GenericObject, queryGenerator: QueryGeneratorBase) => Promise<void>,
|
|
135
|
+
afterCallback: ((Model: ModelClass, models: Array<Model>, options: GenericObject, queryGenerator: QueryGeneratorBase) => Promise<void>) | null | undefined,
|
|
136
|
+
afterOperationCallback: ((Model: ModelClass, dirtyModels: Set<Model>, options: GenericObject, queryGenerator: QueryGeneratorBase) => Promise<void>) | null | undefined,
|
|
137
|
+
): Promise<Array<Model> | undefined>;
|
|
138
|
+
|
|
139
|
+
public setPersisted(models: Array<Models> | PreparedModels, value: boolean): void;
|
|
140
|
+
public start(): Promise<void>;
|
|
141
|
+
public stop(): Promise<void>;
|
|
142
|
+
|
|
143
|
+
public runSaveHooks(Model: ModelClass, models: Array<Model>, operationHookName: string, saveHookName: string, options: GenericObject): Promise<Array<any>>;
|
|
144
|
+
public dropTable(Model: ModelClass, options?: GenericObject): Promise<any>;
|
|
145
|
+
public dropTables(Models: Models, options?: GenericObject): Promise<Array<any>>;
|
|
146
|
+
public createTable(Model: ModelClass, options?: GenericObject): Promise<any>;
|
|
147
|
+
public createTables(Models: Models, options?: GenericObject): Promise<Array<any>>;
|
|
148
|
+
public defineTable(): Promise<any>;
|
|
149
|
+
public defineConstraints(): Promise<any>;
|
|
150
|
+
public defineIndexes(): Promise<any>;
|
|
151
|
+
public renameTable(): Promise<any>;
|
|
152
|
+
public renameColumn(): Promise<any>;
|
|
153
|
+
public dropColumn(): Promise<any>;
|
|
154
|
+
public alterColumn(): Promise<any>;
|
|
155
|
+
public addColumn(): Promise<any>;
|
|
156
|
+
public addConstraint(): Promise<any>;
|
|
157
|
+
public addIndex(): Promise<any>;
|
|
158
|
+
public insert(Model: ModelClass, models: Array<Model | GenericObject> | Model | GenericObject, options?: GenericObject): Promise<Array<Models> | undefined>;
|
|
159
|
+
public upsert(Model: ModelClass, models: Array<Model | GenericObject> | Model | GenericObject, options?: GenericObject): Promise<Array<Models> | undefined>;
|
|
160
|
+
public update(Model: ModelClass, models: Array<Model | GenericObject> | Model | GenericObject, options?: GenericObject): Promise<Array<Models> | undefined>;
|
|
161
|
+
public updateAll(queryEngine: QueryEngine | ModelClass, model: Model | GenericObject, options?: GenericObject): Promise<any>;
|
|
162
|
+
public destroyModels(Model: ModelClass, models: Array<Model> | Model, options?: GenericObject): Promise<Array<Model>>;
|
|
163
|
+
public destroy(queryEngineOrModel: QueryEngine | ModelClass, modelsOrOptions: Array<Models> | Model | GenericObject, options?: GenericObject): Promise<any>;
|
|
164
|
+
public select(queryEngine: QueryEngine, options?: GenericObject): AsyncIterator<Model>;
|
|
165
|
+
public aggregate(queryEngine: QueryEngine, literal: LiteralBase, options?: GenericObject): Promise<number>;
|
|
166
|
+
public average(queryEngine: QueryEngine, field: Field | string, options?: GenericObject): Promise<number>;
|
|
167
|
+
public count(queryEngine: QueryEngine, field: Field | string, options?: GenericObject): Promise<number>;
|
|
168
|
+
public min(queryEngine: QueryEngine, field: Field | string, options?: GenericObject): Promise<number>;
|
|
169
|
+
public max(queryEngine: QueryEngine, field: Field | string, options?: GenericObject): Promise<number>;
|
|
170
|
+
public sum(queryEngine: QueryEngine, field: Field | string, options?: GenericObject): Promise<number>;
|
|
171
|
+
public pluck(queryEngine: QueryEngine, fields: Array<Field> | Array<string> | Field | string, options?: GenericObject): Promise<Array<any> | Array<Array<any>>>;
|
|
172
|
+
public exists(queryEngine: QueryEngine, options?: GenericObject): Promise<boolean>;
|
|
173
|
+
public truncate(Model: ModelClass, options?: GenericObject): Promise<void>;
|
|
174
|
+
public query(sql: string | GenericObject, options?: GenericObject): Promise<any>;
|
|
175
|
+
public transaction(callback: (connection: ConnectionBase) => any, options?: GenericObject): Promise<any>;
|
|
176
|
+
public getDefaultFieldValue(type: string | LiteralBase, context: DefaultValueContext): Promise<any>;
|
|
177
|
+
public dirtyFieldHelper(context: DirtyFieldHelperContext): any;
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
export default ConnectionBase;
|
|
@@ -1,15 +1,15 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
|
|
3
|
-
const moment
|
|
4
|
-
const EventEmitter
|
|
5
|
-
const Nife
|
|
6
|
-
const SqlString
|
|
7
|
-
const { QueryEngine }
|
|
8
|
-
const Utils
|
|
9
|
-
const ModelBase
|
|
10
|
-
const Literals
|
|
11
|
-
const QueryGeneratorBase
|
|
12
|
-
const Types
|
|
3
|
+
const moment = require('moment');
|
|
4
|
+
const EventEmitter = require('events');
|
|
5
|
+
const Nife = require('nife');
|
|
6
|
+
const SqlString = require('sqlstring');
|
|
7
|
+
const { QueryEngine } = require('../query-engine');
|
|
8
|
+
const Utils = require('../utils');
|
|
9
|
+
const { Model: ModelBase } = require('../model');
|
|
10
|
+
const Literals = require('./literals');
|
|
11
|
+
const QueryGeneratorBase = require('./query-generator-base');
|
|
12
|
+
const Types = require('../types');
|
|
13
13
|
|
|
14
14
|
const LiteralBase = Literals.LiteralBase;
|
|
15
15
|
|
|
@@ -18,6 +18,8 @@ const LiteralBase = Literals.LiteralBase;
|
|
|
18
18
|
///
|
|
19
19
|
/// Alias: Connection
|
|
20
20
|
class ConnectionBase extends EventEmitter {
|
|
21
|
+
static Literals = Literals;
|
|
22
|
+
|
|
21
23
|
static dialect = 'none';
|
|
22
24
|
|
|
23
25
|
static _isMythixConnection = true;
|
|
@@ -48,6 +50,29 @@ class ConnectionBase extends EventEmitter {
|
|
|
48
50
|
return false;
|
|
49
51
|
}
|
|
50
52
|
|
|
53
|
+
static getLiteralClassByName(_name) {
|
|
54
|
+
if (!_name)
|
|
55
|
+
return;
|
|
56
|
+
|
|
57
|
+
let name = Nife.capitalize(_name.toLowerCase());
|
|
58
|
+
|
|
59
|
+
if (name === 'Literal')
|
|
60
|
+
return Literals.Literal;
|
|
61
|
+
else if (name === 'Base')
|
|
62
|
+
return Literals.LiteralBase;
|
|
63
|
+
|
|
64
|
+
return Literals[`${name}Literal`];
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
static Literal(name, ...args) {
|
|
68
|
+
const LiteralClass = this.getLiteralClassByName(name);
|
|
69
|
+
if (!LiteralClass)
|
|
70
|
+
throw new Error(`${this.constructor.name}::Literal: Unable to locate literal class for literal name "${name}".`);
|
|
71
|
+
|
|
72
|
+
let literal = new LiteralClass(...args);
|
|
73
|
+
return literal;
|
|
74
|
+
}
|
|
75
|
+
|
|
51
76
|
constructor(_options) {
|
|
52
77
|
super();
|
|
53
78
|
|
|
@@ -62,7 +87,7 @@ class ConnectionBase extends EventEmitter {
|
|
|
62
87
|
|
|
63
88
|
Object.defineProperties(this, {
|
|
64
89
|
'dialect': {
|
|
65
|
-
|
|
90
|
+
enumerable: false,
|
|
66
91
|
configurable: true,
|
|
67
92
|
get: () => {
|
|
68
93
|
return this.constructor.dialect;
|
|
@@ -72,25 +97,25 @@ class ConnectionBase extends EventEmitter {
|
|
|
72
97
|
},
|
|
73
98
|
'_models': {
|
|
74
99
|
writable: true,
|
|
75
|
-
|
|
100
|
+
enumerable: false,
|
|
76
101
|
configurable: true,
|
|
77
102
|
value: {},
|
|
78
103
|
},
|
|
79
104
|
'_options': {
|
|
80
105
|
writable: true,
|
|
81
|
-
|
|
106
|
+
enumerable: false,
|
|
82
107
|
configurable: true,
|
|
83
108
|
value: options,
|
|
84
109
|
},
|
|
85
110
|
'_modelCache': {
|
|
86
111
|
writable: true,
|
|
87
|
-
|
|
112
|
+
enumerable: false,
|
|
88
113
|
configurable: true,
|
|
89
114
|
value: new Map(),
|
|
90
115
|
},
|
|
91
116
|
'queryGenerator': {
|
|
92
117
|
writable: true,
|
|
93
|
-
|
|
118
|
+
enumerable: false,
|
|
94
119
|
configurable: true,
|
|
95
120
|
value: options.queryGenerator,
|
|
96
121
|
},
|
|
@@ -99,6 +124,32 @@ class ConnectionBase extends EventEmitter {
|
|
|
99
124
|
this.registerModels(options.models);
|
|
100
125
|
}
|
|
101
126
|
|
|
127
|
+
getLockMode(options) {
|
|
128
|
+
if (!options)
|
|
129
|
+
return { lock: false, read: false, write: false };
|
|
130
|
+
|
|
131
|
+
const throwError = () => {
|
|
132
|
+
throw new Error(`${this.constructor.name}::getLockMode: "lock" must be the name of a model (lock: "ModelName"), or an object specifying the model and the lock mode (lock: { modelName: "ModelName", read: true, write: true, dependents: true, noWait: false }).`);
|
|
133
|
+
};
|
|
134
|
+
|
|
135
|
+
if (Nife.instanceOf(options, 'string')) {
|
|
136
|
+
let Model = this.getModel(options);
|
|
137
|
+
if (!Model)
|
|
138
|
+
throwError();
|
|
139
|
+
|
|
140
|
+
return { lock: true, modelName: options, read: true, write: true };
|
|
141
|
+
} else if (Nife.instanceOf(options, 'object')) {
|
|
142
|
+
let modelName = options.modelName;
|
|
143
|
+
let Model = this.getModel(modelName);
|
|
144
|
+
if (!Model)
|
|
145
|
+
throwError();
|
|
146
|
+
|
|
147
|
+
return Object.assign({ lock: true, modelName, read: true, write: true }, options);
|
|
148
|
+
} else {
|
|
149
|
+
throwError();
|
|
150
|
+
}
|
|
151
|
+
}
|
|
152
|
+
|
|
102
153
|
getDefaultOrder(Model, options) {
|
|
103
154
|
let order = Model.defaultOrder(options);
|
|
104
155
|
if (!order)
|
|
@@ -121,6 +172,23 @@ class ConnectionBase extends EventEmitter {
|
|
|
121
172
|
return order.map((value) => ((value.indexOf(':') < 0) ? `${modelName}:${value}` : value));
|
|
122
173
|
}
|
|
123
174
|
|
|
175
|
+
isLimitSupportedInContext(options) {
|
|
176
|
+
return true;
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
isOrderSupportedInContext(_options) {
|
|
180
|
+
let options = _options || {};
|
|
181
|
+
if (options.isSubQuery) {
|
|
182
|
+
let subQueryOperator = options.subQueryOperator;
|
|
183
|
+
if (subQueryOperator === 'EXISTS' || subQueryOperator === 'NOT EXISTS')
|
|
184
|
+
return true;
|
|
185
|
+
|
|
186
|
+
return 'PROJECTION_ONLY';
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
return true;
|
|
190
|
+
}
|
|
191
|
+
|
|
124
192
|
_getFromModelCache(Model, key, defaultValue) {
|
|
125
193
|
let cache = this._modelCache.get(Model);
|
|
126
194
|
if (!cache)
|
|
@@ -324,6 +392,17 @@ class ConnectionBase extends EventEmitter {
|
|
|
324
392
|
return queryGenerator._distinctLiteralToString(literal, options);
|
|
325
393
|
}
|
|
326
394
|
|
|
395
|
+
_fieldLiteralToString(literal, options) {
|
|
396
|
+
if (!literal || !LiteralBase.isLiteral(literal))
|
|
397
|
+
return;
|
|
398
|
+
|
|
399
|
+
let queryGenerator = this.getQueryGenerator();
|
|
400
|
+
if (!queryGenerator)
|
|
401
|
+
return;
|
|
402
|
+
|
|
403
|
+
return queryGenerator._fieldLiteralToString(literal, options);
|
|
404
|
+
}
|
|
405
|
+
|
|
327
406
|
_maxLiteralToString(literal, options) {
|
|
328
407
|
if (!literal || !LiteralBase.isLiteral(literal))
|
|
329
408
|
return;
|
|
@@ -364,6 +443,8 @@ class ConnectionBase extends EventEmitter {
|
|
|
364
443
|
return this._countLiteralToString(literal, options);
|
|
365
444
|
else if (Literals.DistinctLiteral.isLiteralType(literal))
|
|
366
445
|
return this._distinctLiteralToString(literal, options);
|
|
446
|
+
else if (Literals.FieldLiteral.isLiteralType(literal))
|
|
447
|
+
return this._fieldLiteralToString(literal, options);
|
|
367
448
|
else if (Literals.MaxLiteral.isLiteralType(literal))
|
|
368
449
|
return this._maxLiteralToString(literal, options);
|
|
369
450
|
else if (Literals.MinLiteral.isLiteralType(literal))
|
|
@@ -601,7 +682,7 @@ class ConnectionBase extends EventEmitter {
|
|
|
601
682
|
Object.defineProperties(finalResult, {
|
|
602
683
|
'_mythixPreparedModels': {
|
|
603
684
|
writable: true,
|
|
604
|
-
|
|
685
|
+
enumerable: false,
|
|
605
686
|
configurable: true,
|
|
606
687
|
value: finalResult,
|
|
607
688
|
},
|
|
@@ -909,6 +990,156 @@ class ConnectionBase extends EventEmitter {
|
|
|
909
990
|
this.removeAllListeners();
|
|
910
991
|
}
|
|
911
992
|
|
|
993
|
+
async runSaveHooks(Model, models, operationHookName, saveHookName, options) {
|
|
994
|
+
const throwError = (error) => {
|
|
995
|
+
throw error;
|
|
996
|
+
};
|
|
997
|
+
|
|
998
|
+
let promises = [];
|
|
999
|
+
let context = { connection: this, Model, options, self: null };
|
|
1000
|
+
|
|
1001
|
+
for (let i = 0, il = models.length; i < il; i++) {
|
|
1002
|
+
let model = models[i];
|
|
1003
|
+
|
|
1004
|
+
context.self = model;
|
|
1005
|
+
|
|
1006
|
+
let promise = model[operationHookName](context);
|
|
1007
|
+
if (!Nife.instanceOf(promise, 'promise'))
|
|
1008
|
+
promise = Promise.resolve(promise);
|
|
1009
|
+
|
|
1010
|
+
promise = promise.then(async () => await model[saveHookName](context), throwError);
|
|
1011
|
+
if (!Nife.instanceOf(promise, 'promise'))
|
|
1012
|
+
promise = Promise.resolve(promise);
|
|
1013
|
+
|
|
1014
|
+
promises.push(promise);
|
|
1015
|
+
}
|
|
1016
|
+
|
|
1017
|
+
await Promise.all(promises);
|
|
1018
|
+
}
|
|
1019
|
+
|
|
1020
|
+
async dropTable(Model, options) {
|
|
1021
|
+
throw new Error(`${this.constructor.name}::dropTable: This operation is not supported for this connection type.`);
|
|
1022
|
+
}
|
|
1023
|
+
|
|
1024
|
+
async dropTables(_Models, options) {
|
|
1025
|
+
if (!_Models)
|
|
1026
|
+
return;
|
|
1027
|
+
|
|
1028
|
+
// First we collect all models and put them into a map
|
|
1029
|
+
let modelMap = _Models;
|
|
1030
|
+
|
|
1031
|
+
if (Nife.instanceOf(_Models, 'array', 'function')) {
|
|
1032
|
+
modelMap = {};
|
|
1033
|
+
|
|
1034
|
+
let Models = Nife.toArray(_Models).filter(Boolean);
|
|
1035
|
+
for (let i = 0, il = Models.length; i < il; i++) {
|
|
1036
|
+
let Model = Models[i];
|
|
1037
|
+
let modelName = Model.getModelName();
|
|
1038
|
+
|
|
1039
|
+
modelMap[modelName] = Model;
|
|
1040
|
+
}
|
|
1041
|
+
}
|
|
1042
|
+
|
|
1043
|
+
// Second we sort the model names in creation order,
|
|
1044
|
+
// and going in reverse of that order we destroy
|
|
1045
|
+
// each table.
|
|
1046
|
+
let modelNames = Object.keys(modelMap);
|
|
1047
|
+
let sortedModelNames = Utils.sortModelNamesByCreationOrder(this, modelNames);
|
|
1048
|
+
let results = [];
|
|
1049
|
+
|
|
1050
|
+
for (let i = sortedModelNames.length - 1; i >= 0; i--) {
|
|
1051
|
+
let modelName = sortedModelNames[i];
|
|
1052
|
+
let Model = modelMap[modelName];
|
|
1053
|
+
|
|
1054
|
+
results.push(await this.dropTable(Model, options));
|
|
1055
|
+
}
|
|
1056
|
+
|
|
1057
|
+
return results;
|
|
1058
|
+
}
|
|
1059
|
+
|
|
1060
|
+
async createTable(Model, options) {
|
|
1061
|
+
throw new Error(`${this.constructor.name}::createTable: This operation is not supported for this connection type.`);
|
|
1062
|
+
}
|
|
1063
|
+
|
|
1064
|
+
async createTables(_Models, options) {
|
|
1065
|
+
if (!_Models)
|
|
1066
|
+
return;
|
|
1067
|
+
|
|
1068
|
+
// First we collect all models and put them into a map
|
|
1069
|
+
let modelMap = _Models;
|
|
1070
|
+
|
|
1071
|
+
if (Nife.instanceOf(_Models, 'array', 'function')) {
|
|
1072
|
+
modelMap = {};
|
|
1073
|
+
|
|
1074
|
+
let Models = Nife.toArray(_Models).filter(Boolean);
|
|
1075
|
+
for (let i = 0, il = Models.length; i < il; i++) {
|
|
1076
|
+
let Model = Models[i];
|
|
1077
|
+
let modelName = Model.getModelName();
|
|
1078
|
+
|
|
1079
|
+
modelMap[modelName] = Model;
|
|
1080
|
+
}
|
|
1081
|
+
}
|
|
1082
|
+
|
|
1083
|
+
// Second we sort the model names in creation order,
|
|
1084
|
+
// and then create the tables in that order
|
|
1085
|
+
let modelNames = Object.keys(modelMap);
|
|
1086
|
+
let sortedModelNames = Utils.sortModelNamesByCreationOrder(this, modelNames);
|
|
1087
|
+
let results = [];
|
|
1088
|
+
|
|
1089
|
+
for (let i = 0, il = sortedModelNames.length; i < il; i++) {
|
|
1090
|
+
let modelName = sortedModelNames[i];
|
|
1091
|
+
let Model = modelMap[modelName];
|
|
1092
|
+
|
|
1093
|
+
results.push(await this.createTable(Model, options));
|
|
1094
|
+
}
|
|
1095
|
+
|
|
1096
|
+
return results;
|
|
1097
|
+
}
|
|
1098
|
+
|
|
1099
|
+
// Define operations
|
|
1100
|
+
|
|
1101
|
+
async defineTable() {
|
|
1102
|
+
throw new Error(`${this.constructor.name}::defineTable: This operation is not supported for this connection type.`);
|
|
1103
|
+
}
|
|
1104
|
+
|
|
1105
|
+
async defineConstraints() {
|
|
1106
|
+
throw new Error(`${this.constructor.name}::defineConstraints: This operation is not supported for this connection type.`);
|
|
1107
|
+
}
|
|
1108
|
+
|
|
1109
|
+
async defineIndexes() {
|
|
1110
|
+
throw new Error(`${this.constructor.name}::defineIndexes: This operation is not supported for this connection type.`);
|
|
1111
|
+
}
|
|
1112
|
+
|
|
1113
|
+
// Alter operations
|
|
1114
|
+
|
|
1115
|
+
async renameTable() {
|
|
1116
|
+
throw new Error(`${this.constructor.name}::renameTable: This operation is not supported for this connection type.`);
|
|
1117
|
+
}
|
|
1118
|
+
|
|
1119
|
+
async renameColumn() {
|
|
1120
|
+
throw new Error(`${this.constructor.name}::renameColumn: This operation is not supported for this connection type.`);
|
|
1121
|
+
}
|
|
1122
|
+
|
|
1123
|
+
async dropColumn() {
|
|
1124
|
+
throw new Error(`${this.constructor.name}::dropColumn: This operation is not supported for this connection type.`);
|
|
1125
|
+
}
|
|
1126
|
+
|
|
1127
|
+
async alterColumn() {
|
|
1128
|
+
throw new Error(`${this.constructor.name}::alterColumn: This operation is not supported for this connection type.`);
|
|
1129
|
+
}
|
|
1130
|
+
|
|
1131
|
+
async addColumn() {
|
|
1132
|
+
throw new Error(`${this.constructor.name}::addColumn: This operation is not supported for this connection type.`);
|
|
1133
|
+
}
|
|
1134
|
+
|
|
1135
|
+
async addConstraint() {
|
|
1136
|
+
throw new Error(`${this.constructor.name}::addConstraint: This operation is not supported for this connection type.`);
|
|
1137
|
+
}
|
|
1138
|
+
|
|
1139
|
+
async addIndex() {
|
|
1140
|
+
throw new Error(`${this.constructor.name}::addIndex: This operation is not supported for this connection type.`);
|
|
1141
|
+
}
|
|
1142
|
+
|
|
912
1143
|
// eslint-disable-next-line no-unused-vars
|
|
913
1144
|
async insert(Model, models, _options) {
|
|
914
1145
|
throw new Error(`${this.constructor.name}::insert: This operation is not supported for this connection type.`);
|
|
@@ -983,6 +1214,31 @@ class ConnectionBase extends EventEmitter {
|
|
|
983
1214
|
async exists(queryEngine, options) {
|
|
984
1215
|
throw new Error(`${this.constructor.name}::exists: This operation is not supported for this connection type.`);
|
|
985
1216
|
}
|
|
1217
|
+
|
|
1218
|
+
// eslint-disable-next-line no-unused-vars
|
|
1219
|
+
async truncate(Model, options) {
|
|
1220
|
+
throw new Error(`${this.constructor.name}::truncate: This operation is not supported for this connection type.`);
|
|
1221
|
+
}
|
|
1222
|
+
|
|
1223
|
+
// eslint-disable-next-line no-unused-vars
|
|
1224
|
+
async query(sql, options) {
|
|
1225
|
+
throw new Error(`${this.constructor.name}::transaction: This operation is not supported for this connection type.`);
|
|
1226
|
+
}
|
|
1227
|
+
|
|
1228
|
+
// eslint-disable-next-line no-unused-vars
|
|
1229
|
+
async transaction(callback, options) {
|
|
1230
|
+
throw new Error(`${this.constructor.name}::transaction: This operation is not supported for this connection type.`);
|
|
1231
|
+
}
|
|
1232
|
+
|
|
1233
|
+
// eslint-disable-next-line no-unused-vars
|
|
1234
|
+
async getDefaultFieldValue(type, context) {
|
|
1235
|
+
throw new Error(`${this.constructor.name}::getDefaultFieldValue: This operation is not supported for this connection type.`);
|
|
1236
|
+
}
|
|
1237
|
+
|
|
1238
|
+
// eslint-disable-next-line no-unused-vars
|
|
1239
|
+
dirtyFieldHelper(context) {
|
|
1240
|
+
|
|
1241
|
+
}
|
|
986
1242
|
}
|
|
987
1243
|
|
|
988
1244
|
module.exports = ConnectionBase;
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
const LiteralFieldBase = require('./literal-field-base');
|
|
4
|
+
|
|
5
|
+
class FieldLiteral extends LiteralFieldBase {
|
|
6
|
+
toString(connection, options) {
|
|
7
|
+
if (!connection)
|
|
8
|
+
return `${this.constructor.name} {}`;
|
|
9
|
+
|
|
10
|
+
return connection.literalToString(this, options);
|
|
11
|
+
}
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
module.exports = FieldLiteral;
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
export { default as AverageLiteral } from './average-literal';
|
|
2
|
+
export { default as CountLiteral } from './count-literal';
|
|
3
|
+
export { default as DistinctLiteral } from './distinct-literal';
|
|
4
|
+
export { default as FieldLiteral } from './field-literal';
|
|
5
|
+
export { default as Literal } from './literal';
|
|
6
|
+
export { default as LiteralBase } from './literal-base';
|
|
7
|
+
export { default as LiteralFieldBase } from './literal-field-base';
|
|
8
|
+
export { default as MaxLiteral } from './max-literal';
|
|
9
|
+
export { default as MinLiteral } from './min-literal';
|
|
10
|
+
export { default as SumLiteral } from './sum-literal';
|
|
@@ -1,23 +1,25 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
|
|
3
|
-
const
|
|
4
|
-
const
|
|
5
|
-
const
|
|
6
|
-
const
|
|
7
|
-
const
|
|
8
|
-
const
|
|
9
|
-
const
|
|
10
|
-
const
|
|
11
|
-
const
|
|
3
|
+
const AverageLiteral = require('./average-literal');
|
|
4
|
+
const CountLiteral = require('./count-literal');
|
|
5
|
+
const DistinctLiteral = require('./distinct-literal');
|
|
6
|
+
const FieldLiteral = require('./field-literal');
|
|
7
|
+
const Literal = require('./literal');
|
|
8
|
+
const LiteralBase = require('./literal-base');
|
|
9
|
+
const LiteralFieldBase = require('./literal-field-base');
|
|
10
|
+
const MaxLiteral = require('./max-literal');
|
|
11
|
+
const MinLiteral = require('./min-literal');
|
|
12
|
+
const SumLiteral = require('./sum-literal');
|
|
12
13
|
|
|
13
14
|
module.exports = {
|
|
14
|
-
LiteralBase,
|
|
15
|
-
LiteralFieldBase,
|
|
16
|
-
Literal,
|
|
17
15
|
AverageLiteral,
|
|
18
16
|
CountLiteral,
|
|
17
|
+
DistinctLiteral,
|
|
18
|
+
FieldLiteral,
|
|
19
|
+
Literal,
|
|
20
|
+
LiteralBase,
|
|
21
|
+
LiteralFieldBase,
|
|
19
22
|
MaxLiteral,
|
|
20
23
|
MinLiteral,
|
|
21
|
-
DistinctLiteral,
|
|
22
24
|
SumLiteral,
|
|
23
25
|
};
|