tspace-mysql 1.1.8 → 1.2.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.
- package/README.md +115 -38
- package/dist/cli/generate/make.d.ts +4 -0
- package/dist/cli/generate/make.js +45 -0
- package/dist/cli/index.js +27 -13
- package/dist/cli/migrate/make.js +5 -4
- package/dist/cli/models/make.d.ts +1 -1
- package/dist/cli/models/make.js +2 -2
- package/dist/cli/models/model.js +3 -4
- package/dist/cli/query/index.d.ts +4 -0
- package/dist/cli/query/index.js +7 -0
- package/dist/lib/connection/index.d.ts +8 -32
- package/dist/lib/connection/index.js +50 -37
- package/dist/lib/connection/options.d.ts +4 -0
- package/dist/lib/{config/env.js → connection/options.js} +10 -7
- package/dist/lib/constants/index.d.ts +2 -2
- package/dist/lib/constants/index.js +14 -11
- package/dist/lib/tspace/AbstractDB.d.ts +2 -0
- package/dist/lib/tspace/AbstractDatabase.d.ts +23 -19
- package/dist/lib/tspace/AbstractDatabase.js +29 -26
- package/dist/lib/tspace/AbstractModel.d.ts +5 -4
- package/dist/lib/tspace/Blueprint.js +4 -2
- package/dist/lib/tspace/DB.d.ts +23 -2
- package/dist/lib/tspace/DB.js +93 -30
- package/dist/lib/tspace/Database.d.ts +26 -13
- package/dist/lib/tspace/Database.js +920 -777
- package/dist/lib/tspace/Interface.d.ts +26 -0
- package/dist/lib/tspace/Logger.js +5 -4
- package/dist/lib/tspace/Model.d.ts +73 -23
- package/dist/lib/tspace/Model.js +1208 -867
- package/dist/lib/tspace/ProxyHandler.d.ts +9 -1
- package/dist/lib/tspace/ProxyHandler.js +8 -9
- package/dist/lib/tspace/Schema.js +35 -22
- package/dist/lib/utils/index.d.ts +0 -1
- package/dist/lib/utils/index.js +15 -44
- package/package.json +4 -2
- package/dist/lib/config/env.d.ts +0 -4
package/dist/lib/tspace/Model.js
CHANGED
|
@@ -1,24 +1,33 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
3
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
4
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
5
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
6
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
7
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
8
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
9
|
+
});
|
|
10
|
+
};
|
|
2
11
|
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
12
|
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
13
|
};
|
|
5
14
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
15
|
exports.Model = void 0;
|
|
7
16
|
const pluralize_1 = __importDefault(require("pluralize"));
|
|
8
|
-
const AbstractModel_1 = require("./AbstractModel");
|
|
9
17
|
const DB_1 = require("./DB");
|
|
18
|
+
const AbstractModel_1 = require("./AbstractModel");
|
|
10
19
|
const ProxyHandler_1 = require("./ProxyHandler");
|
|
11
20
|
class Model extends AbstractModel_1.AbstractModel {
|
|
12
21
|
constructor() {
|
|
13
22
|
super();
|
|
14
23
|
/**
|
|
15
24
|
*
|
|
16
|
-
* Get initialize for model
|
|
25
|
+
* @Get initialize for model
|
|
17
26
|
*/
|
|
18
27
|
this._initialModel();
|
|
19
28
|
/**
|
|
20
29
|
*
|
|
21
|
-
* Define Setup for model
|
|
30
|
+
* @Define Setup for model
|
|
22
31
|
*/
|
|
23
32
|
this.define();
|
|
24
33
|
return new Proxy(this, ProxyHandler_1.proxyHandler);
|
|
@@ -28,23 +37,20 @@ class Model extends AbstractModel_1.AbstractModel {
|
|
|
28
37
|
* define for initialize of models
|
|
29
38
|
* @return {void} void
|
|
30
39
|
*/
|
|
31
|
-
define() {
|
|
40
|
+
define() {
|
|
41
|
+
}
|
|
32
42
|
/**
|
|
33
43
|
*
|
|
34
44
|
* Assign function callback in model
|
|
35
45
|
* @return {this} this
|
|
36
46
|
*/
|
|
37
47
|
useRegistry() {
|
|
38
|
-
this.$state.set('REGISTRY', {
|
|
39
|
-
...this.$state.get('REGISTRY'),
|
|
40
|
-
attach: this._attach,
|
|
41
|
-
detach: this._detach
|
|
42
|
-
});
|
|
48
|
+
this.$state.set('REGISTRY', Object.assign(Object.assign({}, this.$state.get('REGISTRY')), { attach: this._attach, detach: this._detach }));
|
|
43
49
|
return this;
|
|
44
50
|
}
|
|
45
51
|
/**
|
|
46
52
|
*
|
|
47
|
-
* Assign
|
|
53
|
+
* Assign primary column in model
|
|
48
54
|
* @return {this} this
|
|
49
55
|
*/
|
|
50
56
|
usePrimaryKey(primary) {
|
|
@@ -52,7 +58,7 @@ class Model extends AbstractModel_1.AbstractModel {
|
|
|
52
58
|
return this;
|
|
53
59
|
}
|
|
54
60
|
/**
|
|
55
|
-
* Assign
|
|
61
|
+
* Assign generate uuid when creating in model
|
|
56
62
|
* @param {string?} column [column=uuid] make new name column for custom column replace uuid with this
|
|
57
63
|
* @return {this} this
|
|
58
64
|
*/
|
|
@@ -132,7 +138,8 @@ class Model extends AbstractModel_1.AbstractModel {
|
|
|
132
138
|
* @return {this} this
|
|
133
139
|
*/
|
|
134
140
|
useTableSingular() {
|
|
135
|
-
|
|
141
|
+
var _a;
|
|
142
|
+
const table = this._classToTableName((_a = this.constructor) === null || _a === void 0 ? void 0 : _a.name, { singular: true });
|
|
136
143
|
this.$state.set('TABLE_NAME', `\`${pluralize_1.default.singular(table)}\``);
|
|
137
144
|
return this;
|
|
138
145
|
}
|
|
@@ -142,7 +149,8 @@ class Model extends AbstractModel_1.AbstractModel {
|
|
|
142
149
|
* @return {this} this
|
|
143
150
|
*/
|
|
144
151
|
useTablePlural() {
|
|
145
|
-
|
|
152
|
+
var _a;
|
|
153
|
+
const table = this._classToTableName((_a = this.constructor) === null || _a === void 0 ? void 0 : _a.name);
|
|
146
154
|
this.$state.set('TABLE_NAME', `\`${pluralize_1.default.plural(table)}\``);
|
|
147
155
|
return this;
|
|
148
156
|
}
|
|
@@ -153,10 +161,11 @@ class Model extends AbstractModel_1.AbstractModel {
|
|
|
153
161
|
* @return {this} this
|
|
154
162
|
*/
|
|
155
163
|
buildMethodRelation(name, callback) {
|
|
164
|
+
var _a;
|
|
156
165
|
this.with(name);
|
|
157
166
|
const r = this.$state.get('WITH').find((data) => data.name === name);
|
|
158
167
|
this._assertError(r == null, `relation ${name} not be register !`);
|
|
159
|
-
const relationHasExists = Object.values(this.$constants('RELATIONSHIP'))
|
|
168
|
+
const relationHasExists = (_a = Object.values(this.$constants('RELATIONSHIP'))) === null || _a === void 0 ? void 0 : _a.includes(r.relation);
|
|
160
169
|
this._assertError(!relationHasExists, `unknown relationship in [${this.$constants('RELATIONSHIP')}] !`);
|
|
161
170
|
if (callback == null) {
|
|
162
171
|
r.query = new r.model();
|
|
@@ -168,17 +177,36 @@ class Model extends AbstractModel_1.AbstractModel {
|
|
|
168
177
|
/**
|
|
169
178
|
*
|
|
170
179
|
* Clone instance of model
|
|
171
|
-
* @
|
|
180
|
+
* @param {Model} instance instance of model
|
|
172
181
|
* @return {this} this
|
|
173
182
|
*/
|
|
174
183
|
clone(instance) {
|
|
175
|
-
if (!(instance instanceof Model)) {
|
|
176
|
-
throw new Error('value is not a instanceof Model');
|
|
177
|
-
}
|
|
178
184
|
const copy = Object.fromEntries(instance.$state.get());
|
|
179
185
|
this.$state.clone(copy);
|
|
180
186
|
return this;
|
|
181
187
|
}
|
|
188
|
+
/**
|
|
189
|
+
*
|
|
190
|
+
* Copy an instance of model
|
|
191
|
+
* @param {Model} instance instance of model
|
|
192
|
+
* @param {Object} options keep data
|
|
193
|
+
* @return {Model} Model
|
|
194
|
+
*/
|
|
195
|
+
copyModel(instance, options) {
|
|
196
|
+
this._assertError(!(instance instanceof Model), 'value is not a instanceof Model');
|
|
197
|
+
const copy = Object.fromEntries(instance.$state.get());
|
|
198
|
+
const newInstance = new Model();
|
|
199
|
+
newInstance.$state.clone(copy);
|
|
200
|
+
if ((options === null || options === void 0 ? void 0 : options.insert) == null)
|
|
201
|
+
newInstance.$state.set('INSERT', '');
|
|
202
|
+
if ((options === null || options === void 0 ? void 0 : options.update) == null)
|
|
203
|
+
newInstance.$state.set('UPDATE', '');
|
|
204
|
+
if ((options === null || options === void 0 ? void 0 : options.delete) == null)
|
|
205
|
+
newInstance.$state.set('DELETE', '');
|
|
206
|
+
if ((options === null || options === void 0 ? void 0 : options.where) == null)
|
|
207
|
+
newInstance.$state.set('WHERE', '');
|
|
208
|
+
return newInstance;
|
|
209
|
+
}
|
|
182
210
|
/**
|
|
183
211
|
* Assign ignore delete_at in model
|
|
184
212
|
* @param {boolean} condition
|
|
@@ -203,11 +231,7 @@ class Model extends AbstractModel_1.AbstractModel {
|
|
|
203
231
|
* @return {this} this
|
|
204
232
|
*/
|
|
205
233
|
registry(func) {
|
|
206
|
-
this.$state.set('REGISTRY', {
|
|
207
|
-
...func,
|
|
208
|
-
attach: this._attach,
|
|
209
|
-
detach: this._detach
|
|
210
|
-
});
|
|
234
|
+
this.$state.set('REGISTRY', Object.assign(Object.assign({}, func), { attach: this._attach, detach: this._detach }));
|
|
211
235
|
return this;
|
|
212
236
|
}
|
|
213
237
|
/**
|
|
@@ -218,9 +242,10 @@ class Model extends AbstractModel_1.AbstractModel {
|
|
|
218
242
|
*/
|
|
219
243
|
with(...nameRelations) {
|
|
220
244
|
const relations = nameRelations.map((name) => {
|
|
221
|
-
|
|
245
|
+
var _a, _b;
|
|
246
|
+
const relation = (_a = this.$state.get('RELATION')) === null || _a === void 0 ? void 0 : _a.find((data) => data.name === name);
|
|
222
247
|
this._assertError(relation == null, `relation ${name} not be register !`);
|
|
223
|
-
const relationHasExists = Object.values(this.$constants('RELATIONSHIP'))
|
|
248
|
+
const relationHasExists = (_b = Object.values(this.$constants('RELATIONSHIP'))) === null || _b === void 0 ? void 0 : _b.includes(relation.relation);
|
|
224
249
|
this._assertError(!relationHasExists, `unknown relationship in [${this.$constants('RELATIONSHIP')}] !`);
|
|
225
250
|
if (relation.query == null)
|
|
226
251
|
relation.query = new relation.model();
|
|
@@ -250,6 +275,17 @@ class Model extends AbstractModel_1.AbstractModel {
|
|
|
250
275
|
this.$state.set('WITH_EXISTS', true);
|
|
251
276
|
return this;
|
|
252
277
|
}
|
|
278
|
+
/**
|
|
279
|
+
*
|
|
280
|
+
* Use relations in registry of model return only exists result of relation query
|
|
281
|
+
* @param {...string} nameRelations if data exists return blank
|
|
282
|
+
* @return {this} this
|
|
283
|
+
*/
|
|
284
|
+
has(...nameRelations) {
|
|
285
|
+
this.with(...nameRelations);
|
|
286
|
+
this.$state.set('WITH_EXISTS', true);
|
|
287
|
+
return this;
|
|
288
|
+
}
|
|
253
289
|
/**
|
|
254
290
|
*
|
|
255
291
|
* Use relation '${name}' registry of model return callback this query model
|
|
@@ -258,9 +294,10 @@ class Model extends AbstractModel_1.AbstractModel {
|
|
|
258
294
|
* @return {this} this
|
|
259
295
|
*/
|
|
260
296
|
withQuery(nameRelation, callback) {
|
|
297
|
+
var _a;
|
|
261
298
|
const relation = this.$state.get('WITH').find((data) => data.name === nameRelation);
|
|
262
299
|
this._assertError(relation == null, `relation ${nameRelation} not be register !`);
|
|
263
|
-
const relationHasExists = Object.values(this.$constants('RELATIONSHIP'))
|
|
300
|
+
const relationHasExists = (_a = Object.values(this.$constants('RELATIONSHIP'))) === null || _a === void 0 ? void 0 : _a.includes(relation.relation);
|
|
264
301
|
this._assertError(!relationHasExists, `unknown relationship in [${this.$constants('RELATIONSHIP')}] !`);
|
|
265
302
|
relation.query = callback(new relation.model());
|
|
266
303
|
return this;
|
|
@@ -273,9 +310,10 @@ class Model extends AbstractModel_1.AbstractModel {
|
|
|
273
310
|
*/
|
|
274
311
|
relations(...nameRelations) {
|
|
275
312
|
const relations = nameRelations.map((name) => {
|
|
276
|
-
|
|
313
|
+
var _a, _b;
|
|
314
|
+
const relation = (_a = this.$state.get('RELATION')) === null || _a === void 0 ? void 0 : _a.find((data) => data.name === name);
|
|
277
315
|
this._assertError(relation == null, `relation ${name} not be register !`);
|
|
278
|
-
const relationHasExists = Object.values(this.$constants('RELATIONSHIP'))
|
|
316
|
+
const relationHasExists = (_b = Object.values(this.$constants('RELATIONSHIP'))) === null || _b === void 0 ? void 0 : _b.includes(relation.relation);
|
|
279
317
|
this._assertError(!relationHasExists, `unknown relationship in [${this.$constants('RELATIONSHIP')}] !`);
|
|
280
318
|
if (relation.query == null)
|
|
281
319
|
relation.query = new relation.model();
|
|
@@ -312,9 +350,10 @@ class Model extends AbstractModel_1.AbstractModel {
|
|
|
312
350
|
* @return {this} this
|
|
313
351
|
*/
|
|
314
352
|
relationQuery(nameRelation, callback) {
|
|
353
|
+
var _a;
|
|
315
354
|
const relation = this.$state.get('WITH').find((data) => data.name === nameRelation);
|
|
316
355
|
this._assertError(relation == null, `relation ${nameRelation} not be register !`);
|
|
317
|
-
const relationHasExists = Object.values(this.$constants('RELATIONSHIP'))
|
|
356
|
+
const relationHasExists = (_a = Object.values(this.$constants('RELATIONSHIP'))) === null || _a === void 0 ? void 0 : _a.includes(relation.relation);
|
|
318
357
|
this._assertError(!relationHasExists, `unknown relationship in [${this.$constants('RELATIONSHIP')}] !`);
|
|
319
358
|
relation.query = callback(new relation.model());
|
|
320
359
|
return this;
|
|
@@ -420,19 +459,19 @@ class Model extends AbstractModel_1.AbstractModel {
|
|
|
420
459
|
return this;
|
|
421
460
|
}
|
|
422
461
|
/**
|
|
423
|
-
|
|
424
|
-
|
|
425
|
-
|
|
426
|
-
|
|
427
|
-
|
|
428
|
-
|
|
429
|
-
|
|
430
|
-
|
|
431
|
-
|
|
432
|
-
|
|
433
|
-
|
|
434
|
-
|
|
435
|
-
|
|
462
|
+
* Assign the relation in model Objects
|
|
463
|
+
* @param {object} relation registry relation in your model
|
|
464
|
+
* @type {object} relation
|
|
465
|
+
* @property {class} model
|
|
466
|
+
* @property {string?} name
|
|
467
|
+
* @property {string?} as
|
|
468
|
+
* @property {string?} localKey
|
|
469
|
+
* @property {string?} foreignKey
|
|
470
|
+
* @property {string?} freezeTable
|
|
471
|
+
* @param {function?} callback callback of query
|
|
472
|
+
* @return {this} this
|
|
473
|
+
*/
|
|
474
|
+
hasOneBuilder({ name, as, model, localKey, foreignKey, freezeTable }, callback) {
|
|
436
475
|
const nameRelation = name == null
|
|
437
476
|
? this._functionRelationName()
|
|
438
477
|
: String(name);
|
|
@@ -467,7 +506,7 @@ class Model extends AbstractModel_1.AbstractModel {
|
|
|
467
506
|
* @param {function?} callback callback of query
|
|
468
507
|
* @return {this} this
|
|
469
508
|
*/
|
|
470
|
-
|
|
509
|
+
hasManyBuilder({ name, as, model, localKey, foreignKey, freezeTable }, callback) {
|
|
471
510
|
const nameRelation = name == null
|
|
472
511
|
? this._functionRelationName()
|
|
473
512
|
: String(name);
|
|
@@ -502,7 +541,7 @@ class Model extends AbstractModel_1.AbstractModel {
|
|
|
502
541
|
* @param {function?} callback callback of query
|
|
503
542
|
* @return {this} this
|
|
504
543
|
*/
|
|
505
|
-
|
|
544
|
+
belongsToBuilder({ name, as, model, localKey, foreignKey, freezeTable }, callback) {
|
|
506
545
|
const nameRelation = name == null
|
|
507
546
|
? this._functionRelationName()
|
|
508
547
|
: String(name);
|
|
@@ -537,7 +576,7 @@ class Model extends AbstractModel_1.AbstractModel {
|
|
|
537
576
|
* @param {function?} callback callback of query
|
|
538
577
|
* @return {this} this
|
|
539
578
|
*/
|
|
540
|
-
|
|
579
|
+
belongsToManyBuilder({ name, as, model, localKey, foreignKey, freezeTable }, callback) {
|
|
541
580
|
const nameRelation = name == null
|
|
542
581
|
? this._functionRelationName()
|
|
543
582
|
: String(name);
|
|
@@ -563,55 +602,65 @@ class Model extends AbstractModel_1.AbstractModel {
|
|
|
563
602
|
* return only in trashed (data has been remove)
|
|
564
603
|
* @return {promise}
|
|
565
604
|
*/
|
|
566
|
-
|
|
567
|
-
|
|
568
|
-
|
|
569
|
-
|
|
605
|
+
trashed() {
|
|
606
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
607
|
+
this.disableSoftDelete();
|
|
608
|
+
this.whereNotNull(this._valuePattern(this.$state.get('SOFT_DELETE_FORMAT')));
|
|
609
|
+
const sql = this._buildQueryModel();
|
|
610
|
+
return yield this._execute({ sql, type: 'GET' });
|
|
611
|
+
});
|
|
570
612
|
}
|
|
571
613
|
/**
|
|
572
614
|
* return all only in trashed (data has been remove)
|
|
573
615
|
* @return {promise}
|
|
574
616
|
*/
|
|
575
|
-
|
|
576
|
-
this
|
|
577
|
-
|
|
578
|
-
|
|
579
|
-
|
|
617
|
+
onlyTrashed() {
|
|
618
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
619
|
+
this.disableSoftDelete();
|
|
620
|
+
this.whereNotNull(this._valuePattern(this.$state.get('SOFT_DELETE_FORMAT')));
|
|
621
|
+
const sql = this._buildQueryModel();
|
|
622
|
+
return yield this._execute({ sql, type: 'GET' });
|
|
623
|
+
});
|
|
580
624
|
}
|
|
581
625
|
/**
|
|
582
626
|
* restore data in trashed
|
|
583
627
|
* @return {promise}
|
|
584
628
|
*/
|
|
585
|
-
|
|
586
|
-
|
|
587
|
-
|
|
588
|
-
|
|
589
|
-
|
|
590
|
-
|
|
591
|
-
|
|
592
|
-
|
|
593
|
-
|
|
594
|
-
|
|
595
|
-
|
|
596
|
-
|
|
597
|
-
|
|
629
|
+
restore() {
|
|
630
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
631
|
+
this.disableSoftDelete();
|
|
632
|
+
const updatedAt = this._valuePattern(this.$state.get('TIMESTAMP_FORMAT').UPDATED_AT);
|
|
633
|
+
const deletedAt = this._valuePattern(this.$state.get('SOFT_DELETE_FORMAT'));
|
|
634
|
+
const query = this.$state.get('TIMESTAMP')
|
|
635
|
+
? `${deletedAt} = NULL , ${updatedAt} = '${this.$utils.timestamp()}'`
|
|
636
|
+
: `${deletedAt} = NULL`;
|
|
637
|
+
this.$state.set('UPDATE', [
|
|
638
|
+
`${this.$constants('UPDATE')}`,
|
|
639
|
+
`${this.$state.get('TABLE_NAME')}`,
|
|
640
|
+
`SET ${query}`
|
|
641
|
+
].join(' '));
|
|
642
|
+
this.$state.set('SAVE', 'UPDATE');
|
|
643
|
+
return yield this.save();
|
|
644
|
+
});
|
|
598
645
|
}
|
|
599
646
|
/**
|
|
600
647
|
*
|
|
601
648
|
* @override Method
|
|
602
649
|
* @return {promise<string>}
|
|
603
650
|
*/
|
|
604
|
-
|
|
605
|
-
|
|
606
|
-
|
|
607
|
-
|
|
608
|
-
|
|
609
|
-
|
|
610
|
-
|
|
611
|
-
|
|
612
|
-
|
|
613
|
-
|
|
614
|
-
|
|
651
|
+
exceptColumns() {
|
|
652
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
653
|
+
const sql = [
|
|
654
|
+
`${this.$constants('SHOW')}`,
|
|
655
|
+
`${this.$constants('COLUMNS')}`,
|
|
656
|
+
`${this.$constants('FROM')}`,
|
|
657
|
+
`${this.$state.get('TABLE_NAME')}`
|
|
658
|
+
].join(' ');
|
|
659
|
+
const rawColumns = yield this.queryStatement(sql);
|
|
660
|
+
const columns = rawColumns.map((column) => column.Field);
|
|
661
|
+
const removeExcept = columns.filter((column) => !this.$state.get('EXCEPT').includes(column));
|
|
662
|
+
return removeExcept.join(',');
|
|
663
|
+
});
|
|
615
664
|
}
|
|
616
665
|
/**
|
|
617
666
|
*
|
|
@@ -634,12 +683,14 @@ class Model extends AbstractModel_1.AbstractModel {
|
|
|
634
683
|
* @override Method
|
|
635
684
|
* @return {promise<string>}
|
|
636
685
|
*/
|
|
637
|
-
|
|
638
|
-
|
|
639
|
-
|
|
640
|
-
|
|
641
|
-
this.
|
|
642
|
-
|
|
686
|
+
toJSON() {
|
|
687
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
688
|
+
const sql = this._buildQueryModel();
|
|
689
|
+
const result = yield this.queryStatement(sql);
|
|
690
|
+
if (this.$state.get('HIDDEN').length)
|
|
691
|
+
this._hiddenColumnModel(result);
|
|
692
|
+
return JSON.stringify(result);
|
|
693
|
+
});
|
|
643
694
|
}
|
|
644
695
|
/**
|
|
645
696
|
*
|
|
@@ -647,12 +698,14 @@ class Model extends AbstractModel_1.AbstractModel {
|
|
|
647
698
|
* @param {string=} column [column=id]
|
|
648
699
|
* @return {promise<Array>}
|
|
649
700
|
*/
|
|
650
|
-
|
|
651
|
-
this
|
|
652
|
-
|
|
653
|
-
|
|
654
|
-
|
|
655
|
-
|
|
701
|
+
toArray(column = 'id') {
|
|
702
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
703
|
+
this.select(column);
|
|
704
|
+
const sql = this._buildQueryModel();
|
|
705
|
+
const result = yield this.queryStatement(sql);
|
|
706
|
+
const toArray = result.map((data) => data[column]);
|
|
707
|
+
return toArray;
|
|
708
|
+
});
|
|
656
709
|
}
|
|
657
710
|
/**
|
|
658
711
|
*
|
|
@@ -660,15 +713,14 @@ class Model extends AbstractModel_1.AbstractModel {
|
|
|
660
713
|
* @param {string=} column [column=id]
|
|
661
714
|
* @return {promise<number>}
|
|
662
715
|
*/
|
|
663
|
-
|
|
664
|
-
|
|
665
|
-
|
|
666
|
-
`${this.$constants('AVG')}(${column})
|
|
667
|
-
|
|
668
|
-
|
|
669
|
-
|
|
670
|
-
|
|
671
|
-
return result?.shift()?.avg || 0;
|
|
716
|
+
avg(column = 'id') {
|
|
717
|
+
var _a;
|
|
718
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
719
|
+
this.select(`${this.$constants('AVG')}(${column}) ${this.$constants('AS')} avg`);
|
|
720
|
+
const sql = this._buildQueryModel();
|
|
721
|
+
const result = yield this.queryStatement(sql);
|
|
722
|
+
return ((_a = result === null || result === void 0 ? void 0 : result.shift()) === null || _a === void 0 ? void 0 : _a.avg) || 0;
|
|
723
|
+
});
|
|
672
724
|
}
|
|
673
725
|
/**
|
|
674
726
|
*
|
|
@@ -676,15 +728,14 @@ class Model extends AbstractModel_1.AbstractModel {
|
|
|
676
728
|
* @param {string} column [column=id]
|
|
677
729
|
* @return {promise<number>}
|
|
678
730
|
*/
|
|
679
|
-
|
|
680
|
-
|
|
681
|
-
|
|
682
|
-
`${this.$constants('SUM')}(${column})
|
|
683
|
-
|
|
684
|
-
|
|
685
|
-
|
|
686
|
-
|
|
687
|
-
return result?.shift()?.sum || 0;
|
|
731
|
+
sum(column = 'id') {
|
|
732
|
+
var _a;
|
|
733
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
734
|
+
this.select(`${this.$constants('SUM')}(${column}) ${this.$constants('AS')} sum`);
|
|
735
|
+
const sql = this._buildQueryModel();
|
|
736
|
+
const result = yield this.queryStatement(sql);
|
|
737
|
+
return ((_a = result === null || result === void 0 ? void 0 : result.shift()) === null || _a === void 0 ? void 0 : _a.sum) || 0;
|
|
738
|
+
});
|
|
688
739
|
}
|
|
689
740
|
/**
|
|
690
741
|
*
|
|
@@ -692,15 +743,14 @@ class Model extends AbstractModel_1.AbstractModel {
|
|
|
692
743
|
* @param {string=} column [column=id]
|
|
693
744
|
* @return {promise<number>}
|
|
694
745
|
*/
|
|
695
|
-
|
|
696
|
-
|
|
697
|
-
|
|
698
|
-
`${this.$constants('MAX')}(${column})
|
|
699
|
-
|
|
700
|
-
|
|
701
|
-
|
|
702
|
-
|
|
703
|
-
return result?.shift()?.max || 0;
|
|
746
|
+
max(column = 'id') {
|
|
747
|
+
var _a;
|
|
748
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
749
|
+
this.select(`${this.$constants('MAX')}(${column}) ${this.$constants('AS')} max`);
|
|
750
|
+
const sql = this._buildQueryModel();
|
|
751
|
+
const result = yield this.queryStatement(sql);
|
|
752
|
+
return ((_a = result === null || result === void 0 ? void 0 : result.shift()) === null || _a === void 0 ? void 0 : _a.max) || 0;
|
|
753
|
+
});
|
|
704
754
|
}
|
|
705
755
|
/**
|
|
706
756
|
*
|
|
@@ -708,15 +758,14 @@ class Model extends AbstractModel_1.AbstractModel {
|
|
|
708
758
|
* @param {string=} column [column=id]
|
|
709
759
|
* @return {promise<number>}
|
|
710
760
|
*/
|
|
711
|
-
|
|
712
|
-
|
|
713
|
-
|
|
714
|
-
`${this.$constants('MIN')}(${column})
|
|
715
|
-
|
|
716
|
-
|
|
717
|
-
|
|
718
|
-
|
|
719
|
-
return result?.shift()?.min || 0;
|
|
761
|
+
min(column = 'id') {
|
|
762
|
+
var _a;
|
|
763
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
764
|
+
this.select(`${this.$constants('MIN')}(${column}) ${this.$constants('AS')} min`);
|
|
765
|
+
const sql = this._buildQueryModel();
|
|
766
|
+
const result = yield this.queryStatement(sql);
|
|
767
|
+
return ((_a = result === null || result === void 0 ? void 0 : result.shift()) === null || _a === void 0 ? void 0 : _a.min) || 0;
|
|
768
|
+
});
|
|
720
769
|
}
|
|
721
770
|
/**
|
|
722
771
|
*
|
|
@@ -724,163 +773,247 @@ class Model extends AbstractModel_1.AbstractModel {
|
|
|
724
773
|
* @param {string=} column [column=id]
|
|
725
774
|
* @return {promise<number>}
|
|
726
775
|
*/
|
|
727
|
-
|
|
728
|
-
|
|
729
|
-
|
|
730
|
-
`${this.$constants('COUNT')}(${column})
|
|
731
|
-
|
|
732
|
-
|
|
733
|
-
|
|
734
|
-
|
|
735
|
-
|
|
776
|
+
count(column = 'id') {
|
|
777
|
+
var _a;
|
|
778
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
779
|
+
this.select(`${this.$constants('COUNT')}(${column}) ${this.$constants('AS')} total`);
|
|
780
|
+
const sql = this._buildQueryModel();
|
|
781
|
+
const result = yield this.queryStatement(sql);
|
|
782
|
+
return ((_a = result === null || result === void 0 ? void 0 : result.shift()) === null || _a === void 0 ? void 0 : _a.total) || 0;
|
|
783
|
+
});
|
|
784
|
+
}
|
|
785
|
+
/**
|
|
786
|
+
*
|
|
787
|
+
* execute data return result is exists
|
|
788
|
+
* @return {promise<boolean>}
|
|
789
|
+
*/
|
|
790
|
+
exists() {
|
|
791
|
+
var _a;
|
|
792
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
793
|
+
this.select(this.$state.get('PRIMARY_KEY'));
|
|
794
|
+
const sql = this._buildQueryModel();
|
|
795
|
+
const result = yield this.queryStatement([
|
|
796
|
+
`${this.$constants('SELECT')}`,
|
|
797
|
+
`${this.$constants('EXISTS')}`,
|
|
798
|
+
`(${sql} ${this.$constants('LIMIT')} 1)`,
|
|
799
|
+
`${this.$constants('AS')} 'exists'`
|
|
800
|
+
].join(' '));
|
|
801
|
+
return !!((_a = result === null || result === void 0 ? void 0 : result.shift()) === null || _a === void 0 ? void 0 : _a.exists) || false;
|
|
802
|
+
});
|
|
736
803
|
}
|
|
737
804
|
/**
|
|
738
805
|
* delete data from the database
|
|
739
806
|
* @override Method
|
|
740
807
|
* @return {promise<boolean>}
|
|
741
808
|
*/
|
|
742
|
-
|
|
743
|
-
|
|
744
|
-
|
|
745
|
-
|
|
746
|
-
|
|
747
|
-
|
|
748
|
-
|
|
749
|
-
`${this.$
|
|
809
|
+
delete() {
|
|
810
|
+
var _a, _b;
|
|
811
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
812
|
+
if (!this.$state.get('WHERE'))
|
|
813
|
+
throw new Error("Can't delete without where condition");
|
|
814
|
+
if (this.$state.get('SOFT_DELETE')) {
|
|
815
|
+
const deletedAt = this._valuePattern(this.$state.get('SOFT_DELETE_FORMAT'));
|
|
816
|
+
const query = `${deletedAt} = '${this.$utils.timestamp()}'`;
|
|
817
|
+
let sql = [
|
|
818
|
+
`${this.$constants('UPDATE')}`,
|
|
819
|
+
`${this.$state.get('TABLE_NAME')}`,
|
|
820
|
+
`${this.$constants('SET')}`,
|
|
821
|
+
`${query}`
|
|
822
|
+
].join(' ');
|
|
823
|
+
if (this.$state.get('TIMESTAMP')) {
|
|
824
|
+
const updatedAt = this._valuePattern(this.$state.get('TIMESTAMP_FORMAT').UPDATED_AT);
|
|
825
|
+
sql = `${sql} , ${updatedAt} = '${this.$utils.timestamp()}'`;
|
|
826
|
+
}
|
|
827
|
+
this.$state.set('UPDATE', `${sql} ${this.$state.get('WHERE')}`);
|
|
828
|
+
const result = yield this.actionStatement({ sql: this.$state.get('UPDATE') });
|
|
829
|
+
return (_a = !!result) !== null && _a !== void 0 ? _a : false;
|
|
830
|
+
}
|
|
831
|
+
this.$state.set('DELETE', [
|
|
832
|
+
`${this.$constants('DELETE')}`,
|
|
833
|
+
`${this.$state.get('FROM')}`,
|
|
750
834
|
`${this.$state.get('TABLE_NAME')}`,
|
|
751
|
-
`${this.$
|
|
752
|
-
|
|
835
|
+
`${this.$state.get('WHERE')}`
|
|
836
|
+
].join(' '));
|
|
837
|
+
const result = yield this.actionStatement({ sql: this.$state.get('DELETE') });
|
|
838
|
+
return (_b = !!result) !== null && _b !== void 0 ? _b : false;
|
|
839
|
+
});
|
|
840
|
+
}
|
|
841
|
+
/**
|
|
842
|
+
*
|
|
843
|
+
* force delete data from the database
|
|
844
|
+
* @return {promise<boolean>}
|
|
845
|
+
*/
|
|
846
|
+
forceDelete() {
|
|
847
|
+
var _a;
|
|
848
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
849
|
+
const sql = [
|
|
850
|
+
`${this.$constants('DELETE')}`,
|
|
851
|
+
`${this.$state.get('FROM')}`,
|
|
852
|
+
`${this.$state.get('TABLE_NAME')}`,
|
|
853
|
+
`${this.$state.get('WHERE')}`
|
|
753
854
|
].join(' ');
|
|
754
|
-
|
|
755
|
-
|
|
756
|
-
|
|
757
|
-
}
|
|
758
|
-
this.$state.set('UPDATE', `${sql} ${this.$state.get('WHERE')}`);
|
|
759
|
-
const result = await this.actionStatement({ sql: this.$state.get('UPDATE') });
|
|
760
|
-
return !!result ?? false;
|
|
761
|
-
}
|
|
762
|
-
this.$state.set('DELETE', [
|
|
763
|
-
`${this.$constants('DELETE')}`,
|
|
764
|
-
`${this.$state.get('FROM')}`,
|
|
765
|
-
`${this.$state.get('TABLE_NAME')}`,
|
|
766
|
-
`${this.$state.get('WHERE')}`
|
|
767
|
-
].join(' '));
|
|
768
|
-
const result = await this.actionStatement({ sql: this.$state.get('DELETE') });
|
|
769
|
-
return !!result ?? false;
|
|
855
|
+
const result = yield this.actionStatement({ sql });
|
|
856
|
+
return (_a = !!result) !== null && _a !== void 0 ? _a : false;
|
|
857
|
+
});
|
|
770
858
|
}
|
|
771
859
|
/**
|
|
772
860
|
*
|
|
773
861
|
* @override Method
|
|
774
862
|
* @return {promise<any>}
|
|
775
863
|
*/
|
|
776
|
-
|
|
777
|
-
|
|
778
|
-
|
|
779
|
-
|
|
780
|
-
|
|
781
|
-
|
|
864
|
+
first() {
|
|
865
|
+
var _a;
|
|
866
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
867
|
+
this._validateMethod('first');
|
|
868
|
+
if ((_a = this.$state.get('EXCEPT')) === null || _a === void 0 ? void 0 : _a.length)
|
|
869
|
+
this.select(yield this.exceptColumns());
|
|
870
|
+
let sql = this._buildQueryModel();
|
|
871
|
+
if (!sql.includes(this.$constants('LIMIT'))) {
|
|
872
|
+
sql = `${sql} ${this.$constants('LIMIT')} 1`;
|
|
873
|
+
if (this.$state.get('WITH_EXISTS')) {
|
|
874
|
+
sql = `${this._queryRelationsExists()} ${this.$constants('LIMIT')} 1`;
|
|
875
|
+
}
|
|
876
|
+
if (this.$state.get('VOID'))
|
|
877
|
+
return null;
|
|
878
|
+
return yield this._execute({ sql, type: 'FIRST' });
|
|
879
|
+
}
|
|
880
|
+
sql = sql.replace(this.$state.get('LIMIT'), `${this.$constants('LIMIT')} 1`);
|
|
782
881
|
if (this.$state.get('WITH_EXISTS')) {
|
|
783
882
|
sql = `${this._queryRelationsExists()} ${this.$constants('LIMIT')} 1`;
|
|
784
883
|
}
|
|
785
|
-
|
|
786
|
-
|
|
787
|
-
|
|
788
|
-
|
|
789
|
-
sql = `${this._queryRelationsExists()} ${this.$constants('LIMIT')} 1`;
|
|
790
|
-
}
|
|
791
|
-
return await this._execute({ sql, type: 'FIRST' });
|
|
884
|
+
if (this.$state.get('VOID'))
|
|
885
|
+
return null;
|
|
886
|
+
return yield this._execute({ sql, type: 'FIRST' });
|
|
887
|
+
});
|
|
792
888
|
}
|
|
793
889
|
/**
|
|
794
890
|
*
|
|
795
891
|
* @override Method
|
|
796
892
|
* @return {promise<any>}
|
|
797
893
|
*/
|
|
798
|
-
|
|
799
|
-
return
|
|
894
|
+
findOne() {
|
|
895
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
896
|
+
return yield this.first();
|
|
897
|
+
});
|
|
800
898
|
}
|
|
801
899
|
/**
|
|
802
900
|
*
|
|
803
901
|
* @override Method
|
|
804
902
|
* @return {promise<object | Error>}
|
|
805
903
|
*/
|
|
806
|
-
|
|
807
|
-
|
|
808
|
-
|
|
809
|
-
|
|
810
|
-
|
|
811
|
-
|
|
812
|
-
|
|
813
|
-
|
|
814
|
-
|
|
815
|
-
|
|
816
|
-
|
|
817
|
-
|
|
818
|
-
|
|
904
|
+
firstOrError(message, options) {
|
|
905
|
+
var _a;
|
|
906
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
907
|
+
this._validateMethod('firstOrError');
|
|
908
|
+
if ((_a = this.$state.get('EXCEPT')) === null || _a === void 0 ? void 0 : _a.length)
|
|
909
|
+
this.select(yield this.exceptColumns());
|
|
910
|
+
let sql = this._buildQueryModel();
|
|
911
|
+
if (!sql.includes(this.$constants('LIMIT'))) {
|
|
912
|
+
sql = `${sql} ${this.$constants('LIMIT')} 1`;
|
|
913
|
+
return yield this._execute({ sql, type: 'FIRST_OR_ERROR', message, options });
|
|
914
|
+
}
|
|
915
|
+
sql = sql.replace(this.$state.get('LIMIT'), `${this.$constants('LIMIT')} 1`);
|
|
916
|
+
if (this.$state.get('WITH_EXISTS')) {
|
|
917
|
+
sql = this._queryRelationsExists();
|
|
918
|
+
}
|
|
919
|
+
return yield this._execute({ sql, type: 'FIRST_OR_ERROR', message, options });
|
|
920
|
+
});
|
|
819
921
|
}
|
|
820
922
|
/**
|
|
821
923
|
*
|
|
822
924
|
* @override Method
|
|
823
925
|
* @return {promise<any>}
|
|
824
926
|
*/
|
|
825
|
-
|
|
826
|
-
|
|
927
|
+
findOneOrError(message, options) {
|
|
928
|
+
var _a;
|
|
929
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
930
|
+
this._validateMethod('findOneOrError');
|
|
931
|
+
if ((_a = this.$state.get('EXCEPT')) === null || _a === void 0 ? void 0 : _a.length)
|
|
932
|
+
this.select(yield this.exceptColumns());
|
|
933
|
+
let sql = this._buildQueryModel();
|
|
934
|
+
if (!sql.includes(this.$constants('LIMIT'))) {
|
|
935
|
+
sql = `${sql} ${this.$constants('LIMIT')} 1`;
|
|
936
|
+
return yield this._execute({ sql, type: 'FIRST_OR_ERROR', message, options });
|
|
937
|
+
}
|
|
938
|
+
sql = sql.replace(this.$state.get('LIMIT'), `${this.$constants('LIMIT')} 1`);
|
|
939
|
+
if (this.$state.get('WITH_EXISTS')) {
|
|
940
|
+
sql = this._queryRelationsExists();
|
|
941
|
+
}
|
|
942
|
+
return yield this._execute({ sql, type: 'FIRST_OR_ERROR', message, options });
|
|
943
|
+
});
|
|
827
944
|
}
|
|
828
945
|
/**
|
|
829
946
|
*
|
|
830
947
|
* @override Method
|
|
831
948
|
* @return {promise<array>}
|
|
832
949
|
*/
|
|
833
|
-
|
|
834
|
-
|
|
835
|
-
|
|
836
|
-
|
|
837
|
-
|
|
838
|
-
|
|
839
|
-
|
|
840
|
-
|
|
841
|
-
|
|
950
|
+
all() {
|
|
951
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
952
|
+
const sql = [
|
|
953
|
+
`${this.$constants('SELECT')}`,
|
|
954
|
+
`*`,
|
|
955
|
+
`${this.$constants('FROM')}`,
|
|
956
|
+
`${this.$state.get('TABLE_NAME')}`
|
|
957
|
+
].join(' ');
|
|
958
|
+
const result = yield this.queryStatement(sql);
|
|
959
|
+
return result;
|
|
960
|
+
});
|
|
842
961
|
}
|
|
843
962
|
/**
|
|
844
963
|
*
|
|
845
964
|
* @override Method
|
|
846
965
|
* @return {promise<object | null>}
|
|
847
966
|
*/
|
|
848
|
-
|
|
849
|
-
|
|
850
|
-
|
|
851
|
-
|
|
852
|
-
|
|
853
|
-
|
|
854
|
-
|
|
855
|
-
|
|
856
|
-
|
|
857
|
-
|
|
858
|
-
|
|
967
|
+
find(id) {
|
|
968
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
969
|
+
this._validateMethod('find');
|
|
970
|
+
this._handleSoftDelete();
|
|
971
|
+
const sql = [
|
|
972
|
+
`${this.$constants('SELECT')}`,
|
|
973
|
+
`*`,
|
|
974
|
+
`${this.$constants('FROM')}`,
|
|
975
|
+
`${this.$state.get('TABLE_NAME')}`,
|
|
976
|
+
`${this.$constants('WHERE')}`,
|
|
977
|
+
`${this.$state.get('PRIMARY_KEY')} = ${id}`
|
|
978
|
+
].join(' ');
|
|
979
|
+
const result = yield this.queryStatement(sql);
|
|
980
|
+
return (result === null || result === void 0 ? void 0 : result.shift()) || null;
|
|
981
|
+
});
|
|
859
982
|
}
|
|
860
983
|
/**
|
|
861
984
|
*
|
|
862
985
|
* @override Method
|
|
863
986
|
* @return {promise<array>}
|
|
864
987
|
*/
|
|
865
|
-
|
|
866
|
-
|
|
867
|
-
|
|
868
|
-
|
|
869
|
-
|
|
870
|
-
|
|
871
|
-
|
|
872
|
-
|
|
988
|
+
get() {
|
|
989
|
+
var _a;
|
|
990
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
991
|
+
this._validateMethod('get');
|
|
992
|
+
if ((_a = this.$state.get('EXCEPT')) === null || _a === void 0 ? void 0 : _a.length)
|
|
993
|
+
this.select(yield this.exceptColumns());
|
|
994
|
+
let sql = this._buildQueryModel();
|
|
995
|
+
if (this.$state.get('WITH_EXISTS')) {
|
|
996
|
+
sql = this._queryRelationsExists();
|
|
997
|
+
}
|
|
998
|
+
if (this.$state.get('VOID'))
|
|
999
|
+
return [];
|
|
1000
|
+
return yield this._execute({ sql, type: 'GET' });
|
|
1001
|
+
});
|
|
873
1002
|
}
|
|
874
1003
|
/**
|
|
875
1004
|
*
|
|
876
1005
|
* @override Method
|
|
877
1006
|
* @return {promise<array>}
|
|
878
1007
|
*/
|
|
879
|
-
|
|
880
|
-
|
|
881
|
-
|
|
882
|
-
|
|
883
|
-
|
|
1008
|
+
findMany() {
|
|
1009
|
+
var _a;
|
|
1010
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
1011
|
+
this._validateMethod('findMany');
|
|
1012
|
+
if ((_a = this.$state.get('EXCEPT')) === null || _a === void 0 ? void 0 : _a.length)
|
|
1013
|
+
this.select(yield this.exceptColumns());
|
|
1014
|
+
const sql = this._buildQueryModel();
|
|
1015
|
+
return yield this._execute({ sql, type: 'GET' });
|
|
1016
|
+
});
|
|
884
1017
|
}
|
|
885
1018
|
/**
|
|
886
1019
|
*
|
|
@@ -890,40 +1023,44 @@ class Model extends AbstractModel_1.AbstractModel {
|
|
|
890
1023
|
* @property {number} paginationOptions.page
|
|
891
1024
|
* @return {promise<Pagination>}
|
|
892
1025
|
*/
|
|
893
|
-
|
|
894
|
-
|
|
895
|
-
|
|
896
|
-
|
|
897
|
-
limit =
|
|
898
|
-
page =
|
|
899
|
-
|
|
900
|
-
|
|
901
|
-
|
|
902
|
-
|
|
903
|
-
|
|
904
|
-
|
|
905
|
-
|
|
906
|
-
|
|
907
|
-
|
|
908
|
-
|
|
909
|
-
|
|
910
|
-
|
|
911
|
-
|
|
912
|
-
|
|
1026
|
+
pagination(paginationOptions) {
|
|
1027
|
+
var _a, _b;
|
|
1028
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
1029
|
+
this._validateMethod('pagination');
|
|
1030
|
+
let limit = 15;
|
|
1031
|
+
let page = 1;
|
|
1032
|
+
if (paginationOptions != null) {
|
|
1033
|
+
limit = (paginationOptions === null || paginationOptions === void 0 ? void 0 : paginationOptions.limit) || limit;
|
|
1034
|
+
page = (paginationOptions === null || paginationOptions === void 0 ? void 0 : paginationOptions.page) || page;
|
|
1035
|
+
}
|
|
1036
|
+
this._assertError((_a = this.$logger) === null || _a === void 0 ? void 0 : _a.check('limit'), `this '[pagination]' can't support '[limit]' method`);
|
|
1037
|
+
if ((_b = this.$state.get('EXCEPT')) === null || _b === void 0 ? void 0 : _b.length)
|
|
1038
|
+
this.select(yield this.exceptColumns());
|
|
1039
|
+
const offset = (page - 1) * limit;
|
|
1040
|
+
this.$state.set('PER_PAGE', limit);
|
|
1041
|
+
this.$state.set('PAGE', page);
|
|
1042
|
+
let sql = this._buildQueryModel();
|
|
1043
|
+
if (this.$state.get('WITH_EXISTS')) {
|
|
1044
|
+
sql = this._queryRelationsExists();
|
|
1045
|
+
}
|
|
1046
|
+
if (!sql.includes(this.$constants('LIMIT'))) {
|
|
1047
|
+
sql = [
|
|
1048
|
+
`${sql}`,
|
|
1049
|
+
`${this.$constants('LIMIT')}`,
|
|
1050
|
+
`${limit}`,
|
|
1051
|
+
`${this.$constants('OFFSET')}`,
|
|
1052
|
+
`${offset}`
|
|
1053
|
+
].join(' ');
|
|
1054
|
+
return yield this._execute({ sql, type: 'PAGINATION' });
|
|
1055
|
+
}
|
|
1056
|
+
sql = sql.replace(this.$state.get('LIMIT'), [
|
|
913
1057
|
`${this.$constants('LIMIT')}`,
|
|
914
1058
|
`${limit}`,
|
|
915
1059
|
`${this.$constants('OFFSET')}`,
|
|
916
1060
|
`${offset}`
|
|
917
|
-
].join(' ');
|
|
918
|
-
return
|
|
919
|
-
}
|
|
920
|
-
sql = sql.replace(this.$state.get('LIMIT'), [
|
|
921
|
-
`${this.$constants('LIMIT')}`,
|
|
922
|
-
`${limit}`,
|
|
923
|
-
`${this.$constants('OFFSET')}`,
|
|
924
|
-
`${offset}`
|
|
925
|
-
].join(' '));
|
|
926
|
-
return await this._execute({ sql, type: 'PAGINATION' });
|
|
1061
|
+
].join(' '));
|
|
1062
|
+
return yield this._execute({ sql, type: 'PAGINATION' });
|
|
1063
|
+
});
|
|
927
1064
|
}
|
|
928
1065
|
/**
|
|
929
1066
|
*
|
|
@@ -933,14 +1070,44 @@ class Model extends AbstractModel_1.AbstractModel {
|
|
|
933
1070
|
* @property {number} paginationOptions.page
|
|
934
1071
|
* @return {promise<Pagination>}
|
|
935
1072
|
*/
|
|
936
|
-
|
|
937
|
-
|
|
938
|
-
|
|
939
|
-
|
|
940
|
-
limit =
|
|
941
|
-
page =
|
|
942
|
-
|
|
943
|
-
|
|
1073
|
+
paginate(paginationOptions) {
|
|
1074
|
+
var _a, _b;
|
|
1075
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
1076
|
+
this._validateMethod('paginate');
|
|
1077
|
+
let limit = 15;
|
|
1078
|
+
let page = 1;
|
|
1079
|
+
if (paginationOptions != null) {
|
|
1080
|
+
limit = (paginationOptions === null || paginationOptions === void 0 ? void 0 : paginationOptions.limit) || limit;
|
|
1081
|
+
page = (paginationOptions === null || paginationOptions === void 0 ? void 0 : paginationOptions.page) || page;
|
|
1082
|
+
}
|
|
1083
|
+
this._assertError((_a = this.$logger) === null || _a === void 0 ? void 0 : _a.check('limit'), `this '[pagination]' can't support '[limit]' method`);
|
|
1084
|
+
if ((_b = this.$state.get('EXCEPT')) === null || _b === void 0 ? void 0 : _b.length)
|
|
1085
|
+
this.select(yield this.exceptColumns());
|
|
1086
|
+
const offset = (page - 1) * limit;
|
|
1087
|
+
this.$state.set('PER_PAGE', limit);
|
|
1088
|
+
this.$state.set('PAGE', page);
|
|
1089
|
+
let sql = this._buildQueryModel();
|
|
1090
|
+
if (this.$state.get('WITH_EXISTS')) {
|
|
1091
|
+
sql = this._queryRelationsExists();
|
|
1092
|
+
}
|
|
1093
|
+
if (!sql.includes(this.$constants('LIMIT'))) {
|
|
1094
|
+
sql = [
|
|
1095
|
+
`${sql}`,
|
|
1096
|
+
`${this.$constants('LIMIT')}`,
|
|
1097
|
+
`${limit}`,
|
|
1098
|
+
`${this.$constants('OFFSET')}`,
|
|
1099
|
+
`${offset}`
|
|
1100
|
+
].join(' ');
|
|
1101
|
+
return yield this._execute({ sql, type: 'PAGINATION' });
|
|
1102
|
+
}
|
|
1103
|
+
sql = sql.replace(this.$state.get('LIMIT'), [
|
|
1104
|
+
`${this.$constants('LIMIT')}`,
|
|
1105
|
+
`${limit}`,
|
|
1106
|
+
`${this.$constants('OFFSET')}`,
|
|
1107
|
+
`${offset}`
|
|
1108
|
+
].join(' '));
|
|
1109
|
+
return yield this._execute({ sql, type: 'PAGINATION' });
|
|
1110
|
+
});
|
|
944
1111
|
}
|
|
945
1112
|
/**
|
|
946
1113
|
*
|
|
@@ -948,42 +1115,46 @@ class Model extends AbstractModel_1.AbstractModel {
|
|
|
948
1115
|
* @param {string} column
|
|
949
1116
|
* @return {Promise<array>}
|
|
950
1117
|
*/
|
|
951
|
-
|
|
952
|
-
|
|
953
|
-
|
|
954
|
-
|
|
955
|
-
|
|
956
|
-
|
|
957
|
-
|
|
958
|
-
|
|
959
|
-
|
|
960
|
-
|
|
961
|
-
|
|
962
|
-
|
|
963
|
-
|
|
964
|
-
|
|
965
|
-
|
|
966
|
-
|
|
967
|
-
|
|
968
|
-
|
|
969
|
-
|
|
970
|
-
|
|
971
|
-
|
|
972
|
-
|
|
973
|
-
|
|
974
|
-
|
|
975
|
-
|
|
976
|
-
|
|
977
|
-
|
|
978
|
-
|
|
979
|
-
const
|
|
980
|
-
const
|
|
981
|
-
|
|
982
|
-
[column]
|
|
983
|
-
data
|
|
1118
|
+
getGroupBy(column) {
|
|
1119
|
+
var _a;
|
|
1120
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
1121
|
+
if ((_a = this.$state.get('EXCEPT')) === null || _a === void 0 ? void 0 : _a.length)
|
|
1122
|
+
this.select(yield this.exceptColumns());
|
|
1123
|
+
this.$state.set('GROUP_BY', `${this.$constants('GROUP_BY')} ${column}`);
|
|
1124
|
+
this.$state.set('SELECT', [
|
|
1125
|
+
`${this.$state.get('SELECT')},`,
|
|
1126
|
+
`${this.$constants('GROUP_CONCAT')}(id)`,
|
|
1127
|
+
`${this.$constants('AS')} data`
|
|
1128
|
+
].join(' '));
|
|
1129
|
+
const sql = this._buildQueryModel();
|
|
1130
|
+
const results = yield this.queryStatement(sql);
|
|
1131
|
+
let data = [];
|
|
1132
|
+
results.forEach((result) => {
|
|
1133
|
+
var _a, _b;
|
|
1134
|
+
const splits = (_b = (_a = result === null || result === void 0 ? void 0 : result.data) === null || _a === void 0 ? void 0 : _a.split(',')) !== null && _b !== void 0 ? _b : '0';
|
|
1135
|
+
splits.forEach((split) => data = [...data, split]);
|
|
1136
|
+
});
|
|
1137
|
+
const sqlChild = [
|
|
1138
|
+
`${this.$constants('SELECT')}`,
|
|
1139
|
+
`*`,
|
|
1140
|
+
`${this.$constants('FROM')}`,
|
|
1141
|
+
`${this.$state.get('TABLE_NAME')}`,
|
|
1142
|
+
`${this.$constants('WHERE')} id`,
|
|
1143
|
+
`${this.$constants('IN')}`,
|
|
1144
|
+
`(${data.map((a) => `\'${a}\'`).join(',') || ['0']})`
|
|
1145
|
+
].join(' ');
|
|
1146
|
+
const childData = yield this.queryStatement(sqlChild);
|
|
1147
|
+
const child = yield this._executeGroup(childData);
|
|
1148
|
+
const resultData = results.map((result) => {
|
|
1149
|
+
const id = result[column];
|
|
1150
|
+
const newData = child.filter((data) => data[column] === id);
|
|
1151
|
+
return ({
|
|
1152
|
+
[column]: id,
|
|
1153
|
+
data: newData
|
|
1154
|
+
});
|
|
984
1155
|
});
|
|
1156
|
+
return resultData;
|
|
985
1157
|
});
|
|
986
|
-
return resultData;
|
|
987
1158
|
}
|
|
988
1159
|
/**
|
|
989
1160
|
*
|
|
@@ -1120,43 +1291,98 @@ class Model extends AbstractModel_1.AbstractModel {
|
|
|
1120
1291
|
this.$state.set('SAVE', 'INSERT_MULTIPLE');
|
|
1121
1292
|
return this;
|
|
1122
1293
|
}
|
|
1294
|
+
/**
|
|
1295
|
+
*
|
|
1296
|
+
* @param {object} data create not exists data
|
|
1297
|
+
* @override Method
|
|
1298
|
+
* @return {this} this this
|
|
1299
|
+
*/
|
|
1300
|
+
createNotExists(data) {
|
|
1301
|
+
const query = this._queryInsertModel(data);
|
|
1302
|
+
this.$state.set('INSERT', [
|
|
1303
|
+
`${this.$constants('INSERT')}`,
|
|
1304
|
+
`${this.$state.get('TABLE_NAME')}`,
|
|
1305
|
+
`${query}`
|
|
1306
|
+
].join(' '));
|
|
1307
|
+
this.$state.set('SAVE', 'INSERT_NOT_EXISTS');
|
|
1308
|
+
return this;
|
|
1309
|
+
}
|
|
1123
1310
|
/**
|
|
1124
1311
|
*
|
|
1125
1312
|
* @override Method
|
|
1126
1313
|
* @return {Promise<any>}
|
|
1127
1314
|
*/
|
|
1128
|
-
|
|
1129
|
-
|
|
1130
|
-
|
|
1131
|
-
|
|
1132
|
-
|
|
1133
|
-
|
|
1134
|
-
|
|
1135
|
-
|
|
1315
|
+
save() {
|
|
1316
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
1317
|
+
const attributes = this.$attributes;
|
|
1318
|
+
if (attributes != null) {
|
|
1319
|
+
while (true) {
|
|
1320
|
+
if (this.$state.get('WHERE')) {
|
|
1321
|
+
const query = this._queryUpdateModel(attributes);
|
|
1322
|
+
this.$state.set('UPDATE', [
|
|
1323
|
+
`${this.$constants('UPDATE')}`,
|
|
1324
|
+
`${this.$state.get('TABLE_NAME')}`,
|
|
1325
|
+
`${query}`
|
|
1326
|
+
].join(' '));
|
|
1327
|
+
this.$state.set('SAVE', 'UPDATE');
|
|
1328
|
+
break;
|
|
1329
|
+
}
|
|
1330
|
+
const query = this._queryInsertModel(attributes);
|
|
1331
|
+
this.$state.set('INSERT', [
|
|
1332
|
+
`${this.$constants('INSERT')}`,
|
|
1136
1333
|
`${this.$state.get('TABLE_NAME')}`,
|
|
1137
1334
|
`${query}`
|
|
1138
1335
|
].join(' '));
|
|
1139
|
-
this.$state.set('SAVE', '
|
|
1336
|
+
this.$state.set('SAVE', 'INSERT');
|
|
1140
1337
|
break;
|
|
1141
1338
|
}
|
|
1142
|
-
const query = this._queryInsertModel(attributes);
|
|
1143
|
-
this.$state.set('INSERT', [
|
|
1144
|
-
`${this.$constants('INSERT')}`,
|
|
1145
|
-
`${this.$state.get('TABLE_NAME')}`,
|
|
1146
|
-
`${query}`
|
|
1147
|
-
].join(' '));
|
|
1148
|
-
this.$state.set('SAVE', 'INSERT');
|
|
1149
|
-
break;
|
|
1150
1339
|
}
|
|
1151
|
-
|
|
1152
|
-
|
|
1153
|
-
|
|
1154
|
-
|
|
1155
|
-
|
|
1156
|
-
|
|
1157
|
-
|
|
1158
|
-
|
|
1159
|
-
}
|
|
1340
|
+
switch (this.$state.get('SAVE')) {
|
|
1341
|
+
case 'INSERT_MULTIPLE': return yield this._createMultipleModel();
|
|
1342
|
+
case 'INSERT': return yield this._createModel();
|
|
1343
|
+
case 'UPDATE': return yield this._updateModel();
|
|
1344
|
+
case 'INSERT_NOT_EXISTS': return yield this._insertNotExistsModel();
|
|
1345
|
+
case 'UPDATE_OR_INSERT': return yield this._updateOrInsertModel();
|
|
1346
|
+
default: throw new Error(`unknown this [${this.$state.get('SAVE')}]`);
|
|
1347
|
+
}
|
|
1348
|
+
});
|
|
1349
|
+
}
|
|
1350
|
+
/**
|
|
1351
|
+
*
|
|
1352
|
+
* fake data
|
|
1353
|
+
* @param {number} rows number of rows
|
|
1354
|
+
* @return {promise<any>}
|
|
1355
|
+
*/
|
|
1356
|
+
faker(rows = 1) {
|
|
1357
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
1358
|
+
let data = [];
|
|
1359
|
+
const sql = [
|
|
1360
|
+
`${this.$constants('SHOW')}`,
|
|
1361
|
+
`${this.$constants('FIELDS')}`,
|
|
1362
|
+
`${this.$constants('FROM')}`,
|
|
1363
|
+
`${this.$state.get('TABLE_NAME')}`
|
|
1364
|
+
].join(' ');
|
|
1365
|
+
const fields = yield this.queryStatement(sql);
|
|
1366
|
+
for (let row = 0; row < rows; row++) {
|
|
1367
|
+
this._assertError(this.$state.get('TABLE_NAME') === '' || this.$state.get('TABLE_NAME') == null, "unknow this table");
|
|
1368
|
+
let columnAndValue = {};
|
|
1369
|
+
for (const { Field: field, Type: type } of fields) {
|
|
1370
|
+
const passed = ['id', '_id', 'uuid', 'deleted_at', 'deletedAt'].some(p => field.toLowerCase() === p);
|
|
1371
|
+
if (passed)
|
|
1372
|
+
continue;
|
|
1373
|
+
columnAndValue = Object.assign(Object.assign({}, columnAndValue), { [field]: this.$utils.faker(type) });
|
|
1374
|
+
}
|
|
1375
|
+
data = [...data, columnAndValue];
|
|
1376
|
+
}
|
|
1377
|
+
const query = this._queryInsertMultipleModel(data);
|
|
1378
|
+
this.$state.set('INSERT', [
|
|
1379
|
+
`${this.$constants('INSERT')}`,
|
|
1380
|
+
`${this.$state.get('TABLE_NAME')}`,
|
|
1381
|
+
`${query}`
|
|
1382
|
+
].join(' '));
|
|
1383
|
+
this.$state.set('SAVE', 'INSERT_MULTIPLE');
|
|
1384
|
+
return yield this.save();
|
|
1385
|
+
});
|
|
1160
1386
|
}
|
|
1161
1387
|
_valuePattern(value) {
|
|
1162
1388
|
switch (this.$state.get('PATTERN')) {
|
|
@@ -1188,14 +1414,16 @@ class Model extends AbstractModel_1.AbstractModel {
|
|
|
1188
1414
|
return this;
|
|
1189
1415
|
}
|
|
1190
1416
|
_tableName() {
|
|
1191
|
-
|
|
1417
|
+
var _a;
|
|
1418
|
+
return (_a = this.$state.get('TABLE_NAME')) === null || _a === void 0 ? void 0 : _a.replace(/`/g, '');
|
|
1192
1419
|
}
|
|
1193
1420
|
_valueInRelation(relationModel) {
|
|
1421
|
+
var _a, _b;
|
|
1194
1422
|
const relation = relationModel.relation;
|
|
1195
|
-
const model = relationModel.model
|
|
1423
|
+
const model = (_a = relationModel.model) === null || _a === void 0 ? void 0 : _a.name;
|
|
1196
1424
|
const table = relationModel.freezeTable
|
|
1197
1425
|
? relationModel.freezeTable
|
|
1198
|
-
: relationModel.query
|
|
1426
|
+
: (_b = relationModel.query) === null || _b === void 0 ? void 0 : _b._tableName();
|
|
1199
1427
|
const name = relationModel.name;
|
|
1200
1428
|
const as = relationModel.as;
|
|
1201
1429
|
this._assertError(!model || model == null, 'not found model');
|
|
@@ -1216,19 +1444,35 @@ class Model extends AbstractModel_1.AbstractModel {
|
|
|
1216
1444
|
if (checkRelationIsBelongsTo) {
|
|
1217
1445
|
foreignKey = localKey;
|
|
1218
1446
|
localKey = this._valuePattern([
|
|
1219
|
-
`${pluralize_1.default.singular(table
|
|
1447
|
+
`${pluralize_1.default.singular(table !== null && table !== void 0 ? table : '')}`,
|
|
1220
1448
|
`${this.$state.get('PRIMARY_KEY')}`
|
|
1221
1449
|
].join('_'));
|
|
1222
1450
|
}
|
|
1451
|
+
const checkRelationIsBelongsToMany = [
|
|
1452
|
+
relationModel.localKey == null,
|
|
1453
|
+
relationModel.foreignKey == null,
|
|
1454
|
+
relation === this.$constants('RELATIONSHIP').belongsToMany
|
|
1455
|
+
].every(r => r);
|
|
1456
|
+
if (checkRelationIsBelongsToMany) {
|
|
1457
|
+
localKey = this._valuePattern([
|
|
1458
|
+
`${pluralize_1.default.singular(table !== null && table !== void 0 ? table : '')}`,
|
|
1459
|
+
`${this.$state.get('PRIMARY_KEY')}`
|
|
1460
|
+
].join('_'));
|
|
1461
|
+
foreignKey = 'id';
|
|
1462
|
+
}
|
|
1223
1463
|
return { name, as, relation, table, localKey, foreignKey, model };
|
|
1224
1464
|
}
|
|
1465
|
+
_handleSoftDelete() {
|
|
1466
|
+
if (this.$state.get('SOFT_DELETE')) {
|
|
1467
|
+
const deletedAt = this._valuePattern(this.$state.get('SOFT_DELETE_FORMAT'));
|
|
1468
|
+
this.whereNull(deletedAt);
|
|
1469
|
+
}
|
|
1470
|
+
return this;
|
|
1471
|
+
}
|
|
1225
1472
|
_buildQueryModel() {
|
|
1226
1473
|
let sql = [];
|
|
1227
1474
|
while (true) {
|
|
1228
|
-
|
|
1229
|
-
const deletedAt = this._valuePattern(this.$state.get('SOFT_DELETE_FORMAT'));
|
|
1230
|
-
this.whereNull(deletedAt);
|
|
1231
|
-
}
|
|
1475
|
+
this._handleSoftDelete();
|
|
1232
1476
|
if (this.$state.get('INSERT')) {
|
|
1233
1477
|
sql = [
|
|
1234
1478
|
this.$state.get('INSERT')
|
|
@@ -1268,64 +1512,67 @@ class Model extends AbstractModel_1.AbstractModel {
|
|
|
1268
1512
|
}
|
|
1269
1513
|
_showOnly(data) {
|
|
1270
1514
|
let result = [];
|
|
1271
|
-
const hasNameRelation = this.$state.get('WITH').map((w) => w.as
|
|
1515
|
+
const hasNameRelation = this.$state.get('WITH').map((w) => { var _a; return (_a = w.as) !== null && _a !== void 0 ? _a : w.name; });
|
|
1272
1516
|
data.forEach((d) => {
|
|
1273
1517
|
let newData = {};
|
|
1274
1518
|
this.$state.get('ONLY').forEach((only) => {
|
|
1275
1519
|
if (d.hasOwnProperty(only))
|
|
1276
|
-
newData = {
|
|
1520
|
+
newData = Object.assign(Object.assign({}, newData), { [only]: d[only] });
|
|
1277
1521
|
});
|
|
1278
1522
|
hasNameRelation.forEach((name) => {
|
|
1279
1523
|
if (name)
|
|
1280
|
-
newData = {
|
|
1524
|
+
newData = Object.assign(Object.assign({}, newData), { [name]: d[name] });
|
|
1281
1525
|
});
|
|
1282
1526
|
result = [...result, newData];
|
|
1283
1527
|
});
|
|
1284
1528
|
return result;
|
|
1285
1529
|
}
|
|
1286
|
-
|
|
1287
|
-
|
|
1288
|
-
|
|
1289
|
-
|
|
1290
|
-
|
|
1291
|
-
|
|
1292
|
-
|
|
1293
|
-
|
|
1294
|
-
for (const relation of relations) {
|
|
1295
|
-
const relationIsBelongsToMany = relation.relation === this.$constants('RELATIONSHIP').belongsToMany;
|
|
1296
|
-
if (relationIsBelongsToMany) {
|
|
1297
|
-
result = await this._belongsToMany(result, relation);
|
|
1298
|
-
continue;
|
|
1299
|
-
}
|
|
1300
|
-
const dataFromRelation = await this._relation(result, relation);
|
|
1301
|
-
result = this._relationMapData(result, dataFromRelation, relation);
|
|
1302
|
-
}
|
|
1303
|
-
if (this.$state.get('HIDDEN').length)
|
|
1304
|
-
this._hiddenColumnModel(result);
|
|
1305
|
-
return this._returnResult(type, result) || emptyData;
|
|
1306
|
-
}
|
|
1307
|
-
async _executeGroup(dataParents, type = 'GET') {
|
|
1308
|
-
const emptyData = this._returnEmpty(type, dataParents);
|
|
1309
|
-
if (!dataParents.length)
|
|
1310
|
-
return emptyData;
|
|
1311
|
-
const relations = this.$state.get('WITH');
|
|
1312
|
-
if (relations.length) {
|
|
1530
|
+
_execute({ sql, type, message, options }) {
|
|
1531
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
1532
|
+
let result = yield this.queryStatement(sql);
|
|
1533
|
+
if (!result.length)
|
|
1534
|
+
return this._returnEmpty(type, result, message, options);
|
|
1535
|
+
const relations = this.$state.get('WITH');
|
|
1536
|
+
if (!relations.length)
|
|
1537
|
+
return (yield this._returnResult(type, result)) || this._returnEmpty(type, result, message, options);
|
|
1313
1538
|
for (const relation of relations) {
|
|
1314
|
-
|
|
1315
|
-
|
|
1539
|
+
const relationIsBelongsToMany = relation.relation === this.$constants('RELATIONSHIP').belongsToMany;
|
|
1540
|
+
if (relationIsBelongsToMany) {
|
|
1541
|
+
result = yield this._belongsToMany(result, relation);
|
|
1542
|
+
continue;
|
|
1316
1543
|
}
|
|
1317
|
-
|
|
1318
|
-
|
|
1544
|
+
const dataFromRelation = yield this._relation(result, relation);
|
|
1545
|
+
result = this._relationMapData(result, dataFromRelation, relation);
|
|
1319
1546
|
}
|
|
1320
|
-
|
|
1321
|
-
|
|
1322
|
-
this.
|
|
1323
|
-
|
|
1324
|
-
|
|
1547
|
+
if (this.$state.get('HIDDEN').length)
|
|
1548
|
+
this._hiddenColumnModel(result);
|
|
1549
|
+
return (yield this._returnResult(type, result)) || this._returnEmpty(type, result, message, options);
|
|
1550
|
+
});
|
|
1551
|
+
}
|
|
1552
|
+
_executeGroup(dataParents, type = 'GET') {
|
|
1553
|
+
var _a;
|
|
1554
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
1555
|
+
if (!dataParents.length)
|
|
1556
|
+
return this._returnEmpty(type, dataParents);
|
|
1557
|
+
const relations = this.$state.get('WITH');
|
|
1558
|
+
if (relations.length) {
|
|
1559
|
+
for (const relation of relations) {
|
|
1560
|
+
if (relation.relation === this.$constants('RELATIONSHIP').belongsToMany) {
|
|
1561
|
+
return this._belongsToMany(dataParents, relation);
|
|
1562
|
+
}
|
|
1563
|
+
let dataChilds = yield this._relation(dataParents, relation);
|
|
1564
|
+
dataParents = this._relationMapData(dataParents, dataChilds, relation);
|
|
1565
|
+
}
|
|
1566
|
+
}
|
|
1567
|
+
if ((_a = this.$state.get('HIDDEN')) === null || _a === void 0 ? void 0 : _a.length)
|
|
1568
|
+
this._hiddenColumnModel(dataParents);
|
|
1569
|
+
const resultData = yield this._returnResult(type, dataParents);
|
|
1570
|
+
return resultData || this._returnEmpty(type, dataParents);
|
|
1571
|
+
});
|
|
1325
1572
|
}
|
|
1326
1573
|
_relationMapData(dataParents, dataChilds, relations) {
|
|
1327
1574
|
const { name, as, relation, localKey, foreignKey } = this._valueInRelation(relations);
|
|
1328
|
-
const keyRelation = as
|
|
1575
|
+
const keyRelation = as !== null && as !== void 0 ? as : name;
|
|
1329
1576
|
for (const dataParent of dataParents) {
|
|
1330
1577
|
const relationIsHasOneOrBelongsTo = [
|
|
1331
1578
|
this.$constants('RELATIONSHIP').hasOne,
|
|
@@ -1355,7 +1602,8 @@ class Model extends AbstractModel_1.AbstractModel {
|
|
|
1355
1602
|
return dataParents;
|
|
1356
1603
|
}
|
|
1357
1604
|
_handleRelationsExists(relation) {
|
|
1358
|
-
|
|
1605
|
+
var _a;
|
|
1606
|
+
this._assertError(!((_a = Object.keys(relation)) === null || _a === void 0 ? void 0 : _a.length), `unknown [relation]`);
|
|
1359
1607
|
const { localKey, foreignKey } = this._valueInRelation(relation);
|
|
1360
1608
|
const query = relation.query;
|
|
1361
1609
|
this._assertError(query == null, `unknown callback query in [relation : '${relation.name}']`);
|
|
@@ -1369,16 +1617,17 @@ class Model extends AbstractModel_1.AbstractModel {
|
|
|
1369
1617
|
}
|
|
1370
1618
|
}
|
|
1371
1619
|
const sql = clone
|
|
1372
|
-
.bind(this.$pool.
|
|
1620
|
+
.bind(this.$pool.get())
|
|
1373
1621
|
.whereReference(`\`${this._tableName()}\`.\`${localKey}\``, `\`${query._tableName()}\`.\`${foreignKey}\``)
|
|
1374
1622
|
.toString();
|
|
1375
1623
|
return sql;
|
|
1376
1624
|
}
|
|
1377
1625
|
_queryRelationsExists() {
|
|
1626
|
+
var _a;
|
|
1378
1627
|
const relations = this.$state.get('WITH');
|
|
1379
1628
|
for (const index in relations) {
|
|
1380
1629
|
const relation = relations[index];
|
|
1381
|
-
if (!Object.keys(relation)
|
|
1630
|
+
if (!((_a = Object.keys(relation)) === null || _a === void 0 ? void 0 : _a.length))
|
|
1382
1631
|
continue;
|
|
1383
1632
|
const { localKey, foreignKey } = this._valueInRelation(relation);
|
|
1384
1633
|
const query = relation.query;
|
|
@@ -1393,7 +1642,7 @@ class Model extends AbstractModel_1.AbstractModel {
|
|
|
1393
1642
|
}
|
|
1394
1643
|
}
|
|
1395
1644
|
const sql = clone
|
|
1396
|
-
.bind(this.$pool.
|
|
1645
|
+
.bind(this.$pool.get())
|
|
1397
1646
|
.whereReference(`\`${this._tableName()}\`.\`${localKey}\``, `\`${query._tableName()}\`.\`${foreignKey}\``)
|
|
1398
1647
|
.toString();
|
|
1399
1648
|
this.whereExists(sql);
|
|
@@ -1401,135 +1650,199 @@ class Model extends AbstractModel_1.AbstractModel {
|
|
|
1401
1650
|
const sql = this._buildQueryModel();
|
|
1402
1651
|
return sql;
|
|
1403
1652
|
}
|
|
1404
|
-
|
|
1405
|
-
|
|
1406
|
-
|
|
1407
|
-
|
|
1408
|
-
|
|
1409
|
-
const
|
|
1410
|
-
|
|
1411
|
-
|
|
1412
|
-
|
|
1413
|
-
|
|
1414
|
-
}).filter((data) => data != null);
|
|
1415
|
-
const dataPerentId = Array.from(new Set(localKeyId)) || [];
|
|
1416
|
-
if (!dataPerentId.length && this.$state.get('WITH_EXISTS'))
|
|
1417
|
-
return [];
|
|
1418
|
-
const query = await relation.query;
|
|
1419
|
-
this._assertError(query == null, `unknown callback query in [relation : '${relation.name}']`);
|
|
1420
|
-
const dataFromRelation = await query
|
|
1421
|
-
.bind(this.$pool.load())
|
|
1422
|
-
.whereIn(foreignKey, dataPerentId)
|
|
1423
|
-
.debug(this.$state.get('DEBUG'))
|
|
1424
|
-
.get();
|
|
1425
|
-
return dataFromRelation;
|
|
1426
|
-
}
|
|
1427
|
-
async _handleBelongsToMany(dataFromParent, relation, pivotTable) {
|
|
1428
|
-
let { name, localKey, foreignKey } = this._valueInRelation(relation);
|
|
1429
|
-
const localKeyId = dataFromParent.map((parent) => {
|
|
1430
|
-
const data = parent[localKey];
|
|
1431
|
-
if (!parent.hasOwnProperty(localKey)) {
|
|
1432
|
-
this._assertError(data == null, "unknown relationship without primary or foreign key");
|
|
1433
|
-
}
|
|
1434
|
-
return data;
|
|
1435
|
-
}).filter((data) => data != null);
|
|
1436
|
-
const dataPerentId = Array.from(new Set(localKeyId)).join(',') || [];
|
|
1437
|
-
if (!dataPerentId.length && this.$state.get('WITH_EXISTS'))
|
|
1438
|
-
return [];
|
|
1439
|
-
const modelOther = new relation.model();
|
|
1440
|
-
const other = this._classToTableName(modelOther.constructor.name, { singular: true });
|
|
1441
|
-
const otherlocalKey = 'id';
|
|
1442
|
-
const otherforeignKey = this._valuePattern(`${other}Id`);
|
|
1443
|
-
const sqldataChilds = [
|
|
1444
|
-
`${this.$constants('SELECT')}`,
|
|
1445
|
-
`*`,
|
|
1446
|
-
`${this.$constants('FROM')}`,
|
|
1447
|
-
`${pivotTable}`,
|
|
1448
|
-
`${this.$constants('WHERE')}`,
|
|
1449
|
-
`${foreignKey} ${this.$constants('IN')} (${dataPerentId})`
|
|
1450
|
-
].join(' ');
|
|
1451
|
-
let dataChilds = await this.queryStatement(sqldataChilds);
|
|
1452
|
-
const otherId = dataChilds.map((sub) => sub[otherforeignKey]).filter((data) => data != null);
|
|
1453
|
-
const otherArrId = Array.from(new Set(otherId)) || [];
|
|
1454
|
-
const otherdataChilds = await this.queryStatement(modelOther
|
|
1455
|
-
.bind(this.$pool.load())
|
|
1456
|
-
.whereIn(otherlocalKey, otherArrId)
|
|
1457
|
-
.debug(this.$state.get('DEBUG'))
|
|
1458
|
-
.toString());
|
|
1459
|
-
dataChilds.forEach((sub) => {
|
|
1460
|
-
sub[other] = [];
|
|
1461
|
-
otherdataChilds.forEach((otherSub) => {
|
|
1462
|
-
if (otherSub[otherlocalKey] === sub[otherforeignKey]) {
|
|
1463
|
-
sub[other] = otherSub;
|
|
1653
|
+
_relation(parents, relation) {
|
|
1654
|
+
var _a;
|
|
1655
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
1656
|
+
if (!((_a = Object.keys(relation)) === null || _a === void 0 ? void 0 : _a.length))
|
|
1657
|
+
return [];
|
|
1658
|
+
const { localKey, foreignKey } = this._valueInRelation(relation);
|
|
1659
|
+
const localKeyId = parents.map((parent) => {
|
|
1660
|
+
const data = parent[localKey];
|
|
1661
|
+
if (!parent.hasOwnProperty(localKey)) {
|
|
1662
|
+
this._assertError(data == null, `unknown relationship without primary or foreign key in relation : [${relation === null || relation === void 0 ? void 0 : relation.name}]`);
|
|
1464
1663
|
}
|
|
1465
|
-
|
|
1664
|
+
return data;
|
|
1665
|
+
}).filter((data) => data != null);
|
|
1666
|
+
const dataPerentId = Array.from(new Set(localKeyId)) || [];
|
|
1667
|
+
if (!dataPerentId.length && this.$state.get('WITH_EXISTS'))
|
|
1668
|
+
return [];
|
|
1669
|
+
const query = yield relation.query;
|
|
1670
|
+
this._assertError(query == null, `unknown callback query in [relation : ${relation.name}]`);
|
|
1671
|
+
const dataFromRelation = yield query
|
|
1672
|
+
.bind(this.$pool.get())
|
|
1673
|
+
.whereIn(foreignKey, dataPerentId)
|
|
1674
|
+
.debug(this.$state.get('DEBUG'))
|
|
1675
|
+
.get();
|
|
1676
|
+
return dataFromRelation;
|
|
1466
1677
|
});
|
|
1467
|
-
|
|
1468
|
-
|
|
1469
|
-
|
|
1470
|
-
|
|
1471
|
-
|
|
1472
|
-
|
|
1678
|
+
}
|
|
1679
|
+
_handleBelongsToMany(dataFromParent, relation, pivotTable) {
|
|
1680
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
1681
|
+
if (this.$state.get('WITH_EXISTS')) {
|
|
1682
|
+
let { name, localKey, foreignKey, model, table } = this._valueInRelation(relation);
|
|
1683
|
+
const localKeyId = dataFromParent.map((parent) => {
|
|
1684
|
+
const data = parent[localKey];
|
|
1685
|
+
if (!parent.hasOwnProperty(localKey)) {
|
|
1686
|
+
this._assertError(data == null, "unknown relationship without primary or foreign key");
|
|
1687
|
+
}
|
|
1688
|
+
return data;
|
|
1689
|
+
}).filter((data) => data != null);
|
|
1690
|
+
const dataPerentId = Array.from(new Set(localKeyId)).join(',') || [];
|
|
1691
|
+
if (!dataPerentId.length && this.$state.get('WITH_EXISTS'))
|
|
1692
|
+
return [];
|
|
1693
|
+
const modelOther = new relation.model();
|
|
1694
|
+
const other = this._classToTableName(modelOther.constructor.name, { singular: true });
|
|
1695
|
+
const otherlocalKey = 'id';
|
|
1696
|
+
const otherforeignKey = this._valuePattern(`${other}Id`);
|
|
1697
|
+
const sqldataChilds = [
|
|
1698
|
+
`${this.$constants('SELECT')}`,
|
|
1699
|
+
`*`,
|
|
1700
|
+
`${this.$constants('FROM')}`,
|
|
1701
|
+
`${pivotTable}`,
|
|
1702
|
+
`${this.$constants('WHERE')}`,
|
|
1703
|
+
`${otherforeignKey} ${this.$constants('IN')} (${dataPerentId})`
|
|
1704
|
+
].join(' ');
|
|
1705
|
+
let dataChilds = yield this.queryStatement(sqldataChilds);
|
|
1706
|
+
const otherId = dataChilds.map((sub) => sub[otherforeignKey]).filter((data) => data != null);
|
|
1707
|
+
const otherArrId = Array.from(new Set(otherId)) || [];
|
|
1708
|
+
const otherdataChilds = yield this.queryStatement(modelOther
|
|
1709
|
+
.bind(this.$pool.get())
|
|
1710
|
+
.whereIn(otherlocalKey, otherArrId)
|
|
1711
|
+
.debug(this.$state.get('DEBUG'))
|
|
1712
|
+
.toString());
|
|
1713
|
+
dataChilds.forEach((sub) => {
|
|
1714
|
+
sub[other] = [];
|
|
1715
|
+
otherdataChilds.forEach((otherSub) => {
|
|
1716
|
+
if (otherSub[otherlocalKey] === sub[otherforeignKey]) {
|
|
1717
|
+
sub[other] = otherSub;
|
|
1718
|
+
}
|
|
1719
|
+
});
|
|
1720
|
+
});
|
|
1721
|
+
dataFromParent.forEach((dataPerent) => {
|
|
1722
|
+
if (dataPerent[name] == null)
|
|
1723
|
+
dataPerent[name] = [];
|
|
1724
|
+
dataChilds.forEach((sub) => {
|
|
1725
|
+
if (sub[localKey] === dataPerent[otherforeignKey]) {
|
|
1726
|
+
dataPerent[name].push(sub);
|
|
1727
|
+
}
|
|
1728
|
+
});
|
|
1729
|
+
});
|
|
1730
|
+
if (this.$state.get('HIDDEN').length)
|
|
1731
|
+
this._hiddenColumnModel(dataFromParent);
|
|
1732
|
+
return dataFromParent;
|
|
1733
|
+
}
|
|
1734
|
+
let { name, localKey, foreignKey } = this._valueInRelation(relation);
|
|
1735
|
+
const localKeyId = dataFromParent.map((parent) => {
|
|
1736
|
+
const data = parent[localKey];
|
|
1737
|
+
if (!parent.hasOwnProperty(localKey)) {
|
|
1738
|
+
this._assertError(data == null, "unknown relationship without primary or foreign key");
|
|
1473
1739
|
}
|
|
1740
|
+
return data;
|
|
1741
|
+
}).filter((data) => data != null);
|
|
1742
|
+
const dataPerentId = Array.from(new Set(localKeyId)).join(',') || [];
|
|
1743
|
+
if (!dataPerentId.length && this.$state.get('WITH_EXISTS'))
|
|
1744
|
+
return [];
|
|
1745
|
+
const modelOther = new relation.model();
|
|
1746
|
+
const other = this._classToTableName(modelOther.constructor.name, { singular: true });
|
|
1747
|
+
const otherlocalKey = 'id';
|
|
1748
|
+
const otherforeignKey = this._valuePattern(`${other}Id`);
|
|
1749
|
+
const sqldataChilds = [
|
|
1750
|
+
`${this.$constants('SELECT')}`,
|
|
1751
|
+
`*`,
|
|
1752
|
+
`${this.$constants('FROM')}`,
|
|
1753
|
+
`${pivotTable}`,
|
|
1754
|
+
`${this.$constants('WHERE')}`,
|
|
1755
|
+
`${foreignKey} ${this.$constants('IN')} (${dataPerentId})`
|
|
1756
|
+
].join(' ');
|
|
1757
|
+
let dataChilds = yield this.queryStatement(sqldataChilds);
|
|
1758
|
+
const otherId = dataChilds.map((sub) => sub[otherforeignKey]).filter((data) => data != null);
|
|
1759
|
+
const otherArrId = Array.from(new Set(otherId)) || [];
|
|
1760
|
+
const otherdataChilds = yield this.queryStatement(modelOther
|
|
1761
|
+
.bind(this.$pool.get())
|
|
1762
|
+
.whereIn(otherlocalKey, otherArrId)
|
|
1763
|
+
.debug(this.$state.get('DEBUG'))
|
|
1764
|
+
.toString());
|
|
1765
|
+
dataChilds.forEach((sub) => {
|
|
1766
|
+
sub[other] = [];
|
|
1767
|
+
otherdataChilds.forEach((otherSub) => {
|
|
1768
|
+
if (otherSub[otherlocalKey] === sub[otherforeignKey]) {
|
|
1769
|
+
sub[other] = otherSub;
|
|
1770
|
+
}
|
|
1771
|
+
});
|
|
1772
|
+
});
|
|
1773
|
+
dataFromParent.forEach((dataPerent) => {
|
|
1774
|
+
if (dataPerent[name] == null)
|
|
1775
|
+
dataPerent[name] = [];
|
|
1776
|
+
dataChilds.forEach((sub) => {
|
|
1777
|
+
if (sub[foreignKey] === dataPerent[localKey]) {
|
|
1778
|
+
dataPerent[name].push(sub);
|
|
1779
|
+
}
|
|
1780
|
+
});
|
|
1474
1781
|
});
|
|
1782
|
+
if (this.$state.get('HIDDEN').length)
|
|
1783
|
+
this._hiddenColumnModel(dataFromParent);
|
|
1784
|
+
return dataFromParent;
|
|
1475
1785
|
});
|
|
1476
|
-
|
|
1477
|
-
|
|
1478
|
-
|
|
1479
|
-
|
|
1480
|
-
|
|
1481
|
-
|
|
1482
|
-
|
|
1483
|
-
const other = this._classToTableName(modelOther.constructor.name, { singular: true });
|
|
1484
|
-
try {
|
|
1485
|
-
const pivotTable = relation.freezeTable ?? `${local}_${other}`;
|
|
1486
|
-
return await this._handleBelongsToMany(dataFromParent, relation, pivotTable);
|
|
1487
|
-
}
|
|
1488
|
-
catch (err) {
|
|
1786
|
+
}
|
|
1787
|
+
_belongsToMany(dataFromParent, relation) {
|
|
1788
|
+
var _a, _b;
|
|
1789
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
1790
|
+
const local = this._classToTableName(this.constructor.name, { singular: true });
|
|
1791
|
+
const modelOther = new relation.model();
|
|
1792
|
+
const other = this._classToTableName(modelOther.constructor.name, { singular: true });
|
|
1489
1793
|
try {
|
|
1490
|
-
const pivotTable = relation.freezeTable
|
|
1491
|
-
return
|
|
1794
|
+
const pivotTable = (_a = relation.freezeTable) !== null && _a !== void 0 ? _a : `${local}_${other}`;
|
|
1795
|
+
return yield this._handleBelongsToMany(dataFromParent, relation, pivotTable);
|
|
1492
1796
|
}
|
|
1493
|
-
catch (
|
|
1494
|
-
|
|
1797
|
+
catch (err) {
|
|
1798
|
+
try {
|
|
1799
|
+
const pivotTable = (_b = relation.freezeTable) !== null && _b !== void 0 ? _b : `${other}_${local}`;
|
|
1800
|
+
return yield this._handleBelongsToMany(dataFromParent, relation, pivotTable);
|
|
1801
|
+
}
|
|
1802
|
+
catch (e) {
|
|
1803
|
+
throw new Error(err.message);
|
|
1804
|
+
}
|
|
1495
1805
|
}
|
|
1496
|
-
}
|
|
1806
|
+
});
|
|
1497
1807
|
}
|
|
1498
|
-
|
|
1499
|
-
|
|
1500
|
-
this
|
|
1501
|
-
|
|
1502
|
-
|
|
1503
|
-
|
|
1504
|
-
|
|
1505
|
-
|
|
1506
|
-
|
|
1507
|
-
|
|
1508
|
-
|
|
1509
|
-
|
|
1510
|
-
|
|
1511
|
-
|
|
1512
|
-
|
|
1513
|
-
|
|
1514
|
-
|
|
1515
|
-
|
|
1516
|
-
|
|
1517
|
-
|
|
1518
|
-
|
|
1519
|
-
|
|
1520
|
-
|
|
1521
|
-
|
|
1522
|
-
|
|
1523
|
-
|
|
1524
|
-
|
|
1525
|
-
|
|
1808
|
+
_pagination(data) {
|
|
1809
|
+
var _a, _b;
|
|
1810
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
1811
|
+
const currentPage = +(this.$state.get('PAGE'));
|
|
1812
|
+
this.select([
|
|
1813
|
+
`${this.$constants('COUNT')}(${this.$state.get('PRIMARY_KEY')})`,
|
|
1814
|
+
`${this.$constants('AS')}`,
|
|
1815
|
+
`total`
|
|
1816
|
+
].join(' '));
|
|
1817
|
+
const limit = Number(this.$state.get('PER_PAGE'));
|
|
1818
|
+
this._assertError(limit < 1, "minimun less 1 of limit");
|
|
1819
|
+
const sql = this._buildQueryModel();
|
|
1820
|
+
const res = yield this.queryStatement(sql);
|
|
1821
|
+
const total = (_a = res.shift().total) !== null && _a !== void 0 ? _a : 0;
|
|
1822
|
+
let lastPage = Math.ceil(total / limit) || 0;
|
|
1823
|
+
lastPage = lastPage > 1 ? lastPage : 1;
|
|
1824
|
+
const nextPage = currentPage + 1;
|
|
1825
|
+
const prevPage = currentPage - 1 === 0 ? 1 : currentPage - 1;
|
|
1826
|
+
const totalPage = (_b = data === null || data === void 0 ? void 0 : data.length) !== null && _b !== void 0 ? _b : 0;
|
|
1827
|
+
const meta = {
|
|
1828
|
+
total,
|
|
1829
|
+
limit,
|
|
1830
|
+
totalPage,
|
|
1831
|
+
currentPage,
|
|
1832
|
+
lastPage,
|
|
1833
|
+
nextPage,
|
|
1834
|
+
prevPage,
|
|
1835
|
+
};
|
|
1836
|
+
if (this._isPatternSnakeCase()) {
|
|
1837
|
+
return this.$utils.snakeCase(this._result({
|
|
1838
|
+
meta,
|
|
1839
|
+
data
|
|
1840
|
+
}));
|
|
1841
|
+
}
|
|
1842
|
+
return this._result({
|
|
1526
1843
|
meta,
|
|
1527
1844
|
data
|
|
1528
|
-
})
|
|
1529
|
-
}
|
|
1530
|
-
return this._result({
|
|
1531
|
-
meta,
|
|
1532
|
-
data
|
|
1845
|
+
});
|
|
1533
1846
|
});
|
|
1534
1847
|
}
|
|
1535
1848
|
_result(data) {
|
|
@@ -1537,196 +1850,197 @@ class Model extends AbstractModel_1.AbstractModel {
|
|
|
1537
1850
|
return data;
|
|
1538
1851
|
}
|
|
1539
1852
|
_returnEmpty(type, result, message, options) {
|
|
1540
|
-
|
|
1541
|
-
|
|
1542
|
-
|
|
1543
|
-
|
|
1544
|
-
|
|
1545
|
-
|
|
1546
|
-
|
|
1547
|
-
|
|
1548
|
-
if (
|
|
1549
|
-
|
|
1550
|
-
|
|
1551
|
-
|
|
1552
|
-
|
|
1853
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
1854
|
+
let emptyData = null;
|
|
1855
|
+
switch (type) {
|
|
1856
|
+
case 'FIRST': {
|
|
1857
|
+
emptyData = null;
|
|
1858
|
+
break;
|
|
1859
|
+
}
|
|
1860
|
+
case 'FIRST_OR_ERROR': {
|
|
1861
|
+
if (!(result === null || result === void 0 ? void 0 : result.length)) {
|
|
1862
|
+
if (options == null) {
|
|
1863
|
+
throw {
|
|
1864
|
+
message,
|
|
1865
|
+
code: 400
|
|
1866
|
+
};
|
|
1867
|
+
}
|
|
1868
|
+
throw Object.assign({ message }, options);
|
|
1553
1869
|
}
|
|
1554
|
-
|
|
1555
|
-
|
|
1556
|
-
|
|
1870
|
+
emptyData = null;
|
|
1871
|
+
break;
|
|
1872
|
+
}
|
|
1873
|
+
case 'GET': {
|
|
1874
|
+
emptyData = [];
|
|
1875
|
+
break;
|
|
1876
|
+
}
|
|
1877
|
+
case 'PAGINATION': {
|
|
1878
|
+
emptyData = {
|
|
1879
|
+
meta: {
|
|
1880
|
+
total: 0,
|
|
1881
|
+
limit: Number(this.$state.get('PER_PAGE')),
|
|
1882
|
+
totalPage: 0,
|
|
1883
|
+
currentPage: Number(this.$state.get('PAGE')),
|
|
1884
|
+
lastPage: 0,
|
|
1885
|
+
nextPage: 0,
|
|
1886
|
+
prevPage: 0
|
|
1887
|
+
},
|
|
1888
|
+
data: []
|
|
1557
1889
|
};
|
|
1890
|
+
break;
|
|
1891
|
+
}
|
|
1892
|
+
default: {
|
|
1893
|
+
throw new Error('Missing method first get or pagination');
|
|
1558
1894
|
}
|
|
1559
|
-
emptyData = null;
|
|
1560
|
-
break;
|
|
1561
|
-
}
|
|
1562
|
-
case 'GET': {
|
|
1563
|
-
emptyData = [];
|
|
1564
|
-
break;
|
|
1565
|
-
}
|
|
1566
|
-
case 'PAGINATION': {
|
|
1567
|
-
emptyData = {
|
|
1568
|
-
meta: {
|
|
1569
|
-
total: 0,
|
|
1570
|
-
limit: Number(this.$state.get('PER_PAGE')),
|
|
1571
|
-
totalPage: 0,
|
|
1572
|
-
currentPage: Number(this.$state.get('PAGE')),
|
|
1573
|
-
lastPage: 0,
|
|
1574
|
-
nextPage: 0,
|
|
1575
|
-
prevPage: 0
|
|
1576
|
-
},
|
|
1577
|
-
data: []
|
|
1578
|
-
};
|
|
1579
|
-
break;
|
|
1580
1895
|
}
|
|
1581
|
-
|
|
1582
|
-
|
|
1896
|
+
if (this._isPatternSnakeCase()) {
|
|
1897
|
+
const empty = this.$utils.snakeCase(this._result(emptyData));
|
|
1898
|
+
const hook = this.$state.get('HOOK');
|
|
1899
|
+
if (hook === null || hook === void 0 ? void 0 : hook.length)
|
|
1900
|
+
for (let i = 0; i < hook.length; i++)
|
|
1901
|
+
yield hook[i](empty);
|
|
1902
|
+
return empty;
|
|
1583
1903
|
}
|
|
1584
|
-
|
|
1585
|
-
|
|
1586
|
-
|
|
1587
|
-
|
|
1588
|
-
|
|
1904
|
+
const empty = this._result(emptyData);
|
|
1905
|
+
const hook = this.$state.get('HOOK');
|
|
1906
|
+
if (hook === null || hook === void 0 ? void 0 : hook.length)
|
|
1907
|
+
for (let i = 0; i < hook.length; i++)
|
|
1908
|
+
yield hook[i](empty);
|
|
1909
|
+
return empty;
|
|
1910
|
+
});
|
|
1589
1911
|
}
|
|
1590
1912
|
_returnResult(type, data) {
|
|
1591
|
-
|
|
1592
|
-
|
|
1593
|
-
|
|
1594
|
-
|
|
1595
|
-
|
|
1596
|
-
|
|
1597
|
-
|
|
1598
|
-
|
|
1599
|
-
if (this.$state.get('ONLY')?.length) {
|
|
1600
|
-
data = this._showOnly(data);
|
|
1601
|
-
}
|
|
1602
|
-
switch (type) {
|
|
1603
|
-
case 'FIRST': {
|
|
1604
|
-
if (this.$state.get('PLUCK')) {
|
|
1605
|
-
const pluck = this.$state.get('PLUCK');
|
|
1606
|
-
const newData = data.shift();
|
|
1607
|
-
const checkProperty = newData.hasOwnProperty(pluck);
|
|
1608
|
-
this._assertError(!checkProperty, `can't find property '${pluck}' of result`);
|
|
1609
|
-
return this._result(newData[pluck]);
|
|
1913
|
+
var _a, _b, _c, _d, _e;
|
|
1914
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
1915
|
+
if ((_a = Object.keys(this.$state.get('REGISTRY'))) === null || _a === void 0 ? void 0 : _a.length) {
|
|
1916
|
+
for (const d of data) {
|
|
1917
|
+
for (const name in this.$state.get('REGISTRY')) {
|
|
1918
|
+
const registry = this.$state.get('REGISTRY');
|
|
1919
|
+
d[name] = registry[name];
|
|
1920
|
+
}
|
|
1610
1921
|
}
|
|
1611
|
-
return this._result(data.shift() ?? null);
|
|
1612
1922
|
}
|
|
1613
|
-
|
|
1614
|
-
|
|
1615
|
-
const pluck = this.$state.get('PLUCK');
|
|
1616
|
-
const newData = data.shift();
|
|
1617
|
-
const checkProperty = newData.hasOwnProperty(pluck);
|
|
1618
|
-
this._assertError(!checkProperty, `can't find property '${pluck}' of result`);
|
|
1619
|
-
const result = this._result(newData[pluck]) ?? null;
|
|
1620
|
-
return result;
|
|
1621
|
-
}
|
|
1622
|
-
const result = this._result(data.shift() ?? null);
|
|
1623
|
-
if (result == null)
|
|
1624
|
-
throw new Error();
|
|
1625
|
-
return result;
|
|
1923
|
+
if ((_b = this.$state.get('ONLY')) === null || _b === void 0 ? void 0 : _b.length) {
|
|
1924
|
+
data = this._showOnly(data);
|
|
1626
1925
|
}
|
|
1627
|
-
|
|
1628
|
-
|
|
1629
|
-
|
|
1630
|
-
|
|
1631
|
-
|
|
1632
|
-
|
|
1633
|
-
|
|
1634
|
-
|
|
1635
|
-
|
|
1636
|
-
|
|
1926
|
+
let result = null;
|
|
1927
|
+
switch (type) {
|
|
1928
|
+
case 'FIRST': {
|
|
1929
|
+
if (this.$state.get('PLUCK')) {
|
|
1930
|
+
const pluck = this.$state.get('PLUCK');
|
|
1931
|
+
const newData = data.shift();
|
|
1932
|
+
const checkProperty = newData.hasOwnProperty(pluck);
|
|
1933
|
+
this._assertError(!checkProperty, `can't find property '${pluck}' of result`);
|
|
1934
|
+
result = this._result(newData[pluck]);
|
|
1935
|
+
break;
|
|
1936
|
+
}
|
|
1937
|
+
result = this._result((_c = data.shift()) !== null && _c !== void 0 ? _c : null);
|
|
1938
|
+
break;
|
|
1637
1939
|
}
|
|
1638
|
-
|
|
1639
|
-
|
|
1640
|
-
|
|
1641
|
-
|
|
1642
|
-
|
|
1940
|
+
case 'FIRST_OR_ERROR': {
|
|
1941
|
+
if (this.$state.get('PLUCK')) {
|
|
1942
|
+
const pluck = this.$state.get('PLUCK');
|
|
1943
|
+
const newData = data.shift();
|
|
1944
|
+
const checkProperty = newData.hasOwnProperty(pluck);
|
|
1945
|
+
this._assertError(!checkProperty, `can't find property '${pluck}' of result`);
|
|
1946
|
+
result = (_d = this._result(newData[pluck])) !== null && _d !== void 0 ? _d : null;
|
|
1947
|
+
break;
|
|
1948
|
+
}
|
|
1949
|
+
result = this._result((_e = data.shift()) !== null && _e !== void 0 ? _e : null);
|
|
1950
|
+
break;
|
|
1951
|
+
}
|
|
1952
|
+
case 'GET': {
|
|
1953
|
+
if (this.$state.get('CHUNK')) {
|
|
1954
|
+
const r = data.reduce((resultArray, item, index) => {
|
|
1955
|
+
const chunkIndex = Math.floor(index / this.$state.get('CHUNK'));
|
|
1956
|
+
if (!resultArray[chunkIndex])
|
|
1957
|
+
resultArray[chunkIndex] = [];
|
|
1958
|
+
resultArray[chunkIndex].push(item);
|
|
1959
|
+
return resultArray;
|
|
1960
|
+
}, []);
|
|
1961
|
+
result = this._result(r);
|
|
1962
|
+
break;
|
|
1963
|
+
}
|
|
1964
|
+
if (this.$state.get('PLUCK')) {
|
|
1965
|
+
const pluck = this.$state.get('PLUCK');
|
|
1966
|
+
const newData = data.map((d) => d[pluck]);
|
|
1967
|
+
this._assertError(newData.every((d) => d == null), `can't find property '${pluck}' of result`);
|
|
1968
|
+
result = this._result(newData);
|
|
1969
|
+
break;
|
|
1970
|
+
}
|
|
1971
|
+
result = this._result(data);
|
|
1972
|
+
break;
|
|
1973
|
+
}
|
|
1974
|
+
case 'PAGINATION': {
|
|
1975
|
+
result = yield this._pagination(data);
|
|
1976
|
+
break;
|
|
1977
|
+
}
|
|
1978
|
+
default: {
|
|
1979
|
+
throw new Error('Missing method first get or pagination');
|
|
1643
1980
|
}
|
|
1644
|
-
return this._result(data);
|
|
1645
|
-
}
|
|
1646
|
-
case 'PAGINATION': {
|
|
1647
|
-
return this._pagination(data);
|
|
1648
1981
|
}
|
|
1649
|
-
|
|
1650
|
-
|
|
1982
|
+
const hook = this.$state.get('HOOK');
|
|
1983
|
+
if (hook === null || hook === void 0 ? void 0 : hook.length)
|
|
1984
|
+
for (let i = 0; i < hook.length; i++)
|
|
1985
|
+
yield hook[i](result);
|
|
1986
|
+
return result;
|
|
1987
|
+
});
|
|
1988
|
+
}
|
|
1989
|
+
_hiddenColumnModel(data) {
|
|
1990
|
+
const hiddens = this.$state.get('HIDDEN');
|
|
1991
|
+
for (const hidden of hiddens) {
|
|
1992
|
+
for (const objColumn of data) {
|
|
1993
|
+
delete objColumn[hidden];
|
|
1651
1994
|
}
|
|
1652
1995
|
}
|
|
1996
|
+
return data;
|
|
1653
1997
|
}
|
|
1654
|
-
|
|
1655
|
-
|
|
1656
|
-
|
|
1657
|
-
|
|
1658
|
-
|
|
1659
|
-
|
|
1660
|
-
|
|
1661
|
-
});
|
|
1662
|
-
|
|
1663
|
-
return object;
|
|
1664
|
-
}
|
|
1665
|
-
async _attach(name, dataId, fields) {
|
|
1666
|
-
this._assertError(!Array.isArray(dataId), `this ${dataId} is not an array`);
|
|
1667
|
-
const relation = this.$state.get('RELATION')?.find((data) => data.name === name);
|
|
1668
|
-
this._assertError(!relation, `unknown name relation ['${name}'] in model`);
|
|
1669
|
-
const thisTable = this.$utils.columnRelation(this.constructor.name);
|
|
1670
|
-
const relationTable = this._classToTableName(relation.model.name, { singular: true });
|
|
1671
|
-
const result = this.$state.get('RESULT');
|
|
1672
|
-
try {
|
|
1673
|
-
const pivotTable = `${thisTable}_${relationTable}`;
|
|
1674
|
-
const success = await new DB_1.DB().table(pivotTable).createMultiple(dataId.map((id) => {
|
|
1675
|
-
return {
|
|
1676
|
-
[this._valuePattern(`${relationTable}Id`)]: id,
|
|
1677
|
-
[this._valuePattern(`${thisTable}Id`)]: result.id,
|
|
1678
|
-
...fields
|
|
1679
|
-
};
|
|
1680
|
-
})).save();
|
|
1681
|
-
return success;
|
|
1682
|
-
}
|
|
1683
|
-
catch (e) {
|
|
1684
|
-
const errorTable = e.message;
|
|
1685
|
-
const search = errorTable.search("ER_NO_SUCH_TABLE");
|
|
1686
|
-
if (!!search)
|
|
1687
|
-
throw new Error(e.message);
|
|
1998
|
+
_attach(name, dataId, fields) {
|
|
1999
|
+
var _a;
|
|
2000
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
2001
|
+
this._assertError(!Array.isArray(dataId), `this ${dataId} is not an array`);
|
|
2002
|
+
const relation = (_a = this.$state.get('RELATION')) === null || _a === void 0 ? void 0 : _a.find((data) => data.name === name);
|
|
2003
|
+
this._assertError(!relation, `unknown name relation ['${name}'] in model`);
|
|
2004
|
+
const thisTable = this.$utils.columnRelation(this.constructor.name);
|
|
2005
|
+
const relationTable = this._classToTableName(relation.model.name, { singular: true });
|
|
2006
|
+
const result = this.$state.get('RESULT');
|
|
1688
2007
|
try {
|
|
1689
|
-
const pivotTable = `${
|
|
1690
|
-
const success =
|
|
1691
|
-
return {
|
|
1692
|
-
[this._valuePattern(`${relationTable}Id`)]: id,
|
|
1693
|
-
[this._valuePattern(`${thisTable}Id`)]: result.id,
|
|
1694
|
-
...fields
|
|
1695
|
-
};
|
|
2008
|
+
const pivotTable = `${thisTable}_${relationTable}`;
|
|
2009
|
+
const success = yield new DB_1.DB().table(pivotTable).createMultiple(dataId.map((id) => {
|
|
2010
|
+
return Object.assign({ [this._valuePattern(`${relationTable}Id`)]: id, [this._valuePattern(`${thisTable}Id`)]: result.id }, fields);
|
|
1696
2011
|
})).save();
|
|
1697
2012
|
return success;
|
|
1698
2013
|
}
|
|
1699
2014
|
catch (e) {
|
|
1700
|
-
|
|
2015
|
+
const errorTable = e.message;
|
|
2016
|
+
const search = errorTable.search("ER_NO_SUCH_TABLE");
|
|
2017
|
+
if (!!search)
|
|
2018
|
+
throw new Error(e.message);
|
|
2019
|
+
try {
|
|
2020
|
+
const pivotTable = `${relationTable}_${thisTable}`;
|
|
2021
|
+
const success = yield new DB_1.DB().table(pivotTable).createMultiple(dataId.map((id) => {
|
|
2022
|
+
return Object.assign({ [this._valuePattern(`${relationTable}Id`)]: id, [this._valuePattern(`${thisTable}Id`)]: result.id }, fields);
|
|
2023
|
+
})).save();
|
|
2024
|
+
return success;
|
|
2025
|
+
}
|
|
2026
|
+
catch (e) {
|
|
2027
|
+
throw new Error(e.message);
|
|
2028
|
+
}
|
|
1701
2029
|
}
|
|
1702
|
-
}
|
|
2030
|
+
});
|
|
1703
2031
|
}
|
|
1704
|
-
|
|
1705
|
-
this
|
|
1706
|
-
|
|
1707
|
-
|
|
1708
|
-
|
|
1709
|
-
|
|
1710
|
-
|
|
1711
|
-
|
|
1712
|
-
const pivotTable = `${thisTable}_${relationTable}`;
|
|
1713
|
-
for (const id of dataId) {
|
|
1714
|
-
await new DB_1.DB().table(pivotTable)
|
|
1715
|
-
.where(this._valuePattern(`${relationTable}Id`), id)
|
|
1716
|
-
.where(this._valuePattern(`${thisTable}Id`), result.id)
|
|
1717
|
-
.delete();
|
|
1718
|
-
}
|
|
1719
|
-
return true;
|
|
1720
|
-
}
|
|
1721
|
-
catch (e) {
|
|
1722
|
-
const errorTable = e.message;
|
|
1723
|
-
const search = errorTable.search("ER_NO_SUCH_TABLE");
|
|
1724
|
-
if (!!search)
|
|
1725
|
-
throw new Error(e.message);
|
|
2032
|
+
_detach(name, dataId) {
|
|
2033
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
2034
|
+
this._assertError(!Array.isArray(dataId), `this ${dataId} is not an array`);
|
|
2035
|
+
const relation = this.$state.get('RELATION').find((data) => data.name === name);
|
|
2036
|
+
this._assertError(!relation, `unknown name relation [${name}] in model`);
|
|
2037
|
+
const thisTable = this.$utils.columnRelation(this.constructor.name);
|
|
2038
|
+
const relationTable = this._classToTableName(relation.model.name, { singular: true });
|
|
2039
|
+
const result = this.$state.get('RESULT');
|
|
1726
2040
|
try {
|
|
1727
|
-
const pivotTable = `${
|
|
2041
|
+
const pivotTable = `${thisTable}_${relationTable}`;
|
|
1728
2042
|
for (const id of dataId) {
|
|
1729
|
-
|
|
2043
|
+
yield new DB_1.DB().table(pivotTable)
|
|
1730
2044
|
.where(this._valuePattern(`${relationTable}Id`), id)
|
|
1731
2045
|
.where(this._valuePattern(`${thisTable}Id`), result.id)
|
|
1732
2046
|
.delete();
|
|
@@ -1734,18 +2048,34 @@ class Model extends AbstractModel_1.AbstractModel {
|
|
|
1734
2048
|
return true;
|
|
1735
2049
|
}
|
|
1736
2050
|
catch (e) {
|
|
1737
|
-
|
|
2051
|
+
const errorTable = e.message;
|
|
2052
|
+
const search = errorTable.search("ER_NO_SUCH_TABLE");
|
|
2053
|
+
if (!!search)
|
|
2054
|
+
throw new Error(e.message);
|
|
2055
|
+
try {
|
|
2056
|
+
const pivotTable = `${relationTable}_${thisTable}`;
|
|
2057
|
+
for (const id of dataId) {
|
|
2058
|
+
yield new DB_1.DB().table(pivotTable)
|
|
2059
|
+
.where(this._valuePattern(`${relationTable}Id`), id)
|
|
2060
|
+
.where(this._valuePattern(`${thisTable}Id`), result.id)
|
|
2061
|
+
.delete();
|
|
2062
|
+
}
|
|
2063
|
+
return true;
|
|
2064
|
+
}
|
|
2065
|
+
catch (e) {
|
|
2066
|
+
throw new Error(e.message);
|
|
2067
|
+
}
|
|
1738
2068
|
}
|
|
1739
|
-
}
|
|
2069
|
+
});
|
|
1740
2070
|
}
|
|
1741
2071
|
_queryUpdateModel(objects) {
|
|
1742
2072
|
if (this.$state.get('TIMESTAMP')) {
|
|
1743
2073
|
const updatedAt = this._valuePattern(this.$state.get('TIMESTAMP_FORMAT').UPDATED_AT);
|
|
1744
|
-
objects = {
|
|
1745
|
-
[updatedAt]: this.$utils.timestamp(),
|
|
1746
|
-
};
|
|
2074
|
+
objects = Object.assign(Object.assign({}, objects), { [updatedAt]: this.$utils.timestamp() });
|
|
1747
2075
|
}
|
|
1748
2076
|
const keyValue = Object.entries(objects).map(([column, value]) => {
|
|
2077
|
+
if (typeof value === 'string')
|
|
2078
|
+
value = value === null || value === void 0 ? void 0 : value.replace(/'/g, '');
|
|
1749
2079
|
return `${column} = ${value == null || value === 'NULL'
|
|
1750
2080
|
? 'NULL'
|
|
1751
2081
|
: typeof value === 'string' && value.includes(this.$constants('RAW'))
|
|
@@ -1760,21 +2090,17 @@ class Model extends AbstractModel_1.AbstractModel {
|
|
|
1760
2090
|
const format = this.$state.get('TIMESTAMP_FORMAT');
|
|
1761
2091
|
const createdAt = this._valuePattern(format.CREATED_AT);
|
|
1762
2092
|
const updatedAt = this._valuePattern(format.UPDATED_AT);
|
|
1763
|
-
objects = {
|
|
1764
|
-
[createdAt]: this.$utils.timestamp(),
|
|
1765
|
-
[updatedAt]: this.$utils.timestamp(),
|
|
1766
|
-
};
|
|
2093
|
+
objects = Object.assign(Object.assign({}, objects), { [createdAt]: this.$utils.timestamp(), [updatedAt]: this.$utils.timestamp() });
|
|
1767
2094
|
}
|
|
1768
2095
|
const hasUUID = objects.hasOwnProperty(this.$state.get('UUID_FORMAT'));
|
|
1769
2096
|
if (this.$state.get('UUID') && !hasUUID) {
|
|
1770
2097
|
const uuidFormat = this.$state.get('UUID_FORMAT');
|
|
1771
|
-
objects = {
|
|
1772
|
-
...objects,
|
|
1773
|
-
[uuidFormat]: this.$utils.generateUUID()
|
|
1774
|
-
};
|
|
2098
|
+
objects = Object.assign(Object.assign({}, objects), { [uuidFormat]: this.$utils.generateUUID() });
|
|
1775
2099
|
}
|
|
1776
2100
|
const columns = Object.keys(objects).map((column) => `\`${column}\``);
|
|
1777
2101
|
const values = Object.values(objects).map((value) => {
|
|
2102
|
+
if (typeof value === 'string')
|
|
2103
|
+
value = value === null || value === void 0 ? void 0 : value.replace(/'/g, '');
|
|
1778
2104
|
return `${value == null || value === 'NULL'
|
|
1779
2105
|
? 'NULL'
|
|
1780
2106
|
: typeof value === 'string' && value.includes(this.$constants('RAW'))
|
|
@@ -1788,18 +2114,16 @@ class Model extends AbstractModel_1.AbstractModel {
|
|
|
1788
2114
|
].join(' ');
|
|
1789
2115
|
}
|
|
1790
2116
|
_queryInsertMultipleModel(data) {
|
|
2117
|
+
var _a;
|
|
1791
2118
|
let values = [];
|
|
1792
|
-
let columns = Object.keys([...data]
|
|
2119
|
+
let columns = Object.keys((_a = [...data]) === null || _a === void 0 ? void 0 : _a.shift()).map((column) => `\`${column}\``);
|
|
1793
2120
|
for (let objects of data) {
|
|
1794
2121
|
const hasTimestamp = this.$state.get('TIMESTAMP');
|
|
1795
2122
|
if (hasTimestamp) {
|
|
1796
2123
|
const format = this.$state.get('TIMESTAMP_FORMAT');
|
|
1797
2124
|
const createdAt = this._valuePattern(format.CREATED_AT);
|
|
1798
2125
|
const updatedAt = this._valuePattern(format.UPDATED_AT);
|
|
1799
|
-
objects = {
|
|
1800
|
-
[createdAt]: this.$utils.timestamp(),
|
|
1801
|
-
[updatedAt]: this.$utils.timestamp(),
|
|
1802
|
-
};
|
|
2126
|
+
objects = Object.assign(Object.assign({}, objects), { [createdAt]: this.$utils.timestamp(), [updatedAt]: this.$utils.timestamp() });
|
|
1803
2127
|
columns = [
|
|
1804
2128
|
...columns,
|
|
1805
2129
|
`\`${createdAt}\``,
|
|
@@ -1809,16 +2133,15 @@ class Model extends AbstractModel_1.AbstractModel {
|
|
|
1809
2133
|
const hasUUID = objects.hasOwnProperty(this.$state.get('UUID_FORMAT'));
|
|
1810
2134
|
if (this.$state.get('UUID') && !hasUUID) {
|
|
1811
2135
|
const uuidFormat = this.$state.get('UUID_FORMAT');
|
|
1812
|
-
objects = {
|
|
1813
|
-
...objects,
|
|
1814
|
-
[uuidFormat]: this.$utils.generateUUID()
|
|
1815
|
-
};
|
|
2136
|
+
objects = Object.assign(Object.assign({}, objects), { [uuidFormat]: this.$utils.generateUUID() });
|
|
1816
2137
|
columns = [
|
|
1817
2138
|
...columns,
|
|
1818
2139
|
`\`${uuidFormat}\``
|
|
1819
2140
|
];
|
|
1820
2141
|
}
|
|
1821
2142
|
const v = Object.values(objects).map((value) => {
|
|
2143
|
+
if (typeof value === 'string')
|
|
2144
|
+
value = value === null || value === void 0 ? void 0 : value.replace(/'/g, '');
|
|
1822
2145
|
return `${value == null || value === 'NULL'
|
|
1823
2146
|
? 'NULL'
|
|
1824
2147
|
: typeof value === 'string' && value.includes(this.$constants('RAW'))
|
|
@@ -1836,181 +2159,182 @@ class Model extends AbstractModel_1.AbstractModel {
|
|
|
1836
2159
|
`${values.join(',')}`
|
|
1837
2160
|
].join(' ');
|
|
1838
2161
|
}
|
|
1839
|
-
|
|
1840
|
-
|
|
1841
|
-
|
|
1842
|
-
|
|
1843
|
-
|
|
1844
|
-
|
|
1845
|
-
|
|
1846
|
-
|
|
1847
|
-
|
|
1848
|
-
|
|
1849
|
-
|
|
1850
|
-
|
|
1851
|
-
|
|
1852
|
-
|
|
1853
|
-
|
|
1854
|
-
|
|
1855
|
-
|
|
1856
|
-
|
|
1857
|
-
|
|
1858
|
-
|
|
1859
|
-
|
|
1860
|
-
|
|
1861
|
-
|
|
1862
|
-
|
|
1863
|
-
|
|
1864
|
-
|
|
1865
|
-
|
|
1866
|
-
|
|
1867
|
-
|
|
1868
|
-
|
|
2162
|
+
_insertNotExistsModel() {
|
|
2163
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
2164
|
+
this._assertError(!this.$state.get('WHERE'), "can't insert [insertNotExists] without where condition");
|
|
2165
|
+
this._handleSoftDelete();
|
|
2166
|
+
let sql = [
|
|
2167
|
+
`${this.$constants('SELECT')}`,
|
|
2168
|
+
`${this.$constants('EXISTS')}(${this.$constants('SELECT')}`,
|
|
2169
|
+
`*`,
|
|
2170
|
+
`${this.$state.get('FROM')}`,
|
|
2171
|
+
`${this.$state.get('TABLE_NAME')}`,
|
|
2172
|
+
`${this.$state.get('WHERE')}`,
|
|
2173
|
+
`${this.$constants('LIMIT')} 1)`,
|
|
2174
|
+
`${this.$constants('AS')} 'exists'`
|
|
2175
|
+
].join(' ');
|
|
2176
|
+
let check = false;
|
|
2177
|
+
const [{ exists: result }] = yield this.queryStatement(sql);
|
|
2178
|
+
check = !!parseInt(result);
|
|
2179
|
+
switch (check) {
|
|
2180
|
+
case false: {
|
|
2181
|
+
const [result, id] = yield this.actionStatement({
|
|
2182
|
+
sql: this.$state.get('INSERT'),
|
|
2183
|
+
returnId: true
|
|
2184
|
+
});
|
|
2185
|
+
if (result) {
|
|
2186
|
+
const sql = [
|
|
2187
|
+
`${this.$state.get('SELECT')}`,
|
|
2188
|
+
`${this.$state.get('FROM')}`,
|
|
2189
|
+
`${this.$state.get('TABLE_NAME')}`,
|
|
2190
|
+
`${this.$constants('WHERE')} ${this.$state.get('TABLE_NAME')}.\`id\` = '${id}'`
|
|
2191
|
+
].join(' ');
|
|
2192
|
+
const data = yield this.queryStatement(sql);
|
|
2193
|
+
return (data === null || data === void 0 ? void 0 : data.shift()) || null;
|
|
2194
|
+
}
|
|
2195
|
+
return null;
|
|
2196
|
+
}
|
|
2197
|
+
case true: {
|
|
2198
|
+
return null;
|
|
2199
|
+
}
|
|
2200
|
+
default: {
|
|
2201
|
+
return null;
|
|
1869
2202
|
}
|
|
1870
|
-
return null;
|
|
1871
2203
|
}
|
|
1872
|
-
|
|
2204
|
+
});
|
|
2205
|
+
}
|
|
2206
|
+
_createModel() {
|
|
2207
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
2208
|
+
const [result, id] = yield this.actionStatement({
|
|
2209
|
+
sql: this.$state.get('INSERT'),
|
|
2210
|
+
returnId: true
|
|
2211
|
+
});
|
|
2212
|
+
if (this.$state.get('VOID'))
|
|
1873
2213
|
return null;
|
|
2214
|
+
if (result) {
|
|
2215
|
+
const sql = [
|
|
2216
|
+
`${this.$state.get('SELECT')}`,
|
|
2217
|
+
`${this.$state.get('FROM')}`,
|
|
2218
|
+
`${this.$state.get('TABLE_NAME')}`,
|
|
2219
|
+
`${this.$constants('WHERE')} ${this.$state.get('TABLE_NAME')}.\`id\` = ${id}`
|
|
2220
|
+
].join(' ');
|
|
2221
|
+
const data = yield this.queryStatement(sql);
|
|
2222
|
+
const result = (data === null || data === void 0 ? void 0 : data.shift()) || null;
|
|
2223
|
+
this.$state.set('RESULT', result);
|
|
2224
|
+
const hook = this.$state.get('HOOK');
|
|
2225
|
+
if (hook === null || hook === void 0 ? void 0 : hook.length)
|
|
2226
|
+
for (let i = 0; i < hook.length; i++)
|
|
2227
|
+
yield hook[i](result);
|
|
2228
|
+
return result;
|
|
1874
2229
|
}
|
|
1875
|
-
|
|
2230
|
+
const hook = this.$state.get('HOOK');
|
|
2231
|
+
if (hook === null || hook === void 0 ? void 0 : hook.length)
|
|
2232
|
+
for (let i = 0; i < hook.length; i++)
|
|
2233
|
+
yield hook[i](result || []);
|
|
2234
|
+
return null;
|
|
2235
|
+
});
|
|
2236
|
+
}
|
|
2237
|
+
_createMultipleModel() {
|
|
2238
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
2239
|
+
const [result, id] = yield this.actionStatement({
|
|
2240
|
+
sql: this.$state.get('INSERT'),
|
|
2241
|
+
returnId: true
|
|
2242
|
+
});
|
|
2243
|
+
if (this.$state.get('VOID'))
|
|
1876
2244
|
return null;
|
|
2245
|
+
if (result) {
|
|
2246
|
+
const arrayId = [...Array(result)].map((_, i) => i + id);
|
|
2247
|
+
const sql = [
|
|
2248
|
+
`${this.$state.get('SELECT')}`,
|
|
2249
|
+
`${this.$state.get('FROM')}`,
|
|
2250
|
+
`${this.$state.get('TABLE_NAME')}`,
|
|
2251
|
+
`${this.$constants('WHERE')} id`,
|
|
2252
|
+
`${this.$constants('IN')} (${arrayId})`
|
|
2253
|
+
].join(' ');
|
|
2254
|
+
const data = yield this.queryStatement(sql);
|
|
2255
|
+
const resultData = data || null;
|
|
2256
|
+
this.$state.set('RESULT', resultData);
|
|
2257
|
+
return resultData;
|
|
1877
2258
|
}
|
|
1878
|
-
|
|
1879
|
-
}
|
|
1880
|
-
async _createModel() {
|
|
1881
|
-
const [result, id] = await this.actionStatement({
|
|
1882
|
-
sql: this.$state.get('INSERT'),
|
|
1883
|
-
returnId: true
|
|
2259
|
+
return null;
|
|
1884
2260
|
});
|
|
1885
|
-
if (this.$state.get('VOID'))
|
|
1886
|
-
return Promise.resolve();
|
|
1887
|
-
if (result) {
|
|
1888
|
-
const sql = [
|
|
1889
|
-
`${this.$state.get('SELECT')}`,
|
|
1890
|
-
`${this.$state.get('FROM')}`,
|
|
1891
|
-
`${this.$state.get('TABLE_NAME')}`,
|
|
1892
|
-
`${this.$constants('WHERE')} id = ${id}`
|
|
1893
|
-
].join(' ');
|
|
1894
|
-
const data = await this.queryStatement(sql);
|
|
1895
|
-
const result = data?.shift() || null;
|
|
1896
|
-
this.$state.set('RESULT', result);
|
|
1897
|
-
return result;
|
|
1898
|
-
}
|
|
1899
|
-
return null;
|
|
1900
2261
|
}
|
|
1901
|
-
|
|
1902
|
-
|
|
1903
|
-
|
|
1904
|
-
|
|
1905
|
-
|
|
1906
|
-
|
|
1907
|
-
|
|
1908
|
-
|
|
1909
|
-
|
|
1910
|
-
|
|
1911
|
-
|
|
1912
|
-
|
|
1913
|
-
|
|
1914
|
-
|
|
1915
|
-
|
|
1916
|
-
|
|
1917
|
-
|
|
1918
|
-
|
|
1919
|
-
|
|
1920
|
-
|
|
1921
|
-
|
|
1922
|
-
return null;
|
|
1923
|
-
}
|
|
1924
|
-
async _updateOrInsertModel() {
|
|
1925
|
-
this._assertError(!this.$state.get('WHERE'), "can't update or insert [updateOrInsert] without where condition");
|
|
1926
|
-
let sql = '';
|
|
1927
|
-
let check = false;
|
|
1928
|
-
sql = [
|
|
1929
|
-
`${this.$constants('SELECT')}`,
|
|
1930
|
-
`${this.$constants('EXISTS')}(${this.$constants('SELECT')}`,
|
|
1931
|
-
`*`,
|
|
1932
|
-
`${this.$state.get('FROM')}`,
|
|
1933
|
-
`${this.$state.get('TABLE_NAME')}`,
|
|
1934
|
-
`${this.$state.get('WHERE')}`,
|
|
1935
|
-
`${this.$constants('LIMIT')} 1)`,
|
|
1936
|
-
`${this.$constants('AS')} 'exists'`
|
|
1937
|
-
].join(' ');
|
|
1938
|
-
const [{ exists: result }] = await this.queryStatement(sql);
|
|
1939
|
-
check = !!Number.parseInt(result);
|
|
1940
|
-
switch (check) {
|
|
1941
|
-
case false: {
|
|
1942
|
-
const [result, id] = await this.actionStatement({
|
|
1943
|
-
sql: this.$state.get('INSERT'),
|
|
1944
|
-
returnId: true
|
|
1945
|
-
});
|
|
1946
|
-
if (this.$state.get('VOID'))
|
|
1947
|
-
return Promise.resolve();
|
|
1948
|
-
if (result) {
|
|
1949
|
-
const sql = [
|
|
1950
|
-
`${this.$state.get('SELECT')}`,
|
|
1951
|
-
`${this.$state.get('FROM')}`,
|
|
1952
|
-
`${this.$state.get('TABLE_NAME')}`,
|
|
1953
|
-
`${this.$constants('WHERE')} id = ${id}`
|
|
1954
|
-
].join(' ');
|
|
1955
|
-
const data = await this.queryStatement(sql);
|
|
1956
|
-
const resultData = { ...data?.shift(), action_status: 'insert' } || null;
|
|
1957
|
-
this.$state.set('RESULT', resultData);
|
|
1958
|
-
return resultData;
|
|
2262
|
+
_updateOrInsertModel() {
|
|
2263
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
2264
|
+
this._assertError(!this.$state.get('WHERE'), "Can't update or insert [updateOrInsert] without where condition");
|
|
2265
|
+
const clone = new Model().copyModel(this, { where: true });
|
|
2266
|
+
const check = (yield clone.exists()) || false;
|
|
2267
|
+
switch (check) {
|
|
2268
|
+
case false: {
|
|
2269
|
+
const [result, id] = yield this.actionStatement({
|
|
2270
|
+
sql: this.$state.get('INSERT'),
|
|
2271
|
+
returnId: true
|
|
2272
|
+
});
|
|
2273
|
+
if (this.$state.get('VOID'))
|
|
2274
|
+
return null;
|
|
2275
|
+
if (result) {
|
|
2276
|
+
const sql = new Model().copyModel(this).where('id', id).toString();
|
|
2277
|
+
const data = yield this.queryStatement(sql);
|
|
2278
|
+
const resultData = Object.assign(Object.assign({}, data === null || data === void 0 ? void 0 : data.shift()), { action_status: 'insert' }) || null;
|
|
2279
|
+
this.$state.set('RESULT', resultData);
|
|
2280
|
+
return resultData;
|
|
2281
|
+
}
|
|
2282
|
+
return null;
|
|
1959
2283
|
}
|
|
1960
|
-
|
|
1961
|
-
|
|
1962
|
-
|
|
1963
|
-
|
|
1964
|
-
|
|
1965
|
-
|
|
1966
|
-
|
|
1967
|
-
|
|
1968
|
-
|
|
1969
|
-
|
|
1970
|
-
|
|
1971
|
-
|
|
1972
|
-
|
|
1973
|
-
|
|
1974
|
-
`${this.$state.get('FROM')}`,
|
|
1975
|
-
`${this.$state.get('TABLE_NAME')}`,
|
|
1976
|
-
`${this.$state.get('WHERE')}`
|
|
1977
|
-
].join(' '));
|
|
1978
|
-
if (data?.length > 1) {
|
|
1979
|
-
for (const val of data) {
|
|
1980
|
-
val.action_status = 'update';
|
|
2284
|
+
case true: {
|
|
2285
|
+
const result = yield this.actionStatement({
|
|
2286
|
+
sql: new Model().copyModel(this, { update: true }).toString()
|
|
2287
|
+
});
|
|
2288
|
+
if (this.$state.get('VOID'))
|
|
2289
|
+
return null;
|
|
2290
|
+
if (result) {
|
|
2291
|
+
const sql = new Model().copyModel(this, { where: true }).toString();
|
|
2292
|
+
const data = yield this.queryStatement(sql);
|
|
2293
|
+
if ((data === null || data === void 0 ? void 0 : data.length) > 1) {
|
|
2294
|
+
for (const val of data) {
|
|
2295
|
+
val.action_status = 'update';
|
|
2296
|
+
}
|
|
2297
|
+
return data || [];
|
|
1981
2298
|
}
|
|
1982
|
-
return data ||
|
|
2299
|
+
return Object.assign(Object.assign({}, data === null || data === void 0 ? void 0 : data.shift()), { action_status: 'update' }) || null;
|
|
1983
2300
|
}
|
|
1984
|
-
return
|
|
2301
|
+
return null;
|
|
2302
|
+
}
|
|
2303
|
+
default: {
|
|
2304
|
+
return null;
|
|
1985
2305
|
}
|
|
1986
|
-
return null;
|
|
1987
2306
|
}
|
|
1988
|
-
|
|
2307
|
+
});
|
|
2308
|
+
}
|
|
2309
|
+
_updateModel(ignoreWhere = false) {
|
|
2310
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
2311
|
+
this._assertError(!this.$state.get('WHERE') && !ignoreWhere, "can't update [update] without where condition");
|
|
2312
|
+
const sql = this._buildQueryModel();
|
|
2313
|
+
if (this.$state.get('VOID'))
|
|
1989
2314
|
return null;
|
|
2315
|
+
if (!this.$state.get('VOID')) {
|
|
2316
|
+
const [result] = yield this.actionStatement({ sql, returnId: true });
|
|
2317
|
+
if (!result)
|
|
2318
|
+
return null;
|
|
2319
|
+
let data = yield this.queryStatement([
|
|
2320
|
+
`${this.$state.get('SELECT')}`,
|
|
2321
|
+
`${this.$state.get('FROM')}`,
|
|
2322
|
+
`${this.$state.get('TABLE_NAME')}`,
|
|
2323
|
+
`${this.$state.get('WHERE')}`
|
|
2324
|
+
].join(' '));
|
|
2325
|
+
if ((data === null || data === void 0 ? void 0 : data.length) > 1) {
|
|
2326
|
+
data = data || [];
|
|
2327
|
+
}
|
|
2328
|
+
else {
|
|
2329
|
+
data = (data === null || data === void 0 ? void 0 : data.shift()) || null;
|
|
2330
|
+
}
|
|
2331
|
+
this.$state.set('RESULT', data);
|
|
2332
|
+
return data;
|
|
1990
2333
|
}
|
|
1991
|
-
|
|
1992
|
-
|
|
1993
|
-
|
|
1994
|
-
|
|
1995
|
-
const [result] = await this.actionStatement({ sql: [
|
|
1996
|
-
`${this.$state.get('UPDATE')}`,
|
|
1997
|
-
`${this.$state.get('WHERE')}`
|
|
1998
|
-
].join(' '), returnId: true });
|
|
1999
|
-
if (this.$state.get('VOID'))
|
|
2000
|
-
return Promise.resolve();
|
|
2001
|
-
if (!result)
|
|
2002
|
-
return null;
|
|
2003
|
-
const data = await this.queryStatement([
|
|
2004
|
-
`${this.$state.get('SELECT')}`,
|
|
2005
|
-
`${this.$state.get('FROM')}`,
|
|
2006
|
-
`${this.$state.get('TABLE_NAME')}`,
|
|
2007
|
-
`${this.$state.get('WHERE')}`
|
|
2008
|
-
].join(' '));
|
|
2009
|
-
if (data?.length > 1)
|
|
2010
|
-
return data || [];
|
|
2011
|
-
const res = data?.shift() || null;
|
|
2012
|
-
this.$state.set('RESULT', res);
|
|
2013
|
-
return res;
|
|
2334
|
+
const [result] = yield this.actionStatement({ sql, returnId: true });
|
|
2335
|
+
if (!result)
|
|
2336
|
+
return null;
|
|
2337
|
+
});
|
|
2014
2338
|
}
|
|
2015
2339
|
_assertError(condition = true, message = 'error') {
|
|
2016
2340
|
if (typeof condition === 'string') {
|
|
@@ -2032,38 +2356,55 @@ class Model extends AbstractModel_1.AbstractModel {
|
|
|
2032
2356
|
this._assertError(!Object.values(this.$constants('RELATIONSHIP')).includes(r.relation), `unknown relationship in [${this.$constants('RELATIONSHIP')}] !`);
|
|
2033
2357
|
return r;
|
|
2034
2358
|
}
|
|
2359
|
+
_validateMethod(method) {
|
|
2360
|
+
switch (method.toLocaleLowerCase()) {
|
|
2361
|
+
case 'paginate':
|
|
2362
|
+
case 'pagination':
|
|
2363
|
+
case 'findOneOrError':
|
|
2364
|
+
case 'firstOrError':
|
|
2365
|
+
case 'findOne':
|
|
2366
|
+
case 'findMany':
|
|
2367
|
+
case 'first':
|
|
2368
|
+
case 'get': {
|
|
2369
|
+
const methodCallings = this.$logger.get();
|
|
2370
|
+
const methodsNotAllowed = [
|
|
2371
|
+
'create',
|
|
2372
|
+
'createNotExists',
|
|
2373
|
+
'updateOrCreate',
|
|
2374
|
+
'updateOrInsert',
|
|
2375
|
+
'insertOrUpdate',
|
|
2376
|
+
'update'
|
|
2377
|
+
];
|
|
2378
|
+
const findMethodNotAllowed = methodCallings.find((methodCalling) => methodsNotAllowed.includes(methodCalling));
|
|
2379
|
+
this._assertError(methodCallings.some((methodCalling) => methodsNotAllowed.includes(methodCalling)), `this method ${method} can't using method : [ ${findMethodNotAllowed} ]`);
|
|
2380
|
+
break;
|
|
2381
|
+
}
|
|
2382
|
+
}
|
|
2383
|
+
}
|
|
2035
2384
|
_initialModel() {
|
|
2036
|
-
this.$state =
|
|
2385
|
+
this.$state = (() => {
|
|
2386
|
+
let db = new Map(Object.entries(Object.assign({}, this.$constants('MODEL'))));
|
|
2387
|
+
let original = new Map(Object.entries(Object.assign({}, this.$constants('MODEL'))));
|
|
2388
|
+
return {
|
|
2389
|
+
original: () => original,
|
|
2390
|
+
get: (key) => {
|
|
2391
|
+
if (key == null)
|
|
2392
|
+
return db;
|
|
2393
|
+
this._assertError(!db.has(key), `can't get this [${key}]`);
|
|
2394
|
+
return db.get(key);
|
|
2395
|
+
},
|
|
2396
|
+
set: (key, value) => {
|
|
2397
|
+
this._assertError(!db.has(key), `can't set this [${key}]`);
|
|
2398
|
+
db.set(key, value);
|
|
2399
|
+
},
|
|
2400
|
+
clone: (data) => {
|
|
2401
|
+
db = new Map(Object.entries(Object.assign({}, data)));
|
|
2402
|
+
}
|
|
2403
|
+
};
|
|
2404
|
+
})();
|
|
2037
2405
|
this._makeTableName();
|
|
2038
2406
|
return this;
|
|
2039
2407
|
}
|
|
2040
|
-
_setupModel() {
|
|
2041
|
-
let db = new Map(Object.entries({ ...this.$constants('MODEL') }));
|
|
2042
|
-
return {
|
|
2043
|
-
get: (key) => {
|
|
2044
|
-
if (key == null)
|
|
2045
|
-
return db;
|
|
2046
|
-
this._assertError(!db.has(key), `can't get this [${key}]`);
|
|
2047
|
-
return db.get(key);
|
|
2048
|
-
},
|
|
2049
|
-
set: (key, value) => {
|
|
2050
|
-
this._assertError(!db.has(key), `can't set this [${key}]`);
|
|
2051
|
-
db.set(key, value);
|
|
2052
|
-
},
|
|
2053
|
-
clone: (data) => {
|
|
2054
|
-
db = new Map(Object.entries({ ...data }));
|
|
2055
|
-
}
|
|
2056
|
-
};
|
|
2057
|
-
}
|
|
2058
|
-
_bindMethod(name, func) {
|
|
2059
|
-
Object.defineProperty(this, name, {
|
|
2060
|
-
value: func,
|
|
2061
|
-
enumerable: true,
|
|
2062
|
-
configurable: true,
|
|
2063
|
-
writable: true
|
|
2064
|
-
});
|
|
2065
|
-
return;
|
|
2066
|
-
}
|
|
2067
2408
|
}
|
|
2068
2409
|
exports.Model = Model;
|
|
2069
2410
|
exports.default = Model;
|