tspace-mysql 1.5.3 → 1.5.5
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/build/lib/Interface.d.ts +2 -2
- package/build/lib/core/Abstracts/AbstractBuilder.d.ts +1 -1
- package/build/lib/core/Builder.d.ts +4 -2
- package/build/lib/core/Builder.js +53 -52
- package/build/lib/core/Handlers/State.js +33 -1
- package/build/lib/core/Model.d.ts +22 -7
- package/build/lib/core/Model.js +148 -79
- package/package.json +1 -1
package/build/lib/Interface.d.ts
CHANGED
|
@@ -39,7 +39,7 @@ export interface RelationShip {
|
|
|
39
39
|
belongsTo: string;
|
|
40
40
|
belongsToMany: string;
|
|
41
41
|
}
|
|
42
|
-
export interface Pagination {
|
|
42
|
+
export interface Pagination<K = any[]> {
|
|
43
43
|
meta: {
|
|
44
44
|
total: number;
|
|
45
45
|
limit: number;
|
|
@@ -49,7 +49,7 @@ export interface Pagination {
|
|
|
49
49
|
next_page: number;
|
|
50
50
|
prev_page: number;
|
|
51
51
|
};
|
|
52
|
-
data:
|
|
52
|
+
data: K;
|
|
53
53
|
}
|
|
54
54
|
export interface Backup {
|
|
55
55
|
database: string;
|
|
@@ -100,7 +100,7 @@ declare abstract class AbstractBuilder {
|
|
|
100
100
|
abstract findOne(): Promise<Record<string, any> | null>;
|
|
101
101
|
abstract findMany(): Promise<any[]>;
|
|
102
102
|
abstract getGroupBy(column: string): Promise<any[]>;
|
|
103
|
-
abstract
|
|
103
|
+
abstract findGroupBy(column: string): Promise<any[]>;
|
|
104
104
|
abstract toArray(column: string): Promise<any[]>;
|
|
105
105
|
abstract toJSON(): Promise<string>;
|
|
106
106
|
abstract toSQL(): string;
|
|
@@ -1207,7 +1207,7 @@ declare class Builder extends AbstractBuilder {
|
|
|
1207
1207
|
* @param {string} column
|
|
1208
1208
|
* @return {promise<Array>}
|
|
1209
1209
|
*/
|
|
1210
|
-
|
|
1210
|
+
findGroupBy(column: string): Promise<any[]>;
|
|
1211
1211
|
/**
|
|
1212
1212
|
* The 'save' method is used to persist a new 'Model' or new 'DB' instance or update an existing model instance in the database.
|
|
1213
1213
|
*
|
|
@@ -1310,10 +1310,12 @@ declare class Builder extends AbstractBuilder {
|
|
|
1310
1310
|
delete?: boolean;
|
|
1311
1311
|
where?: boolean;
|
|
1312
1312
|
limit?: boolean;
|
|
1313
|
-
|
|
1313
|
+
orderBy?: boolean;
|
|
1314
1314
|
join?: boolean;
|
|
1315
|
+
offset?: boolean;
|
|
1315
1316
|
groupBy?: boolean;
|
|
1316
1317
|
select?: boolean;
|
|
1318
|
+
having?: boolean;
|
|
1317
1319
|
}): Builder;
|
|
1318
1320
|
protected _queryBuilder(): {
|
|
1319
1321
|
select: () => string;
|
|
@@ -26,6 +26,7 @@ const utils_1 = require("../utils");
|
|
|
26
26
|
const constants_1 = require("../constants");
|
|
27
27
|
const DB_1 = require("./DB");
|
|
28
28
|
const connection_1 = require("../connection");
|
|
29
|
+
const State_1 = require("./Handlers/State");
|
|
29
30
|
class Builder extends AbstractBuilder_1.AbstractBuilder {
|
|
30
31
|
constructor() {
|
|
31
32
|
super();
|
|
@@ -499,7 +500,7 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
|
|
|
499
500
|
throw new Error(`This 'whereIn' method is required array only`);
|
|
500
501
|
const values = array.length
|
|
501
502
|
? `${array.map((value) => this._checkValueHasRaw(this.$utils.escape(value))).join(',')}`
|
|
502
|
-
: this.$constants('NULL');
|
|
503
|
+
: this.$constants(this.$constants('NULL'));
|
|
503
504
|
this.$state.set('WHERE', [
|
|
504
505
|
...this.$state.get('WHERE'),
|
|
505
506
|
[
|
|
@@ -524,7 +525,7 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
|
|
|
524
525
|
throw new Error(`This 'orWhereIn' method is required array only`);
|
|
525
526
|
const values = array.length
|
|
526
527
|
? `${array.map((value) => this._checkValueHasRaw(this.$utils.escape(value))).join(',')}`
|
|
527
|
-
: this.$constants('NULL');
|
|
528
|
+
: this.$constants(this.$constants('NULL'));
|
|
528
529
|
this.$state.set('WHERE', [
|
|
529
530
|
...this.$state.get('WHERE'),
|
|
530
531
|
[
|
|
@@ -700,9 +701,9 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
|
|
|
700
701
|
this.$state.get('WHERE').length ? `${this.$constants('AND')}` : '',
|
|
701
702
|
`${this.bindColumn(column)}`,
|
|
702
703
|
`${this.$constants('BETWEEN')}`,
|
|
703
|
-
`${this.$constants('NULL')}`,
|
|
704
|
+
`${this.$constants(this.$constants('NULL'))}`,
|
|
704
705
|
`${this.$constants('AND')}`,
|
|
705
|
-
`${this.$constants('NULL')}`
|
|
706
|
+
`${this.$constants(this.$constants('NULL'))}`
|
|
706
707
|
].join(' ')
|
|
707
708
|
]);
|
|
708
709
|
return this;
|
|
@@ -739,9 +740,9 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
|
|
|
739
740
|
this.$state.get('WHERE').length ? `${this.$constants('OR')}` : '',
|
|
740
741
|
`${this.bindColumn(column)}`,
|
|
741
742
|
`${this.$constants('BETWEEN')}`,
|
|
742
|
-
`${this.$constants('NULL')}`,
|
|
743
|
+
`${this.$constants(this.$constants('NULL'))}`,
|
|
743
744
|
`${this.$constants('AND')}`,
|
|
744
|
-
`${this.$constants('NULL')}`
|
|
745
|
+
`${this.$constants(this.$constants('NULL'))}`
|
|
745
746
|
].join(' ')
|
|
746
747
|
]);
|
|
747
748
|
return this;
|
|
@@ -778,9 +779,9 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
|
|
|
778
779
|
this.$state.get('WHERE').length ? `${this.$constants('AND')}` : '',
|
|
779
780
|
`${this.bindColumn(column)}`,
|
|
780
781
|
`${this.$constants('NOT_BETWEEN')}`,
|
|
781
|
-
`${this.$constants('NULL')}`,
|
|
782
|
+
`${this.$constants(this.$constants('NULL'))}`,
|
|
782
783
|
`${this.$constants('AND')}`,
|
|
783
|
-
`${this.$constants('NULL')}`
|
|
784
|
+
`${this.$constants(this.$constants('NULL'))}`
|
|
784
785
|
].join(' ')
|
|
785
786
|
]);
|
|
786
787
|
return this;
|
|
@@ -817,9 +818,9 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
|
|
|
817
818
|
this.$state.get('WHERE').length ? `${this.$constants('OR')}` : '',
|
|
818
819
|
`${this.bindColumn(column)}`,
|
|
819
820
|
`${this.$constants('NOT_BETWEEN')}`,
|
|
820
|
-
`${this.$constants('NULL')}`,
|
|
821
|
+
`${this.$constants(this.$constants('NULL'))}`,
|
|
821
822
|
`${this.$constants('AND')}`,
|
|
822
|
-
`${this.$constants('NULL')}`
|
|
823
|
+
`${this.$constants(this.$constants('NULL'))}`
|
|
823
824
|
].join(' ')
|
|
824
825
|
]);
|
|
825
826
|
return this;
|
|
@@ -1961,8 +1962,8 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
|
|
|
1961
1962
|
if (typeof value === 'string' && !(value.includes(this.$constants('RAW')))) {
|
|
1962
1963
|
value = this.$utils.escapeActions(value);
|
|
1963
1964
|
}
|
|
1964
|
-
return `${this.bindColumn(column)} = ${value == null || value === 'NULL'
|
|
1965
|
-
? 'NULL'
|
|
1965
|
+
return `${this.bindColumn(column)} = ${value == null || value === this.$constants('NULL')
|
|
1966
|
+
? this.$constants('NULL')
|
|
1966
1967
|
: typeof value === 'string' && value.includes(this.$constants('RAW'))
|
|
1967
1968
|
? `${this.$utils.covertBooleanToNumber(value)}`.replace(this.$constants('RAW'), '')
|
|
1968
1969
|
: `'${this.$utils.covertBooleanToNumber(value)}'`}`;
|
|
@@ -2255,13 +2256,14 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
|
|
|
2255
2256
|
*/
|
|
2256
2257
|
pagination(paginationOptions) {
|
|
2257
2258
|
return __awaiter(this, void 0, void 0, function* () {
|
|
2258
|
-
var _a, _b
|
|
2259
|
+
var _a, _b;
|
|
2259
2260
|
let limit = 15;
|
|
2260
2261
|
let page = 1;
|
|
2261
2262
|
if (paginationOptions != null) {
|
|
2262
2263
|
limit = (paginationOptions === null || paginationOptions === void 0 ? void 0 : paginationOptions.limit) || limit;
|
|
2263
2264
|
page = (paginationOptions === null || paginationOptions === void 0 ? void 0 : paginationOptions.page) || page;
|
|
2264
2265
|
}
|
|
2266
|
+
limit = limit > 1000 ? 1000 : limit;
|
|
2265
2267
|
const currentPage = page;
|
|
2266
2268
|
const nextPage = currentPage + 1;
|
|
2267
2269
|
const prevPage = currentPage - 1 === 0 ? 1 : currentPage - 1;
|
|
@@ -2285,22 +2287,14 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
|
|
|
2285
2287
|
},
|
|
2286
2288
|
data: []
|
|
2287
2289
|
};
|
|
2288
|
-
const
|
|
2289
|
-
|
|
2290
|
-
|
|
2291
|
-
|
|
2292
|
-
|
|
2293
|
-
const sqlTotal = [
|
|
2294
|
-
sqlCount,
|
|
2295
|
-
this.$state.get('FROM'),
|
|
2296
|
-
this.$state.get('TABLE_NAME'),
|
|
2297
|
-
this._queryBuilder().where()
|
|
2298
|
-
].join(' ');
|
|
2299
|
-
const count = yield this._queryStatement(sqlTotal);
|
|
2300
|
-
const total = ((_b = count === null || count === void 0 ? void 0 : count.shift()) === null || _b === void 0 ? void 0 : _b.total) || 0;
|
|
2290
|
+
const total = yield new DB_1.DB()
|
|
2291
|
+
.copyBuilder(this, { where: true, join: true })
|
|
2292
|
+
.bind(this.$pool.get())
|
|
2293
|
+
.debug(this.$state.get('DEBUG'))
|
|
2294
|
+
.count();
|
|
2301
2295
|
let lastPage = Math.ceil(total / limit) || 0;
|
|
2302
2296
|
lastPage = lastPage > 1 ? lastPage : 1;
|
|
2303
|
-
const totalPage = (
|
|
2297
|
+
const totalPage = (_b = result === null || result === void 0 ? void 0 : result.length) !== null && _b !== void 0 ? _b : 0;
|
|
2304
2298
|
return {
|
|
2305
2299
|
meta: {
|
|
2306
2300
|
total: total,
|
|
@@ -2590,9 +2584,11 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
|
|
|
2590
2584
|
count() {
|
|
2591
2585
|
return __awaiter(this, arguments, void 0, function* (column = 'id') {
|
|
2592
2586
|
const distinct = this.$state.get('DISTINCT');
|
|
2593
|
-
column =
|
|
2594
|
-
?
|
|
2595
|
-
:
|
|
2587
|
+
column = column === '*'
|
|
2588
|
+
? '*'
|
|
2589
|
+
: distinct
|
|
2590
|
+
? `${this.$constants('DISTINCT')} ${this.bindColumn(column)}`
|
|
2591
|
+
: `${this.bindColumn(column)}`;
|
|
2596
2592
|
this.selectRaw(`${this.$constants('COUNT')}(${column}) ${this.$constants('AS')} \`aggregate\``);
|
|
2597
2593
|
const sql = this._queryBuilder().select();
|
|
2598
2594
|
const result = yield this._queryStatement(sql);
|
|
@@ -2795,7 +2791,7 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
|
|
|
2795
2791
|
* @param {string} column
|
|
2796
2792
|
* @return {promise<Array>}
|
|
2797
2793
|
*/
|
|
2798
|
-
|
|
2794
|
+
findGroupBy(column) {
|
|
2799
2795
|
return __awaiter(this, void 0, void 0, function* () {
|
|
2800
2796
|
return yield this.getGroupBy(column);
|
|
2801
2797
|
});
|
|
@@ -3031,7 +3027,7 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
|
|
|
3031
3027
|
return `(${Object.values(value).map((v) => {
|
|
3032
3028
|
if (typeof v === 'object' && v != null && !Array.isArray(v))
|
|
3033
3029
|
return `'${JSON.stringify(v)}'`;
|
|
3034
|
-
return v == null ? 'NULL' : `'${v}'`;
|
|
3030
|
+
return v == null ? this.$constants('NULL') : `'${v}'`;
|
|
3035
3031
|
}).join(', ')})`;
|
|
3036
3032
|
});
|
|
3037
3033
|
return values;
|
|
@@ -3159,24 +3155,29 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
|
|
|
3159
3155
|
const newInstance = new Builder();
|
|
3160
3156
|
newInstance.$state.clone(copy);
|
|
3161
3157
|
newInstance.$state.set('SAVE', '');
|
|
3162
|
-
|
|
3158
|
+
newInstance.$state.set('DEBUG', false);
|
|
3159
|
+
if ((options === null || options === void 0 ? void 0 : options.insert) == null || !options.insert)
|
|
3163
3160
|
newInstance.$state.set('INSERT', '');
|
|
3164
|
-
if ((options === null || options === void 0 ? void 0 : options.update) == null)
|
|
3161
|
+
if ((options === null || options === void 0 ? void 0 : options.update) == null || !options.update)
|
|
3165
3162
|
newInstance.$state.set('UPDATE', '');
|
|
3166
|
-
if ((options === null || options === void 0 ? void 0 : options.delete) == null)
|
|
3163
|
+
if ((options === null || options === void 0 ? void 0 : options.delete) == null || !options.delete)
|
|
3167
3164
|
newInstance.$state.set('DELETE', '');
|
|
3168
|
-
if ((options === null || options === void 0 ? void 0 : options.where) == null)
|
|
3169
|
-
newInstance.$state.set('WHERE',
|
|
3170
|
-
if ((options === null || options === void 0 ? void 0 : options.limit) == null)
|
|
3165
|
+
if ((options === null || options === void 0 ? void 0 : options.where) == null || !options.where)
|
|
3166
|
+
newInstance.$state.set('WHERE', []);
|
|
3167
|
+
if ((options === null || options === void 0 ? void 0 : options.limit) == null || !options.limit)
|
|
3171
3168
|
newInstance.$state.set('LIMIT', '');
|
|
3172
|
-
if ((options === null || options === void 0 ? void 0 : options.offset) == null)
|
|
3169
|
+
if ((options === null || options === void 0 ? void 0 : options.offset) == null || !options.offset)
|
|
3173
3170
|
newInstance.$state.set('OFFSET', '');
|
|
3174
|
-
if ((options === null || options === void 0 ? void 0 : options.groupBy) == null)
|
|
3171
|
+
if ((options === null || options === void 0 ? void 0 : options.groupBy) == null || !options.groupBy)
|
|
3175
3172
|
newInstance.$state.set('GROUP_BY', '');
|
|
3176
|
-
if ((options === null || options === void 0 ? void 0 : options.
|
|
3177
|
-
newInstance.$state.set('
|
|
3178
|
-
if ((options === null || options === void 0 ? void 0 : options.
|
|
3179
|
-
newInstance.$state.set('
|
|
3173
|
+
if ((options === null || options === void 0 ? void 0 : options.orderBy) == null || !options.orderBy)
|
|
3174
|
+
newInstance.$state.set('ORDER_BY', []);
|
|
3175
|
+
if ((options === null || options === void 0 ? void 0 : options.select) == null || !options.select)
|
|
3176
|
+
newInstance.$state.set('SELECT', []);
|
|
3177
|
+
if ((options === null || options === void 0 ? void 0 : options.join) == null || !options.join)
|
|
3178
|
+
newInstance.$state.set('JOIN', []);
|
|
3179
|
+
if ((options === null || options === void 0 ? void 0 : options.having) == null || !options.having)
|
|
3180
|
+
newInstance.$state.set('HAVING', '');
|
|
3180
3181
|
return newInstance;
|
|
3181
3182
|
}
|
|
3182
3183
|
_queryBuilder() {
|
|
@@ -3224,7 +3225,6 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
|
|
|
3224
3225
|
this.$state.get('TABLE_NAME'),
|
|
3225
3226
|
bindJoin(this.$state.get('JOIN')),
|
|
3226
3227
|
bindWhere(this.$state.get('WHERE')),
|
|
3227
|
-
// this.$state.get('GROUP_BY'),
|
|
3228
3228
|
bindGroupBy(this.$state.get('GROUP_BY')),
|
|
3229
3229
|
this.$state.get('HAVING'),
|
|
3230
3230
|
bindOrderBy(this.$state.get('ORDER_BY')),
|
|
@@ -3523,8 +3523,8 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
|
|
|
3523
3523
|
if (typeof value === 'string' && !(value.includes(this.$constants('RAW')))) {
|
|
3524
3524
|
value = this.$utils.escapeActions(value);
|
|
3525
3525
|
}
|
|
3526
|
-
return `${this.bindColumn(column)} = ${value == null || value === 'NULL'
|
|
3527
|
-
? 'NULL'
|
|
3526
|
+
return `${this.bindColumn(column)} = ${value == null || value === this.$constants('NULL')
|
|
3527
|
+
? this.$constants('NULL')
|
|
3528
3528
|
: this._checkValueHasRaw(value)}`;
|
|
3529
3529
|
});
|
|
3530
3530
|
return `${this.$constants('SET')} ${values}`;
|
|
@@ -3536,8 +3536,8 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
|
|
|
3536
3536
|
if (typeof value === 'string' && !(value.includes(this.$constants('RAW')))) {
|
|
3537
3537
|
value = this.$utils.escapeActions(value);
|
|
3538
3538
|
}
|
|
3539
|
-
return `${value == null || value === 'NULL'
|
|
3540
|
-
? 'NULL'
|
|
3539
|
+
return `${value == null || value === this.$constants('NULL')
|
|
3540
|
+
? this.$constants('NULL')
|
|
3541
3541
|
: this._checkValueHasRaw(value)}`;
|
|
3542
3542
|
});
|
|
3543
3543
|
return [
|
|
@@ -3555,8 +3555,8 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
|
|
|
3555
3555
|
if (typeof value === 'string' && !(value.includes(this.$constants('RAW')))) {
|
|
3556
3556
|
value = this.$utils.escapeActions(value);
|
|
3557
3557
|
}
|
|
3558
|
-
return `${value == null || value === 'NULL'
|
|
3559
|
-
? 'NULL'
|
|
3558
|
+
return `${value == null || value === this.$constants('NULL')
|
|
3559
|
+
? this.$constants('NULL')
|
|
3560
3560
|
: this._checkValueHasRaw(value)}`;
|
|
3561
3561
|
});
|
|
3562
3562
|
values.push(`(${vals.join(',')})`);
|
|
@@ -3572,7 +3572,7 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
|
|
|
3572
3572
|
if (useDefault)
|
|
3573
3573
|
return [operator, '='];
|
|
3574
3574
|
if (operator == null)
|
|
3575
|
-
throw new Error(
|
|
3575
|
+
throw new Error(`The arguments are required. Please check the your arguments in method 'where' or etc methods.`);
|
|
3576
3576
|
if (operator.toUpperCase() === this.$constants('LIKE')) {
|
|
3577
3577
|
operator = operator.toUpperCase();
|
|
3578
3578
|
}
|
|
@@ -3598,6 +3598,7 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
|
|
|
3598
3598
|
}
|
|
3599
3599
|
};
|
|
3600
3600
|
})();
|
|
3601
|
+
this.$state = new State_1.StateHandler('default');
|
|
3601
3602
|
this.$logger = (() => {
|
|
3602
3603
|
let logger = [];
|
|
3603
3604
|
return {
|
|
@@ -1,7 +1,39 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.StateHandler = void 0;
|
|
4
|
-
const STATE_DEFAULT = {
|
|
4
|
+
const STATE_DEFAULT = {
|
|
5
|
+
PRIMARY_KEY: 'id',
|
|
6
|
+
VOID: false,
|
|
7
|
+
RESULT: null,
|
|
8
|
+
DISTINCT: false,
|
|
9
|
+
PLUCK: '',
|
|
10
|
+
SAVE: '',
|
|
11
|
+
DELETE: '',
|
|
12
|
+
UPDATE: '',
|
|
13
|
+
INSERT: '',
|
|
14
|
+
SELECT: [],
|
|
15
|
+
ONLY: [],
|
|
16
|
+
EXCEPTS: [],
|
|
17
|
+
CHUNK: 0,
|
|
18
|
+
COUNT: '',
|
|
19
|
+
FROM: 'FROM',
|
|
20
|
+
JOIN: [],
|
|
21
|
+
WHERE: [],
|
|
22
|
+
GROUP_BY: [],
|
|
23
|
+
ORDER_BY: [],
|
|
24
|
+
LIMIT: '',
|
|
25
|
+
OFFSET: '',
|
|
26
|
+
HAVING: '',
|
|
27
|
+
TABLE_NAME: '',
|
|
28
|
+
UUID_CUSTOM: '',
|
|
29
|
+
HIDDEN: [],
|
|
30
|
+
DEBUG: false,
|
|
31
|
+
UUID: false,
|
|
32
|
+
PAGE: 1,
|
|
33
|
+
PER_PAGE: 1,
|
|
34
|
+
HOOKS: [],
|
|
35
|
+
RETURN_TYPE: null
|
|
36
|
+
};
|
|
5
37
|
const STATE_DB = {
|
|
6
38
|
PRIMARY_KEY: 'id',
|
|
7
39
|
VOID: false,
|
|
@@ -1297,14 +1297,14 @@ declare class Model<TSchema extends Record<string, Blueprint | string | number |
|
|
|
1297
1297
|
* @param {Function?} cb callback function return query sql
|
|
1298
1298
|
* @return {promise<array>} Array
|
|
1299
1299
|
*/
|
|
1300
|
-
get<K
|
|
1300
|
+
get<K>(cb?: Function): Promise<Partial<(TSchema & TRelation & K)>[]>;
|
|
1301
1301
|
/**
|
|
1302
1302
|
*
|
|
1303
1303
|
* @override
|
|
1304
1304
|
* @param {Function?} cb callback function return query sql
|
|
1305
1305
|
* @return {promise<array>} Array
|
|
1306
1306
|
*/
|
|
1307
|
-
findMany<K
|
|
1307
|
+
findMany<K>(cb?: Function): Promise<Partial<(TSchema & TRelation & K)>[]>;
|
|
1308
1308
|
/**
|
|
1309
1309
|
* @override
|
|
1310
1310
|
* @param {object?} paginationOptions by default page = 1 , limit = 15
|
|
@@ -1312,10 +1312,10 @@ declare class Model<TSchema extends Record<string, Blueprint | string | number |
|
|
|
1312
1312
|
* @property {number} paginationOptions.page
|
|
1313
1313
|
* @return {promise<Pagination>} Pagination
|
|
1314
1314
|
*/
|
|
1315
|
-
pagination(paginationOptions?: {
|
|
1315
|
+
pagination<K>(paginationOptions?: {
|
|
1316
1316
|
limit?: number;
|
|
1317
1317
|
page?: number;
|
|
1318
|
-
}): Promise<Pagination
|
|
1318
|
+
}): Promise<Pagination<Partial<(TSchema & TRelation & K)>[]>>;
|
|
1319
1319
|
/**
|
|
1320
1320
|
*
|
|
1321
1321
|
* @override
|
|
@@ -1324,16 +1324,22 @@ declare class Model<TSchema extends Record<string, Blueprint | string | number |
|
|
|
1324
1324
|
* @property {number} paginationOptions.page
|
|
1325
1325
|
* @return {promise<Pagination>} Pagination
|
|
1326
1326
|
*/
|
|
1327
|
-
paginate(paginationOptions?: {
|
|
1327
|
+
paginate<K>(paginationOptions?: {
|
|
1328
1328
|
limit?: number;
|
|
1329
1329
|
page?: number;
|
|
1330
|
-
}): Promise<Pagination
|
|
1330
|
+
}): Promise<Pagination<Partial<(TSchema & TRelation & K)>[]>>;
|
|
1331
1331
|
/**
|
|
1332
1332
|
* @override
|
|
1333
1333
|
* @param {string} column
|
|
1334
1334
|
* @return {Promise<array>} Array
|
|
1335
1335
|
*/
|
|
1336
|
-
getGroupBy(column:
|
|
1336
|
+
getGroupBy<K extends Extract<keyof TSchema, string> | `${string}.${string}`>(column: K): Promise<any[]>;
|
|
1337
|
+
/**
|
|
1338
|
+
* @override
|
|
1339
|
+
* @param {string} column
|
|
1340
|
+
* @return {Promise<array>} Array
|
|
1341
|
+
*/
|
|
1342
|
+
findGroupBy<K extends Extract<keyof TSchema, string> | `${string}.${string}`>(column: K): Promise<any[]>;
|
|
1337
1343
|
/**
|
|
1338
1344
|
* @override
|
|
1339
1345
|
* @param {object} data for insert
|
|
@@ -1467,7 +1473,16 @@ declare class Model<TSchema extends Record<string, Blueprint | string | number |
|
|
|
1467
1473
|
when: Record<string, any>;
|
|
1468
1474
|
columns: Record<string, string | number | boolean | null | undefined>;
|
|
1469
1475
|
}[]): this;
|
|
1476
|
+
/**
|
|
1477
|
+
* The 'getSchemaModel' method is used get a schema model
|
|
1478
|
+
* @return {Record<string, Blueprint> | null} Record<string, Blueprint> | null
|
|
1479
|
+
*/
|
|
1470
1480
|
getSchemaModel(): Record<string, Blueprint> | null;
|
|
1481
|
+
/**
|
|
1482
|
+
* The 'validation' method is used validate the column by validating
|
|
1483
|
+
* @param {ValidateSchema} schema
|
|
1484
|
+
* @return {this} this
|
|
1485
|
+
*/
|
|
1471
1486
|
validation(schema?: ValidateSchema): this;
|
|
1472
1487
|
/**
|
|
1473
1488
|
* The 'bindPattern' method is used to covert column relate with pattern
|
package/build/lib/core/Model.js
CHANGED
|
@@ -311,7 +311,7 @@ class Model extends AbstractModel_1.AbstractModel {
|
|
|
311
311
|
this.$constants('PATTERN').camelCase
|
|
312
312
|
];
|
|
313
313
|
if (!allowPattern.includes(pattern)) {
|
|
314
|
-
this._assertError(`tspace-mysql support only pattern
|
|
314
|
+
throw this._assertError(`The 'tspace-mysql' support only pattern '${this.$constants('PATTERN').snake_case}', '${this.$constants('PATTERN').camelCase}'`);
|
|
315
315
|
}
|
|
316
316
|
this.$state.set('PATTERN', pattern);
|
|
317
317
|
this._makeTableName();
|
|
@@ -561,7 +561,7 @@ class Model extends AbstractModel_1.AbstractModel {
|
|
|
561
561
|
*/
|
|
562
562
|
beforeCreatingTable(fn) {
|
|
563
563
|
if (!(fn instanceof Function))
|
|
564
|
-
this._assertError(`This '${fn}' is not a function
|
|
564
|
+
throw this._assertError(`This '${fn}' is not a function.`);
|
|
565
565
|
this.$state.set('BEFORE_CREATING_TABLE', fn);
|
|
566
566
|
return this;
|
|
567
567
|
}
|
|
@@ -633,9 +633,13 @@ class Model extends AbstractModel_1.AbstractModel {
|
|
|
633
633
|
var _a, _b;
|
|
634
634
|
this.relations(name);
|
|
635
635
|
const relation = this.$state.get('RELATIONS').find((data) => data.name === name);
|
|
636
|
-
|
|
636
|
+
if (relation == null) {
|
|
637
|
+
throw this._assertError(`This Relation '${String(name)}' not be register in Model '${(_a = this.constructor) === null || _a === void 0 ? void 0 : _a.name}'.`);
|
|
638
|
+
}
|
|
637
639
|
const relationHasExists = (_b = Object.values(this.$constants('RELATIONSHIP'))) === null || _b === void 0 ? void 0 : _b.includes(relation.relation);
|
|
638
|
-
|
|
640
|
+
if (!relationHasExists) {
|
|
641
|
+
throw this._assertError(`Unknown relationship in '${this.$constants('RELATIONSHIP')}'.`);
|
|
642
|
+
}
|
|
639
643
|
if (callback == null) {
|
|
640
644
|
relation.query = new relation.model();
|
|
641
645
|
return this;
|
|
@@ -973,7 +977,9 @@ class Model extends AbstractModel_1.AbstractModel {
|
|
|
973
977
|
* @return {Model} Model
|
|
974
978
|
*/
|
|
975
979
|
copyModel(instance, options) {
|
|
976
|
-
|
|
980
|
+
if (!(instance instanceof Model)) {
|
|
981
|
+
throw this._assertError('This instance is not an instanceof Model.');
|
|
982
|
+
}
|
|
977
983
|
const copy = Object.fromEntries(instance.$state.get());
|
|
978
984
|
const newInstance = new Model();
|
|
979
985
|
newInstance.$state.clone(copy);
|
|
@@ -1908,7 +1914,7 @@ class Model extends AbstractModel_1.AbstractModel {
|
|
|
1908
1914
|
for (let column in columns) {
|
|
1909
1915
|
const operator = '=';
|
|
1910
1916
|
const value = this.$utils.escape(columns[column]);
|
|
1911
|
-
if (value
|
|
1917
|
+
if (value === null) {
|
|
1912
1918
|
this.whereNull(column);
|
|
1913
1919
|
continue;
|
|
1914
1920
|
}
|
|
@@ -1978,7 +1984,7 @@ class Model extends AbstractModel_1.AbstractModel {
|
|
|
1978
1984
|
const c = this._columnPattern(String(column));
|
|
1979
1985
|
const values = array.length
|
|
1980
1986
|
? `${array.map((value) => this._checkValueHasRaw(this.$utils.escape(value))).join(',')}`
|
|
1981
|
-
: this.$constants('NULL');
|
|
1987
|
+
: this.$constants(this.$constants('NULL'));
|
|
1982
1988
|
this.$state.set('WHERE', [
|
|
1983
1989
|
...this.$state.get('WHERE'),
|
|
1984
1990
|
[
|
|
@@ -2002,7 +2008,7 @@ class Model extends AbstractModel_1.AbstractModel {
|
|
|
2002
2008
|
const c = this._columnPattern(String(column));
|
|
2003
2009
|
const values = array.length
|
|
2004
2010
|
? `${array.map((value) => this._checkValueHasRaw(this.$utils.escape(value))).join(',')}`
|
|
2005
|
-
: this.$constants('NULL');
|
|
2011
|
+
: this.$constants(this.$constants('NULL'));
|
|
2006
2012
|
this.$state.set('WHERE', [
|
|
2007
2013
|
...this.$state.get('WHERE'),
|
|
2008
2014
|
[
|
|
@@ -2163,9 +2169,9 @@ class Model extends AbstractModel_1.AbstractModel {
|
|
|
2163
2169
|
this.$state.get('WHERE').length ? `${this.$constants('AND')}` : '',
|
|
2164
2170
|
`${this.bindColumn(c)}`,
|
|
2165
2171
|
`${this.$constants('BETWEEN')}`,
|
|
2166
|
-
`${this.$constants('NULL')}`,
|
|
2172
|
+
`${this.$constants(this.$constants('NULL'))}`,
|
|
2167
2173
|
`${this.$constants('AND')}`,
|
|
2168
|
-
`${this.$constants('NULL')}`
|
|
2174
|
+
`${this.$constants(this.$constants('NULL'))}`
|
|
2169
2175
|
].join(' ')
|
|
2170
2176
|
]);
|
|
2171
2177
|
return this;
|
|
@@ -2201,9 +2207,9 @@ class Model extends AbstractModel_1.AbstractModel {
|
|
|
2201
2207
|
this.$state.get('WHERE').length ? `${this.$constants('OR')}` : '',
|
|
2202
2208
|
`${this.bindColumn(c)}`,
|
|
2203
2209
|
`${this.$constants('BETWEEN')}`,
|
|
2204
|
-
`${this.$constants('NULL')}`,
|
|
2210
|
+
`${this.$constants(this.$constants('NULL'))}`,
|
|
2205
2211
|
`${this.$constants('AND')}`,
|
|
2206
|
-
`${this.$constants('NULL')}`
|
|
2212
|
+
`${this.$constants(this.$constants('NULL'))}`
|
|
2207
2213
|
].join(' ')
|
|
2208
2214
|
]);
|
|
2209
2215
|
return this;
|
|
@@ -2239,9 +2245,9 @@ class Model extends AbstractModel_1.AbstractModel {
|
|
|
2239
2245
|
this.$state.get('WHERE').length ? `${this.$constants('AND')}` : '',
|
|
2240
2246
|
`${this.bindColumn(c)}`,
|
|
2241
2247
|
`${this.$constants('NOT_BETWEEN')}`,
|
|
2242
|
-
`${this.$constants('NULL')}`,
|
|
2248
|
+
`${this.$constants(this.$constants('NULL'))}`,
|
|
2243
2249
|
`${this.$constants('AND')}`,
|
|
2244
|
-
`${this.$constants('NULL')}`
|
|
2250
|
+
`${this.$constants(this.$constants('NULL'))}`
|
|
2245
2251
|
].join(' ')
|
|
2246
2252
|
]);
|
|
2247
2253
|
return this;
|
|
@@ -2277,9 +2283,9 @@ class Model extends AbstractModel_1.AbstractModel {
|
|
|
2277
2283
|
this.$state.get('WHERE').length ? `${this.$constants('OR')}` : '',
|
|
2278
2284
|
`${this.bindColumn(c)}`,
|
|
2279
2285
|
`${this.$constants('NOT_BETWEEN')}`,
|
|
2280
|
-
`${this.$constants('NULL')}`,
|
|
2286
|
+
`${this.$constants(this.$constants('NULL'))}`,
|
|
2281
2287
|
`${this.$constants('AND')}`,
|
|
2282
|
-
`${this.$constants('NULL')}`
|
|
2288
|
+
`${this.$constants(this.$constants('NULL'))}`
|
|
2283
2289
|
].join(' ')
|
|
2284
2290
|
]);
|
|
2285
2291
|
return this;
|
|
@@ -2520,7 +2526,9 @@ class Model extends AbstractModel_1.AbstractModel {
|
|
|
2520
2526
|
delete() {
|
|
2521
2527
|
return __awaiter(this, void 0, void 0, function* () {
|
|
2522
2528
|
var _a, _b;
|
|
2523
|
-
|
|
2529
|
+
if (!this.$state.get('WHERE').length) {
|
|
2530
|
+
throw this._assertError("The 'delete' method requires the use of 'where' conditions.");
|
|
2531
|
+
}
|
|
2524
2532
|
this.limit(1);
|
|
2525
2533
|
if (this.$state.get('SOFT_DELETE')) {
|
|
2526
2534
|
const deletedAt = this._valuePattern(this.$state.get('SOFT_DELETE_FORMAT'));
|
|
@@ -2554,7 +2562,9 @@ class Model extends AbstractModel_1.AbstractModel {
|
|
|
2554
2562
|
deleteMany() {
|
|
2555
2563
|
return __awaiter(this, void 0, void 0, function* () {
|
|
2556
2564
|
var _a, _b;
|
|
2557
|
-
|
|
2565
|
+
if (!this.$state.get('WHERE').length) {
|
|
2566
|
+
throw this._assertError("The 'deleteMany' method requires the use of 'where' conditions.");
|
|
2567
|
+
}
|
|
2558
2568
|
if (this.$state.get('SOFT_DELETE')) {
|
|
2559
2569
|
const deletedAt = this._valuePattern(this.$state.get('SOFT_DELETE_FORMAT'));
|
|
2560
2570
|
const sql = new Model()
|
|
@@ -2640,7 +2650,9 @@ class Model extends AbstractModel_1.AbstractModel {
|
|
|
2640
2650
|
sql = String((_b = this.$relation) === null || _b === void 0 ? void 0 : _b.loadExists());
|
|
2641
2651
|
if (cb) {
|
|
2642
2652
|
const callbackSql = cb(sql);
|
|
2643
|
-
|
|
2653
|
+
if (callbackSql == null || callbackSql === '') {
|
|
2654
|
+
throw this._assertError('Please provide a callback for execution');
|
|
2655
|
+
}
|
|
2644
2656
|
sql = callbackSql;
|
|
2645
2657
|
}
|
|
2646
2658
|
return yield this._execute({
|
|
@@ -2710,7 +2722,9 @@ class Model extends AbstractModel_1.AbstractModel {
|
|
|
2710
2722
|
sql = String((_b = this.$relation) === null || _b === void 0 ? void 0 : _b.loadExists());
|
|
2711
2723
|
if (cb) {
|
|
2712
2724
|
const callbackSql = cb(sql);
|
|
2713
|
-
|
|
2725
|
+
if (callbackSql == null || callbackSql === '') {
|
|
2726
|
+
throw this._assertError('Please provide a callback for execution');
|
|
2727
|
+
}
|
|
2714
2728
|
sql = callbackSql;
|
|
2715
2729
|
}
|
|
2716
2730
|
return yield this._execute({
|
|
@@ -2744,9 +2758,10 @@ class Model extends AbstractModel_1.AbstractModel {
|
|
|
2744
2758
|
let limit = 15;
|
|
2745
2759
|
let page = 1;
|
|
2746
2760
|
if (paginationOptions != null) {
|
|
2747
|
-
limit = (paginationOptions === null || paginationOptions === void 0 ? void 0 : paginationOptions.limit) || limit;
|
|
2761
|
+
limit = ((paginationOptions === null || paginationOptions === void 0 ? void 0 : paginationOptions.limit) || limit);
|
|
2748
2762
|
page = (paginationOptions === null || paginationOptions === void 0 ? void 0 : paginationOptions.page) || page;
|
|
2749
2763
|
}
|
|
2764
|
+
limit = limit > 1000 ? 1000 : limit;
|
|
2750
2765
|
if ((_a = this.$state.get('EXCEPTS')) === null || _a === void 0 ? void 0 : _a.length)
|
|
2751
2766
|
this.select(...yield this.exceptColumns());
|
|
2752
2767
|
const offset = (page - 1) * limit;
|
|
@@ -2818,6 +2833,16 @@ class Model extends AbstractModel_1.AbstractModel {
|
|
|
2818
2833
|
return this._resultHandler(resultData);
|
|
2819
2834
|
});
|
|
2820
2835
|
}
|
|
2836
|
+
/**
|
|
2837
|
+
* @override
|
|
2838
|
+
* @param {string} column
|
|
2839
|
+
* @return {Promise<array>} Array
|
|
2840
|
+
*/
|
|
2841
|
+
findGroupBy(column) {
|
|
2842
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
2843
|
+
return this.getGroupBy(column);
|
|
2844
|
+
});
|
|
2845
|
+
}
|
|
2821
2846
|
/**
|
|
2822
2847
|
* @override
|
|
2823
2848
|
* @param {object} data for insert
|
|
@@ -2984,7 +3009,7 @@ class Model extends AbstractModel_1.AbstractModel {
|
|
|
2984
3009
|
*/
|
|
2985
3010
|
createOrSelect(data) {
|
|
2986
3011
|
if (!Object.keys(data).length)
|
|
2987
|
-
throw
|
|
3012
|
+
throw this._assertError('This method must be required');
|
|
2988
3013
|
this.$state.set('DATA', data);
|
|
2989
3014
|
const queryInsert = this._queryInsertModel(data);
|
|
2990
3015
|
this.$state.set('INSERT', [
|
|
@@ -3035,7 +3060,8 @@ class Model extends AbstractModel_1.AbstractModel {
|
|
|
3035
3060
|
* @return {this} this
|
|
3036
3061
|
*/
|
|
3037
3062
|
createNotExists(data) {
|
|
3038
|
-
|
|
3063
|
+
if (!Object.keys(data).length)
|
|
3064
|
+
throw this._assertError('This method must be required');
|
|
3039
3065
|
this.$state.set('DATA', data);
|
|
3040
3066
|
const query = this._queryInsertModel(data);
|
|
3041
3067
|
this.$state.set('INSERT', [
|
|
@@ -3064,8 +3090,9 @@ class Model extends AbstractModel_1.AbstractModel {
|
|
|
3064
3090
|
* @return {this} this
|
|
3065
3091
|
*/
|
|
3066
3092
|
updateMultiple(cases) {
|
|
3067
|
-
if (!cases.length)
|
|
3068
|
-
this._assertError(`The method 'updateMultiple' array must not be empty.`);
|
|
3093
|
+
if (!cases.length) {
|
|
3094
|
+
throw this._assertError(`The method 'updateMultiple' array must not be empty.`);
|
|
3095
|
+
}
|
|
3069
3096
|
this.limit(cases.length);
|
|
3070
3097
|
const updateColumns = cases.reduce((columns, item) => {
|
|
3071
3098
|
return (item.columns && Object.keys(item.columns).forEach(key => columns[key] = [
|
|
@@ -3090,10 +3117,12 @@ class Model extends AbstractModel_1.AbstractModel {
|
|
|
3090
3117
|
}
|
|
3091
3118
|
for (let i = cases.length - 1; i >= 0; i--) {
|
|
3092
3119
|
const c = cases[i];
|
|
3093
|
-
if (c.when == null || !Object.keys(c.when).length)
|
|
3094
|
-
this._assertError(`This 'when' property is missing some properties
|
|
3095
|
-
|
|
3096
|
-
|
|
3120
|
+
if (c.when == null || !Object.keys(c.when).length) {
|
|
3121
|
+
throw this._assertError(`This 'when' property is missing some properties.`);
|
|
3122
|
+
}
|
|
3123
|
+
if (c.columns == null || !Object.keys(c.columns).length) {
|
|
3124
|
+
throw this._assertError(`This 'columns' property is missing some properties.`);
|
|
3125
|
+
}
|
|
3097
3126
|
const when = Object.entries(c.when).map(([key, value]) => {
|
|
3098
3127
|
value = this.$utils.escape(value);
|
|
3099
3128
|
value = this._valueTrueFalse(value);
|
|
@@ -3120,8 +3149,8 @@ class Model extends AbstractModel_1.AbstractModel {
|
|
|
3120
3149
|
if (typeof value === 'string' && !(value.includes(this.$constants('RAW')))) {
|
|
3121
3150
|
value = this.$utils.escapeActions(value);
|
|
3122
3151
|
}
|
|
3123
|
-
return `${this.bindColumn(column)} = ${value == null || value === 'NULL'
|
|
3124
|
-
? 'NULL'
|
|
3152
|
+
return `${this.bindColumn(column)} = ${value == null || value === this.$constants('NULL')
|
|
3153
|
+
? this.$constants('NULL')
|
|
3125
3154
|
: this._checkValueHasRaw(value)}`;
|
|
3126
3155
|
});
|
|
3127
3156
|
const query = `${this.$constants('SET')} ${keyValue.join(', ')}`;
|
|
@@ -3134,11 +3163,20 @@ class Model extends AbstractModel_1.AbstractModel {
|
|
|
3134
3163
|
this.$state.set('SAVE', 'UPDATE');
|
|
3135
3164
|
return this;
|
|
3136
3165
|
}
|
|
3166
|
+
/**
|
|
3167
|
+
* The 'getSchemaModel' method is used get a schema model
|
|
3168
|
+
* @return {Record<string, Blueprint> | null} Record<string, Blueprint> | null
|
|
3169
|
+
*/
|
|
3137
3170
|
getSchemaModel() {
|
|
3138
3171
|
if (this.$schema == null)
|
|
3139
3172
|
return this.$state.get('SCHEMA_TABLE');
|
|
3140
3173
|
return this.$schema;
|
|
3141
3174
|
}
|
|
3175
|
+
/**
|
|
3176
|
+
* The 'validation' method is used validate the column by validating
|
|
3177
|
+
* @param {ValidateSchema} schema
|
|
3178
|
+
* @return {this} this
|
|
3179
|
+
*/
|
|
3142
3180
|
validation(schema) {
|
|
3143
3181
|
this.$state.set('VALIDATE_SCHEMA', true);
|
|
3144
3182
|
this.$state.set('VALIDATE_SCHEMA_DEFINED', schema);
|
|
@@ -3196,7 +3234,9 @@ class Model extends AbstractModel_1.AbstractModel {
|
|
|
3196
3234
|
};
|
|
3197
3235
|
});
|
|
3198
3236
|
for (let row = 0; row < rows; row++) {
|
|
3199
|
-
|
|
3237
|
+
if (this.$state.get('TABLE_NAME') === '' || this.$state.get('TABLE_NAME') == null) {
|
|
3238
|
+
throw this._assertError("Unknow this table.");
|
|
3239
|
+
}
|
|
3200
3240
|
let columnAndValue = {};
|
|
3201
3241
|
for (const { Field: field, Type: type } of fields) {
|
|
3202
3242
|
const deletedAt = this._valuePattern(this.$state.get('SOFT_DELETE_FORMAT'));
|
|
@@ -3231,7 +3271,7 @@ class Model extends AbstractModel_1.AbstractModel {
|
|
|
3231
3271
|
const existsTables = checkTables.map((c) => Object.values(c)[0]);
|
|
3232
3272
|
const schemaModel = this.getSchemaModel();
|
|
3233
3273
|
if (schemaModel == null)
|
|
3234
|
-
|
|
3274
|
+
throw this._assertError(schemaModel == null, 'Schema model not found');
|
|
3235
3275
|
const checkTableIsExists = existsTables.some((table) => table === this.getTableName());
|
|
3236
3276
|
const syncForeignKey = (_j) => __awaiter(this, [_j], void 0, function* ({ schemaModel, model }) {
|
|
3237
3277
|
var _k;
|
|
@@ -3492,7 +3532,7 @@ class Model extends AbstractModel_1.AbstractModel {
|
|
|
3492
3532
|
return;
|
|
3493
3533
|
const schemaTable = this.getSchemaModel();
|
|
3494
3534
|
if (schemaTable == null) {
|
|
3495
|
-
|
|
3535
|
+
throw this._assertError(`This method "validateSchema" isn't validation without schema. Please use the method "useSchema" for define your schema.`);
|
|
3496
3536
|
}
|
|
3497
3537
|
const schemaTableDefined = this.$state.get('VALIDATE_SCHEMA_DEFINED');
|
|
3498
3538
|
const schema = schemaTableDefined !== null && schemaTableDefined !== void 0 ? schemaTableDefined : Object.keys(schemaTable).reduce((acc, key) => {
|
|
@@ -3515,54 +3555,61 @@ class Model extends AbstractModel_1.AbstractModel {
|
|
|
3515
3555
|
if (regexDate.test(r) || regexDateTime.test(r)) {
|
|
3516
3556
|
if (typeOf(new Date(r)) === typeOf(new s()))
|
|
3517
3557
|
continue;
|
|
3518
|
-
this._assertError(`This column "${column}" is must be type "${typeOf(new s())}"
|
|
3558
|
+
throw this._assertError(`This column "${column}" is must be type "${typeOf(new s())}".`);
|
|
3519
3559
|
}
|
|
3520
3560
|
if (typeOf(r) === typeOf(new s()))
|
|
3521
3561
|
continue;
|
|
3522
|
-
this._assertError(`This column "${column}" is must be type "${typeOf(new s())}"
|
|
3523
|
-
|
|
3562
|
+
throw this._assertError(`This column "${column}" is must be type "${typeOf(new s())}".`);
|
|
3563
|
+
}
|
|
3564
|
+
if ((s.require && action === 'insert') && (r === '' || r == null)) {
|
|
3565
|
+
throw this._assertError(`This column "${column}" is required.`);
|
|
3524
3566
|
}
|
|
3525
|
-
if (s.require && action === 'insert')
|
|
3526
|
-
this._assertError(r === '' || r == null, `This column "${column}" is required`);
|
|
3527
3567
|
if (r == null)
|
|
3528
3568
|
continue;
|
|
3529
|
-
|
|
3530
|
-
|
|
3569
|
+
if ((regexDate.test(r) || regexDateTime.test(r)) && typeOf(new Date(r)) !== typeOf(new s.type())) {
|
|
3570
|
+
throw this._assertError(`This column "${column}" is must be type "${typeOf(new s.type())}".`);
|
|
3571
|
+
}
|
|
3572
|
+
if (typeOf(r) !== typeOf(new s.type())) {
|
|
3573
|
+
throw this._assertError(`This column "${column}" is must be type "${typeOf(new s.type())}".`);
|
|
3574
|
+
}
|
|
3531
3575
|
if (s.json) {
|
|
3532
3576
|
try {
|
|
3533
3577
|
JSON.parse(r);
|
|
3534
3578
|
}
|
|
3535
3579
|
catch (_) {
|
|
3536
|
-
this._assertError(`This column "${column}" is must be JSON
|
|
3580
|
+
throw this._assertError(`This column "${column}" is must be JSON.`);
|
|
3537
3581
|
}
|
|
3538
3582
|
}
|
|
3539
|
-
if (s.length) {
|
|
3540
|
-
|
|
3583
|
+
if (s.length && (`${r}`.length > s.length)) {
|
|
3584
|
+
throw this._assertError(`This column "${column}" is more than "${s.length}" length of characters.`);
|
|
3541
3585
|
}
|
|
3542
|
-
if (s.maxLength) {
|
|
3543
|
-
|
|
3586
|
+
if (s.maxLength && (`${r}`.length > s.maxLength)) {
|
|
3587
|
+
throw this._assertError(`This column "${column}" is more than "${s.maxLength}" length of characters.`);
|
|
3544
3588
|
}
|
|
3545
|
-
if (s.minLength) {
|
|
3546
|
-
|
|
3589
|
+
if (s.minLength && (`${r}`.length < s.minLength)) {
|
|
3590
|
+
throw this._assertError(`This column "${column}" is less than "${s.minLength}" length of characters`);
|
|
3547
3591
|
}
|
|
3548
|
-
if (s.max)
|
|
3549
|
-
|
|
3550
|
-
if (s.min)
|
|
3551
|
-
|
|
3552
|
-
if (s.enum && s.enum.length) {
|
|
3553
|
-
|
|
3592
|
+
if (s.max && r > s.max)
|
|
3593
|
+
throw this._assertError(`This column "${column}" is more than "${s.max}"`);
|
|
3594
|
+
if (s.min && r < s.min)
|
|
3595
|
+
throw this._assertError(`This column "${column}" is less than "${s.min}"`);
|
|
3596
|
+
if ((s.enum && s.enum.length) && !s.enum.some((e) => e === r)) {
|
|
3597
|
+
throw this._assertError(`This column "${column}" is must be in ${s.enum.map((e) => `"${e}"`)}`);
|
|
3598
|
+
}
|
|
3599
|
+
if (s.match && !s.match.test(r)) {
|
|
3600
|
+
throw this._assertError(`This column "${column}" is not match a regular expression`);
|
|
3601
|
+
}
|
|
3602
|
+
if (s.fn && !(yield s.fn(r))) {
|
|
3603
|
+
throw this._assertError(`This column "${column}" is not valid with function`);
|
|
3554
3604
|
}
|
|
3555
|
-
if (s.match)
|
|
3556
|
-
return this._assertError(!s.match.test(r), `This column "${column}" is not match a regular expression`);
|
|
3557
|
-
if (s.fn)
|
|
3558
|
-
return this._assertError(!(yield s.fn(r)), `This column "${column}" is not valid with function`);
|
|
3559
3605
|
if (s.unique && action === 'insert') {
|
|
3560
3606
|
const exist = yield new Model()
|
|
3561
3607
|
.copyModel(this, { select: true, where: true, limit: true })
|
|
3562
3608
|
.where(column, r)
|
|
3563
3609
|
.debug(this.$state.get('DEBUG'))
|
|
3564
3610
|
.exists();
|
|
3565
|
-
|
|
3611
|
+
if (exist)
|
|
3612
|
+
throw this._assertError(`This column "${column}" is duplicated`);
|
|
3566
3613
|
}
|
|
3567
3614
|
}
|
|
3568
3615
|
return;
|
|
@@ -3605,12 +3652,14 @@ class Model extends AbstractModel_1.AbstractModel {
|
|
|
3605
3652
|
var _a;
|
|
3606
3653
|
const currentPage = +(this.$state.get('PAGE'));
|
|
3607
3654
|
const limit = Number(this.$state.get('PER_PAGE'));
|
|
3608
|
-
|
|
3655
|
+
if (limit < 1) {
|
|
3656
|
+
throw this._assertError("This pagination needed limit minimun less 1 for limit");
|
|
3657
|
+
}
|
|
3609
3658
|
const total = yield new Model()
|
|
3610
|
-
.copyModel(this, { where: true })
|
|
3659
|
+
.copyModel(this, { where: true, join: true })
|
|
3611
3660
|
.bind(this.$pool.get())
|
|
3612
3661
|
.debug(this.$state.get('DEBUG'))
|
|
3613
|
-
.count();
|
|
3662
|
+
.count(this.$state.get('PRIMARY_KEY'));
|
|
3614
3663
|
let lastPage = Math.ceil(total / limit) || 0;
|
|
3615
3664
|
lastPage = lastPage > 1 ? lastPage : 1;
|
|
3616
3665
|
const nextPage = currentPage + 1;
|
|
@@ -3690,7 +3739,7 @@ class Model extends AbstractModel_1.AbstractModel {
|
|
|
3690
3739
|
};
|
|
3691
3740
|
break;
|
|
3692
3741
|
}
|
|
3693
|
-
default: this._assertError('Missing method first get or pagination');
|
|
3742
|
+
default: throw this._assertError('Missing method first get or pagination');
|
|
3694
3743
|
}
|
|
3695
3744
|
if (this._isPatternSnakeCase()) {
|
|
3696
3745
|
const empty = this.$utils.snakeCase(this._resultHandler(emptyData));
|
|
@@ -3762,7 +3811,9 @@ class Model extends AbstractModel_1.AbstractModel {
|
|
|
3762
3811
|
const pluck = this.$state.get('PLUCK');
|
|
3763
3812
|
const newData = res[0];
|
|
3764
3813
|
const checkProperty = newData.hasOwnProperty(pluck);
|
|
3765
|
-
|
|
3814
|
+
if (!checkProperty) {
|
|
3815
|
+
this._assertError(`Can't find property '${pluck}' of results.`);
|
|
3816
|
+
}
|
|
3766
3817
|
result = this._resultHandler(newData[pluck]);
|
|
3767
3818
|
break;
|
|
3768
3819
|
}
|
|
@@ -3781,7 +3832,9 @@ class Model extends AbstractModel_1.AbstractModel {
|
|
|
3781
3832
|
const pluck = this.$state.get('PLUCK');
|
|
3782
3833
|
const newData = res[0];
|
|
3783
3834
|
const checkProperty = newData.hasOwnProperty(pluck);
|
|
3784
|
-
|
|
3835
|
+
if (!checkProperty) {
|
|
3836
|
+
throw this._assertError(`Can't find property '${pluck}' of results`);
|
|
3837
|
+
}
|
|
3785
3838
|
result = (_d = this._resultHandler(newData[pluck])) !== null && _d !== void 0 ? _d : null;
|
|
3786
3839
|
break;
|
|
3787
3840
|
}
|
|
@@ -3810,7 +3863,9 @@ class Model extends AbstractModel_1.AbstractModel {
|
|
|
3810
3863
|
if (this.$state.get('PLUCK')) {
|
|
3811
3864
|
const pluck = this.$state.get('PLUCK');
|
|
3812
3865
|
const newData = data.map((d) => d[pluck]);
|
|
3813
|
-
|
|
3866
|
+
if (newData.every((d) => d == null)) {
|
|
3867
|
+
throw this._assertError(`Can't find property '${pluck}' of results.`);
|
|
3868
|
+
}
|
|
3814
3869
|
result = this._resultHandler(newData);
|
|
3815
3870
|
break;
|
|
3816
3871
|
}
|
|
@@ -3849,9 +3904,13 @@ class Model extends AbstractModel_1.AbstractModel {
|
|
|
3849
3904
|
_attach(name, dataId, fields) {
|
|
3850
3905
|
return __awaiter(this, void 0, void 0, function* () {
|
|
3851
3906
|
var _a;
|
|
3852
|
-
|
|
3907
|
+
if (!Array.isArray(dataId)) {
|
|
3908
|
+
throw this._assertError(`This '${dataId}' is not an array.`);
|
|
3909
|
+
}
|
|
3853
3910
|
const relation = (_a = this.$state.get('RELATION')) === null || _a === void 0 ? void 0 : _a.find((data) => data.name === name);
|
|
3854
|
-
|
|
3911
|
+
if (!relation) {
|
|
3912
|
+
throw this._assertError(`Unknown relation '${name}' in model.`);
|
|
3913
|
+
}
|
|
3855
3914
|
const thisTable = this.$utils.columnRelation(this.constructor.name);
|
|
3856
3915
|
const relationTable = this._classToTableName(relation.model.name, { singular: true });
|
|
3857
3916
|
const result = this.$state.get('RESULT');
|
|
@@ -3882,9 +3941,13 @@ class Model extends AbstractModel_1.AbstractModel {
|
|
|
3882
3941
|
}
|
|
3883
3942
|
_detach(name, dataId) {
|
|
3884
3943
|
return __awaiter(this, void 0, void 0, function* () {
|
|
3885
|
-
|
|
3944
|
+
if (!Array.isArray(dataId)) {
|
|
3945
|
+
throw this._assertError(`This '${dataId}' is not an array.`);
|
|
3946
|
+
}
|
|
3886
3947
|
const relation = this.$state.get('RELATION').find((data) => data.name === name);
|
|
3887
|
-
|
|
3948
|
+
if (!relation) {
|
|
3949
|
+
throw this._assertError(`Unknown relation '${name}' in model.`);
|
|
3950
|
+
}
|
|
3888
3951
|
const thisTable = this.$utils.columnRelation(this.constructor.name);
|
|
3889
3952
|
const relationTable = this._classToTableName(relation.model.name, { singular: true });
|
|
3890
3953
|
const result = this.$state.get('RESULT');
|
|
@@ -3929,8 +3992,8 @@ class Model extends AbstractModel_1.AbstractModel {
|
|
|
3929
3992
|
if (typeof value === 'string' && !(value.includes(this.$constants('RAW')))) {
|
|
3930
3993
|
value = this.$utils.escapeActions(value);
|
|
3931
3994
|
}
|
|
3932
|
-
return `${this.bindColumn(column)} = ${value == null || value === 'NULL'
|
|
3933
|
-
? 'NULL'
|
|
3995
|
+
return `${this.bindColumn(column)} = ${value == null || value === this.$constants('NULL')
|
|
3996
|
+
? this.$constants('NULL')
|
|
3934
3997
|
: this._checkValueHasRaw(value)}`;
|
|
3935
3998
|
});
|
|
3936
3999
|
return `${this.$constants('SET')} ${keyValue.join(', ')}`;
|
|
@@ -3954,8 +4017,8 @@ class Model extends AbstractModel_1.AbstractModel {
|
|
|
3954
4017
|
if (typeof value === 'string' && !(value.includes(this.$constants('RAW')))) {
|
|
3955
4018
|
value = this.$utils.escapeActions(value);
|
|
3956
4019
|
}
|
|
3957
|
-
return `${value == null || value === 'NULL'
|
|
3958
|
-
? 'NULL'
|
|
4020
|
+
return `${value == null || value === this.$constants('NULL')
|
|
4021
|
+
? this.$constants('NULL')
|
|
3959
4022
|
: this._checkValueHasRaw(value)}`;
|
|
3960
4023
|
});
|
|
3961
4024
|
const sql = [
|
|
@@ -3996,8 +4059,8 @@ class Model extends AbstractModel_1.AbstractModel {
|
|
|
3996
4059
|
if (typeof value === 'string' && !(value.includes(this.$constants('RAW')))) {
|
|
3997
4060
|
value = this.$utils.escapeActions(value);
|
|
3998
4061
|
}
|
|
3999
|
-
return `${value == null || value === 'NULL'
|
|
4000
|
-
? 'NULL'
|
|
4062
|
+
return `${value == null || value === this.$constants('NULL')
|
|
4063
|
+
? this.$constants('NULL')
|
|
4001
4064
|
: this._checkValueHasRaw(value)}`;
|
|
4002
4065
|
});
|
|
4003
4066
|
values = [
|
|
@@ -4013,7 +4076,9 @@ class Model extends AbstractModel_1.AbstractModel {
|
|
|
4013
4076
|
}
|
|
4014
4077
|
_insertNotExistsModel() {
|
|
4015
4078
|
return __awaiter(this, void 0, void 0, function* () {
|
|
4016
|
-
|
|
4079
|
+
if (!this.$state.get('WHERE').length) {
|
|
4080
|
+
throw this._assertError("The 'createNotExists' method requires the use of 'where' conditions.");
|
|
4081
|
+
}
|
|
4017
4082
|
const check = (yield new Model()
|
|
4018
4083
|
.copyModel(this, { where: true, select: true, limit: true })
|
|
4019
4084
|
.bind(this.$pool.get())
|
|
@@ -4262,13 +4327,17 @@ class Model extends AbstractModel_1.AbstractModel {
|
|
|
4262
4327
|
const methodCallings = this.$logger.get();
|
|
4263
4328
|
const methodsNotAllowed = methodChangeStatements;
|
|
4264
4329
|
const findMethodNotAllowed = methodCallings.find((methodCalling) => methodsNotAllowed.includes(methodCalling));
|
|
4265
|
-
|
|
4330
|
+
if (methodCallings.some((methodCalling) => methodsNotAllowed.includes(methodCalling))) {
|
|
4331
|
+
throw this._assertError(`This method '${method}' can't using the method '${findMethodNotAllowed}'.`);
|
|
4332
|
+
}
|
|
4266
4333
|
break;
|
|
4267
4334
|
}
|
|
4268
4335
|
case 'save': {
|
|
4269
4336
|
const methodCallings = this.$logger.get();
|
|
4270
4337
|
const methodsSomeAllowed = methodChangeStatements;
|
|
4271
|
-
|
|
4338
|
+
if (!methodCallings.some((methodCalling) => methodsSomeAllowed.includes(methodCalling))) {
|
|
4339
|
+
throw this._assertError(`This ${method} method need some ${methodsSomeAllowed.map(v => `'${v}'`).join(', ')} methods.`);
|
|
4340
|
+
}
|
|
4272
4341
|
break;
|
|
4273
4342
|
}
|
|
4274
4343
|
}
|