mythix-orm-sql-base 1.7.4 → 1.8.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.
|
@@ -10,10 +10,14 @@ const {
|
|
|
10
10
|
Model: ModelBase,
|
|
11
11
|
} = require('mythix-orm');
|
|
12
12
|
|
|
13
|
+
const SQLQueryGeneratorBase = require('./sql-query-generator-base');
|
|
14
|
+
|
|
13
15
|
const SAVE_POINT_NAME_CHARS = [ 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P' ];
|
|
14
16
|
const MODEL_RELATIONS = Symbol.for('_mythixModelRelations');
|
|
15
17
|
|
|
16
18
|
class SQLConnectionBase extends ConnectionBase {
|
|
19
|
+
static DefaultQueryGenerator = SQLQueryGeneratorBase;
|
|
20
|
+
|
|
17
21
|
prepareArrayValuesForSQL(_array) {
|
|
18
22
|
let array = Nife.arrayFlatten(_array);
|
|
19
23
|
|
|
@@ -95,7 +99,7 @@ class SQLConnectionBase extends ConnectionBase {
|
|
|
95
99
|
return true;
|
|
96
100
|
};
|
|
97
101
|
|
|
98
|
-
let context = queryEngine.
|
|
102
|
+
let context = queryEngine.getOperationContext();
|
|
99
103
|
let fields = this.findAllFieldsFromFieldProjectionMap(columns);
|
|
100
104
|
let rootModelName = context.rootModelName;
|
|
101
105
|
let modelData = {};
|
|
@@ -234,7 +238,7 @@ class SQLConnectionBase extends ConnectionBase {
|
|
|
234
238
|
if (Nife.isEmpty(modelDataMap))
|
|
235
239
|
return [];
|
|
236
240
|
|
|
237
|
-
let queryContext = queryEngine.
|
|
241
|
+
let queryContext = queryEngine.getOperationContext();
|
|
238
242
|
let rootModelName = queryContext.rootModelName;
|
|
239
243
|
let RootModel = queryContext.rootModel;
|
|
240
244
|
if (!rootModelName || !RootModel)
|
|
@@ -398,6 +402,7 @@ class SQLConnectionBase extends ConnectionBase {
|
|
|
398
402
|
throw new Error(`${this.constructor.name}::update: Model's primary key is empty. Models being updated must have a valid primary key.`);
|
|
399
403
|
|
|
400
404
|
query = query[primaryKeyFieldName].EQ(pkFieldValue);
|
|
405
|
+
query = await this.finalizeQuery('update', query, options);
|
|
401
406
|
|
|
402
407
|
let sqlStr = queryGenerator.generateUpdateStatement(Model, model, query, options);
|
|
403
408
|
if (!sqlStr)
|
|
@@ -419,11 +424,13 @@ class SQLConnectionBase extends ConnectionBase {
|
|
|
419
424
|
if (!queryEngine)
|
|
420
425
|
throw new Error(`${this.constructor.name}::updateAll: Model class or query is required to update.`);
|
|
421
426
|
|
|
422
|
-
let
|
|
427
|
+
let options = Object.assign({}, _options || {}, { isUpdateOperation: true });
|
|
428
|
+
queryEngine = await this.finalizeQuery('update', queryEngine, options);
|
|
429
|
+
|
|
430
|
+
let rootModel = queryEngine.getOperationContext().rootModel;
|
|
423
431
|
if (!rootModel)
|
|
424
432
|
throw new Error(`${this.constructor.name}::updateAll: Root model not found, and is required to update.`);
|
|
425
433
|
|
|
426
|
-
let options = Object.assign({}, _options || {}, { isUpdateOperation: true });
|
|
427
434
|
let queryGenerator = this.getQueryGenerator();
|
|
428
435
|
let sqlStr = queryGenerator.generateUpdateStatement(rootModel, model, queryEngine, options);
|
|
429
436
|
|
|
@@ -441,8 +448,9 @@ class SQLConnectionBase extends ConnectionBase {
|
|
|
441
448
|
if (options.truncate !== true)
|
|
442
449
|
return;
|
|
443
450
|
|
|
451
|
+
let query = await this.finalizeQuery('delete', Model.where(this).unscoped(), options);
|
|
444
452
|
let queryGenerator = this.getQueryGenerator();
|
|
445
|
-
let sqlStr = queryGenerator.generateDeleteStatement(Model,
|
|
453
|
+
let sqlStr = queryGenerator.generateDeleteStatement(Model, query, options);
|
|
446
454
|
|
|
447
455
|
return await this.query(sqlStr, options);
|
|
448
456
|
}
|
|
@@ -478,7 +486,8 @@ class SQLConnectionBase extends ConnectionBase {
|
|
|
478
486
|
if (Nife.isEmpty(pkIDs))
|
|
479
487
|
return;
|
|
480
488
|
|
|
481
|
-
let
|
|
489
|
+
let query = await this.finalizeQuery('delete', Model.where(this).id.EQ(pkIDs), options);
|
|
490
|
+
let sqlStr = queryGenerator.generateDeleteStatement(Model, query);
|
|
482
491
|
if (!sqlStr)
|
|
483
492
|
return;
|
|
484
493
|
|
|
@@ -503,11 +512,13 @@ class SQLConnectionBase extends ConnectionBase {
|
|
|
503
512
|
if (!queryEngine)
|
|
504
513
|
throw new Error(`${this.constructor.name}::destroy: Model class or query is required to destroy.`);
|
|
505
514
|
|
|
506
|
-
let
|
|
515
|
+
let options = modelsOrOptions || {};
|
|
516
|
+
queryEngine = await this.finalizeQuery('delete', queryEngine, options);
|
|
517
|
+
|
|
518
|
+
let rootModel = queryEngine.getOperationContext().rootModel;
|
|
507
519
|
if (!rootModel)
|
|
508
520
|
throw new Error(`${this.constructor.name}::destroy: Root model not found, and is required to destroy.`);
|
|
509
521
|
|
|
510
|
-
let options = modelsOrOptions || {};
|
|
511
522
|
let queryGenerator = this.getQueryGenerator();
|
|
512
523
|
let sqlStr = queryGenerator.generateDeleteStatement(rootModel, queryEngine, options);
|
|
513
524
|
return await this.query(sqlStr, options);
|
|
@@ -525,8 +536,10 @@ class SQLConnectionBase extends ConnectionBase {
|
|
|
525
536
|
throw new TypeError(`${this.constructor.name}::select: First argument must be a model class or a query.`);
|
|
526
537
|
}
|
|
527
538
|
|
|
528
|
-
let
|
|
529
|
-
|
|
539
|
+
let options = _options || {};
|
|
540
|
+
queryEngine = await this.finalizeQuery('read', queryEngine, options);
|
|
541
|
+
|
|
542
|
+
let queryContext = queryEngine.getOperationContext();
|
|
530
543
|
let batchSize = options.batchSize || 500;
|
|
531
544
|
let startIndex = queryContext.offset || 0;
|
|
532
545
|
let queryGenerator = this.getQueryGenerator();
|
|
@@ -574,9 +587,10 @@ class SQLConnectionBase extends ConnectionBase {
|
|
|
574
587
|
if (!queryEngine)
|
|
575
588
|
throw new TypeError(`${this.constructor.name}::aggregate: First argument must be a model class or a query.`);
|
|
576
589
|
|
|
590
|
+
queryEngine = await this.finalizeQuery('read', queryEngine, options);
|
|
577
591
|
queryEngine = queryEngine.clone();
|
|
578
592
|
|
|
579
|
-
let queryContext = queryEngine.
|
|
593
|
+
let queryContext = queryEngine.getOperationContext();
|
|
580
594
|
let distinct = queryContext.distinct;
|
|
581
595
|
if (distinct) {
|
|
582
596
|
let fullyQualifiedFieldName = distinct.getFullyQualifiedFieldName();
|
|
@@ -611,7 +625,7 @@ class SQLConnectionBase extends ConnectionBase {
|
|
|
611
625
|
if (!queryEngine)
|
|
612
626
|
throw new TypeError(`${this.constructor.name}::average: First argument must be a model class or a query.`);
|
|
613
627
|
|
|
614
|
-
let rootModel = queryEngine.
|
|
628
|
+
let rootModel = queryEngine.getOperationContext().rootModel;
|
|
615
629
|
let field = Utils.fieldToFullyQualifiedName(_field, rootModel);
|
|
616
630
|
|
|
617
631
|
return await this.aggregate(queryEngine, new Literals.AverageLiteral(field), options);
|
|
@@ -622,7 +636,9 @@ class SQLConnectionBase extends ConnectionBase {
|
|
|
622
636
|
if (!queryEngine)
|
|
623
637
|
throw new TypeError(`${this.constructor.name}::count: First argument must be a model class or a query.`);
|
|
624
638
|
|
|
625
|
-
|
|
639
|
+
queryEngine = await this.finalizeQuery('read', queryEngine, options);
|
|
640
|
+
|
|
641
|
+
let rootModel = queryEngine.getOperationContext().rootModel;
|
|
626
642
|
let field = (_field) ? Utils.fieldToFullyQualifiedName(_field, rootModel) : null;
|
|
627
643
|
|
|
628
644
|
return await this.aggregate(queryEngine, new Literals.CountLiteral(field), options);
|
|
@@ -633,7 +649,9 @@ class SQLConnectionBase extends ConnectionBase {
|
|
|
633
649
|
if (!queryEngine)
|
|
634
650
|
throw new TypeError(`${this.constructor.name}::min: First argument must be a model class or a query.`);
|
|
635
651
|
|
|
636
|
-
|
|
652
|
+
queryEngine = await this.finalizeQuery('read', queryEngine, options);
|
|
653
|
+
|
|
654
|
+
let rootModel = queryEngine.getOperationContext().rootModel;
|
|
637
655
|
let field = Utils.fieldToFullyQualifiedName(_field, rootModel);
|
|
638
656
|
|
|
639
657
|
return await this.aggregate(queryEngine, new Literals.MinLiteral(field), options);
|
|
@@ -644,7 +662,9 @@ class SQLConnectionBase extends ConnectionBase {
|
|
|
644
662
|
if (!queryEngine)
|
|
645
663
|
throw new TypeError(`${this.constructor.name}::max: First argument must be a model class or a query.`);
|
|
646
664
|
|
|
647
|
-
|
|
665
|
+
queryEngine = await this.finalizeQuery('read', queryEngine, options);
|
|
666
|
+
|
|
667
|
+
let rootModel = queryEngine.getOperationContext().rootModel;
|
|
648
668
|
let field = Utils.fieldToFullyQualifiedName(_field, rootModel);
|
|
649
669
|
|
|
650
670
|
return await this.aggregate(queryEngine, new Literals.MaxLiteral(field), options);
|
|
@@ -655,7 +675,9 @@ class SQLConnectionBase extends ConnectionBase {
|
|
|
655
675
|
if (!queryEngine)
|
|
656
676
|
throw new TypeError(`${this.constructor.name}::sum: First argument must be a model class or a query.`);
|
|
657
677
|
|
|
658
|
-
|
|
678
|
+
queryEngine = await this.finalizeQuery('read', queryEngine, options);
|
|
679
|
+
|
|
680
|
+
let rootModel = queryEngine.getOperationContext().rootModel;
|
|
659
681
|
let field = Utils.fieldToFullyQualifiedName(_field, rootModel);
|
|
660
682
|
|
|
661
683
|
return await this.aggregate(queryEngine, new Literals.SumLiteral(field), options);
|
|
@@ -683,7 +705,9 @@ class SQLConnectionBase extends ConnectionBase {
|
|
|
683
705
|
throw new TypeError(`${this.constructor.name}::pluck: First argument must be a model class or a query.`);
|
|
684
706
|
}
|
|
685
707
|
|
|
686
|
-
|
|
708
|
+
queryEngine = await this.finalizeQuery('read', queryEngine, options);
|
|
709
|
+
|
|
710
|
+
let queryContext = queryEngine.getOperationContext();
|
|
687
711
|
let rootModel = queryContext.rootModel;
|
|
688
712
|
let rootModelName = rootModel.getModelName();
|
|
689
713
|
|
|
@@ -227,7 +227,7 @@ class SQLQueryGeneratorBase extends QueryGeneratorBase {
|
|
|
227
227
|
let sqlOperator = this.generateSelectQueryOperatorFromQueryEngineOperator(queryPart, operator, value, false, options);
|
|
228
228
|
|
|
229
229
|
if (QueryEngine.isQuery(value)) {
|
|
230
|
-
if (!value.
|
|
230
|
+
if (!value.queryHasConditions())
|
|
231
231
|
return '';
|
|
232
232
|
|
|
233
233
|
if (sqlOperator === '=')
|
|
@@ -339,7 +339,7 @@ class SQLQueryGeneratorBase extends QueryGeneratorBase {
|
|
|
339
339
|
items.push(joinInfo);
|
|
340
340
|
};
|
|
341
341
|
|
|
342
|
-
let query = queryEngine.
|
|
342
|
+
let query = queryEngine.getOperationStack();
|
|
343
343
|
let joins = new Map();
|
|
344
344
|
|
|
345
345
|
for (let i = 0, il = query.length; i < il; i++) {
|
|
@@ -353,11 +353,11 @@ class SQLQueryGeneratorBase extends QueryGeneratorBase {
|
|
|
353
353
|
|
|
354
354
|
// If the query has a condition, then it is a sub-query
|
|
355
355
|
// not a join
|
|
356
|
-
if (operatorValue.
|
|
356
|
+
if (operatorValue.getOperationContext().condition)
|
|
357
357
|
continue;
|
|
358
358
|
|
|
359
359
|
let joinType = this.generateSQLJoinTypeFromQueryEngineJoinType(queryPart.joinType, queryPart.joinOuter, options);
|
|
360
|
-
let joinInfo = this.getJoinTableInfoFromQueryContexts(queryPart, operatorValue.
|
|
360
|
+
let joinInfo = this.getJoinTableInfoFromQueryContexts(queryPart, operatorValue.getOperationContext(), joinType, options);
|
|
361
361
|
|
|
362
362
|
addToJoins(joinInfo);
|
|
363
363
|
}
|
|
@@ -391,7 +391,7 @@ class SQLQueryGeneratorBase extends QueryGeneratorBase {
|
|
|
391
391
|
return '';
|
|
392
392
|
};
|
|
393
393
|
|
|
394
|
-
let query = queryEngine.
|
|
394
|
+
let query = queryEngine.getOperationStack();
|
|
395
395
|
let sqlParts = [];
|
|
396
396
|
let hasValue = false;
|
|
397
397
|
|
|
@@ -590,7 +590,7 @@ class SQLQueryGeneratorBase extends QueryGeneratorBase {
|
|
|
590
590
|
if (options.includeRelations === true)
|
|
591
591
|
queryEngine = queryEngine.clone().PROJECT('*');
|
|
592
592
|
|
|
593
|
-
let rootModel = queryEngine.
|
|
593
|
+
let rootModel = queryEngine.getOperationContext().rootModel;
|
|
594
594
|
if (!rootModel)
|
|
595
595
|
throw new Error(`${this.constructor.name}::generateSelectStatement: No root model found.`);
|
|
596
596
|
|
|
@@ -1138,8 +1138,8 @@ class SQLQueryGeneratorBase extends QueryGeneratorBase {
|
|
|
1138
1138
|
}
|
|
1139
1139
|
|
|
1140
1140
|
let escapedTableName = this.getEscapedTableName(Model, options);
|
|
1141
|
-
if (queryEngine && queryEngine.
|
|
1142
|
-
if (queryEngine.
|
|
1141
|
+
if (queryEngine && queryEngine.queryHasConditions()) {
|
|
1142
|
+
if (queryEngine.queryHasJoins()) {
|
|
1143
1143
|
let pkField = Model.getPrimaryKeyField();
|
|
1144
1144
|
if (!pkField)
|
|
1145
1145
|
throw new Error(`${this.constructor.name}::generateDeleteStatement: Can not delete using table joins on a table with no primary key field.`);
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "mythix-orm-sql-base",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.8.0",
|
|
4
4
|
"description": "SQL base support for Mythix ORM",
|
|
5
5
|
"main": "lib/index",
|
|
6
6
|
"type": "commonjs",
|
|
@@ -33,7 +33,7 @@
|
|
|
33
33
|
},
|
|
34
34
|
"homepage": "https://github.com/th317erd/mythix-orm-sql-base#readme",
|
|
35
35
|
"peerDependencies": {
|
|
36
|
-
"mythix-orm": "^1.
|
|
36
|
+
"mythix-orm": "^1.10.2"
|
|
37
37
|
},
|
|
38
38
|
"dependencies": {
|
|
39
39
|
"luxon": "^3.1.0",
|