mythix-orm-sql-base 1.4.8 → 1.5.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/index.d.ts ADDED
@@ -0,0 +1,4 @@
1
+ export * from './sql-connection-base';
2
+ export { default as SQLConnectionBase } from './sql-connection-base';
3
+ export * from './sql-query-generator-base';
4
+ export { default as SQLQueryGeneratorBase } from './sql-query-generator-base';
@@ -0,0 +1,36 @@
1
+ import { ConnectionBase, Field, ModelClass, QueryEngine, QueryResults, Model } from 'mythix-orm';
2
+ import { GenericObject } from 'mythix-orm/lib/interfaces/common';
3
+
4
+ export declare interface ModelDataFromQueryResults {
5
+ [ key: string ]: Array<GenericObject>;
6
+ }
7
+
8
+ declare class SQLConnectionBase extends ConnectionBase {
9
+ public prepareArrayValuesForSQL(array: Array<any>): Array<any>;
10
+ public generateSavePointName(): string;
11
+
12
+ public findAllFieldsFromFieldProjectionMap(
13
+ projectionFieldMap: Map<string, string> | Array<string>
14
+ ): Array<Field>;
15
+
16
+ public buildModelDataMapFromSelectResults(
17
+ queryEngine: QueryEngine,
18
+ result: QueryResults
19
+ ): ModelDataFromQueryResults;
20
+
21
+ public enableForeignKeyConstraints(enable: boolean): Promise<void>;
22
+
23
+ buildModelsFromModelDataMap(
24
+ queryEngine: QueryEngine,
25
+ modelDataMap: ModelDataFromQueryResults,
26
+ callback: (Model: ModelClass, model: Model) => Model,
27
+ ): Array<Model>;
28
+
29
+ updateModelsFromResults(
30
+ Model: ModelClass,
31
+ storedModels: Array<Model>,
32
+ results: QueryResults
33
+ ): Array<Model>;
34
+ }
35
+
36
+ export default SQLConnectionBase;
@@ -14,57 +14,6 @@ const SAVE_POINT_NAME_CHARS = [ 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P'
14
14
  const MODEL_RELATIONS = Symbol.for('_mythixModelRelations');
15
15
 
16
16
  class SQLConnectionBase extends ConnectionBase {
17
- static Literals = Literals;
18
-
19
- static getLiteralClassByName(_name) {
20
- if (!_name)
21
- return;
22
-
23
- let name = Nife.capitalize(_name.toLowerCase());
24
-
25
- if (name === 'Literal')
26
- return Literals.Literal;
27
- else if (name === 'Base')
28
- return Literals.LiteralBase;
29
-
30
- return Literals[`${name}Literal`];
31
- }
32
-
33
- static Literal(name, ...args) {
34
- const LiteralClass = this.getLiteralClassByName(name);
35
- if (!LiteralClass)
36
- throw new Error(`${this.constructor.name}::Literal: Unable to locate literal class for literal name "${name}".`);
37
-
38
- let literal = new LiteralClass(...args);
39
- return literal;
40
- }
41
-
42
- getLockMode(options) {
43
- if (!options)
44
- return { lock: false, read: false, write: false };
45
-
46
- const throwError = () => {
47
- 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 }).`);
48
- };
49
-
50
- if (Nife.instanceOf(options, 'string')) {
51
- let Model = this.getModel(options);
52
- if (!Model)
53
- throwError();
54
-
55
- return { lock: true, modelName: options, read: true, write: true };
56
- } else if (Nife.instanceOf(options, 'object')) {
57
- let modelName = options.modelName;
58
- let Model = this.getModel(modelName);
59
- if (!Model)
60
- throwError();
61
-
62
- return Object.assign({ lock: true, read: true, write: true }, options);
63
- } else {
64
- throwError();
65
- }
66
- }
67
-
68
17
  prepareArrayValuesForSQL(_array) {
69
18
  let array = Nife.arrayFlatten(_array);
70
19
 
@@ -233,7 +182,7 @@ class SQLConnectionBase extends ConnectionBase {
233
182
  Object.defineProperties(rootModel, {
234
183
  [MODEL_RELATIONS]: {
235
184
  writable: true,
236
- enumberable: false,
185
+ enumerable: false,
237
186
  configurable: true,
238
187
  value: {},
239
188
  },
@@ -251,6 +200,11 @@ class SQLConnectionBase extends ConnectionBase {
251
200
  return modelData;
252
201
  }
253
202
 
203
+ // eslint-disable-next-line no-unused-vars
204
+ async enableForeignKeyConstraints(enable) {
205
+ throw new Error(`${this.constructor.name}::enableForeignKeyConstraints: This operation is not supported for this connection type.`);
206
+ }
207
+
254
208
  buildModelsFromModelDataMap(queryEngine, modelDataMap, callback) {
255
209
  if (Nife.isEmpty(modelDataMap))
256
210
  return [];
@@ -325,30 +279,7 @@ class SQLConnectionBase extends ConnectionBase {
325
279
  return storedModels;
326
280
  }
327
281
 
328
- async runSaveHooks(Model, models, operationHookName, saveHookName, options) {
329
- const throwError = (error) => {
330
- throw error;
331
- };
332
-
333
- let promises = [];
334
- let context = { connection: this, Model, options };
335
-
336
- for (let i = 0, il = models.length; i < il; i++) {
337
- let model = models[i];
338
- let promise = model[operationHookName](context);
339
-
340
- if (!Nife.instanceOf(promise, 'promise'))
341
- promise = Promise.resolve(promise);
342
-
343
- promise = promise.then(async () => await model[saveHookName](context), throwError);
344
- if (!Nife.instanceOf(promise, 'promise'))
345
- promise = Promise.resolve(promise);
346
-
347
- promises.push(promise);
348
- }
349
-
350
- await Promise.all(promises);
351
- }
282
+ // --------------------------------------------- //
352
283
 
353
284
  async dropTable(Model, options) {
354
285
  let queryGenerator = this.getQueryGenerator();
@@ -358,42 +289,6 @@ class SQLConnectionBase extends ConnectionBase {
358
289
  return await this.query(createTableSQL, options);
359
290
  }
360
291
 
361
- async dropTables(_Models, options) {
362
- if (!_Models)
363
- return;
364
-
365
- // First we collect all models and put them into a map
366
- let modelMap = _Models;
367
-
368
- if (Nife.instanceOf(_Models, 'array', 'function')) {
369
- modelMap = {};
370
-
371
- let Models = Nife.toArray(_Models).filter(Boolean);
372
- for (let i = 0, il = Models.length; i < il; i++) {
373
- let Model = Models[i];
374
- let modelName = Model.getModelName();
375
-
376
- modelMap[modelName] = Model;
377
- }
378
- }
379
-
380
- // Second we sort the model names in creation order,
381
- // and going in reverse of that order we destroy
382
- // each table.
383
- let modelNames = Object.keys(modelMap);
384
- let sortedModelNames = Utils.sortModelNamesByCreationOrder(this, modelNames);
385
- let results = [];
386
-
387
- for (let i = sortedModelNames.length - 1; i >= 0; i--) {
388
- let modelName = sortedModelNames[i];
389
- let Model = modelMap[modelName];
390
-
391
- results.push(await this.dropTable(Model, options));
392
- }
393
-
394
- return results;
395
- }
396
-
397
292
  async createTable(Model, options) {
398
293
  let queryGenerator = this.getQueryGenerator();
399
294
  let createTableSQL = queryGenerator.generateCreateTableStatement(Model, options);
@@ -413,41 +308,6 @@ class SQLConnectionBase extends ConnectionBase {
413
308
  return result;
414
309
  }
415
310
 
416
- async createTables(_Models, options) {
417
- if (!_Models)
418
- return;
419
-
420
- // First we collect all models and put them into a map
421
- let modelMap = _Models;
422
-
423
- if (Nife.instanceOf(_Models, 'array', 'function')) {
424
- modelMap = {};
425
-
426
- let Models = Nife.toArray(_Models).filter(Boolean);
427
- for (let i = 0, il = Models.length; i < il; i++) {
428
- let Model = Models[i];
429
- let modelName = Model.getModelName();
430
-
431
- modelMap[modelName] = Model;
432
- }
433
- }
434
-
435
- // Second we sort the model names in creation order,
436
- // and then create the tables in that order
437
- let modelNames = Object.keys(modelMap);
438
- let sortedModelNames = Utils.sortModelNamesByCreationOrder(this, modelNames);
439
- let results = [];
440
-
441
- for (let i = 0, il = sortedModelNames.length; i < il; i++) {
442
- let modelName = sortedModelNames[i];
443
- let Model = modelMap[modelName];
444
-
445
- results.push(await this.createTable(Model, options));
446
- }
447
-
448
- return results;
449
- }
450
-
451
311
  async insert(Model, models, _options) {
452
312
  return await this.bulkModelOperation(
453
313
  Model,
@@ -860,55 +720,6 @@ class SQLConnectionBase extends ConnectionBase {
860
720
  let count = await this.count(queryEngine, null, options);
861
721
  return (count > 0);
862
722
  }
863
-
864
- // eslint-disable-next-line no-unused-vars
865
- async enableForeignKeyConstraints(enable) {
866
- throw new Error(`${this.constructor.name}::enableForeignKeyConstraints: This operation is not supported for this connection type.`);
867
- }
868
-
869
- // Define operations
870
-
871
- async defineTable() {
872
- throw new Error(`${this.constructor.name}::defineTable: This operation is not supported for this connection type.`);
873
- }
874
-
875
- async defineConstraints() {
876
- throw new Error(`${this.constructor.name}::defineConstraints: This operation is not supported for this connection type.`);
877
- }
878
-
879
- async defineIndexes() {
880
- throw new Error(`${this.constructor.name}::defineIndexes: This operation is not supported for this connection type.`);
881
- }
882
-
883
- // Alter operations
884
-
885
- async renameTable() {
886
- throw new Error(`${this.constructor.name}::renameTable: This operation is not supported for this connection type.`);
887
- }
888
-
889
- async renameColumn() {
890
- throw new Error(`${this.constructor.name}::renameColumn: This operation is not supported for this connection type.`);
891
- }
892
-
893
- async dropColumn() {
894
- throw new Error(`${this.constructor.name}::dropColumn: This operation is not supported for this connection type.`);
895
- }
896
-
897
- async alterColumn() {
898
- throw new Error(`${this.constructor.name}::alterColumn: This operation is not supported for this connection type.`);
899
- }
900
-
901
- async addColumn() {
902
- throw new Error(`${this.constructor.name}::addColumn: This operation is not supported for this connection type.`);
903
- }
904
-
905
- async addConstraint() {
906
- throw new Error(`${this.constructor.name}::addConstraint: This operation is not supported for this connection type.`);
907
- }
908
-
909
- async addIndex() {
910
- throw new Error(`${this.constructor.name}::addIndex: This operation is not supported for this connection type.`);
911
- }
912
723
  }
913
724
 
914
725
  module.exports = SQLConnectionBase;
@@ -0,0 +1,181 @@
1
+ import { Field, FieldOrderInfo, JoinTableInfo, Model, ModelClass, PreparedModels, QueryEngine, QueryGeneratorBase } from 'mythix-orm';
2
+ import { LiteralBase } from 'mythix-orm/lib/connection/literals';
3
+ import { GenericObject } from 'mythix-orm/lib/interfaces/common';
4
+ import { Type } from 'mythix-orm/lib/types';
5
+
6
+ export declare interface QueryConditionContext {
7
+ queryPart: GenericObject;
8
+ field: Field;
9
+ sqlOperator: string;
10
+ operator: string | LiteralBase;
11
+ value: any;
12
+ }
13
+
14
+ declare class SQLQueryGeneratorBase extends QueryGeneratorBase {
15
+ public prepareArrayValuesForSQL(array: Array<any>): Array<any>;
16
+ public parseFieldProjection(value: string, getRawField: boolean): string | Field | undefined;
17
+ public parseFieldProjectionToFieldMap(selectStatement: string): Map<string, Field | string>;
18
+
19
+ public generateSelectQueryFieldProjection(
20
+ queryEngine: QueryEngine,
21
+ options?: GenericObject,
22
+ asMap?: false | undefined,
23
+ ): Array<string>;
24
+
25
+ public generateSelectQueryFieldProjection(
26
+ queryEngine: QueryEngine,
27
+ options?: GenericObject,
28
+ asMap?: true,
29
+ ): Map<string, string>;
30
+
31
+ public generateSelectQueryOperatorFromQueryEngineOperator(
32
+ queryPart: GenericObject,
33
+ operator: string | LiteralBase,
34
+ value: any,
35
+ valueIsReference: boolean,
36
+ options?: GenericObject,
37
+ ): string;
38
+
39
+ public formatLikeValue(context: QueryConditionContext): any;
40
+ public generateConditionPostfix(context: QueryConditionContext): string;
41
+
42
+ public generateSelectQueryCondition(
43
+ queryPart: GenericObject,
44
+ value: any,
45
+ options?: GenericObject,
46
+ ): string;
47
+
48
+ public generateFromTableOrTableJoin(
49
+ Model: ModelClass,
50
+ joinType: string | LiteralBase,
51
+ options?: GenericObject
52
+ ): string;
53
+
54
+ public generateSelectJoinOnTableQueryCondition(
55
+ leftQueryPart: GenericObject,
56
+ rightQueryPart: GenericObject,
57
+ leftField: Field,
58
+ rightField: Field,
59
+ operator: string | LiteralBase,
60
+ options?: GenericObject,
61
+ ): string;
62
+
63
+ public generateJoinOnTableQueryConditions(
64
+ joinInfos: Array<JoinTableInfo>,
65
+ options?: GenericObject
66
+ ): string;
67
+
68
+ public generateSQLJoinTypeFromQueryEngineJoinType(joinType: LiteralBase, outer: boolean, options?: GenericObject): LiteralBase;
69
+ public generateSQLJoinTypeFromQueryEngineJoinType(joinType: string, outer: boolean, options?: GenericObject): string;
70
+ public sortJoinRelationOrder(joins: Map<string, Array<JoinTableInfo>>): Array<string>;
71
+ public generateSelectQueryJoinTables(queryEngine: QueryEngine, options?: GenericObject): string;
72
+ public generateSelectWhereConditions(queryEngine: QueryEngine, options?: GenericObject): string;
73
+ public generateOrderClause(
74
+ orders: LiteralBase | FieldOrderInfo | Array<LiteralBase | FieldOrderInfo>,
75
+ options?: GenericObject
76
+ ): string;
77
+
78
+ public generateLimitClause(limit: LiteralBase | number | string, options?: GenericObject): string;
79
+ public generateOffsetClause(offset: LiteralBase | number | string, options?: GenericObject): string;
80
+ public generateSelectOrderLimitOffset(queryEngine: QueryEngine, options?: GenericObject): string;
81
+ public generateWhereAndOrderLimitOffset(queryEngine: QueryEngine, options?: GenericObject): string;
82
+
83
+ public generateSelectStatement(
84
+ queryEngine: QueryEngine,
85
+ options?: GenericObject
86
+ ): string | { sql: string, projectionFields: Map<string, string> };
87
+
88
+ public getFieldDefaultValue(
89
+ field: Field,
90
+ fieldName: string,
91
+ options?: GenericObject,
92
+ ): string | LiteralBase | undefined;
93
+
94
+ public generateIndexName(
95
+ Model: ModelClass,
96
+ field: Field,
97
+ index: string | true,
98
+ options?: GenericObject
99
+ ): string;
100
+
101
+ public generateColumnIndexes(
102
+ Model: ModelClass,
103
+ field: Field,
104
+ indexes: string | boolean | Array<string | boolean | Array<string>>,
105
+ options?: GenericObject,
106
+ ): Array<string>;
107
+
108
+ public generateDropTableStatement(Model: ModelClass, options?: GenericObject): string;
109
+ public generateForeignKeyConstraint(field: Field, type: Type, options?: GenericObject): string;
110
+ public generateCreateTableStatementInnerTail(Model: ModelClass, options?: GenericObject): Array<string>;
111
+ public generateCreateTableStatementOuterTail(Model: ModelClass, options?: GenericObject): Array<string>;
112
+ public generateCreateTableStatement(Model: ModelClass, options?: GenericObject): string;
113
+
114
+ public generateInsertFieldValuesFromModel(
115
+ model: Model,
116
+ options?: GenericObject
117
+ ): { modelChanges: GenericObject, rowValues: string } | undefined;
118
+
119
+ public generateInsertValuesFromModels(
120
+ Model: ModelClass,
121
+ models: Model | Array<Model> | PreparedModels,
122
+ options?: GenericObject,
123
+ ): { modelChanges: Array<GenericObject>, values: string } | undefined;
124
+
125
+ public generateInsertStatementTail(
126
+ Model: ModelClass,
127
+ model: Model,
128
+ options: GenericObject,
129
+ context: {
130
+ escapedTableName: string,
131
+ modelChanges: Array<GenericObject>,
132
+ dirtyFields: Array<Field>,
133
+ },
134
+ ): string | undefined;
135
+
136
+ public generateInsertStatement(
137
+ Model: ModelClass,
138
+ models: Model | Array<Model> | PreparedModels,
139
+ options?: GenericObject,
140
+ ): string;
141
+
142
+ public generateUpdateStatementTail(
143
+ Model: ModelClass,
144
+ model: Model | GenericObject,
145
+ queryEngine: QueryEngine,
146
+ options: GenericObject,
147
+ context: {
148
+ queryEngine: QueryEngine,
149
+ escapedTableName: string,
150
+ modelChanges: Array<GenericObject>,
151
+ dirtyFields: Array<Field>,
152
+ where: string,
153
+ },
154
+ ): string | undefined;
155
+
156
+ public generateUpdateStatement(
157
+ Model: ModelClass,
158
+ model: Model | GenericObject,
159
+ queryEngine: QueryEngine,
160
+ options?: GenericObject,
161
+ ): string;
162
+
163
+ public generateDeleteStatement(Model: ModelClass, queryEngine: QueryEngine, options?: GenericObject): string;
164
+ public generateTruncateTableStatement(Model: ModelClass, options?: GenericObject): string;
165
+
166
+ public _collectRemoteReturningFields(Model: ModelClass): Array<string>;
167
+ public _collectReturningFields(
168
+ Model: ModelClass,
169
+ model: Model,
170
+ options: GenericObject,
171
+ context: {
172
+ escapedTableName: string,
173
+ modelChanges: Array<GenericObject>,
174
+ dirtyFields: Array<Field>,
175
+ },
176
+ ): string | undefined;
177
+
178
+ toConnectionString(queryEngine: QueryEngine, options?: GenericObject): string;
179
+ }
180
+
181
+ export default SQLQueryGeneratorBase;
@@ -14,8 +14,8 @@ const DefaultHelpers = Types.DefaultHelpers;
14
14
  const LiteralBase = Literals.LiteralBase;
15
15
 
16
16
  class SQLQueryGeneratorBase extends QueryGeneratorBase {
17
- prepareArrayValuesForSQL(...args) {
18
- return this.connection.prepareArrayValuesForSQL(...args);
17
+ prepareArrayValuesForSQL(array) {
18
+ return this.connection.prepareArrayValuesForSQL(array);
19
19
  }
20
20
 
21
21
  parseFieldProjection = (str, getRawField) => {
@@ -247,11 +247,15 @@ class SQLQueryGeneratorBase extends QueryGeneratorBase {
247
247
  }
248
248
 
249
249
  // eslint-disable-next-line no-unused-vars
250
- generateFromTableOrTableJoin(Model, joinType, options) {
250
+ generateFromTableOrTableJoin(Model, _joinType, options) {
251
251
  if (!Model)
252
252
  throw new Error(`${this.constructor.name}::generateFromTableOrTableJoin: No valid model provided.`);
253
253
 
254
254
  let escapedTableName = this.getEscapedTableName(Model, options);
255
+ let joinType = _joinType;
256
+ if (joinType && LiteralBase.isLiteral(joinType))
257
+ joinType = joinType.toString(this.connection);
258
+
255
259
  return (joinType) ? `${joinType} ${escapedTableName}` : `FROM ${escapedTableName}`;
256
260
  }
257
261
 
@@ -863,7 +867,7 @@ class SQLQueryGeneratorBase extends QueryGeneratorBase {
863
867
 
864
868
  generateInsertFieldValuesFromModel(model, _options) {
865
869
  if (!model)
866
- return '';
870
+ return;
867
871
 
868
872
  let options = _options || {};
869
873
  let sqlParts = [];
@@ -871,7 +875,7 @@ class SQLQueryGeneratorBase extends QueryGeneratorBase {
871
875
  let dirtyFields = model._getDirtyFields({ insert: true });
872
876
 
873
877
  if (dirtyFields && Object.keys(dirtyFields).length === 0)
874
- return '';
878
+ return;
875
879
 
876
880
  model.iterateFields(
877
881
  ({ field, fieldName }) => {
@@ -912,7 +916,7 @@ class SQLQueryGeneratorBase extends QueryGeneratorBase {
912
916
  let allModelChanges = [];
913
917
 
914
918
  if (Nife.isEmpty(models))
915
- return '';
919
+ return;
916
920
 
917
921
  let sqlParts = [];
918
922
  let subOptions = this.stackAssign(options, { dirtyFields });
@@ -1182,6 +1186,12 @@ class SQLQueryGeneratorBase extends QueryGeneratorBase {
1182
1186
  return `RETURNING ${returnFields.join(',')}`;
1183
1187
  }
1184
1188
 
1189
+ // eslint-disable-next-line no-unused-vars
1190
+ generateTruncateTableStatement(Model, _options) {
1191
+ let escapedTableName = this.escapeID(Model.getTableName(this.connection));
1192
+ return `TRUNCATE TABLE ${escapedTableName}`;
1193
+ }
1194
+
1185
1195
  toConnectionString(queryEngine, options) {
1186
1196
  return this.generateSelectStatement(queryEngine, options);
1187
1197
  }
package/package.json CHANGED
@@ -1,8 +1,8 @@
1
1
  {
2
2
  "name": "mythix-orm-sql-base",
3
- "version": "1.4.8",
3
+ "version": "1.5.1",
4
4
  "description": "SQL base support for Mythix ORM",
5
- "main": "lib/index.js",
5
+ "main": "lib/index",
6
6
  "type": "commonjs",
7
7
  "scripts": {
8
8
  "coverage": "clear ; node ./node_modules/.bin/nyc ./node_modules/.bin/jasmine",
@@ -33,17 +33,17 @@
33
33
  },
34
34
  "homepage": "https://github.com/th317erd/mythix-orm-sql-base#readme",
35
35
  "peerDependencies": {
36
- "mythix-orm": "^1.5.6"
36
+ "mythix-orm": "^1.6.2"
37
37
  },
38
38
  "dependencies": {
39
- "nife": "^1.11.3",
40
- "uuid": "^8.3.2"
39
+ "nife": "^1.12.1",
40
+ "uuid": "^9.0.0"
41
41
  },
42
42
  "devDependencies": {
43
43
  "@spothero/eslint-plugin-spothero": "github:spothero/eslint-plugin-spothero",
44
44
  "better-sqlite3": "^7.6.2",
45
- "eslint": "^8.13.0",
46
- "jasmine": "^4.3.0",
45
+ "eslint": "^8.23.1",
46
+ "jasmine": "^4.4.0",
47
47
  "moment": "^2.29.4",
48
48
  "nyc": "^15.1.0"
49
49
  },