mythix-orm-sql-base 1.0.2 → 1.0.5
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/sql-connection-base.js +82 -10
- package/lib/sql-query-generator-base.js +13 -13
- package/package.json +3 -3
|
@@ -46,7 +46,7 @@ class SQLConnectionBase extends ConnectionBase {
|
|
|
46
46
|
if (item === null)
|
|
47
47
|
return true;
|
|
48
48
|
|
|
49
|
-
if (
|
|
49
|
+
if (Literals.LiteralBase.isLiteral(item))
|
|
50
50
|
return true;
|
|
51
51
|
|
|
52
52
|
if (!Nife.instanceOf(item, 'string', 'number', 'bigint', 'boolean'))
|
|
@@ -303,12 +303,12 @@ class SQLConnectionBase extends ConnectionBase {
|
|
|
303
303
|
let model = models[i];
|
|
304
304
|
let promise = model[operationHookName](context);
|
|
305
305
|
|
|
306
|
-
if (!(promise
|
|
307
|
-
promise = Promise.resolve();
|
|
306
|
+
if (!Nife.instanceOf(promise, 'promise'))
|
|
307
|
+
promise = Promise.resolve(promise);
|
|
308
308
|
|
|
309
309
|
promise = promise.then(async () => await model[saveHookName](context), throwError);
|
|
310
|
-
if (!(promise
|
|
311
|
-
promise = Promise.resolve();
|
|
310
|
+
if (!Nife.instanceOf(promise, 'promise'))
|
|
311
|
+
promise = Promise.resolve(promise);
|
|
312
312
|
|
|
313
313
|
promises.push(promise);
|
|
314
314
|
}
|
|
@@ -324,6 +324,42 @@ class SQLConnectionBase extends ConnectionBase {
|
|
|
324
324
|
return await this.query(createTableSQL, options);
|
|
325
325
|
}
|
|
326
326
|
|
|
327
|
+
async dropTables(_Models, options) {
|
|
328
|
+
if (!_Models)
|
|
329
|
+
return;
|
|
330
|
+
|
|
331
|
+
// First we collect all models and put them into a map
|
|
332
|
+
let modelMap = _Models;
|
|
333
|
+
|
|
334
|
+
if (Nife.instanceOf(_Models, 'array', 'function')) {
|
|
335
|
+
modelMap = {};
|
|
336
|
+
|
|
337
|
+
let Models = Nife.toArray(_Models).filter(Boolean);
|
|
338
|
+
for (let i = 0, il = Models.length; i < il; i++) {
|
|
339
|
+
let Model = Models[i];
|
|
340
|
+
let modelName = Model.getModelName();
|
|
341
|
+
|
|
342
|
+
modelMap[modelName] = Model;
|
|
343
|
+
}
|
|
344
|
+
}
|
|
345
|
+
|
|
346
|
+
// Second we sort the model names in creation order,
|
|
347
|
+
// and going in reverse of that order we destroy
|
|
348
|
+
// each table.
|
|
349
|
+
let modelNames = Object.keys(modelMap);
|
|
350
|
+
let sortedModelNames = Utils.sortModelNamesByCreationOrder(this, modelNames);
|
|
351
|
+
let results = [];
|
|
352
|
+
|
|
353
|
+
for (let i = sortedModelNames.length - 1; i >= 0; i--) {
|
|
354
|
+
let modelName = sortedModelNames[i];
|
|
355
|
+
let Model = modelMap[modelName];
|
|
356
|
+
|
|
357
|
+
results.push(await this.dropTable(Model, options));
|
|
358
|
+
}
|
|
359
|
+
|
|
360
|
+
return results;
|
|
361
|
+
}
|
|
362
|
+
|
|
327
363
|
async createTable(Model, options) {
|
|
328
364
|
let queryGenerator = this.getQueryGenerator();
|
|
329
365
|
let createTableSQL = queryGenerator.generateCreateTableStatement(Model, options);
|
|
@@ -343,6 +379,41 @@ class SQLConnectionBase extends ConnectionBase {
|
|
|
343
379
|
return result;
|
|
344
380
|
}
|
|
345
381
|
|
|
382
|
+
async createTables(_Models, options) {
|
|
383
|
+
if (!_Models)
|
|
384
|
+
return;
|
|
385
|
+
|
|
386
|
+
// First we collect all models and put them into a map
|
|
387
|
+
let modelMap = _Models;
|
|
388
|
+
|
|
389
|
+
if (Nife.instanceOf(_Models, 'array', 'function')) {
|
|
390
|
+
modelMap = {};
|
|
391
|
+
|
|
392
|
+
let Models = Nife.toArray(_Models).filter(Boolean);
|
|
393
|
+
for (let i = 0, il = Models.length; i < il; i++) {
|
|
394
|
+
let Model = Models[i];
|
|
395
|
+
let modelName = Model.getModelName();
|
|
396
|
+
|
|
397
|
+
modelMap[modelName] = Model;
|
|
398
|
+
}
|
|
399
|
+
}
|
|
400
|
+
|
|
401
|
+
// Second we sort the model names in creation order,
|
|
402
|
+
// and then create the tables in that order
|
|
403
|
+
let modelNames = Object.keys(modelMap);
|
|
404
|
+
let sortedModelNames = Utils.sortModelNamesByCreationOrder(this, modelNames);
|
|
405
|
+
let results = [];
|
|
406
|
+
|
|
407
|
+
for (let i = 0, il = sortedModelNames.length; i < il; i++) {
|
|
408
|
+
let modelName = sortedModelNames[i];
|
|
409
|
+
let Model = modelMap[modelName];
|
|
410
|
+
|
|
411
|
+
results.push(await this.createTable(Model, options));
|
|
412
|
+
}
|
|
413
|
+
|
|
414
|
+
return results;
|
|
415
|
+
}
|
|
416
|
+
|
|
346
417
|
async insert(Model, models, _options) {
|
|
347
418
|
return await this.bulkModelOperation(
|
|
348
419
|
Model,
|
|
@@ -573,7 +644,8 @@ class SQLConnectionBase extends ConnectionBase {
|
|
|
573
644
|
|
|
574
645
|
async aggregate(_queryEngine, _literal, options) {
|
|
575
646
|
let literal = _literal;
|
|
576
|
-
|
|
647
|
+
|
|
648
|
+
if (!Literals.LiteralBase.isLiteral(literal))
|
|
577
649
|
throw new Error(`${this.constructor.name}::aggregate: Second argument must be a Literal instance.`);
|
|
578
650
|
|
|
579
651
|
let queryEngine = this.toQueryEngine(_queryEngine);
|
|
@@ -671,9 +743,9 @@ class SQLConnectionBase extends ConnectionBase {
|
|
|
671
743
|
if (_options && !Nife.instanceOf(_options, 'object'))
|
|
672
744
|
throw new TypeError(`${this.constructor.name}::pluck: "options" isn't an object. Did you pass a field by accident?`);
|
|
673
745
|
|
|
674
|
-
let options
|
|
675
|
-
let
|
|
676
|
-
let fields
|
|
746
|
+
let options = _options || {};
|
|
747
|
+
let moreThanOneFieldRequested = (Array.isArray(_fields) && _fields.length > 1);
|
|
748
|
+
let fields = Nife.arrayFlatten(Nife.toArray(_fields)).filter(Boolean);
|
|
677
749
|
|
|
678
750
|
if (Nife.isEmpty(fields))
|
|
679
751
|
throw new Error(`${this.constructor.name}::pluck: You must supply "fields" to pluck.`);
|
|
@@ -744,7 +816,7 @@ class SQLConnectionBase extends ConnectionBase {
|
|
|
744
816
|
});
|
|
745
817
|
}
|
|
746
818
|
|
|
747
|
-
if (!
|
|
819
|
+
if (!moreThanOneFieldRequested)
|
|
748
820
|
finalResults = finalResults.map((row) => row[0]);
|
|
749
821
|
|
|
750
822
|
return finalResults;
|
|
@@ -118,7 +118,7 @@ class SQLQueryGeneratorBase extends QueryGeneratorBase {
|
|
|
118
118
|
|
|
119
119
|
// eslint-disable-next-line no-unused-vars
|
|
120
120
|
generateSelectQueryOperatorFromQueryEngineOperator(operator, value, valueIsReference, options) {
|
|
121
|
-
if (operator
|
|
121
|
+
if (LiteralBase.isLiteral(operator))
|
|
122
122
|
return operator.toString(this.connection);
|
|
123
123
|
|
|
124
124
|
switch (operator) {
|
|
@@ -422,7 +422,7 @@ class SQLQueryGeneratorBase extends QueryGeneratorBase {
|
|
|
422
422
|
|
|
423
423
|
// eslint-disable-next-line no-unused-vars
|
|
424
424
|
generateOrderClause(_orders, _options) {
|
|
425
|
-
if (_orders
|
|
425
|
+
if (LiteralBase.isLiteral(_orders))
|
|
426
426
|
return _orders.toString(this.connection);
|
|
427
427
|
|
|
428
428
|
let orders = Nife.toArray(_orders).filter(Boolean);
|
|
@@ -434,7 +434,7 @@ class SQLQueryGeneratorBase extends QueryGeneratorBase {
|
|
|
434
434
|
for (let i = 0, il = orders.length; i < il; i++) {
|
|
435
435
|
let orderField = orders[i];
|
|
436
436
|
|
|
437
|
-
if (orderField
|
|
437
|
+
if (LiteralBase.isLiteral(orderField)) {
|
|
438
438
|
orderByParts.push(orderField.toString(this.connection));
|
|
439
439
|
continue;
|
|
440
440
|
}
|
|
@@ -469,7 +469,7 @@ class SQLQueryGeneratorBase extends QueryGeneratorBase {
|
|
|
469
469
|
|
|
470
470
|
// eslint-disable-next-line no-unused-vars
|
|
471
471
|
generateLimitClause(limit, options) {
|
|
472
|
-
if (limit
|
|
472
|
+
if (LiteralBase.isLiteral(limit))
|
|
473
473
|
return limit.toString(this.connection);
|
|
474
474
|
|
|
475
475
|
return `LIMIT ${limit}`;
|
|
@@ -477,7 +477,7 @@ class SQLQueryGeneratorBase extends QueryGeneratorBase {
|
|
|
477
477
|
|
|
478
478
|
// eslint-disable-next-line no-unused-vars
|
|
479
479
|
generateOffsetClause(offset, options) {
|
|
480
|
-
if (offset
|
|
480
|
+
if (LiteralBase.isLiteral(offset))
|
|
481
481
|
return offset.toString(this.connection);
|
|
482
482
|
|
|
483
483
|
return `OFFSET ${offset}`;
|
|
@@ -586,7 +586,7 @@ class SQLQueryGeneratorBase extends QueryGeneratorBase {
|
|
|
586
586
|
}
|
|
587
587
|
}
|
|
588
588
|
|
|
589
|
-
if (defaultValue
|
|
589
|
+
if (LiteralBase.isLiteral(defaultValue)) {
|
|
590
590
|
if (defaultValue.options.escape === false)
|
|
591
591
|
escapeValue = false;
|
|
592
592
|
|
|
@@ -767,7 +767,7 @@ class SQLQueryGeneratorBase extends QueryGeneratorBase {
|
|
|
767
767
|
let defaultValue = this.getFieldDefaultValue(field, fieldName, { remoteOnly: true });
|
|
768
768
|
|
|
769
769
|
if (field.primaryKey) {
|
|
770
|
-
if (field.primaryKey
|
|
770
|
+
if (LiteralBase.isLiteral(field.primaryKey))
|
|
771
771
|
constraintParts.push(field.primaryKey.toString(this.connection));
|
|
772
772
|
else
|
|
773
773
|
constraintParts.push('PRIMARY KEY');
|
|
@@ -776,7 +776,7 @@ class SQLQueryGeneratorBase extends QueryGeneratorBase {
|
|
|
776
776
|
constraintParts.push('NOT NULL');
|
|
777
777
|
} else {
|
|
778
778
|
if (field.unique) {
|
|
779
|
-
if (field.unique
|
|
779
|
+
if (LiteralBase.isLiteral(field.unique))
|
|
780
780
|
constraintParts.push(field.unique.toString(this.connection));
|
|
781
781
|
else
|
|
782
782
|
constraintParts.push('UNIQUE');
|
|
@@ -838,7 +838,7 @@ class SQLQueryGeneratorBase extends QueryGeneratorBase {
|
|
|
838
838
|
|
|
839
839
|
modelChanges[fieldName] = fieldValue;
|
|
840
840
|
|
|
841
|
-
if (fieldValue
|
|
841
|
+
if (LiteralBase.isLiteral(fieldValue))
|
|
842
842
|
fieldValue = fieldValue.toString(this.connection);
|
|
843
843
|
else
|
|
844
844
|
fieldValue = this.escape(field, fieldValue);
|
|
@@ -866,7 +866,7 @@ class SQLQueryGeneratorBase extends QueryGeneratorBase {
|
|
|
866
866
|
|
|
867
867
|
for (let i = 0, il = models.length; i < il; i++) {
|
|
868
868
|
let model = models[i];
|
|
869
|
-
if (!(model
|
|
869
|
+
if (!ModelBase.isModel(model))
|
|
870
870
|
model = new Model(model);
|
|
871
871
|
|
|
872
872
|
let { rowValues, modelChanges } = this.generateInsertFieldValuesFromModel(model, subOptions);
|
|
@@ -940,7 +940,7 @@ class SQLQueryGeneratorBase extends QueryGeneratorBase {
|
|
|
940
940
|
}
|
|
941
941
|
|
|
942
942
|
let model = _model;
|
|
943
|
-
if (!(model
|
|
943
|
+
if (!ModelBase.isModel(model)) {
|
|
944
944
|
let newModel = new Model();
|
|
945
945
|
newModel.clearDirty();
|
|
946
946
|
newModel.setAttributes(model);
|
|
@@ -967,7 +967,7 @@ class SQLQueryGeneratorBase extends QueryGeneratorBase {
|
|
|
967
967
|
let dirtyField = dirtyFields[i];
|
|
968
968
|
let fieldValue = modelChanges[dirtyField.fieldName].current;
|
|
969
969
|
let escapedColumnName = this.escapeID(dirtyField.columnName);
|
|
970
|
-
let escapedValue = (fieldValue
|
|
970
|
+
let escapedValue = (LiteralBase.isLiteral(fieldValue)) ? fieldValue.toString(this.connection) : this.escape(dirtyField, fieldValue);
|
|
971
971
|
if (!escapedValue)
|
|
972
972
|
continue;
|
|
973
973
|
|
|
@@ -1082,7 +1082,7 @@ class SQLQueryGeneratorBase extends QueryGeneratorBase {
|
|
|
1082
1082
|
continue;
|
|
1083
1083
|
|
|
1084
1084
|
let fieldValue = dirtyStatus.current;
|
|
1085
|
-
if (!(fieldValue
|
|
1085
|
+
if (!LiteralBase.isLiteral(fieldValue))
|
|
1086
1086
|
continue;
|
|
1087
1087
|
|
|
1088
1088
|
if (!fieldValue.options.remote)
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "mythix-orm-sql-base",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.5",
|
|
4
4
|
"description": "SQL base support for Mythix ORM",
|
|
5
5
|
"main": "lib/index.js",
|
|
6
6
|
"type": "commonjs",
|
|
@@ -33,10 +33,10 @@
|
|
|
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.1.0"
|
|
37
37
|
},
|
|
38
38
|
"dependencies": {
|
|
39
|
-
"nife": "^1.11.
|
|
39
|
+
"nife": "^1.11.3",
|
|
40
40
|
"uuid": "^8.3.2"
|
|
41
41
|
},
|
|
42
42
|
"devDependencies": {
|