tspace-mysql 1.6.5 → 1.6.6-dev.1
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 +50 -3
- package/build/cli/models/make.js +2 -2
- package/build/cli/models/make.js.map +1 -1
- package/build/cli/models/model.d.ts +5 -1
- package/build/cli/models/model.js +31 -2
- package/build/cli/models/model.js.map +1 -1
- package/build/cli/tables/make.js +1 -1
- package/build/cli/tables/make.js.map +1 -1
- package/build/cli/tables/table.d.ts +5 -1
- package/build/cli/tables/table.js +32 -4
- package/build/cli/tables/table.js.map +1 -1
- package/build/lib/connection/index.d.ts +1 -0
- package/build/lib/connection/index.js +25 -16
- package/build/lib/connection/index.js.map +1 -1
- package/build/lib/connection/options.js +17 -15
- package/build/lib/connection/options.js.map +1 -1
- package/build/lib/constants/index.d.ts +96 -1
- package/build/lib/constants/index.js +3 -0
- package/build/lib/constants/index.js.map +1 -1
- package/build/lib/core/Abstracts/AbstractBuilder.d.ts +5 -7
- package/build/lib/core/Abstracts/AbstractBuilder.js +0 -5
- package/build/lib/core/Abstracts/AbstractBuilder.js.map +1 -1
- package/build/lib/core/Abstracts/AbstractModel.d.ts +16 -16
- package/build/lib/core/Builder.d.ts +62 -3
- package/build/lib/core/Builder.js +188 -32
- package/build/lib/core/Builder.js.map +1 -1
- package/build/lib/core/DB.d.ts +15 -2
- package/build/lib/core/DB.js +17 -0
- package/build/lib/core/DB.js.map +1 -1
- package/build/lib/core/Handlers/Relation.d.ts +3 -3
- package/build/lib/core/Handlers/Relation.js +75 -50
- package/build/lib/core/Handlers/Relation.js.map +1 -1
- package/build/lib/core/Handlers/State.d.ts +130 -2
- package/build/lib/core/Handlers/State.js +4 -12
- package/build/lib/core/Handlers/State.js.map +1 -1
- package/build/lib/core/Model.d.ts +73 -14
- package/build/lib/core/Model.js +278 -123
- package/build/lib/core/Model.js.map +1 -1
- package/build/lib/core/Schema.js +1 -1
- package/build/lib/types.d.ts +3 -3
- package/build/lib/utils/index.d.ts +4 -1
- package/build/lib/utils/index.js +29 -4
- package/build/lib/utils/index.js.map +1 -1
- package/build/tests/01-Pool.test.js +2 -2
- package/build/tests/01-Pool.test.js.map +1 -1
- package/build/tests/02-DB.test.js.map +1 -1
- package/build/tests/03-Model.test.js +63 -17
- package/build/tests/03-Model.test.js.map +1 -1
- package/build/tests/schema-spec.d.ts +91 -26
- package/build/tests/schema-spec.js +18 -17
- package/build/tests/schema-spec.js.map +1 -1
- package/package.json +2 -1
|
@@ -1,7 +1,6 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.AbstractBuilder = void 0;
|
|
4
|
-
const State_1 = require("../Handlers/State");
|
|
5
4
|
class AbstractBuilder {
|
|
6
5
|
constructor() {
|
|
7
6
|
this.$setters = [
|
|
@@ -13,9 +12,6 @@ class AbstractBuilder {
|
|
|
13
12
|
'$state',
|
|
14
13
|
'$relation'
|
|
15
14
|
];
|
|
16
|
-
this.$utils = {};
|
|
17
|
-
this.$constants = (name) => { };
|
|
18
|
-
this.$state = new State_1.StateHandler('default');
|
|
19
15
|
this.$pool = {
|
|
20
16
|
query: (sql) => { },
|
|
21
17
|
set: (pool) => { },
|
|
@@ -27,7 +23,6 @@ class AbstractBuilder {
|
|
|
27
23
|
reset: () => { },
|
|
28
24
|
check: (value) => true || false
|
|
29
25
|
};
|
|
30
|
-
this.$attributes = null;
|
|
31
26
|
}
|
|
32
27
|
}
|
|
33
28
|
exports.AbstractBuilder = AbstractBuilder;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"AbstractBuilder.js","sourceRoot":"","sources":["../../../../src/lib/core/Abstracts/AbstractBuilder.ts"],"names":[],"mappings":";;;
|
|
1
|
+
{"version":3,"file":"AbstractBuilder.js","sourceRoot":"","sources":["../../../../src/lib/core/Abstracts/AbstractBuilder.ts"],"names":[],"mappings":";;;AAKA,MAAe,eAAe;IAA9B;QAEc,aAAQ,GAAG;YACjB,aAAa;YACb,SAAS;YACT,QAAQ;YACR,YAAY;YACZ,OAAO;YACP,QAAQ;YACR,WAAW;SACd,CAAA;QAQS,UAAK,GAA2D;YACtE,KAAK,EAAE,CAAC,GAAW,EAAE,EAAE,GAAE,CAAC;YAC1B,GAAG,EAAE,CAAC,IAAU,EAAE,EAAE,GAAE,CAAC;YACvB,GAAG,EAAE,GAAG,EAAE,GAAE,CAAC;SAChB,CAAA;QAES,YAAO,GAA8G;YAC3H,GAAG,EAAE,GAAG,EAAE,GAAE,CAAC;YACb,GAAG,EAAE,CAAC,KAAc,EAAE,EAAE,GAAE,CAAC;YAC3B,KAAK,EAAE,GAAG,EAAE,GAAE,CAAC;YACf,KAAK,EAAE,CAAC,KAAc,EAAE,EAAE,CAAC,IAAI,IAAI,KAAK;SAC3C,CAAA;IA2FL,CAAC;CAAA;AAES,0CAAe;AACzB,kBAAe,eAAe,CAAA"}
|
|
@@ -4,30 +4,30 @@ import { Builder } from '../Builder';
|
|
|
4
4
|
import { RelationHandler } from '../Handlers/Relation';
|
|
5
5
|
import { Model } from '../Model';
|
|
6
6
|
declare abstract class AbstractModel<T, R> extends Builder {
|
|
7
|
-
protected $relation: RelationHandler
|
|
8
|
-
protected $schema: Record<string, Blueprint
|
|
9
|
-
protected $validateSchema: TValidateSchema
|
|
10
|
-
protected $table: string
|
|
11
|
-
protected $pattern: TPattern
|
|
12
|
-
protected $hasMany: TRelationQueryOptions[]
|
|
13
|
-
protected $hasOne: TRelationQueryOptions[]
|
|
14
|
-
protected $belongsTo: TRelationQueryOptions[]
|
|
15
|
-
protected $belongsToMany: TRelationQueryOptions[]
|
|
16
|
-
protected $timestamp: boolean
|
|
17
|
-
protected $softDelete: boolean
|
|
18
|
-
protected $uuid: boolean
|
|
19
|
-
protected $uuidColumn: string
|
|
7
|
+
protected $relation: RelationHandler;
|
|
8
|
+
protected $schema: Record<string, Blueprint>;
|
|
9
|
+
protected $validateSchema: TValidateSchema;
|
|
10
|
+
protected $table: string;
|
|
11
|
+
protected $pattern: TPattern;
|
|
12
|
+
protected $hasMany: TRelationQueryOptions[];
|
|
13
|
+
protected $hasOne: TRelationQueryOptions[];
|
|
14
|
+
protected $belongsTo: TRelationQueryOptions[];
|
|
15
|
+
protected $belongsToMany: TRelationQueryOptions[];
|
|
16
|
+
protected $timestamp: boolean;
|
|
17
|
+
protected $softDelete: boolean;
|
|
18
|
+
protected $uuid: boolean;
|
|
19
|
+
protected $uuidColumn: string;
|
|
20
20
|
protected $timestampColumns: {
|
|
21
21
|
createdAt: string;
|
|
22
22
|
updatedAt: string;
|
|
23
|
-
}
|
|
24
|
-
protected $softDeleteColumn: string
|
|
23
|
+
};
|
|
24
|
+
protected $softDeleteColumn: string;
|
|
25
25
|
protected $observer: (new () => {
|
|
26
26
|
selected: Function;
|
|
27
27
|
created: Function;
|
|
28
28
|
updated: Function;
|
|
29
29
|
deleted: Function;
|
|
30
|
-
})
|
|
30
|
+
});
|
|
31
31
|
protected abstract useUUID(): this;
|
|
32
32
|
protected abstract usePrimaryKey(primaryKey: string): this;
|
|
33
33
|
protected abstract useRegistry(): this;
|
|
@@ -8,6 +8,21 @@ declare class Builder extends AbstractBuilder {
|
|
|
8
8
|
* @returns {Builder} instance of the Builder
|
|
9
9
|
*/
|
|
10
10
|
static get instance(): Builder;
|
|
11
|
+
/**
|
|
12
|
+
* The 'unset' method is used to drop a property as desired.
|
|
13
|
+
*
|
|
14
|
+
* @returns {this} this
|
|
15
|
+
*/
|
|
16
|
+
unset(options: {
|
|
17
|
+
select?: boolean;
|
|
18
|
+
where?: boolean;
|
|
19
|
+
join?: boolean;
|
|
20
|
+
limit?: boolean;
|
|
21
|
+
offset?: boolean;
|
|
22
|
+
orderBy?: boolean;
|
|
23
|
+
groupBy?: boolean;
|
|
24
|
+
having?: boolean;
|
|
25
|
+
}): this;
|
|
11
26
|
/**
|
|
12
27
|
* The 'distinct' method is used to apply the DISTINCT keyword to a database query.
|
|
13
28
|
*
|
|
@@ -41,7 +56,16 @@ declare class Builder extends AbstractBuilder {
|
|
|
41
56
|
* @param {string} alias as name of the column
|
|
42
57
|
* @returns {this} this
|
|
43
58
|
*/
|
|
44
|
-
selectObject(object: Record<string, string
|
|
59
|
+
selectObject(object: Record<string, `${string}.${string}`>, alias: string): this;
|
|
60
|
+
/**
|
|
61
|
+
* The 'selectObject' method is used to specify which columns you want to retrieve from a database table.
|
|
62
|
+
*
|
|
63
|
+
* It allows you to choose the specific columns that should be included in the result set to 'Object' of a database query.
|
|
64
|
+
* @param {string} object table name
|
|
65
|
+
* @param {string} alias as name of the column
|
|
66
|
+
* @returns {this} this
|
|
67
|
+
*/
|
|
68
|
+
selectArray(object: Record<string, `${string}.${string}`>, alias: string): this;
|
|
45
69
|
/**
|
|
46
70
|
* The 'sleep' method is used to delay the query.
|
|
47
71
|
*
|
|
@@ -133,6 +157,33 @@ declare class Builder extends AbstractBuilder {
|
|
|
133
157
|
* @returns {this}
|
|
134
158
|
*/
|
|
135
159
|
orWhere(column: string, operator?: any, value?: any): this;
|
|
160
|
+
/**
|
|
161
|
+
* The 'whereDay' method is used to add a "where" clause that filters results based on the day part of a date column.
|
|
162
|
+
*
|
|
163
|
+
* It is especially useful for querying records that fall within a specific day.
|
|
164
|
+
* @param {string} column
|
|
165
|
+
* @param {number} day
|
|
166
|
+
* @returns {this}
|
|
167
|
+
*/
|
|
168
|
+
whereDay(column: string, day: number): this;
|
|
169
|
+
/**
|
|
170
|
+
* The 'whereMonth' method is used to add a "where" clause that filters results based on the month part of a date column.
|
|
171
|
+
*
|
|
172
|
+
* It is especially useful for querying records that fall within a specific month.
|
|
173
|
+
* @param {string} column
|
|
174
|
+
* @param {number} month
|
|
175
|
+
* @returns {this}
|
|
176
|
+
*/
|
|
177
|
+
whereMonth(column: string, month: number): this;
|
|
178
|
+
/**
|
|
179
|
+
* The 'whereYear' method is used to add a "where" clause that filters results based on the year part of a date column.
|
|
180
|
+
*
|
|
181
|
+
* It is especially useful for querying records that fall within a specific year.
|
|
182
|
+
* @param {string} column
|
|
183
|
+
* @param {number} year
|
|
184
|
+
* @returns {this}
|
|
185
|
+
*/
|
|
186
|
+
whereYear(column: string, year: number): this;
|
|
136
187
|
/**
|
|
137
188
|
* The 'whereRaw' method is used to add a raw SQL condition to a database query.
|
|
138
189
|
*
|
|
@@ -580,7 +631,7 @@ declare class Builder extends AbstractBuilder {
|
|
|
580
631
|
* @param {string?} order by default order = 'asc' but you can used 'asc' or 'desc'
|
|
581
632
|
* @returns {this}
|
|
582
633
|
*/
|
|
583
|
-
orderBy(column: string, order?: 'ASC' | 'DESC'): this;
|
|
634
|
+
orderBy(column: string, order?: 'ASC' | 'asc' | 'DESC' | 'desc'): this;
|
|
584
635
|
/**
|
|
585
636
|
* The 'orderByRaw' method is used to specify the order in which the results of a database query should be sorted.
|
|
586
637
|
*
|
|
@@ -992,6 +1043,13 @@ declare class Builder extends AbstractBuilder {
|
|
|
992
1043
|
* @returns {promise<any>}
|
|
993
1044
|
*/
|
|
994
1045
|
rawQuery(sql: string): Promise<any>;
|
|
1046
|
+
/**
|
|
1047
|
+
* This 'rawQuery' method is used to execute sql statement
|
|
1048
|
+
*
|
|
1049
|
+
* @param {string} sql
|
|
1050
|
+
* @returns {promise<any>}
|
|
1051
|
+
*/
|
|
1052
|
+
static rawQuery(sql: string): Promise<any>;
|
|
995
1053
|
/**
|
|
996
1054
|
*
|
|
997
1055
|
* plus value then update
|
|
@@ -1298,7 +1356,7 @@ declare class Builder extends AbstractBuilder {
|
|
|
1298
1356
|
* @param {number} rows number of rows
|
|
1299
1357
|
* @returns {promise<any>}
|
|
1300
1358
|
*/
|
|
1301
|
-
faker(rows: number, cb?:
|
|
1359
|
+
faker(rows: number, cb?: (results: Record<string, any>, index: number) => Record<string, any>): Promise<void>;
|
|
1302
1360
|
/**
|
|
1303
1361
|
*
|
|
1304
1362
|
* truncate of table
|
|
@@ -1343,6 +1401,7 @@ declare class Builder extends AbstractBuilder {
|
|
|
1343
1401
|
any: () => string;
|
|
1344
1402
|
};
|
|
1345
1403
|
protected _resultHandler(data: any): any;
|
|
1404
|
+
protected _resultHandlerExists(data: any): any;
|
|
1346
1405
|
whereReference(tableAndLocalKey: string, tableAndForeignKey?: string): this;
|
|
1347
1406
|
protected _queryStatement(sql: string): Promise<any[]>;
|
|
1348
1407
|
protected _actionStatement({ sql, returnId }: {
|
|
@@ -40,6 +40,30 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
|
|
|
40
40
|
static get instance() {
|
|
41
41
|
return new this();
|
|
42
42
|
}
|
|
43
|
+
/**
|
|
44
|
+
* The 'unset' method is used to drop a property as desired.
|
|
45
|
+
*
|
|
46
|
+
* @returns {this} this
|
|
47
|
+
*/
|
|
48
|
+
unset(options) {
|
|
49
|
+
if ((options === null || options === void 0 ? void 0 : options.select) != null && options.select)
|
|
50
|
+
this.$state.set('SELECT', []);
|
|
51
|
+
if ((options === null || options === void 0 ? void 0 : options.join) != null && options.join)
|
|
52
|
+
this.$state.set('JOIN', []);
|
|
53
|
+
if ((options === null || options === void 0 ? void 0 : options.where) != null && options.where)
|
|
54
|
+
this.$state.set('WHERE', []);
|
|
55
|
+
if ((options === null || options === void 0 ? void 0 : options.groupBy) != null && options.groupBy)
|
|
56
|
+
this.$state.set('GROUP_BY', []);
|
|
57
|
+
if ((options === null || options === void 0 ? void 0 : options.having) != null && options.having)
|
|
58
|
+
this.$state.set('HAVING', '');
|
|
59
|
+
if ((options === null || options === void 0 ? void 0 : options.orderBy) != null && options.orderBy)
|
|
60
|
+
this.$state.set('ORDER_BY', []);
|
|
61
|
+
if ((options === null || options === void 0 ? void 0 : options.limit) != null && options.limit)
|
|
62
|
+
this.$state.set('LIMIT', '');
|
|
63
|
+
if ((options === null || options === void 0 ? void 0 : options.offset) != null && options.offset)
|
|
64
|
+
this.$state.set('OFFSET', '');
|
|
65
|
+
return this;
|
|
66
|
+
}
|
|
43
67
|
/**
|
|
44
68
|
* The 'distinct' method is used to apply the DISTINCT keyword to a database query.
|
|
45
69
|
*
|
|
@@ -126,7 +150,45 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
|
|
|
126
150
|
}
|
|
127
151
|
maping = [...maping, `'${key}'`, `\`${this.getTableName()}\`.\`${value}\``];
|
|
128
152
|
}
|
|
129
|
-
const json =
|
|
153
|
+
const json = `
|
|
154
|
+
${this.$constants('CASE')}
|
|
155
|
+
${this.$constants('WHEN')} COUNT(${Object.values(maping)[1]}) = 0 ${this.$constants('THEN')} ${this.$constants('NULL')}
|
|
156
|
+
${this.$constants('ELSE')} ${this.$constants('JSON_OBJECT')}(${maping.join(' , ')})
|
|
157
|
+
${this.$constants('END')}
|
|
158
|
+
${this.$constants('AS')} \`${alias}\`
|
|
159
|
+
`;
|
|
160
|
+
this.$state.set('SELECT', [...this.$state.get('SELECT'), json]);
|
|
161
|
+
return this;
|
|
162
|
+
}
|
|
163
|
+
/**
|
|
164
|
+
* The 'selectObject' method is used to specify which columns you want to retrieve from a database table.
|
|
165
|
+
*
|
|
166
|
+
* It allows you to choose the specific columns that should be included in the result set to 'Object' of a database query.
|
|
167
|
+
* @param {string} object table name
|
|
168
|
+
* @param {string} alias as name of the column
|
|
169
|
+
* @returns {this} this
|
|
170
|
+
*/
|
|
171
|
+
selectArray(object, alias) {
|
|
172
|
+
if (!Object.keys(object).length)
|
|
173
|
+
throw new Error("The method 'selectArray' is not supported for empty object");
|
|
174
|
+
let maping = [];
|
|
175
|
+
for (const [key, value] of Object.entries(object)) {
|
|
176
|
+
if (/\./.test(value)) {
|
|
177
|
+
const [table, c] = value.split('.');
|
|
178
|
+
maping = [...maping, `'${key}'`, `\`${table}\`.\`${c}\``];
|
|
179
|
+
continue;
|
|
180
|
+
}
|
|
181
|
+
maping = [...maping, `'${key}'`, `\`${this.getTableName()}\`.\`${value}\``];
|
|
182
|
+
}
|
|
183
|
+
const json = `
|
|
184
|
+
${this.$constants('CASE')}
|
|
185
|
+
${this.$constants('WHEN')} COUNT(${Object.values(maping)[1]}) = 0 ${this.$constants('THEN')} ${this.$constants('JSON_ARRAY')}()
|
|
186
|
+
${this.$constants('ELSE')} ${this.$constants('JSON_ARRAYAGG')}(
|
|
187
|
+
${this.$constants('JSON_OBJECT')}(${maping.join(' , ')})
|
|
188
|
+
)
|
|
189
|
+
${this.$constants('END')}
|
|
190
|
+
${this.$constants('AS')} \`${alias}\`
|
|
191
|
+
`;
|
|
130
192
|
this.$state.set('SELECT', [...this.$state.get('SELECT'), json]);
|
|
131
193
|
return this;
|
|
132
194
|
}
|
|
@@ -310,6 +372,66 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
|
|
|
310
372
|
]);
|
|
311
373
|
return this;
|
|
312
374
|
}
|
|
375
|
+
/**
|
|
376
|
+
* The 'whereDay' method is used to add a "where" clause that filters results based on the day part of a date column.
|
|
377
|
+
*
|
|
378
|
+
* It is especially useful for querying records that fall within a specific day.
|
|
379
|
+
* @param {string} column
|
|
380
|
+
* @param {number} day
|
|
381
|
+
* @returns {this}
|
|
382
|
+
*/
|
|
383
|
+
whereDay(column, day) {
|
|
384
|
+
this.$state.set('WHERE', [
|
|
385
|
+
...this.$state.get('WHERE'),
|
|
386
|
+
[
|
|
387
|
+
this.$state.get('WHERE').length ? `${this.$constants('AND')}` : '',
|
|
388
|
+
`DAY(${this.bindColumn(String(column))})`,
|
|
389
|
+
`=`,
|
|
390
|
+
`'${`00${this.$utils.escape(day)}`.slice(-2)}'`
|
|
391
|
+
].join(' ')
|
|
392
|
+
]);
|
|
393
|
+
return this;
|
|
394
|
+
}
|
|
395
|
+
/**
|
|
396
|
+
* The 'whereMonth' method is used to add a "where" clause that filters results based on the month part of a date column.
|
|
397
|
+
*
|
|
398
|
+
* It is especially useful for querying records that fall within a specific month.
|
|
399
|
+
* @param {string} column
|
|
400
|
+
* @param {number} month
|
|
401
|
+
* @returns {this}
|
|
402
|
+
*/
|
|
403
|
+
whereMonth(column, month) {
|
|
404
|
+
this.$state.set('WHERE', [
|
|
405
|
+
...this.$state.get('WHERE'),
|
|
406
|
+
[
|
|
407
|
+
this.$state.get('WHERE').length ? `${this.$constants('AND')}` : '',
|
|
408
|
+
`MONTH(${this.bindColumn(String(column))})`,
|
|
409
|
+
`=`,
|
|
410
|
+
`'${`00${this.$utils.escape(month)}`.slice(-2)}'`
|
|
411
|
+
].join(' ')
|
|
412
|
+
]);
|
|
413
|
+
return this;
|
|
414
|
+
}
|
|
415
|
+
/**
|
|
416
|
+
* The 'whereYear' method is used to add a "where" clause that filters results based on the year part of a date column.
|
|
417
|
+
*
|
|
418
|
+
* It is especially useful for querying records that fall within a specific year.
|
|
419
|
+
* @param {string} column
|
|
420
|
+
* @param {number} year
|
|
421
|
+
* @returns {this}
|
|
422
|
+
*/
|
|
423
|
+
whereYear(column, year) {
|
|
424
|
+
this.$state.set('WHERE', [
|
|
425
|
+
...this.$state.get('WHERE'),
|
|
426
|
+
[
|
|
427
|
+
this.$state.get('WHERE').length ? `${this.$constants('AND')}` : '',
|
|
428
|
+
`YEAR(${this.bindColumn(String(column))})`,
|
|
429
|
+
`=`,
|
|
430
|
+
`'${`0000${this.$utils.escape(year)}`.slice(-4)}'`
|
|
431
|
+
].join(' ')
|
|
432
|
+
]);
|
|
433
|
+
return this;
|
|
434
|
+
}
|
|
313
435
|
/**
|
|
314
436
|
* The 'whereRaw' method is used to add a raw SQL condition to a database query.
|
|
315
437
|
*
|
|
@@ -1400,13 +1522,17 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
|
|
|
1400
1522
|
* @returns {this}
|
|
1401
1523
|
*/
|
|
1402
1524
|
orderBy(column, order = 'ASC') {
|
|
1403
|
-
|
|
1404
|
-
|
|
1405
|
-
|
|
1406
|
-
|
|
1407
|
-
|
|
1408
|
-
|
|
1409
|
-
}
|
|
1525
|
+
const orderBy = [column].map(c => {
|
|
1526
|
+
if (/\./.test(c))
|
|
1527
|
+
return this.bindColumn(c.replace(/'/g, ''));
|
|
1528
|
+
if (c.includes(this.$constants('RAW')))
|
|
1529
|
+
return c === null || c === void 0 ? void 0 : c.replace(this.$constants('RAW'), '');
|
|
1530
|
+
return this.bindColumn(c);
|
|
1531
|
+
}).join(', ');
|
|
1532
|
+
this.$state.set('ORDER_BY', [
|
|
1533
|
+
...this.$state.get('ORDER_BY'),
|
|
1534
|
+
`${orderBy} ${order.toUpperCase()}`
|
|
1535
|
+
]);
|
|
1410
1536
|
this.$state.set('ORDER_BY', [
|
|
1411
1537
|
...this.$state.get('ORDER_BY'),
|
|
1412
1538
|
`\`${column}\` ${order.toUpperCase()}`
|
|
@@ -1561,10 +1687,12 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
|
|
|
1561
1687
|
groupBy(...columns) {
|
|
1562
1688
|
let groupBy = 'id';
|
|
1563
1689
|
if (columns === null || columns === void 0 ? void 0 : columns.length) {
|
|
1564
|
-
groupBy = columns.map(
|
|
1565
|
-
if (
|
|
1566
|
-
return
|
|
1567
|
-
|
|
1690
|
+
groupBy = columns.map(c => {
|
|
1691
|
+
if (/\./.test(c))
|
|
1692
|
+
return this.bindColumn(c.replace(/'/g, ''));
|
|
1693
|
+
if (c.includes(this.$constants('RAW')))
|
|
1694
|
+
return c === null || c === void 0 ? void 0 : c.replace(this.$constants('RAW'), '');
|
|
1695
|
+
return this.bindColumn(c);
|
|
1568
1696
|
}).join(', ');
|
|
1569
1697
|
}
|
|
1570
1698
|
this.$state.set('GROUP_BY', [
|
|
@@ -2231,6 +2359,15 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
|
|
|
2231
2359
|
return yield this._queryStatement(sql);
|
|
2232
2360
|
});
|
|
2233
2361
|
}
|
|
2362
|
+
/**
|
|
2363
|
+
* This 'rawQuery' method is used to execute sql statement
|
|
2364
|
+
*
|
|
2365
|
+
* @param {string} sql
|
|
2366
|
+
* @returns {promise<any>}
|
|
2367
|
+
*/
|
|
2368
|
+
static rawQuery(sql) {
|
|
2369
|
+
return new this().rawQuery(sql);
|
|
2370
|
+
}
|
|
2234
2371
|
/**
|
|
2235
2372
|
*
|
|
2236
2373
|
* plus value then update
|
|
@@ -2327,7 +2464,6 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
|
|
|
2327
2464
|
limit = (paginationOptions === null || paginationOptions === void 0 ? void 0 : paginationOptions.limit) || limit;
|
|
2328
2465
|
page = (paginationOptions === null || paginationOptions === void 0 ? void 0 : paginationOptions.page) || page;
|
|
2329
2466
|
}
|
|
2330
|
-
limit = limit > 1000 ? 1000 : limit;
|
|
2331
2467
|
const currentPage = page;
|
|
2332
2468
|
const nextPage = currentPage + 1;
|
|
2333
2469
|
const prevPage = currentPage - 1 === 0 ? 1 : currentPage - 1;
|
|
@@ -2626,9 +2762,11 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
|
|
|
2626
2762
|
exists() {
|
|
2627
2763
|
return __awaiter(this, void 0, void 0, function* () {
|
|
2628
2764
|
var _a;
|
|
2629
|
-
|
|
2630
|
-
|
|
2631
|
-
|
|
2765
|
+
const sql = new Builder()
|
|
2766
|
+
.copyBuilder(this, { where: true, limit: true, join: true })
|
|
2767
|
+
.selectRaw('1')
|
|
2768
|
+
.limit(1)
|
|
2769
|
+
.toString();
|
|
2632
2770
|
const result = yield this._queryStatement([
|
|
2633
2771
|
`${this.$constants('SELECT')}`,
|
|
2634
2772
|
`${this.$constants('EXISTS')}`,
|
|
@@ -3107,7 +3245,9 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
|
|
|
3107
3245
|
*/
|
|
3108
3246
|
faker(rows, cb) {
|
|
3109
3247
|
return __awaiter(this, void 0, void 0, function* () {
|
|
3110
|
-
|
|
3248
|
+
if (this.$state.get('TABLE_NAME') === '' || this.$state.get('TABLE_NAME') == null) {
|
|
3249
|
+
throw new Error("Unknow this table name");
|
|
3250
|
+
}
|
|
3111
3251
|
const sql = [
|
|
3112
3252
|
`${this.$constants('SHOW')}`,
|
|
3113
3253
|
`${this.$constants('FIELDS')}`,
|
|
@@ -3115,26 +3255,33 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
|
|
|
3115
3255
|
`${this.$state.get('TABLE_NAME')}`
|
|
3116
3256
|
].join(' ');
|
|
3117
3257
|
const fields = yield this._queryStatement(sql);
|
|
3258
|
+
const fakers = [];
|
|
3259
|
+
const uuid = 'uuid';
|
|
3260
|
+
const passed = (field) => ['id', '_id'].some(p => field === p);
|
|
3118
3261
|
for (let row = 0; row < rows; row++) {
|
|
3119
|
-
if (this.$state.get('TABLE_NAME') === '' || this.$state.get('TABLE_NAME') == null) {
|
|
3120
|
-
throw new Error("Unknow this table name");
|
|
3121
|
-
}
|
|
3122
3262
|
let columnAndValue = {};
|
|
3123
3263
|
for (const { Field: field, Type: type } of fields) {
|
|
3124
|
-
|
|
3125
|
-
field.toLowerCase() === '_id' ||
|
|
3126
|
-
field.toLowerCase() === 'uuid';
|
|
3127
|
-
if (passed)
|
|
3264
|
+
if (passed(field))
|
|
3128
3265
|
continue;
|
|
3129
|
-
columnAndValue = Object.assign(Object.assign({}, columnAndValue), { [field]:
|
|
3266
|
+
columnAndValue = Object.assign(Object.assign({}, columnAndValue), { [field]: field === uuid
|
|
3267
|
+
? this.$utils.faker('uuid')
|
|
3268
|
+
: this.$utils.faker(type) });
|
|
3130
3269
|
}
|
|
3131
3270
|
if (cb) {
|
|
3132
|
-
|
|
3271
|
+
fakers.push(cb(columnAndValue, row));
|
|
3133
3272
|
continue;
|
|
3134
3273
|
}
|
|
3135
|
-
|
|
3274
|
+
fakers.push(columnAndValue);
|
|
3275
|
+
}
|
|
3276
|
+
const chunkedData = this.$utils.chunkArray([...fakers], 500);
|
|
3277
|
+
const promises = [];
|
|
3278
|
+
const table = this.getTableName();
|
|
3279
|
+
for (const data of chunkedData) {
|
|
3280
|
+
promises.push(() => {
|
|
3281
|
+
return new DB_1.DB(table).dd(this.$state.get('DEBUG')).createMultiple([...data]).void().save();
|
|
3282
|
+
});
|
|
3136
3283
|
}
|
|
3137
|
-
yield
|
|
3284
|
+
yield Promise.allSettled(promises.map((v) => v()));
|
|
3138
3285
|
return;
|
|
3139
3286
|
});
|
|
3140
3287
|
}
|
|
@@ -3331,7 +3478,17 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
|
|
|
3331
3478
|
};
|
|
3332
3479
|
}
|
|
3333
3480
|
_resultHandler(data) {
|
|
3334
|
-
this.$state.
|
|
3481
|
+
if (!this.$state.get('VOID')) {
|
|
3482
|
+
this.$state.set('RESULT', data);
|
|
3483
|
+
}
|
|
3484
|
+
this.$state.reset();
|
|
3485
|
+
this.$logger.reset();
|
|
3486
|
+
return data;
|
|
3487
|
+
}
|
|
3488
|
+
_resultHandlerExists(data) {
|
|
3489
|
+
if (!this.$state.get('VOID')) {
|
|
3490
|
+
this.$state.set('RESULT', data);
|
|
3491
|
+
}
|
|
3335
3492
|
this.$state.reset();
|
|
3336
3493
|
this.$logger.reset();
|
|
3337
3494
|
return data;
|
|
@@ -3415,7 +3572,6 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
|
|
|
3415
3572
|
.toString();
|
|
3416
3573
|
const data = yield this._queryStatement(sql);
|
|
3417
3574
|
const resultData = (data === null || data === void 0 ? void 0 : data.shift()) || null;
|
|
3418
|
-
this.$state.set('RESULT', resultData);
|
|
3419
3575
|
return this._resultHandler(resultData);
|
|
3420
3576
|
});
|
|
3421
3577
|
}
|
|
@@ -3700,9 +3856,9 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
|
|
|
3700
3856
|
this.$constants = (name) => {
|
|
3701
3857
|
if (name == null)
|
|
3702
3858
|
return constants_1.CONSTANTS;
|
|
3703
|
-
if (!constants_1.CONSTANTS.hasOwnProperty(name
|
|
3859
|
+
if (!constants_1.CONSTANTS.hasOwnProperty(name))
|
|
3704
3860
|
throw new Error(`Not found that constant : '${name}'`);
|
|
3705
|
-
return constants_1.CONSTANTS[name
|
|
3861
|
+
return constants_1.CONSTANTS[name];
|
|
3706
3862
|
};
|
|
3707
3863
|
}
|
|
3708
3864
|
}
|