tspace-mysql 1.3.7 → 1.3.9
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 +25 -18
- package/dist/cli/generate/make.js +19 -5
- package/dist/cli/generate/model.d.ts +1 -1
- package/dist/cli/generate/model.js +3 -18
- package/dist/cli/migrate/make.d.ts +1 -1
- package/dist/cli/migrate/make.js +2 -2
- package/dist/cli/models/model.js +1 -14
- package/dist/lib/connection/options.js +16 -15
- package/dist/lib/constants/index.js +7 -2
- package/dist/lib/tspace/Abstract/AbstractModel.d.ts +4 -2
- package/dist/lib/tspace/Blueprint.d.ts +16 -16
- package/dist/lib/tspace/Blueprint.js +35 -27
- package/dist/lib/tspace/Builder.d.ts +13 -0
- package/dist/lib/tspace/Builder.js +92 -15
- package/dist/lib/tspace/Interface.d.ts +1 -0
- package/dist/lib/tspace/Model.d.ts +41 -13
- package/dist/lib/tspace/Model.js +134 -81
- package/dist/lib/tspace/Schema.d.ts +1 -1
- package/dist/lib/tspace/Schema.js +7 -7
- package/dist/lib/utils/index.d.ts +1 -0
- package/dist/lib/utils/index.js +2 -0
- package/package.json +1 -1
- package/dist/lib/tspace/AbstractBuilder.d.ts +0 -122
- package/dist/lib/tspace/AbstractBuilder.js +0 -33
- package/dist/lib/tspace/AbstractDB.d.ts +0 -20
- package/dist/lib/tspace/AbstractDB.js +0 -11
- package/dist/lib/tspace/AbstractModel.d.ts +0 -45
- package/dist/lib/tspace/AbstractModel.js +0 -11
|
@@ -300,9 +300,9 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
|
|
|
300
300
|
whereIn(column, array) {
|
|
301
301
|
if (!Array.isArray(array))
|
|
302
302
|
throw new Error(`[${array}] is't array`);
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
303
|
+
const values = array.length
|
|
304
|
+
? `${array.map((value) => this._checkValueHasRaw(this.$utils.escape(value))).join(',')}`
|
|
305
|
+
: this.$constants('NULL');
|
|
306
306
|
this.$state.set('WHERE', [
|
|
307
307
|
this._queryWhereIsExists()
|
|
308
308
|
? `${this.$state.get('WHERE')} ${this.$constants('AND')}`
|
|
@@ -322,9 +322,9 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
|
|
|
322
322
|
orWhereIn(column, array) {
|
|
323
323
|
if (!Array.isArray(array))
|
|
324
324
|
throw new Error(`[${array}] is't array`);
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
325
|
+
const values = array.length
|
|
326
|
+
? `${array.map((value) => this._checkValueHasRaw(this.$utils.escape(value))).join(',')}`
|
|
327
|
+
: this.$constants('NULL');
|
|
328
328
|
this.$state.set('WHERE', [
|
|
329
329
|
this._queryWhereIsExists()
|
|
330
330
|
? `${this.$state.get('WHERE')} ${this.$constants('OR')}`
|
|
@@ -345,9 +345,9 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
|
|
|
345
345
|
const sql = this.$state.get('WHERE');
|
|
346
346
|
if (!Array.isArray(array))
|
|
347
347
|
throw new Error(`[${array}] is't array`);
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
348
|
+
const values = array.length
|
|
349
|
+
? `${array.map((value) => this._checkValueHasRaw(this.$utils.escape(value))).join(',')}`
|
|
350
|
+
: this.$constants('NULL');
|
|
351
351
|
this.$state.set('WHERE', [
|
|
352
352
|
this._queryWhereIsExists()
|
|
353
353
|
? `${this.$state.get('WHERE')} ${this.$constants('AND')}`
|
|
@@ -367,9 +367,9 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
|
|
|
367
367
|
orWhereNotIn(column, array) {
|
|
368
368
|
if (!Array.isArray(array))
|
|
369
369
|
throw new Error(`[${array}] is't array`);
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
|
|
370
|
+
const values = array.length
|
|
371
|
+
? `${array.map((value) => this._checkValueHasRaw(this.$utils.escape(value))).join(',')}`
|
|
372
|
+
: this.$constants('NULL');
|
|
373
373
|
this.$state.set('WHERE', [
|
|
374
374
|
this._queryWhereIsExists()
|
|
375
375
|
? `${this.$state.get('WHERE')} ${this.$constants('OR')}`
|
|
@@ -387,6 +387,8 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
|
|
|
387
387
|
* @return {this}
|
|
388
388
|
*/
|
|
389
389
|
whereSubQuery(column, subQuery) {
|
|
390
|
+
if (!this.$utils.isQuery(subQuery))
|
|
391
|
+
throw new Error(`This "${subQuery}" is invalid sub query`);
|
|
390
392
|
this.$state.set('WHERE', [
|
|
391
393
|
this._queryWhereIsExists()
|
|
392
394
|
? `${this.$state.get('WHERE')} ${this.$constants('AND')}`
|
|
@@ -404,6 +406,8 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
|
|
|
404
406
|
* @return {this}
|
|
405
407
|
*/
|
|
406
408
|
whereNotSubQuery(column, subQuery) {
|
|
409
|
+
if (!this.$utils.isQuery(subQuery))
|
|
410
|
+
throw new Error(`This "${subQuery}" is invalid sub query`);
|
|
407
411
|
this.$state.set('WHERE', [
|
|
408
412
|
this._queryWhereIsExists()
|
|
409
413
|
? `${this.$state.get('WHERE')} ${this.$constants('AND')}`
|
|
@@ -421,6 +425,8 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
|
|
|
421
425
|
* @return {this}
|
|
422
426
|
*/
|
|
423
427
|
orWhereSubQuery(column, subQuery) {
|
|
428
|
+
if (!this.$utils.isQuery(subQuery))
|
|
429
|
+
throw new Error(`This "${subQuery}" is invalid sub query`);
|
|
424
430
|
this.$state.set('WHERE', [
|
|
425
431
|
this._queryWhereIsExists()
|
|
426
432
|
? `${this.$state.get('WHERE')} ${this.$constants('OR')}`
|
|
@@ -438,6 +444,8 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
|
|
|
438
444
|
* @return {this}
|
|
439
445
|
*/
|
|
440
446
|
orWhereNotSubQuery(column, subQuery) {
|
|
447
|
+
if (!this.$utils.isQuery(subQuery))
|
|
448
|
+
throw new Error(`This "${subQuery}" is invalid sub query`);
|
|
441
449
|
this.$state.set('WHERE', [
|
|
442
450
|
this._queryWhereIsExists()
|
|
443
451
|
? `${this.$state.get('WHERE')} ${this.$constants('OR')}`
|
|
@@ -457,8 +465,16 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
|
|
|
457
465
|
whereBetween(column, array) {
|
|
458
466
|
if (!Array.isArray(array))
|
|
459
467
|
throw new Error("Value is't array");
|
|
460
|
-
if (!array.length)
|
|
468
|
+
if (!array.length) {
|
|
469
|
+
this.$state.set('WHERE', [
|
|
470
|
+
this._queryWhereIsExists()
|
|
471
|
+
? `${this.$state.get('WHERE')} ${this.$constants('AND')}`
|
|
472
|
+
: `${this.$constants('WHERE')}`,
|
|
473
|
+
`${this._bindTableAndColumnInQueryWhere(column)} ${this.$constants('BETWEEN')}`,
|
|
474
|
+
`${this.$constants('NULL')} ${this.$constants('AND')} ${this.$constants('NULL')}`
|
|
475
|
+
].join(' '));
|
|
461
476
|
return this;
|
|
477
|
+
}
|
|
462
478
|
const [value1, value2] = array;
|
|
463
479
|
this.$state.set('WHERE', [
|
|
464
480
|
this._queryWhereIsExists()
|
|
@@ -478,8 +494,16 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
|
|
|
478
494
|
orWhereBetween(column, array) {
|
|
479
495
|
if (!Array.isArray(array))
|
|
480
496
|
throw new Error("Value is't array");
|
|
481
|
-
if (!array.length)
|
|
497
|
+
if (!array.length) {
|
|
498
|
+
this.$state.set('WHERE', [
|
|
499
|
+
this._queryWhereIsExists()
|
|
500
|
+
? `${this.$state.get('WHERE')} ${this.$constants('OR')}`
|
|
501
|
+
: `${this.$constants('WHERE')}`,
|
|
502
|
+
`${this._bindTableAndColumnInQueryWhere(column)} ${this.$constants('BETWEEN')}`,
|
|
503
|
+
`${this.$constants('NULL')} ${this.$constants('AND')} ${this.$constants('NULL')}`
|
|
504
|
+
].join(' '));
|
|
482
505
|
return this;
|
|
506
|
+
}
|
|
483
507
|
const [value1, value2] = array;
|
|
484
508
|
this.$state.set('WHERE', [
|
|
485
509
|
this._queryWhereIsExists()
|
|
@@ -499,8 +523,16 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
|
|
|
499
523
|
whereNotBetween(column, array) {
|
|
500
524
|
if (!Array.isArray(array))
|
|
501
525
|
throw new Error("Value is't array");
|
|
502
|
-
if (!array.length)
|
|
526
|
+
if (!array.length) {
|
|
527
|
+
this.$state.set('WHERE', [
|
|
528
|
+
this._queryWhereIsExists()
|
|
529
|
+
? `${this.$state.get('WHERE')} ${this.$constants('AND')}`
|
|
530
|
+
: `${this.$constants('WHERE')}`,
|
|
531
|
+
`${this._bindTableAndColumnInQueryWhere(column)} ${this.$constants('NOT_BETWEEN')}`,
|
|
532
|
+
`${this.$constants('NULL')} ${this.$constants('AND')} ${this.$constants('NULL')}`
|
|
533
|
+
].join(' '));
|
|
503
534
|
return this;
|
|
535
|
+
}
|
|
504
536
|
const [value1, value2] = array;
|
|
505
537
|
this.$state.set('WHERE', [
|
|
506
538
|
this._queryWhereIsExists()
|
|
@@ -511,6 +543,35 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
|
|
|
511
543
|
].join(' '));
|
|
512
544
|
return this;
|
|
513
545
|
}
|
|
546
|
+
/**
|
|
547
|
+
* where not between using [value1, value2]
|
|
548
|
+
* @param {string} column
|
|
549
|
+
* @param {array} array
|
|
550
|
+
* @return {this}
|
|
551
|
+
*/
|
|
552
|
+
orWhereNotBetween(column, array) {
|
|
553
|
+
if (!Array.isArray(array))
|
|
554
|
+
throw new Error("Value is't array");
|
|
555
|
+
if (!array.length) {
|
|
556
|
+
this.$state.set('WHERE', [
|
|
557
|
+
this._queryWhereIsExists()
|
|
558
|
+
? `${this.$state.get('WHERE')} ${this.$constants('OR')}`
|
|
559
|
+
: `${this.$constants('WHERE')}`,
|
|
560
|
+
`${this._bindTableAndColumnInQueryWhere(column)} ${this.$constants('NOT_BETWEEN')}`,
|
|
561
|
+
`${this.$constants('NULL')} ${this.$constants('AND')} ${this.$constants('NULL')}`
|
|
562
|
+
].join(' '));
|
|
563
|
+
return this;
|
|
564
|
+
}
|
|
565
|
+
const [value1, value2] = array;
|
|
566
|
+
this.$state.set('WHERE', [
|
|
567
|
+
this._queryWhereIsExists()
|
|
568
|
+
? `${this.$state.get('WHERE')} ${this.$constants('OR')}`
|
|
569
|
+
: `${this.$constants('WHERE')}`,
|
|
570
|
+
`${this._bindTableAndColumnInQueryWhere(column)} ${this.$constants('NOT_BETWEEN')}`,
|
|
571
|
+
`${this._checkValueHasRaw(this.$utils.escape(value1))} ${this.$constants('AND')} ${this._checkValueHasRaw(this.$utils.escape(value2))}`
|
|
572
|
+
].join(' '));
|
|
573
|
+
return this;
|
|
574
|
+
}
|
|
514
575
|
/**
|
|
515
576
|
* where null using NULL
|
|
516
577
|
* @param {string} column
|
|
@@ -2016,6 +2077,22 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
|
|
|
2016
2077
|
});
|
|
2017
2078
|
});
|
|
2018
2079
|
}
|
|
2080
|
+
/**
|
|
2081
|
+
*
|
|
2082
|
+
* get schema from table
|
|
2083
|
+
* @return {this} this this
|
|
2084
|
+
*/
|
|
2085
|
+
getSchema() {
|
|
2086
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
2087
|
+
const sql = [
|
|
2088
|
+
`${this.$constants('SHOW')}`,
|
|
2089
|
+
`${this.$constants('COLUMNS')}`,
|
|
2090
|
+
`${this.$constants('FROM')}`,
|
|
2091
|
+
`\`${this.$state.get('TABLE_NAME').replace(/\`/g, '')}\``
|
|
2092
|
+
].join(' ');
|
|
2093
|
+
return yield this.queryStatement(sql);
|
|
2094
|
+
});
|
|
2095
|
+
}
|
|
2019
2096
|
/**
|
|
2020
2097
|
*
|
|
2021
2098
|
* show values in table
|
|
@@ -40,7 +40,7 @@ declare class Model extends AbstractModel {
|
|
|
40
40
|
* import { Blueprint } from 'tspace-mysql'
|
|
41
41
|
* class User extends Model {
|
|
42
42
|
* constructor() {
|
|
43
|
-
* this.
|
|
43
|
+
* this.useSchema ({
|
|
44
44
|
* id : new Blueprint().int().notNull().primary().autoIncrement(),
|
|
45
45
|
* uuid : new Blueprint().varchar(50).null(),
|
|
46
46
|
* email : new Blueprint().varchar(50).null(),
|
|
@@ -52,7 +52,7 @@ declare class Model extends AbstractModel {
|
|
|
52
52
|
* }
|
|
53
53
|
* @return {this} this
|
|
54
54
|
*/
|
|
55
|
-
protected
|
|
55
|
+
protected useSchema(schema: Record<string, any>): this;
|
|
56
56
|
/**
|
|
57
57
|
*
|
|
58
58
|
* Assign function callback in model like constructor()
|
|
@@ -211,17 +211,25 @@ declare class Model extends AbstractModel {
|
|
|
211
211
|
* @example
|
|
212
212
|
* class User extends Model {
|
|
213
213
|
* constructor() {
|
|
214
|
-
* this.useValidationSchema(
|
|
215
|
-
* id : Number,
|
|
216
|
-
* email : String,
|
|
217
|
-
* name : String,
|
|
218
|
-
* date : Date
|
|
219
|
-
* })
|
|
214
|
+
* this.useValidationSchema()
|
|
220
215
|
* }
|
|
221
216
|
* }
|
|
222
217
|
* @return {this} this
|
|
223
218
|
*/
|
|
224
|
-
protected useValidationSchema(schema
|
|
219
|
+
protected useValidationSchema(schema?: null | Record<string, NumberConstructor | StringConstructor | DateConstructor>): this;
|
|
220
|
+
/**
|
|
221
|
+
*
|
|
222
|
+
* Assign schema column in model for validation data types
|
|
223
|
+
* @param {Object<NumberConstructor | StringConstructor | DateConstructor>} schema types (String Number and Date)
|
|
224
|
+
* @example
|
|
225
|
+
* class User extends Model {
|
|
226
|
+
* constructor() {
|
|
227
|
+
* this.useValidationSchema()
|
|
228
|
+
* }
|
|
229
|
+
* }
|
|
230
|
+
* @return {this} this
|
|
231
|
+
*/
|
|
232
|
+
protected useValidateSchema(schema?: null | Record<string, NumberConstructor | StringConstructor | DateConstructor>): this;
|
|
225
233
|
/**
|
|
226
234
|
* Assign hook function when execute returned results to callback function
|
|
227
235
|
* @param {Array<Function>} arrayFunctions functions for callback result
|
|
@@ -342,11 +350,18 @@ declare class Model extends AbstractModel {
|
|
|
342
350
|
with(...nameRelations: Array<string>): this;
|
|
343
351
|
/**
|
|
344
352
|
*
|
|
345
|
-
* Use relations in registry of model return
|
|
353
|
+
* Use relations in registry of model return ignore soft delete
|
|
354
|
+
* @param {...string} nameRelations if data exists return blank
|
|
355
|
+
* @return {this} this
|
|
356
|
+
*/
|
|
357
|
+
withAll(...nameRelations: Array<string>): this;
|
|
358
|
+
/**
|
|
359
|
+
*
|
|
360
|
+
* Use relations in registry of model return only in trash (soft delete)
|
|
346
361
|
* @param {...string} nameRelations if data exists return blank
|
|
347
362
|
* @return {this} this
|
|
348
363
|
*/
|
|
349
|
-
|
|
364
|
+
withTrashed(...nameRelations: Array<string>): this;
|
|
350
365
|
/**
|
|
351
366
|
*
|
|
352
367
|
* Use relations in registry of model return only exists result of relation query
|
|
@@ -547,7 +562,14 @@ declare class Model extends AbstractModel {
|
|
|
547
562
|
* @param {...string} nameRelations if data exists return blank
|
|
548
563
|
* @return {this} this
|
|
549
564
|
*/
|
|
550
|
-
|
|
565
|
+
relationsAll(...nameRelations: Array<string>): this;
|
|
566
|
+
/**
|
|
567
|
+
*
|
|
568
|
+
* Use relations in registry of model return only in trash (soft delete)
|
|
569
|
+
* @param {...string} nameRelations if data exists return blank
|
|
570
|
+
* @return {this} this
|
|
571
|
+
*/
|
|
572
|
+
relationsTrashed(...nameRelations: Array<string>): this;
|
|
551
573
|
/**
|
|
552
574
|
* Assign the relation in model Objects
|
|
553
575
|
* @param {object} relations registry relation in your model
|
|
@@ -654,6 +676,12 @@ declare class Model extends AbstractModel {
|
|
|
654
676
|
* @return {this} this
|
|
655
677
|
*/
|
|
656
678
|
protected belongsToManyBuilder({ name, as, model, localKey, foreignKey, freezeTable, pivot }: RelationQuery, callback?: Function): this;
|
|
679
|
+
/**
|
|
680
|
+
* where not null using NULL
|
|
681
|
+
* @override
|
|
682
|
+
* @return {this}
|
|
683
|
+
*/
|
|
684
|
+
whereTrashed(): this;
|
|
657
685
|
/**
|
|
658
686
|
* return only in trashed (data has been remove)
|
|
659
687
|
* @return {promise}
|
|
@@ -996,7 +1024,7 @@ declare class Model extends AbstractModel {
|
|
|
996
1024
|
private _handleRelations;
|
|
997
1025
|
private _handleRelationsQuery;
|
|
998
1026
|
private _validateMethod;
|
|
999
|
-
private
|
|
1027
|
+
private _checkSchemaOrNextError;
|
|
1000
1028
|
private _initialModel;
|
|
1001
1029
|
}
|
|
1002
1030
|
export { Model };
|
package/dist/lib/tspace/Model.js
CHANGED
|
@@ -73,7 +73,7 @@ class Model extends AbstractModel_1.AbstractModel {
|
|
|
73
73
|
* import { Blueprint } from 'tspace-mysql'
|
|
74
74
|
* class User extends Model {
|
|
75
75
|
* constructor() {
|
|
76
|
-
* this.
|
|
76
|
+
* this.useSchema ({
|
|
77
77
|
* id : new Blueprint().int().notNull().primary().autoIncrement(),
|
|
78
78
|
* uuid : new Blueprint().varchar(50).null(),
|
|
79
79
|
* email : new Blueprint().varchar(50).null(),
|
|
@@ -85,8 +85,8 @@ class Model extends AbstractModel_1.AbstractModel {
|
|
|
85
85
|
* }
|
|
86
86
|
* @return {this} this
|
|
87
87
|
*/
|
|
88
|
-
|
|
89
|
-
this.$state.set('
|
|
88
|
+
useSchema(schema) {
|
|
89
|
+
this.$state.set('SCHEMA_TABLE', schema);
|
|
90
90
|
return this;
|
|
91
91
|
}
|
|
92
92
|
/**
|
|
@@ -194,7 +194,7 @@ class Model extends AbstractModel_1.AbstractModel {
|
|
|
194
194
|
this.$constants('PATTERN').snake_case,
|
|
195
195
|
this.$constants('PATTERN').camelCase
|
|
196
196
|
];
|
|
197
|
-
this._assertError(!allowPattern.includes(pattern), `tspace-mysql support only pattern [${
|
|
197
|
+
this._assertError(!allowPattern.includes(pattern), `tspace-mysql support only pattern ["${this.$constants('PATTERN').snake_case}","${this.$constants('PATTERN').camelCase}"]`);
|
|
198
198
|
this.$state.set('PATTERN', pattern);
|
|
199
199
|
return this;
|
|
200
200
|
}
|
|
@@ -301,18 +301,31 @@ class Model extends AbstractModel_1.AbstractModel {
|
|
|
301
301
|
* @example
|
|
302
302
|
* class User extends Model {
|
|
303
303
|
* constructor() {
|
|
304
|
-
* this.useValidationSchema(
|
|
305
|
-
* id : Number,
|
|
306
|
-
* email : String,
|
|
307
|
-
* name : String,
|
|
308
|
-
* date : Date
|
|
309
|
-
* })
|
|
304
|
+
* this.useValidationSchema()
|
|
310
305
|
* }
|
|
311
306
|
* }
|
|
312
307
|
* @return {this} this
|
|
313
308
|
*/
|
|
314
309
|
useValidationSchema(schema) {
|
|
315
|
-
this.$state.set('
|
|
310
|
+
this.$state.set('VALIDATE_SCHEMA', true);
|
|
311
|
+
this.$state.set('VALIDATE_SCHEMA_DEFINED', schema);
|
|
312
|
+
return this;
|
|
313
|
+
}
|
|
314
|
+
/**
|
|
315
|
+
*
|
|
316
|
+
* Assign schema column in model for validation data types
|
|
317
|
+
* @param {Object<NumberConstructor | StringConstructor | DateConstructor>} schema types (String Number and Date)
|
|
318
|
+
* @example
|
|
319
|
+
* class User extends Model {
|
|
320
|
+
* constructor() {
|
|
321
|
+
* this.useValidationSchema()
|
|
322
|
+
* }
|
|
323
|
+
* }
|
|
324
|
+
* @return {this} this
|
|
325
|
+
*/
|
|
326
|
+
useValidateSchema(schema) {
|
|
327
|
+
this.$state.set('VALIDATE_SCHEMA', true);
|
|
328
|
+
this.$state.set('VALIDATE_SCHEMA_DEFINED', schema);
|
|
316
329
|
return this;
|
|
317
330
|
}
|
|
318
331
|
/**
|
|
@@ -340,8 +353,8 @@ class Model extends AbstractModel_1.AbstractModel {
|
|
|
340
353
|
*/
|
|
341
354
|
exceptColumns() {
|
|
342
355
|
return __awaiter(this, void 0, void 0, function* () {
|
|
343
|
-
if (this.$state.get('
|
|
344
|
-
const columns = Object.keys(this.$state.get('
|
|
356
|
+
if (this.$state.get('SCHEMA_TABLE')) {
|
|
357
|
+
const columns = Object.keys(this.$state.get('SCHEMA_TABLE'));
|
|
345
358
|
const removeExcept = columns.filter((column) => {
|
|
346
359
|
const excepts = this.$state.get('EXCEPT');
|
|
347
360
|
return excepts.every((except) => except !== column);
|
|
@@ -437,7 +450,7 @@ class Model extends AbstractModel_1.AbstractModel {
|
|
|
437
450
|
return result;
|
|
438
451
|
}
|
|
439
452
|
catch (e) {
|
|
440
|
-
yield this.
|
|
453
|
+
yield this._checkSchemaOrNextError(e);
|
|
441
454
|
return yield this.queryStatement(sql);
|
|
442
455
|
}
|
|
443
456
|
});
|
|
@@ -465,7 +478,7 @@ class Model extends AbstractModel_1.AbstractModel {
|
|
|
465
478
|
return result;
|
|
466
479
|
}
|
|
467
480
|
catch (e) {
|
|
468
|
-
yield this.
|
|
481
|
+
yield this._checkSchemaOrNextError(e);
|
|
469
482
|
return yield this.actionStatement({
|
|
470
483
|
sql,
|
|
471
484
|
returnId
|
|
@@ -549,11 +562,32 @@ class Model extends AbstractModel_1.AbstractModel {
|
|
|
549
562
|
}
|
|
550
563
|
/**
|
|
551
564
|
*
|
|
552
|
-
* Use relations in registry of model return
|
|
565
|
+
* Use relations in registry of model return ignore soft delete
|
|
553
566
|
* @param {...string} nameRelations if data exists return blank
|
|
554
567
|
* @return {this} this
|
|
555
568
|
*/
|
|
556
|
-
|
|
569
|
+
withAll(...nameRelations) {
|
|
570
|
+
const relations = this._handleRelations(nameRelations);
|
|
571
|
+
relations.forEach(relation => relation.all = true);
|
|
572
|
+
const setRelations = this.$state.get('RELATIONS').length
|
|
573
|
+
? [...relations.map((w) => {
|
|
574
|
+
const exists = this.$state.get('RELATIONS').find((r) => r.name === w.name);
|
|
575
|
+
if (exists)
|
|
576
|
+
return null;
|
|
577
|
+
return w;
|
|
578
|
+
}).filter((d) => d != null),
|
|
579
|
+
...this.$state.get('RELATIONS')]
|
|
580
|
+
: relations;
|
|
581
|
+
this.$state.set('RELATIONS', setRelations);
|
|
582
|
+
return this;
|
|
583
|
+
}
|
|
584
|
+
/**
|
|
585
|
+
*
|
|
586
|
+
* Use relations in registry of model return only in trash (soft delete)
|
|
587
|
+
* @param {...string} nameRelations if data exists return blank
|
|
588
|
+
* @return {this} this
|
|
589
|
+
*/
|
|
590
|
+
withTrashed(...nameRelations) {
|
|
557
591
|
const relations = this._handleRelations(nameRelations);
|
|
558
592
|
relations.forEach(relation => relation.trashed = true);
|
|
559
593
|
const setRelations = this.$state.get('RELATIONS').length
|
|
@@ -799,8 +833,17 @@ class Model extends AbstractModel_1.AbstractModel {
|
|
|
799
833
|
* @param {...string} nameRelations if data exists return blank
|
|
800
834
|
* @return {this} this
|
|
801
835
|
*/
|
|
802
|
-
|
|
803
|
-
return this.
|
|
836
|
+
relationsAll(...nameRelations) {
|
|
837
|
+
return this.withAll(...nameRelations);
|
|
838
|
+
}
|
|
839
|
+
/**
|
|
840
|
+
*
|
|
841
|
+
* Use relations in registry of model return only in trash (soft delete)
|
|
842
|
+
* @param {...string} nameRelations if data exists return blank
|
|
843
|
+
* @return {this} this
|
|
844
|
+
*/
|
|
845
|
+
relationsTrashed(...nameRelations) {
|
|
846
|
+
return this.withTrashed(...nameRelations);
|
|
804
847
|
}
|
|
805
848
|
/**
|
|
806
849
|
* Assign the relation in model Objects
|
|
@@ -1047,6 +1090,16 @@ class Model extends AbstractModel_1.AbstractModel {
|
|
|
1047
1090
|
r.query = callback(new r.model());
|
|
1048
1091
|
return this;
|
|
1049
1092
|
}
|
|
1093
|
+
/**
|
|
1094
|
+
* where not null using NULL
|
|
1095
|
+
* @override
|
|
1096
|
+
* @return {this}
|
|
1097
|
+
*/
|
|
1098
|
+
whereTrashed() {
|
|
1099
|
+
this.disableSoftDelete();
|
|
1100
|
+
this.whereNotNull(this._valuePattern(this.$state.get('SOFT_DELETE_FORMAT')));
|
|
1101
|
+
return this;
|
|
1102
|
+
}
|
|
1050
1103
|
/**
|
|
1051
1104
|
* return only in trashed (data has been remove)
|
|
1052
1105
|
* @return {promise}
|
|
@@ -1667,44 +1720,7 @@ class Model extends AbstractModel_1.AbstractModel {
|
|
|
1667
1720
|
`${this.$constants('FROM')}`,
|
|
1668
1721
|
`\`${this.$state.get('TABLE_NAME').replace(/\`/g, '')}\``
|
|
1669
1722
|
].join(' ');
|
|
1670
|
-
|
|
1671
|
-
const schemas = raw.map((r) => {
|
|
1672
|
-
let schema = { [r.Field]: String };
|
|
1673
|
-
const numberLists = [
|
|
1674
|
-
'tinyint',
|
|
1675
|
-
'smallint',
|
|
1676
|
-
'mediumint',
|
|
1677
|
-
'int',
|
|
1678
|
-
'bigint',
|
|
1679
|
-
'float',
|
|
1680
|
-
'double',
|
|
1681
|
-
'decimal',
|
|
1682
|
-
'real',
|
|
1683
|
-
'bit',
|
|
1684
|
-
'boolean',
|
|
1685
|
-
'serial'
|
|
1686
|
-
];
|
|
1687
|
-
const dateAndTimeLists = [
|
|
1688
|
-
'date',
|
|
1689
|
-
'datetime',
|
|
1690
|
-
'time',
|
|
1691
|
-
'timestamp',
|
|
1692
|
-
'year'
|
|
1693
|
-
];
|
|
1694
|
-
if (numberLists.includes(r.Type)) {
|
|
1695
|
-
schema = {
|
|
1696
|
-
[r.Field]: Number
|
|
1697
|
-
};
|
|
1698
|
-
}
|
|
1699
|
-
if (dateAndTimeLists.includes(r.Type)) {
|
|
1700
|
-
schema = {
|
|
1701
|
-
[r.Field]: Date
|
|
1702
|
-
};
|
|
1703
|
-
}
|
|
1704
|
-
return schema;
|
|
1705
|
-
});
|
|
1706
|
-
const result = Object.assign({}, ...schemas);
|
|
1707
|
-
return result;
|
|
1723
|
+
return yield this.queryStatement(sql);
|
|
1708
1724
|
});
|
|
1709
1725
|
}
|
|
1710
1726
|
/**
|
|
@@ -1949,27 +1965,37 @@ class Model extends AbstractModel_1.AbstractModel {
|
|
|
1949
1965
|
return result;
|
|
1950
1966
|
}
|
|
1951
1967
|
_validateSchema(results) {
|
|
1952
|
-
const schema = this.$state.get('SCHEMA');
|
|
1953
|
-
if (schema == null)
|
|
1954
|
-
return;
|
|
1955
1968
|
if (!results.length)
|
|
1956
1969
|
return;
|
|
1970
|
+
const validateSchema = Boolean(this.$state.get('VALIDATE_SCHEMA'));
|
|
1971
|
+
if (!validateSchema)
|
|
1972
|
+
return;
|
|
1973
|
+
const schemaTable = this.$state.get('SCHEMA_TABLE');
|
|
1974
|
+
const schemaTableDefined = this.$state.get('VALIDATE_SCHEMA_DEFINED');
|
|
1975
|
+
this._assertError(schemaTableDefined == null && schemaTable == null, "Can't validate schema withouted schema");
|
|
1976
|
+
const schema = schemaTableDefined !== null && schemaTableDefined !== void 0 ? schemaTableDefined : Object.keys(schemaTable).reduce((acc, key) => {
|
|
1977
|
+
acc[key] = schemaTable[key].valueType;
|
|
1978
|
+
return acc;
|
|
1979
|
+
}, {});
|
|
1980
|
+
if (schema == null)
|
|
1981
|
+
return;
|
|
1957
1982
|
const typeOf = (data) => Object.prototype.toString.apply(data).slice(8, -1).toLocaleLowerCase();
|
|
1958
1983
|
const regexDate = /[0-9]{4}-(0[1-9]|1[0-2])-(0[1-9]|[1-2][0-9]|3[0-1])/;
|
|
1959
1984
|
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]/;
|
|
1960
|
-
const
|
|
1985
|
+
const select = this.$state.get('SELECT');
|
|
1986
|
+
const selectedAll = select.replace('SELECT', '').trim() === '*';
|
|
1961
1987
|
for (const result of results) {
|
|
1962
1988
|
const schemaKeys = Object.keys(schema);
|
|
1963
1989
|
const resultKeys = Object.keys(result);
|
|
1964
1990
|
if (schemaKeys.some(s => !resultKeys.includes(s)) && selectedAll) {
|
|
1965
1991
|
const columns = schemaKeys.filter(x => !resultKeys.includes(x));
|
|
1966
|
-
this._assertError(`Not found this column
|
|
1992
|
+
this._assertError(`Not found this column "${columns.join(', ')}" in result`);
|
|
1967
1993
|
}
|
|
1968
1994
|
for (const column in result) {
|
|
1969
1995
|
const s = schema[column];
|
|
1970
1996
|
if (s == null && selectedAll) {
|
|
1971
1997
|
if (!schemaKeys.every(s => resultKeys.includes(s))) {
|
|
1972
|
-
this._assertError(`Not found this column
|
|
1998
|
+
this._assertError(`Not found this column "${column}" in result`);
|
|
1973
1999
|
}
|
|
1974
2000
|
continue;
|
|
1975
2001
|
}
|
|
@@ -1978,13 +2004,13 @@ class Model extends AbstractModel_1.AbstractModel {
|
|
|
1978
2004
|
if (regexDate.test(result[column]) || regexDateTime.test(result[column])) {
|
|
1979
2005
|
if (typeOf(new Date(result[column])) === typeOf(new s()))
|
|
1980
2006
|
continue;
|
|
1981
|
-
this._assertError(`This column
|
|
2007
|
+
this._assertError(`This column "${column}" is invalid schema field type`);
|
|
1982
2008
|
}
|
|
1983
2009
|
if (result[column] == null)
|
|
1984
2010
|
continue;
|
|
1985
2011
|
if (typeOf(result[column]) === typeOf(new s()))
|
|
1986
2012
|
continue;
|
|
1987
|
-
this._assertError(`This column
|
|
2013
|
+
this._assertError(`This column "${column}" is invalid schema field type`);
|
|
1988
2014
|
}
|
|
1989
2015
|
}
|
|
1990
2016
|
return;
|
|
@@ -2152,7 +2178,8 @@ class Model extends AbstractModel_1.AbstractModel {
|
|
|
2152
2178
|
.bind(this.$pool.get())
|
|
2153
2179
|
.whereIn(foreignKey, dataPerentId)
|
|
2154
2180
|
.debug(this.$state.get('DEBUG'))
|
|
2155
|
-
.when(relation.trashed, (query) => query.
|
|
2181
|
+
.when(relation.trashed, (query) => query.whereTrashed())
|
|
2182
|
+
.when(relation.all, (query) => query.disableSoftDelete())
|
|
2156
2183
|
.get();
|
|
2157
2184
|
return dataFromRelation;
|
|
2158
2185
|
});
|
|
@@ -2185,7 +2212,8 @@ class Model extends AbstractModel_1.AbstractModel {
|
|
|
2185
2212
|
const sqldataChilds = queryChildModel
|
|
2186
2213
|
.whereIn(localKeyPivotTable, dataPerentId)
|
|
2187
2214
|
.when(relation.exists, (query) => query.whereExists(sql))
|
|
2188
|
-
.when(relation.trashed, (query) => query.
|
|
2215
|
+
.when(relation.trashed, (query) => query.whereTrashed())
|
|
2216
|
+
.when(relation.all, (query) => query.disableSoftDelete())
|
|
2189
2217
|
.toString();
|
|
2190
2218
|
const dataChilds = yield this.queryStatement(sqldataChilds);
|
|
2191
2219
|
const otherId = dataChilds.map((sub) => sub[otherforeignKey]).filter((data) => data != null);
|
|
@@ -2803,23 +2831,48 @@ class Model extends AbstractModel_1.AbstractModel {
|
|
|
2803
2831
|
}
|
|
2804
2832
|
}
|
|
2805
2833
|
}
|
|
2806
|
-
|
|
2807
|
-
var _a;
|
|
2834
|
+
_checkSchemaOrNextError(e) {
|
|
2835
|
+
var _a, _b, _c, _d, _e;
|
|
2808
2836
|
return __awaiter(this, void 0, void 0, function* () {
|
|
2809
|
-
const createTable = this.$state.get('CREATE_TABLE');
|
|
2810
|
-
if (createTable == null)
|
|
2811
|
-
throw e;
|
|
2812
|
-
const errorMessage = (_a = e === null || e === void 0 ? void 0 : e.message) !== null && _a !== void 0 ? _a : '';
|
|
2813
|
-
const errorWhenTableIsNotExists = "doesn't exist";
|
|
2814
|
-
if (!errorMessage.toLocaleLowerCase().includes(errorWhenTableIsNotExists))
|
|
2815
|
-
throw e;
|
|
2816
|
-
if (this.$state.get('QUERIES') > 3)
|
|
2817
|
-
throw e;
|
|
2818
2837
|
try {
|
|
2838
|
+
if (this.$state.get('QUERIES') > 3)
|
|
2839
|
+
throw e;
|
|
2840
|
+
const schemaTable = this.$state.get('SCHEMA_TABLE');
|
|
2841
|
+
if (schemaTable == null)
|
|
2842
|
+
throw e;
|
|
2843
|
+
const errorMessage = (_a = e === null || e === void 0 ? void 0 : e.message) !== null && _a !== void 0 ? _a : '';
|
|
2844
|
+
if (errorMessage.toLocaleLowerCase().includes('unknown column')) {
|
|
2845
|
+
const pattern = /'([^']+)'/;
|
|
2846
|
+
const column = errorMessage.match(pattern)
|
|
2847
|
+
? String(errorMessage.match(pattern)[0]).replace(/'/g, '').split('.').pop()
|
|
2848
|
+
: null;
|
|
2849
|
+
if (column == null)
|
|
2850
|
+
throw e;
|
|
2851
|
+
const type = (_c = (_b = schemaTable[column]) === null || _b === void 0 ? void 0 : _b.type) !== null && _c !== void 0 ? _c : null;
|
|
2852
|
+
const attributes = (_e = (_d = schemaTable[column]) === null || _d === void 0 ? void 0 : _d.attributes) !== null && _e !== void 0 ? _e : null;
|
|
2853
|
+
if (type == null || attributes == null)
|
|
2854
|
+
throw e;
|
|
2855
|
+
const entries = Object.entries(schemaTable);
|
|
2856
|
+
const indexWithColumn = entries.findIndex(([key]) => key === column);
|
|
2857
|
+
const findAfterIndex = indexWithColumn ? entries[indexWithColumn - 1][0] : null;
|
|
2858
|
+
if (findAfterIndex == null)
|
|
2859
|
+
throw e;
|
|
2860
|
+
const sql = [
|
|
2861
|
+
`${this.$constants('ALTER_TABLE')}`,
|
|
2862
|
+
`${this.$state.get('TABLE_NAME')}`,
|
|
2863
|
+
`${this.$constants('ADD')}`,
|
|
2864
|
+
`\`${column}\` ${type} ${attributes.join(' ')}`,
|
|
2865
|
+
`${this.$constants('AFTER')} \`${findAfterIndex !== null && findAfterIndex !== void 0 ? findAfterIndex : ''}\``
|
|
2866
|
+
].join(' ');
|
|
2867
|
+
yield this.queryStatement(sql);
|
|
2868
|
+
return;
|
|
2869
|
+
}
|
|
2870
|
+
if (!errorMessage.toLocaleLowerCase().includes("doesn't exist"))
|
|
2871
|
+
throw e;
|
|
2819
2872
|
const tableName = this.$state.get('TABLE_NAME');
|
|
2820
|
-
|
|
2821
|
-
|
|
2822
|
-
|
|
2873
|
+
const sql = new Schema_1.Schema().createTable(tableName, schemaTable);
|
|
2874
|
+
yield this.queryStatement(sql);
|
|
2875
|
+
return;
|
|
2823
2876
|
}
|
|
2824
2877
|
catch (e) {
|
|
2825
2878
|
throw e;
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { Builder } from "./Builder";
|
|
2
2
|
declare class Schema extends Builder {
|
|
3
3
|
table: (table: string, schemas: Record<string, any>) => Promise<void>;
|
|
4
|
-
createTable: (table: string, schemas: Record<string, any>) =>
|
|
4
|
+
createTable: (table: string, schemas: Record<string, any>) => string;
|
|
5
5
|
}
|
|
6
6
|
export { Schema };
|
|
7
7
|
export default Schema;
|