tspace-mysql 1.5.2 → 1.5.4
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 +8 -8
- package/build/lib/Interface.d.ts +2 -2
- package/build/lib/connection/options.js +1 -1
- package/build/lib/constants/index.d.ts +1 -3
- package/build/lib/constants/index.js +0 -96
- package/build/lib/core/Abstracts/AbstractBuilder.d.ts +2 -2
- package/build/lib/core/Abstracts/AbstractBuilder.js +1 -4
- package/build/lib/core/Builder.d.ts +5 -3
- package/build/lib/core/Builder.js +91 -69
- package/build/lib/core/DB.js +1 -1
- package/build/lib/core/Handlers/State.d.ts +2 -2
- package/build/lib/core/Handlers/State.js +126 -12
- package/build/lib/core/Model.d.ts +97 -76
- package/build/lib/core/Model.js +208 -93
- package/build/lib/core/Type.d.ts +55 -1
- package/package.json +1 -1
package/build/lib/core/Model.js
CHANGED
|
@@ -18,9 +18,9 @@ const DB_1 = require("./DB");
|
|
|
18
18
|
const Schema_1 = require("./Schema");
|
|
19
19
|
const AbstractModel_1 = require("./Abstracts/AbstractModel");
|
|
20
20
|
const Proxy_1 = require("./Handlers/Proxy");
|
|
21
|
-
const State_1 = require("./Handlers/State");
|
|
22
21
|
const Relation_1 = require("./Handlers/Relation");
|
|
23
22
|
const Blueprint_1 = require("./Blueprint");
|
|
23
|
+
const State_1 = require("./Handlers/State");
|
|
24
24
|
let globalSettings = {
|
|
25
25
|
softDelete: false,
|
|
26
26
|
uuid: false,
|
|
@@ -310,7 +310,9 @@ class Model extends AbstractModel_1.AbstractModel {
|
|
|
310
310
|
this.$constants('PATTERN').snake_case,
|
|
311
311
|
this.$constants('PATTERN').camelCase
|
|
312
312
|
];
|
|
313
|
-
|
|
313
|
+
if (!allowPattern.includes(pattern)) {
|
|
314
|
+
throw this._assertError(`The 'tspace-mysql' support only pattern '${this.$constants('PATTERN').snake_case}', '${this.$constants('PATTERN').camelCase}'`);
|
|
315
|
+
}
|
|
314
316
|
this.$state.set('PATTERN', pattern);
|
|
315
317
|
this._makeTableName();
|
|
316
318
|
return this;
|
|
@@ -559,7 +561,7 @@ class Model extends AbstractModel_1.AbstractModel {
|
|
|
559
561
|
*/
|
|
560
562
|
beforeCreatingTable(fn) {
|
|
561
563
|
if (!(fn instanceof Function))
|
|
562
|
-
this._assertError(`This '${fn}' is not a function
|
|
564
|
+
throw this._assertError(`This '${fn}' is not a function.`);
|
|
563
565
|
this.$state.set('BEFORE_CREATING_TABLE', fn);
|
|
564
566
|
return this;
|
|
565
567
|
}
|
|
@@ -631,9 +633,13 @@ class Model extends AbstractModel_1.AbstractModel {
|
|
|
631
633
|
var _a, _b;
|
|
632
634
|
this.relations(name);
|
|
633
635
|
const relation = this.$state.get('RELATIONS').find((data) => data.name === name);
|
|
634
|
-
|
|
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
|
+
}
|
|
635
639
|
const relationHasExists = (_b = Object.values(this.$constants('RELATIONSHIP'))) === null || _b === void 0 ? void 0 : _b.includes(relation.relation);
|
|
636
|
-
|
|
640
|
+
if (!relationHasExists) {
|
|
641
|
+
throw this._assertError(`Unknown relationship in '${this.$constants('RELATIONSHIP')}'.`);
|
|
642
|
+
}
|
|
637
643
|
if (callback == null) {
|
|
638
644
|
relation.query = new relation.model();
|
|
639
645
|
return this;
|
|
@@ -788,13 +794,18 @@ class Model extends AbstractModel_1.AbstractModel {
|
|
|
788
794
|
groupBy(...columns) {
|
|
789
795
|
let groupBy = 'id';
|
|
790
796
|
if (columns === null || columns === void 0 ? void 0 : columns.length) {
|
|
791
|
-
groupBy = columns.map(
|
|
792
|
-
if (
|
|
793
|
-
return
|
|
794
|
-
|
|
797
|
+
groupBy = columns.map(c => {
|
|
798
|
+
if (/\./.test(c))
|
|
799
|
+
return this.bindColumn(c);
|
|
800
|
+
if (c.includes(this.$constants('RAW')))
|
|
801
|
+
return c === null || c === void 0 ? void 0 : c.replace(this.$constants('RAW'), '');
|
|
802
|
+
return `\`${c}\``;
|
|
795
803
|
}).join(', ');
|
|
796
804
|
}
|
|
797
|
-
this.$state.set('GROUP_BY',
|
|
805
|
+
this.$state.set('GROUP_BY', [
|
|
806
|
+
...this.$state.get('GROUP_BY'),
|
|
807
|
+
`${groupBy}`
|
|
808
|
+
]);
|
|
798
809
|
return this;
|
|
799
810
|
}
|
|
800
811
|
/**
|
|
@@ -966,7 +977,9 @@ class Model extends AbstractModel_1.AbstractModel {
|
|
|
966
977
|
* @return {Model} Model
|
|
967
978
|
*/
|
|
968
979
|
copyModel(instance, options) {
|
|
969
|
-
|
|
980
|
+
if (!(instance instanceof Model)) {
|
|
981
|
+
throw this._assertError('This instance is not an instanceof Model.');
|
|
982
|
+
}
|
|
970
983
|
const copy = Object.fromEntries(instance.$state.get());
|
|
971
984
|
const newInstance = new Model();
|
|
972
985
|
newInstance.$state.clone(copy);
|
|
@@ -1839,13 +1852,19 @@ class Model extends AbstractModel_1.AbstractModel {
|
|
|
1839
1852
|
* @return {this} this
|
|
1840
1853
|
*/
|
|
1841
1854
|
where(column, operator, value) {
|
|
1842
|
-
if (typeof column === 'object'
|
|
1855
|
+
if (typeof column === 'object') {
|
|
1843
1856
|
return this.whereObject(column);
|
|
1844
1857
|
}
|
|
1845
1858
|
const c = this._columnPattern(String(column));
|
|
1846
1859
|
[value, operator] = this._valueAndOperator(value, operator, arguments.length === 2);
|
|
1847
1860
|
value = this.$utils.escape(value);
|
|
1848
1861
|
value = this._valueTrueFalse(value);
|
|
1862
|
+
if (value === null) {
|
|
1863
|
+
return this.whereNull(column);
|
|
1864
|
+
}
|
|
1865
|
+
if (Array.isArray(value)) {
|
|
1866
|
+
return this.whereIn(column, value);
|
|
1867
|
+
}
|
|
1849
1868
|
this.$state.set('WHERE', [
|
|
1850
1869
|
...this.$state.get('WHERE'),
|
|
1851
1870
|
[
|
|
@@ -1869,6 +1888,12 @@ class Model extends AbstractModel_1.AbstractModel {
|
|
|
1869
1888
|
const c = this._columnPattern(String(column));
|
|
1870
1889
|
value = this.$utils.escape(value);
|
|
1871
1890
|
value = this._valueTrueFalse(value);
|
|
1891
|
+
if (value === null) {
|
|
1892
|
+
return this.orWhereNull(column);
|
|
1893
|
+
}
|
|
1894
|
+
if (Array.isArray(value)) {
|
|
1895
|
+
return this.orWhereIn(column, value);
|
|
1896
|
+
}
|
|
1872
1897
|
this.$state.set('WHERE', [
|
|
1873
1898
|
...this.$state.get('WHERE'),
|
|
1874
1899
|
[
|
|
@@ -1887,14 +1912,18 @@ class Model extends AbstractModel_1.AbstractModel {
|
|
|
1887
1912
|
*/
|
|
1888
1913
|
whereObject(columns) {
|
|
1889
1914
|
for (let column in columns) {
|
|
1890
|
-
column = this._columnPattern(String(column));
|
|
1891
1915
|
const operator = '=';
|
|
1892
1916
|
const value = this.$utils.escape(columns[column]);
|
|
1917
|
+
if (value === null) {
|
|
1918
|
+
this.whereNull(column);
|
|
1919
|
+
continue;
|
|
1920
|
+
}
|
|
1921
|
+
const c = this._columnPattern(String(column));
|
|
1893
1922
|
this.$state.set('WHERE', [
|
|
1894
1923
|
...this.$state.get('WHERE'),
|
|
1895
1924
|
[
|
|
1896
1925
|
this.$state.get('WHERE').length ? `${this.$constants('AND')}` : '',
|
|
1897
|
-
`${this.bindColumn(String(
|
|
1926
|
+
`${this.bindColumn(String(c))}`,
|
|
1898
1927
|
`${operator}`,
|
|
1899
1928
|
`${this._checkValueHasRaw(value)}`
|
|
1900
1929
|
].join(' ')
|
|
@@ -1955,7 +1984,7 @@ class Model extends AbstractModel_1.AbstractModel {
|
|
|
1955
1984
|
const c = this._columnPattern(String(column));
|
|
1956
1985
|
const values = array.length
|
|
1957
1986
|
? `${array.map((value) => this._checkValueHasRaw(this.$utils.escape(value))).join(',')}`
|
|
1958
|
-
: this.$constants('NULL');
|
|
1987
|
+
: this.$constants(this.$constants('NULL'));
|
|
1959
1988
|
this.$state.set('WHERE', [
|
|
1960
1989
|
...this.$state.get('WHERE'),
|
|
1961
1990
|
[
|
|
@@ -1979,7 +2008,7 @@ class Model extends AbstractModel_1.AbstractModel {
|
|
|
1979
2008
|
const c = this._columnPattern(String(column));
|
|
1980
2009
|
const values = array.length
|
|
1981
2010
|
? `${array.map((value) => this._checkValueHasRaw(this.$utils.escape(value))).join(',')}`
|
|
1982
|
-
: this.$constants('NULL');
|
|
2011
|
+
: this.$constants(this.$constants('NULL'));
|
|
1983
2012
|
this.$state.set('WHERE', [
|
|
1984
2013
|
...this.$state.get('WHERE'),
|
|
1985
2014
|
[
|
|
@@ -2140,9 +2169,9 @@ class Model extends AbstractModel_1.AbstractModel {
|
|
|
2140
2169
|
this.$state.get('WHERE').length ? `${this.$constants('AND')}` : '',
|
|
2141
2170
|
`${this.bindColumn(c)}`,
|
|
2142
2171
|
`${this.$constants('BETWEEN')}`,
|
|
2143
|
-
`${this.$constants('NULL')}`,
|
|
2172
|
+
`${this.$constants(this.$constants('NULL'))}`,
|
|
2144
2173
|
`${this.$constants('AND')}`,
|
|
2145
|
-
`${this.$constants('NULL')}`
|
|
2174
|
+
`${this.$constants(this.$constants('NULL'))}`
|
|
2146
2175
|
].join(' ')
|
|
2147
2176
|
]);
|
|
2148
2177
|
return this;
|
|
@@ -2178,9 +2207,9 @@ class Model extends AbstractModel_1.AbstractModel {
|
|
|
2178
2207
|
this.$state.get('WHERE').length ? `${this.$constants('OR')}` : '',
|
|
2179
2208
|
`${this.bindColumn(c)}`,
|
|
2180
2209
|
`${this.$constants('BETWEEN')}`,
|
|
2181
|
-
`${this.$constants('NULL')}`,
|
|
2210
|
+
`${this.$constants(this.$constants('NULL'))}`,
|
|
2182
2211
|
`${this.$constants('AND')}`,
|
|
2183
|
-
`${this.$constants('NULL')}`
|
|
2212
|
+
`${this.$constants(this.$constants('NULL'))}`
|
|
2184
2213
|
].join(' ')
|
|
2185
2214
|
]);
|
|
2186
2215
|
return this;
|
|
@@ -2216,9 +2245,9 @@ class Model extends AbstractModel_1.AbstractModel {
|
|
|
2216
2245
|
this.$state.get('WHERE').length ? `${this.$constants('AND')}` : '',
|
|
2217
2246
|
`${this.bindColumn(c)}`,
|
|
2218
2247
|
`${this.$constants('NOT_BETWEEN')}`,
|
|
2219
|
-
`${this.$constants('NULL')}`,
|
|
2248
|
+
`${this.$constants(this.$constants('NULL'))}`,
|
|
2220
2249
|
`${this.$constants('AND')}`,
|
|
2221
|
-
`${this.$constants('NULL')}`
|
|
2250
|
+
`${this.$constants(this.$constants('NULL'))}`
|
|
2222
2251
|
].join(' ')
|
|
2223
2252
|
]);
|
|
2224
2253
|
return this;
|
|
@@ -2254,9 +2283,9 @@ class Model extends AbstractModel_1.AbstractModel {
|
|
|
2254
2283
|
this.$state.get('WHERE').length ? `${this.$constants('OR')}` : '',
|
|
2255
2284
|
`${this.bindColumn(c)}`,
|
|
2256
2285
|
`${this.$constants('NOT_BETWEEN')}`,
|
|
2257
|
-
`${this.$constants('NULL')}`,
|
|
2286
|
+
`${this.$constants(this.$constants('NULL'))}`,
|
|
2258
2287
|
`${this.$constants('AND')}`,
|
|
2259
|
-
`${this.$constants('NULL')}`
|
|
2288
|
+
`${this.$constants(this.$constants('NULL'))}`
|
|
2260
2289
|
].join(' ')
|
|
2261
2290
|
]);
|
|
2262
2291
|
return this;
|
|
@@ -2497,7 +2526,9 @@ class Model extends AbstractModel_1.AbstractModel {
|
|
|
2497
2526
|
delete() {
|
|
2498
2527
|
return __awaiter(this, void 0, void 0, function* () {
|
|
2499
2528
|
var _a, _b;
|
|
2500
|
-
|
|
2529
|
+
if (!this.$state.get('WHERE').length) {
|
|
2530
|
+
throw this._assertError("The 'delete' method requires the use of 'where' conditions.");
|
|
2531
|
+
}
|
|
2501
2532
|
this.limit(1);
|
|
2502
2533
|
if (this.$state.get('SOFT_DELETE')) {
|
|
2503
2534
|
const deletedAt = this._valuePattern(this.$state.get('SOFT_DELETE_FORMAT'));
|
|
@@ -2531,7 +2562,9 @@ class Model extends AbstractModel_1.AbstractModel {
|
|
|
2531
2562
|
deleteMany() {
|
|
2532
2563
|
return __awaiter(this, void 0, void 0, function* () {
|
|
2533
2564
|
var _a, _b;
|
|
2534
|
-
|
|
2565
|
+
if (!this.$state.get('WHERE').length) {
|
|
2566
|
+
throw this._assertError("The 'deleteMany' method requires the use of 'where' conditions.");
|
|
2567
|
+
}
|
|
2535
2568
|
if (this.$state.get('SOFT_DELETE')) {
|
|
2536
2569
|
const deletedAt = this._valuePattern(this.$state.get('SOFT_DELETE_FORMAT'));
|
|
2537
2570
|
const sql = new Model()
|
|
@@ -2581,6 +2614,22 @@ class Model extends AbstractModel_1.AbstractModel {
|
|
|
2581
2614
|
return Boolean(this._resultHandler((_b = !!result) !== null && _b !== void 0 ? _b : false));
|
|
2582
2615
|
});
|
|
2583
2616
|
}
|
|
2617
|
+
/**
|
|
2618
|
+
* @override
|
|
2619
|
+
* @param {string=} column [column=id]
|
|
2620
|
+
* @return {promise<Array>}
|
|
2621
|
+
*/
|
|
2622
|
+
toArray(column) {
|
|
2623
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
2624
|
+
if (column == null)
|
|
2625
|
+
column = 'id';
|
|
2626
|
+
this.selectRaw(`${this.bindColumn(column)}`);
|
|
2627
|
+
const sql = this._queryBuilder().select();
|
|
2628
|
+
const result = yield this._queryStatement(sql);
|
|
2629
|
+
const toArray = result.map((data) => data[column]);
|
|
2630
|
+
return this._resultHandler(toArray);
|
|
2631
|
+
});
|
|
2632
|
+
}
|
|
2584
2633
|
/**
|
|
2585
2634
|
*
|
|
2586
2635
|
* @override
|
|
@@ -2601,7 +2650,9 @@ class Model extends AbstractModel_1.AbstractModel {
|
|
|
2601
2650
|
sql = String((_b = this.$relation) === null || _b === void 0 ? void 0 : _b.loadExists());
|
|
2602
2651
|
if (cb) {
|
|
2603
2652
|
const callbackSql = cb(sql);
|
|
2604
|
-
|
|
2653
|
+
if (callbackSql == null || callbackSql === '') {
|
|
2654
|
+
throw this._assertError('Please provide a callback for execution');
|
|
2655
|
+
}
|
|
2605
2656
|
sql = callbackSql;
|
|
2606
2657
|
}
|
|
2607
2658
|
return yield this._execute({
|
|
@@ -2671,7 +2722,9 @@ class Model extends AbstractModel_1.AbstractModel {
|
|
|
2671
2722
|
sql = String((_b = this.$relation) === null || _b === void 0 ? void 0 : _b.loadExists());
|
|
2672
2723
|
if (cb) {
|
|
2673
2724
|
const callbackSql = cb(sql);
|
|
2674
|
-
|
|
2725
|
+
if (callbackSql == null || callbackSql === '') {
|
|
2726
|
+
throw this._assertError('Please provide a callback for execution');
|
|
2727
|
+
}
|
|
2675
2728
|
sql = callbackSql;
|
|
2676
2729
|
}
|
|
2677
2730
|
return yield this._execute({
|
|
@@ -2705,9 +2758,10 @@ class Model extends AbstractModel_1.AbstractModel {
|
|
|
2705
2758
|
let limit = 15;
|
|
2706
2759
|
let page = 1;
|
|
2707
2760
|
if (paginationOptions != null) {
|
|
2708
|
-
limit = (paginationOptions === null || paginationOptions === void 0 ? void 0 : paginationOptions.limit) || limit;
|
|
2761
|
+
limit = ((paginationOptions === null || paginationOptions === void 0 ? void 0 : paginationOptions.limit) || limit);
|
|
2709
2762
|
page = (paginationOptions === null || paginationOptions === void 0 ? void 0 : paginationOptions.page) || page;
|
|
2710
2763
|
}
|
|
2764
|
+
limit = limit > 1000 ? 1000 : limit;
|
|
2711
2765
|
if ((_a = this.$state.get('EXCEPTS')) === null || _a === void 0 ? void 0 : _a.length)
|
|
2712
2766
|
this.select(...yield this.exceptColumns());
|
|
2713
2767
|
const offset = (page - 1) * limit;
|
|
@@ -2779,6 +2833,16 @@ class Model extends AbstractModel_1.AbstractModel {
|
|
|
2779
2833
|
return this._resultHandler(resultData);
|
|
2780
2834
|
});
|
|
2781
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
|
+
}
|
|
2782
2846
|
/**
|
|
2783
2847
|
* @override
|
|
2784
2848
|
* @param {object} data for insert
|
|
@@ -2945,7 +3009,7 @@ class Model extends AbstractModel_1.AbstractModel {
|
|
|
2945
3009
|
*/
|
|
2946
3010
|
createOrSelect(data) {
|
|
2947
3011
|
if (!Object.keys(data).length)
|
|
2948
|
-
throw
|
|
3012
|
+
throw this._assertError('This method must be required');
|
|
2949
3013
|
this.$state.set('DATA', data);
|
|
2950
3014
|
const queryInsert = this._queryInsertModel(data);
|
|
2951
3015
|
this.$state.set('INSERT', [
|
|
@@ -2996,7 +3060,8 @@ class Model extends AbstractModel_1.AbstractModel {
|
|
|
2996
3060
|
* @return {this} this
|
|
2997
3061
|
*/
|
|
2998
3062
|
createNotExists(data) {
|
|
2999
|
-
|
|
3063
|
+
if (!Object.keys(data).length)
|
|
3064
|
+
throw this._assertError('This method must be required');
|
|
3000
3065
|
this.$state.set('DATA', data);
|
|
3001
3066
|
const query = this._queryInsertModel(data);
|
|
3002
3067
|
this.$state.set('INSERT', [
|
|
@@ -3025,8 +3090,9 @@ class Model extends AbstractModel_1.AbstractModel {
|
|
|
3025
3090
|
* @return {this} this
|
|
3026
3091
|
*/
|
|
3027
3092
|
updateMultiple(cases) {
|
|
3028
|
-
if (!cases.length)
|
|
3029
|
-
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
|
+
}
|
|
3030
3096
|
this.limit(cases.length);
|
|
3031
3097
|
const updateColumns = cases.reduce((columns, item) => {
|
|
3032
3098
|
return (item.columns && Object.keys(item.columns).forEach(key => columns[key] = [
|
|
@@ -3051,10 +3117,12 @@ class Model extends AbstractModel_1.AbstractModel {
|
|
|
3051
3117
|
}
|
|
3052
3118
|
for (let i = cases.length - 1; i >= 0; i--) {
|
|
3053
3119
|
const c = cases[i];
|
|
3054
|
-
if (c.when == null || !Object.keys(c.when).length)
|
|
3055
|
-
this._assertError(`This 'when' property is missing some properties
|
|
3056
|
-
|
|
3057
|
-
|
|
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
|
+
}
|
|
3058
3126
|
const when = Object.entries(c.when).map(([key, value]) => {
|
|
3059
3127
|
value = this.$utils.escape(value);
|
|
3060
3128
|
value = this._valueTrueFalse(value);
|
|
@@ -3081,8 +3149,8 @@ class Model extends AbstractModel_1.AbstractModel {
|
|
|
3081
3149
|
if (typeof value === 'string' && !(value.includes(this.$constants('RAW')))) {
|
|
3082
3150
|
value = this.$utils.escapeActions(value);
|
|
3083
3151
|
}
|
|
3084
|
-
return `${this.bindColumn(column)} = ${value == null || value === 'NULL'
|
|
3085
|
-
? 'NULL'
|
|
3152
|
+
return `${this.bindColumn(column)} = ${value == null || value === this.$constants('NULL')
|
|
3153
|
+
? this.$constants('NULL')
|
|
3086
3154
|
: this._checkValueHasRaw(value)}`;
|
|
3087
3155
|
});
|
|
3088
3156
|
const query = `${this.$constants('SET')} ${keyValue.join(', ')}`;
|
|
@@ -3095,11 +3163,20 @@ class Model extends AbstractModel_1.AbstractModel {
|
|
|
3095
3163
|
this.$state.set('SAVE', 'UPDATE');
|
|
3096
3164
|
return this;
|
|
3097
3165
|
}
|
|
3166
|
+
/**
|
|
3167
|
+
* The 'getSchemaModel' method is used get a schema model
|
|
3168
|
+
* @return {Record<string, Blueprint> | null} Record<string, Blueprint> | null
|
|
3169
|
+
*/
|
|
3098
3170
|
getSchemaModel() {
|
|
3099
3171
|
if (this.$schema == null)
|
|
3100
3172
|
return this.$state.get('SCHEMA_TABLE');
|
|
3101
3173
|
return this.$schema;
|
|
3102
3174
|
}
|
|
3175
|
+
/**
|
|
3176
|
+
* The 'validation' method is used validate the column by validating
|
|
3177
|
+
* @param {ValidateSchema} schema
|
|
3178
|
+
* @return {this} this
|
|
3179
|
+
*/
|
|
3103
3180
|
validation(schema) {
|
|
3104
3181
|
this.$state.set('VALIDATE_SCHEMA', true);
|
|
3105
3182
|
this.$state.set('VALIDATE_SCHEMA_DEFINED', schema);
|
|
@@ -3157,7 +3234,9 @@ class Model extends AbstractModel_1.AbstractModel {
|
|
|
3157
3234
|
};
|
|
3158
3235
|
});
|
|
3159
3236
|
for (let row = 0; row < rows; row++) {
|
|
3160
|
-
|
|
3237
|
+
if (this.$state.get('TABLE_NAME') === '' || this.$state.get('TABLE_NAME') == null) {
|
|
3238
|
+
throw this._assertError("Unknow this table.");
|
|
3239
|
+
}
|
|
3161
3240
|
let columnAndValue = {};
|
|
3162
3241
|
for (const { Field: field, Type: type } of fields) {
|
|
3163
3242
|
const deletedAt = this._valuePattern(this.$state.get('SOFT_DELETE_FORMAT'));
|
|
@@ -3172,7 +3251,8 @@ class Model extends AbstractModel_1.AbstractModel {
|
|
|
3172
3251
|
}
|
|
3173
3252
|
data.push(columnAndValue);
|
|
3174
3253
|
}
|
|
3175
|
-
|
|
3254
|
+
yield this.createMultiple(data).save();
|
|
3255
|
+
return;
|
|
3176
3256
|
});
|
|
3177
3257
|
}
|
|
3178
3258
|
/**
|
|
@@ -3191,7 +3271,7 @@ class Model extends AbstractModel_1.AbstractModel {
|
|
|
3191
3271
|
const existsTables = checkTables.map((c) => Object.values(c)[0]);
|
|
3192
3272
|
const schemaModel = this.getSchemaModel();
|
|
3193
3273
|
if (schemaModel == null)
|
|
3194
|
-
|
|
3274
|
+
throw this._assertError(schemaModel == null, 'Schema model not found');
|
|
3195
3275
|
const checkTableIsExists = existsTables.some((table) => table === this.getTableName());
|
|
3196
3276
|
const syncForeignKey = (_j) => __awaiter(this, [_j], void 0, function* ({ schemaModel, model }) {
|
|
3197
3277
|
var _k;
|
|
@@ -3452,14 +3532,14 @@ class Model extends AbstractModel_1.AbstractModel {
|
|
|
3452
3532
|
return;
|
|
3453
3533
|
const schemaTable = this.getSchemaModel();
|
|
3454
3534
|
if (schemaTable == null) {
|
|
3455
|
-
|
|
3535
|
+
throw this._assertError(`This method "validateSchema" isn't validation without schema. Please use the method "useSchema" for define your schema.`);
|
|
3456
3536
|
}
|
|
3457
3537
|
const schemaTableDefined = this.$state.get('VALIDATE_SCHEMA_DEFINED');
|
|
3458
3538
|
const schema = schemaTableDefined !== null && schemaTableDefined !== void 0 ? schemaTableDefined : Object.keys(schemaTable).reduce((acc, key) => {
|
|
3459
3539
|
acc[key] = schemaTable[key].valueType;
|
|
3460
3540
|
return acc;
|
|
3461
3541
|
}, {});
|
|
3462
|
-
if (schema == null)
|
|
3542
|
+
if (schema == null || !Object.keys(schema).length)
|
|
3463
3543
|
return;
|
|
3464
3544
|
const regexDate = /[0-9]{4}-(0[1-9]|1[0-2])-(0[1-9]|[1-2][0-9]|3[0-1])/;
|
|
3465
3545
|
const regexDateTime = /[0-9]{4}-(0[1-9]|1[0-2])-(0[1-9]|[1-2][0-9]|3[0-1]) (2[0-3]|[01][0-9]):[0-5][0-9]/;
|
|
@@ -3475,54 +3555,61 @@ class Model extends AbstractModel_1.AbstractModel {
|
|
|
3475
3555
|
if (regexDate.test(r) || regexDateTime.test(r)) {
|
|
3476
3556
|
if (typeOf(new Date(r)) === typeOf(new s()))
|
|
3477
3557
|
continue;
|
|
3478
|
-
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())}".`);
|
|
3479
3559
|
}
|
|
3480
3560
|
if (typeOf(r) === typeOf(new s()))
|
|
3481
3561
|
continue;
|
|
3482
|
-
this._assertError(`This column "${column}" is must be type "${typeOf(new s())}"
|
|
3483
|
-
|
|
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.`);
|
|
3484
3566
|
}
|
|
3485
|
-
if (s.require && action === 'insert')
|
|
3486
|
-
this._assertError(r === '' || r == null, `This column "${column}" is required`);
|
|
3487
3567
|
if (r == null)
|
|
3488
3568
|
continue;
|
|
3489
|
-
|
|
3490
|
-
|
|
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
|
+
}
|
|
3491
3575
|
if (s.json) {
|
|
3492
3576
|
try {
|
|
3493
3577
|
JSON.parse(r);
|
|
3494
3578
|
}
|
|
3495
3579
|
catch (_) {
|
|
3496
|
-
this._assertError(`This column "${column}" is must be JSON
|
|
3580
|
+
throw this._assertError(`This column "${column}" is must be JSON.`);
|
|
3497
3581
|
}
|
|
3498
3582
|
}
|
|
3499
|
-
if (s.length) {
|
|
3500
|
-
|
|
3583
|
+
if (s.length && (`${r}`.length > s.length)) {
|
|
3584
|
+
throw this._assertError(`This column "${column}" is more than "${s.length}" length of characters.`);
|
|
3585
|
+
}
|
|
3586
|
+
if (s.maxLength && (`${r}`.length > s.maxLength)) {
|
|
3587
|
+
throw this._assertError(`This column "${column}" is more than "${s.maxLength}" length of characters.`);
|
|
3588
|
+
}
|
|
3589
|
+
if (s.minLength && (`${r}`.length < s.minLength)) {
|
|
3590
|
+
throw this._assertError(`This column "${column}" is less than "${s.minLength}" length of characters`);
|
|
3501
3591
|
}
|
|
3502
|
-
if (s.
|
|
3503
|
-
|
|
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}"`)}`);
|
|
3504
3598
|
}
|
|
3505
|
-
if (s.
|
|
3506
|
-
|
|
3599
|
+
if (s.match && !s.match.test(r)) {
|
|
3600
|
+
throw this._assertError(`This column "${column}" is not match a regular expression`);
|
|
3507
3601
|
}
|
|
3508
|
-
if (s.
|
|
3509
|
-
|
|
3510
|
-
if (s.min)
|
|
3511
|
-
return this._assertError(r < s.min, `This column "${column}" is less than "${s.min}"`);
|
|
3512
|
-
if (s.enum && s.enum.length) {
|
|
3513
|
-
return this._assertError(!s.enum.some((e) => e === r), `This column "${column}" is must be in ${s.enum.map((e) => `"${e}"`)}`);
|
|
3602
|
+
if (s.fn && !(yield s.fn(r))) {
|
|
3603
|
+
throw this._assertError(`This column "${column}" is not valid with function`);
|
|
3514
3604
|
}
|
|
3515
|
-
if (s.match)
|
|
3516
|
-
return this._assertError(!s.match.test(r), `This column "${column}" is not match a regular expression`);
|
|
3517
|
-
if (s.fn)
|
|
3518
|
-
return this._assertError(!(yield s.fn(r)), `This column "${column}" is not valid with function`);
|
|
3519
3605
|
if (s.unique && action === 'insert') {
|
|
3520
3606
|
const exist = yield new Model()
|
|
3521
3607
|
.copyModel(this, { select: true, where: true, limit: true })
|
|
3522
3608
|
.where(column, r)
|
|
3523
3609
|
.debug(this.$state.get('DEBUG'))
|
|
3524
3610
|
.exists();
|
|
3525
|
-
|
|
3611
|
+
if (exist)
|
|
3612
|
+
throw this._assertError(`This column "${column}" is duplicated`);
|
|
3526
3613
|
}
|
|
3527
3614
|
}
|
|
3528
3615
|
return;
|
|
@@ -3565,12 +3652,14 @@ class Model extends AbstractModel_1.AbstractModel {
|
|
|
3565
3652
|
var _a;
|
|
3566
3653
|
const currentPage = +(this.$state.get('PAGE'));
|
|
3567
3654
|
const limit = Number(this.$state.get('PER_PAGE'));
|
|
3568
|
-
|
|
3655
|
+
if (limit < 1) {
|
|
3656
|
+
throw this._assertError("This pagination needed limit minimun less 1 for limit");
|
|
3657
|
+
}
|
|
3569
3658
|
const total = yield new Model()
|
|
3570
|
-
.copyModel(this, { where: true })
|
|
3659
|
+
.copyModel(this, { where: true, join: true })
|
|
3571
3660
|
.bind(this.$pool.get())
|
|
3572
3661
|
.debug(this.$state.get('DEBUG'))
|
|
3573
|
-
.count();
|
|
3662
|
+
.count(this.$state.get('PRIMARY_KEY'));
|
|
3574
3663
|
let lastPage = Math.ceil(total / limit) || 0;
|
|
3575
3664
|
lastPage = lastPage > 1 ? lastPage : 1;
|
|
3576
3665
|
const nextPage = currentPage + 1;
|
|
@@ -3650,7 +3739,7 @@ class Model extends AbstractModel_1.AbstractModel {
|
|
|
3650
3739
|
};
|
|
3651
3740
|
break;
|
|
3652
3741
|
}
|
|
3653
|
-
default: this._assertError('Missing method first get or pagination');
|
|
3742
|
+
default: throw this._assertError('Missing method first get or pagination');
|
|
3654
3743
|
}
|
|
3655
3744
|
if (this._isPatternSnakeCase()) {
|
|
3656
3745
|
const empty = this.$utils.snakeCase(this._resultHandler(emptyData));
|
|
@@ -3722,7 +3811,9 @@ class Model extends AbstractModel_1.AbstractModel {
|
|
|
3722
3811
|
const pluck = this.$state.get('PLUCK');
|
|
3723
3812
|
const newData = res[0];
|
|
3724
3813
|
const checkProperty = newData.hasOwnProperty(pluck);
|
|
3725
|
-
|
|
3814
|
+
if (!checkProperty) {
|
|
3815
|
+
this._assertError(`Can't find property '${pluck}' of results.`);
|
|
3816
|
+
}
|
|
3726
3817
|
result = this._resultHandler(newData[pluck]);
|
|
3727
3818
|
break;
|
|
3728
3819
|
}
|
|
@@ -3741,7 +3832,9 @@ class Model extends AbstractModel_1.AbstractModel {
|
|
|
3741
3832
|
const pluck = this.$state.get('PLUCK');
|
|
3742
3833
|
const newData = res[0];
|
|
3743
3834
|
const checkProperty = newData.hasOwnProperty(pluck);
|
|
3744
|
-
|
|
3835
|
+
if (!checkProperty) {
|
|
3836
|
+
throw this._assertError(`Can't find property '${pluck}' of results`);
|
|
3837
|
+
}
|
|
3745
3838
|
result = (_d = this._resultHandler(newData[pluck])) !== null && _d !== void 0 ? _d : null;
|
|
3746
3839
|
break;
|
|
3747
3840
|
}
|
|
@@ -3770,7 +3863,9 @@ class Model extends AbstractModel_1.AbstractModel {
|
|
|
3770
3863
|
if (this.$state.get('PLUCK')) {
|
|
3771
3864
|
const pluck = this.$state.get('PLUCK');
|
|
3772
3865
|
const newData = data.map((d) => d[pluck]);
|
|
3773
|
-
|
|
3866
|
+
if (newData.every((d) => d == null)) {
|
|
3867
|
+
throw this._assertError(`Can't find property '${pluck}' of results.`);
|
|
3868
|
+
}
|
|
3774
3869
|
result = this._resultHandler(newData);
|
|
3775
3870
|
break;
|
|
3776
3871
|
}
|
|
@@ -3809,9 +3904,13 @@ class Model extends AbstractModel_1.AbstractModel {
|
|
|
3809
3904
|
_attach(name, dataId, fields) {
|
|
3810
3905
|
return __awaiter(this, void 0, void 0, function* () {
|
|
3811
3906
|
var _a;
|
|
3812
|
-
|
|
3907
|
+
if (!Array.isArray(dataId)) {
|
|
3908
|
+
throw this._assertError(`This '${dataId}' is not an array.`);
|
|
3909
|
+
}
|
|
3813
3910
|
const relation = (_a = this.$state.get('RELATION')) === null || _a === void 0 ? void 0 : _a.find((data) => data.name === name);
|
|
3814
|
-
|
|
3911
|
+
if (!relation) {
|
|
3912
|
+
throw this._assertError(`Unknown relation '${name}' in model.`);
|
|
3913
|
+
}
|
|
3815
3914
|
const thisTable = this.$utils.columnRelation(this.constructor.name);
|
|
3816
3915
|
const relationTable = this._classToTableName(relation.model.name, { singular: true });
|
|
3817
3916
|
const result = this.$state.get('RESULT');
|
|
@@ -3842,9 +3941,13 @@ class Model extends AbstractModel_1.AbstractModel {
|
|
|
3842
3941
|
}
|
|
3843
3942
|
_detach(name, dataId) {
|
|
3844
3943
|
return __awaiter(this, void 0, void 0, function* () {
|
|
3845
|
-
|
|
3944
|
+
if (!Array.isArray(dataId)) {
|
|
3945
|
+
throw this._assertError(`This '${dataId}' is not an array.`);
|
|
3946
|
+
}
|
|
3846
3947
|
const relation = this.$state.get('RELATION').find((data) => data.name === name);
|
|
3847
|
-
|
|
3948
|
+
if (!relation) {
|
|
3949
|
+
throw this._assertError(`Unknown relation '${name}' in model.`);
|
|
3950
|
+
}
|
|
3848
3951
|
const thisTable = this.$utils.columnRelation(this.constructor.name);
|
|
3849
3952
|
const relationTable = this._classToTableName(relation.model.name, { singular: true });
|
|
3850
3953
|
const result = this.$state.get('RESULT');
|
|
@@ -3889,8 +3992,8 @@ class Model extends AbstractModel_1.AbstractModel {
|
|
|
3889
3992
|
if (typeof value === 'string' && !(value.includes(this.$constants('RAW')))) {
|
|
3890
3993
|
value = this.$utils.escapeActions(value);
|
|
3891
3994
|
}
|
|
3892
|
-
return `${this.bindColumn(column)} = ${value == null || value === 'NULL'
|
|
3893
|
-
? 'NULL'
|
|
3995
|
+
return `${this.bindColumn(column)} = ${value == null || value === this.$constants('NULL')
|
|
3996
|
+
? this.$constants('NULL')
|
|
3894
3997
|
: this._checkValueHasRaw(value)}`;
|
|
3895
3998
|
});
|
|
3896
3999
|
return `${this.$constants('SET')} ${keyValue.join(', ')}`;
|
|
@@ -3914,8 +4017,8 @@ class Model extends AbstractModel_1.AbstractModel {
|
|
|
3914
4017
|
if (typeof value === 'string' && !(value.includes(this.$constants('RAW')))) {
|
|
3915
4018
|
value = this.$utils.escapeActions(value);
|
|
3916
4019
|
}
|
|
3917
|
-
return `${value == null || value === 'NULL'
|
|
3918
|
-
? 'NULL'
|
|
4020
|
+
return `${value == null || value === this.$constants('NULL')
|
|
4021
|
+
? this.$constants('NULL')
|
|
3919
4022
|
: this._checkValueHasRaw(value)}`;
|
|
3920
4023
|
});
|
|
3921
4024
|
const sql = [
|
|
@@ -3956,8 +4059,8 @@ class Model extends AbstractModel_1.AbstractModel {
|
|
|
3956
4059
|
if (typeof value === 'string' && !(value.includes(this.$constants('RAW')))) {
|
|
3957
4060
|
value = this.$utils.escapeActions(value);
|
|
3958
4061
|
}
|
|
3959
|
-
return `${value == null || value === 'NULL'
|
|
3960
|
-
? 'NULL'
|
|
4062
|
+
return `${value == null || value === this.$constants('NULL')
|
|
4063
|
+
? this.$constants('NULL')
|
|
3961
4064
|
: this._checkValueHasRaw(value)}`;
|
|
3962
4065
|
});
|
|
3963
4066
|
values = [
|
|
@@ -3973,7 +4076,9 @@ class Model extends AbstractModel_1.AbstractModel {
|
|
|
3973
4076
|
}
|
|
3974
4077
|
_insertNotExistsModel() {
|
|
3975
4078
|
return __awaiter(this, void 0, void 0, function* () {
|
|
3976
|
-
|
|
4079
|
+
if (!this.$state.get('WHERE').length) {
|
|
4080
|
+
throw this._assertError("The 'createNotExists' method requires the use of 'where' conditions.");
|
|
4081
|
+
}
|
|
3977
4082
|
const check = (yield new Model()
|
|
3978
4083
|
.copyModel(this, { where: true, select: true, limit: true })
|
|
3979
4084
|
.bind(this.$pool.get())
|
|
@@ -4046,7 +4151,9 @@ class Model extends AbstractModel_1.AbstractModel {
|
|
|
4046
4151
|
}
|
|
4047
4152
|
_updateOrInsertModel() {
|
|
4048
4153
|
return __awaiter(this, void 0, void 0, function* () {
|
|
4049
|
-
|
|
4154
|
+
if (!this.$state.get('WHERE').length) {
|
|
4155
|
+
throw this._assertError("The 'createOrUpdate' method requires the use of 'where' conditions.");
|
|
4156
|
+
}
|
|
4050
4157
|
const check = (yield new Model()
|
|
4051
4158
|
.copyModel(this, { select: true, where: true, limit: true })
|
|
4052
4159
|
.bind(this.$pool.get())
|
|
@@ -4101,7 +4208,9 @@ class Model extends AbstractModel_1.AbstractModel {
|
|
|
4101
4208
|
}
|
|
4102
4209
|
_insertOrSelectModel() {
|
|
4103
4210
|
return __awaiter(this, void 0, void 0, function* () {
|
|
4104
|
-
|
|
4211
|
+
if (!this.$state.get('WHERE').length) {
|
|
4212
|
+
throw this._assertError("The 'createOrSelect' method requires the use of 'where' conditions.");
|
|
4213
|
+
}
|
|
4105
4214
|
const check = (yield new Model()
|
|
4106
4215
|
.copyModel(this, { select: true, where: true, limit: true })
|
|
4107
4216
|
.bind(this.$pool.get())
|
|
@@ -4150,7 +4259,9 @@ class Model extends AbstractModel_1.AbstractModel {
|
|
|
4150
4259
|
}
|
|
4151
4260
|
_updateModel() {
|
|
4152
4261
|
return __awaiter(this, void 0, void 0, function* () {
|
|
4153
|
-
|
|
4262
|
+
if (!this.$state.get('WHERE').length) {
|
|
4263
|
+
throw this._assertError("The 'update' method requires the use of 'where' conditions.");
|
|
4264
|
+
}
|
|
4154
4265
|
yield this._validateSchema(this.$state.get('DATA'), 'update');
|
|
4155
4266
|
const sql = this._queryBuilder().update();
|
|
4156
4267
|
const result = yield this._actionStatement({ sql });
|
|
@@ -4216,13 +4327,17 @@ class Model extends AbstractModel_1.AbstractModel {
|
|
|
4216
4327
|
const methodCallings = this.$logger.get();
|
|
4217
4328
|
const methodsNotAllowed = methodChangeStatements;
|
|
4218
4329
|
const findMethodNotAllowed = methodCallings.find((methodCalling) => methodsNotAllowed.includes(methodCalling));
|
|
4219
|
-
|
|
4330
|
+
if (methodCallings.some((methodCalling) => methodsNotAllowed.includes(methodCalling))) {
|
|
4331
|
+
throw this._assertError(`This method '${method}' can't using the method '${findMethodNotAllowed}'.`);
|
|
4332
|
+
}
|
|
4220
4333
|
break;
|
|
4221
4334
|
}
|
|
4222
4335
|
case 'save': {
|
|
4223
4336
|
const methodCallings = this.$logger.get();
|
|
4224
4337
|
const methodsSomeAllowed = methodChangeStatements;
|
|
4225
|
-
|
|
4338
|
+
if (!methodCallings.some((methodCalling) => methodsSomeAllowed.includes(methodCalling))) {
|
|
4339
|
+
throw this._assertError(`This ${method} method need some ${methodsSomeAllowed.map(v => `'${v}'`).join(', ')} methods.`);
|
|
4340
|
+
}
|
|
4226
4341
|
break;
|
|
4227
4342
|
}
|
|
4228
4343
|
}
|
|
@@ -4320,7 +4435,7 @@ class Model extends AbstractModel_1.AbstractModel {
|
|
|
4320
4435
|
return this;
|
|
4321
4436
|
}
|
|
4322
4437
|
_initialModel() {
|
|
4323
|
-
this.$state = new State_1.StateHandler(
|
|
4438
|
+
this.$state = new State_1.StateHandler('model');
|
|
4324
4439
|
if (this.$pattern != null)
|
|
4325
4440
|
this.usePattern(this.$pattern);
|
|
4326
4441
|
this._makeTableName();
|