tspace-mysql 1.6.3 → 1.6.5-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 +0 -9
- package/build/lib/connection/index.d.ts +1 -0
- package/build/lib/connection/index.js +34 -25
- 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/Blueprint.d.ts +1 -1
- package/build/lib/core/Blueprint.js +1 -1
- package/build/lib/core/Builder.d.ts +51 -3
- package/build/lib/core/Builder.js +162 -32
- package/build/lib/core/Builder.js.map +1 -1
- package/build/lib/core/DB.d.ts +16 -3
- 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 -54
- 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 +3 -3
- package/build/lib/core/Handlers/State.js.map +1 -1
- package/build/lib/core/Model.d.ts +73 -17
- package/build/lib/core/Model.js +283 -216
- package/build/lib/core/Model.js.map +1 -1
- package/build/lib/core/Repository.d.ts +2 -2
- package/build/lib/core/Schema.js +4 -4
- package/build/lib/core/Schema.js.map +1 -1
- package/build/lib/core/index.js.map +1 -1
- package/build/lib/types.d.ts +3 -3
- package/build/lib/utils/index.d.ts +4 -2
- package/build/lib/utils/index.js +29 -17
- package/build/lib/utils/index.js.map +1 -1
- package/build/tests/03-Model.test.js +83 -14
- package/build/tests/03-Model.test.js.map +1 -1
- package/build/tests/schema-spec.d.ts +19 -2
- package/build/tests/schema-spec.js +12 -11
- package/build/tests/schema-spec.js.map +1 -1
- package/package.json +1 -1
|
@@ -126,7 +126,45 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
|
|
|
126
126
|
}
|
|
127
127
|
maping = [...maping, `'${key}'`, `\`${this.getTableName()}\`.\`${value}\``];
|
|
128
128
|
}
|
|
129
|
-
const json =
|
|
129
|
+
const json = `
|
|
130
|
+
${this.$constants('CASE')}
|
|
131
|
+
${this.$constants('WHEN')} COUNT(${Object.values(maping)[1]}) = 0 ${this.$constants('THEN')} ${this.$constants('NULL')}
|
|
132
|
+
${this.$constants('ELSE')} ${this.$constants('JSON_OBJECT')}(${maping.join(' , ')})
|
|
133
|
+
${this.$constants('END')}
|
|
134
|
+
${this.$constants('AS')} \`${alias}\`
|
|
135
|
+
`;
|
|
136
|
+
this.$state.set('SELECT', [...this.$state.get('SELECT'), json]);
|
|
137
|
+
return this;
|
|
138
|
+
}
|
|
139
|
+
/**
|
|
140
|
+
* The 'selectObject' method is used to specify which columns you want to retrieve from a database table.
|
|
141
|
+
*
|
|
142
|
+
* It allows you to choose the specific columns that should be included in the result set to 'Object' of a database query.
|
|
143
|
+
* @param {string} object table name
|
|
144
|
+
* @param {string} alias as name of the column
|
|
145
|
+
* @returns {this} this
|
|
146
|
+
*/
|
|
147
|
+
selectArray(object, alias) {
|
|
148
|
+
if (!Object.keys(object).length)
|
|
149
|
+
throw new Error("The method 'selectArray' is not supported for empty object");
|
|
150
|
+
let maping = [];
|
|
151
|
+
for (const [key, value] of Object.entries(object)) {
|
|
152
|
+
if (/\./.test(value)) {
|
|
153
|
+
const [table, c] = value.split('.');
|
|
154
|
+
maping = [...maping, `'${key}'`, `\`${table}\`.\`${c}\``];
|
|
155
|
+
continue;
|
|
156
|
+
}
|
|
157
|
+
maping = [...maping, `'${key}'`, `\`${this.getTableName()}\`.\`${value}\``];
|
|
158
|
+
}
|
|
159
|
+
const json = `
|
|
160
|
+
${this.$constants('CASE')}
|
|
161
|
+
${this.$constants('WHEN')} COUNT(${Object.values(maping)[1]}) = 0 ${this.$constants('THEN')} ${this.$constants('JSON_ARRAY')}()
|
|
162
|
+
${this.$constants('ELSE')} ${this.$constants('JSON_ARRAYAGG')}(
|
|
163
|
+
${this.$constants('JSON_OBJECT')}(${maping.join(' , ')})
|
|
164
|
+
)
|
|
165
|
+
${this.$constants('END')}
|
|
166
|
+
${this.$constants('AS')} \`${alias}\`
|
|
167
|
+
`;
|
|
130
168
|
this.$state.set('SELECT', [...this.$state.get('SELECT'), json]);
|
|
131
169
|
return this;
|
|
132
170
|
}
|
|
@@ -310,6 +348,66 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
|
|
|
310
348
|
]);
|
|
311
349
|
return this;
|
|
312
350
|
}
|
|
351
|
+
/**
|
|
352
|
+
* The 'whereDay' method is used to add a "where" clause that filters results based on the day part of a date column.
|
|
353
|
+
*
|
|
354
|
+
* It is especially useful for querying records that fall within a specific day.
|
|
355
|
+
* @param {string} column
|
|
356
|
+
* @param {number} day
|
|
357
|
+
* @returns {this}
|
|
358
|
+
*/
|
|
359
|
+
whereDay(column, day) {
|
|
360
|
+
this.$state.set('WHERE', [
|
|
361
|
+
...this.$state.get('WHERE'),
|
|
362
|
+
[
|
|
363
|
+
this.$state.get('WHERE').length ? `${this.$constants('AND')}` : '',
|
|
364
|
+
`DAY(${this.bindColumn(String(column))})`,
|
|
365
|
+
`=`,
|
|
366
|
+
`'${`00${this.$utils.escape(day)}`.slice(-2)}'`
|
|
367
|
+
].join(' ')
|
|
368
|
+
]);
|
|
369
|
+
return this;
|
|
370
|
+
}
|
|
371
|
+
/**
|
|
372
|
+
* The 'whereMonth' method is used to add a "where" clause that filters results based on the month part of a date column.
|
|
373
|
+
*
|
|
374
|
+
* It is especially useful for querying records that fall within a specific month.
|
|
375
|
+
* @param {string} column
|
|
376
|
+
* @param {number} month
|
|
377
|
+
* @returns {this}
|
|
378
|
+
*/
|
|
379
|
+
whereMonth(column, month) {
|
|
380
|
+
this.$state.set('WHERE', [
|
|
381
|
+
...this.$state.get('WHERE'),
|
|
382
|
+
[
|
|
383
|
+
this.$state.get('WHERE').length ? `${this.$constants('AND')}` : '',
|
|
384
|
+
`MONTH(${this.bindColumn(String(column))})`,
|
|
385
|
+
`=`,
|
|
386
|
+
`'${`00${this.$utils.escape(month)}`.slice(-2)}'`
|
|
387
|
+
].join(' ')
|
|
388
|
+
]);
|
|
389
|
+
return this;
|
|
390
|
+
}
|
|
391
|
+
/**
|
|
392
|
+
* The 'whereYear' method is used to add a "where" clause that filters results based on the year part of a date column.
|
|
393
|
+
*
|
|
394
|
+
* It is especially useful for querying records that fall within a specific year.
|
|
395
|
+
* @param {string} column
|
|
396
|
+
* @param {number} year
|
|
397
|
+
* @returns {this}
|
|
398
|
+
*/
|
|
399
|
+
whereYear(column, year) {
|
|
400
|
+
this.$state.set('WHERE', [
|
|
401
|
+
...this.$state.get('WHERE'),
|
|
402
|
+
[
|
|
403
|
+
this.$state.get('WHERE').length ? `${this.$constants('AND')}` : '',
|
|
404
|
+
`YEAR(${this.bindColumn(String(column))})`,
|
|
405
|
+
`=`,
|
|
406
|
+
`'${`0000${this.$utils.escape(year)}`.slice(-4)}'`
|
|
407
|
+
].join(' ')
|
|
408
|
+
]);
|
|
409
|
+
return this;
|
|
410
|
+
}
|
|
313
411
|
/**
|
|
314
412
|
* The 'whereRaw' method is used to add a raw SQL condition to a database query.
|
|
315
413
|
*
|
|
@@ -659,8 +757,6 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
|
|
|
659
757
|
* @returns {this}
|
|
660
758
|
*/
|
|
661
759
|
whereSubQuery(column, subQuery) {
|
|
662
|
-
if (!this.$utils.isSubQuery(subQuery))
|
|
663
|
-
throw new Error(`This "${subQuery}" is invalid. Sub query is should contain 1 column(s)`);
|
|
664
760
|
this.$state.set('WHERE', [
|
|
665
761
|
...this.$state.get('WHERE'),
|
|
666
762
|
[
|
|
@@ -683,8 +779,6 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
|
|
|
683
779
|
* @returns {this}
|
|
684
780
|
*/
|
|
685
781
|
whereNotSubQuery(column, subQuery) {
|
|
686
|
-
if (!this.$utils.isSubQuery(subQuery))
|
|
687
|
-
throw new Error(`This "${subQuery}" is invalid. Sub query is should contain 1 column(s)`);
|
|
688
782
|
this.$state.set('WHERE', [
|
|
689
783
|
...this.$state.get('WHERE'),
|
|
690
784
|
[
|
|
@@ -707,8 +801,6 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
|
|
|
707
801
|
* @returns {this}
|
|
708
802
|
*/
|
|
709
803
|
orWhereSubQuery(column, subQuery) {
|
|
710
|
-
if (!this.$utils.isSubQuery(subQuery))
|
|
711
|
-
throw new Error(`This "${subQuery}" is invalid. Sub query is should contain 1 column(s)`);
|
|
712
804
|
this.$state.set('WHERE', [
|
|
713
805
|
...this.$state.get('WHERE'),
|
|
714
806
|
[
|
|
@@ -731,8 +823,6 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
|
|
|
731
823
|
* @returns {this}
|
|
732
824
|
*/
|
|
733
825
|
orWhereNotSubQuery(column, subQuery) {
|
|
734
|
-
if (!this.$utils.isSubQuery(subQuery))
|
|
735
|
-
throw new Error(`This "${subQuery}" is invalid sub query (Sub query Operand should contain 1 column(s) not select * )`);
|
|
736
826
|
this.$state.set('WHERE', [
|
|
737
827
|
...this.$state.get('WHERE'),
|
|
738
828
|
[
|
|
@@ -1408,13 +1498,17 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
|
|
|
1408
1498
|
* @returns {this}
|
|
1409
1499
|
*/
|
|
1410
1500
|
orderBy(column, order = 'ASC') {
|
|
1411
|
-
|
|
1412
|
-
|
|
1413
|
-
|
|
1414
|
-
|
|
1415
|
-
|
|
1416
|
-
|
|
1417
|
-
}
|
|
1501
|
+
const orderBy = [column].map(c => {
|
|
1502
|
+
if (/\./.test(c))
|
|
1503
|
+
return this.bindColumn(c.replace(/'/g, ''));
|
|
1504
|
+
if (c.includes(this.$constants('RAW')))
|
|
1505
|
+
return c === null || c === void 0 ? void 0 : c.replace(this.$constants('RAW'), '');
|
|
1506
|
+
return this.bindColumn(c);
|
|
1507
|
+
}).join(', ');
|
|
1508
|
+
this.$state.set('ORDER_BY', [
|
|
1509
|
+
...this.$state.get('ORDER_BY'),
|
|
1510
|
+
`${orderBy} ${order.toUpperCase()}`
|
|
1511
|
+
]);
|
|
1418
1512
|
this.$state.set('ORDER_BY', [
|
|
1419
1513
|
...this.$state.get('ORDER_BY'),
|
|
1420
1514
|
`\`${column}\` ${order.toUpperCase()}`
|
|
@@ -1569,10 +1663,12 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
|
|
|
1569
1663
|
groupBy(...columns) {
|
|
1570
1664
|
let groupBy = 'id';
|
|
1571
1665
|
if (columns === null || columns === void 0 ? void 0 : columns.length) {
|
|
1572
|
-
groupBy = columns.map(
|
|
1573
|
-
if (
|
|
1574
|
-
return
|
|
1575
|
-
|
|
1666
|
+
groupBy = columns.map(c => {
|
|
1667
|
+
if (/\./.test(c))
|
|
1668
|
+
return this.bindColumn(c.replace(/'/g, ''));
|
|
1669
|
+
if (c.includes(this.$constants('RAW')))
|
|
1670
|
+
return c === null || c === void 0 ? void 0 : c.replace(this.$constants('RAW'), '');
|
|
1671
|
+
return this.bindColumn(c);
|
|
1576
1672
|
}).join(', ');
|
|
1577
1673
|
}
|
|
1578
1674
|
this.$state.set('GROUP_BY', [
|
|
@@ -2078,6 +2174,23 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
|
|
|
2078
2174
|
getTableName() {
|
|
2079
2175
|
return this.$state.get('TABLE_NAME').replace(/\`/g, '');
|
|
2080
2176
|
}
|
|
2177
|
+
/**
|
|
2178
|
+
* The 'getColumns' method is used to get columns
|
|
2179
|
+
* @returns {this} this this
|
|
2180
|
+
*/
|
|
2181
|
+
getColumns() {
|
|
2182
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
2183
|
+
const sql = [
|
|
2184
|
+
`${this.$constants('SHOW')}`,
|
|
2185
|
+
`${this.$constants('COLUMNS')}`,
|
|
2186
|
+
`${this.$constants('FROM')}`,
|
|
2187
|
+
`\`${this.$state.get('TABLE_NAME').replace(/\`/g, '')}\``
|
|
2188
|
+
].join(' ');
|
|
2189
|
+
const rawColumns = yield this._queryStatement(sql);
|
|
2190
|
+
const columns = rawColumns.map((column) => column.Field);
|
|
2191
|
+
return columns;
|
|
2192
|
+
});
|
|
2193
|
+
}
|
|
2081
2194
|
/**
|
|
2082
2195
|
* The 'getSchema' method is used to get schema information
|
|
2083
2196
|
* @returns {this} this this
|
|
@@ -2222,6 +2335,15 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
|
|
|
2222
2335
|
return yield this._queryStatement(sql);
|
|
2223
2336
|
});
|
|
2224
2337
|
}
|
|
2338
|
+
/**
|
|
2339
|
+
* This 'rawQuery' method is used to execute sql statement
|
|
2340
|
+
*
|
|
2341
|
+
* @param {string} sql
|
|
2342
|
+
* @returns {promise<any>}
|
|
2343
|
+
*/
|
|
2344
|
+
static rawQuery(sql) {
|
|
2345
|
+
return new this().rawQuery(sql);
|
|
2346
|
+
}
|
|
2225
2347
|
/**
|
|
2226
2348
|
*
|
|
2227
2349
|
* plus value then update
|
|
@@ -2318,7 +2440,6 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
|
|
|
2318
2440
|
limit = (paginationOptions === null || paginationOptions === void 0 ? void 0 : paginationOptions.limit) || limit;
|
|
2319
2441
|
page = (paginationOptions === null || paginationOptions === void 0 ? void 0 : paginationOptions.page) || page;
|
|
2320
2442
|
}
|
|
2321
|
-
limit = limit > 1000 ? 1000 : limit;
|
|
2322
2443
|
const currentPage = page;
|
|
2323
2444
|
const nextPage = currentPage + 1;
|
|
2324
2445
|
const prevPage = currentPage - 1 === 0 ? 1 : currentPage - 1;
|
|
@@ -3098,7 +3219,9 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
|
|
|
3098
3219
|
*/
|
|
3099
3220
|
faker(rows, cb) {
|
|
3100
3221
|
return __awaiter(this, void 0, void 0, function* () {
|
|
3101
|
-
|
|
3222
|
+
if (this.$state.get('TABLE_NAME') === '' || this.$state.get('TABLE_NAME') == null) {
|
|
3223
|
+
throw new Error("Unknow this table name");
|
|
3224
|
+
}
|
|
3102
3225
|
const sql = [
|
|
3103
3226
|
`${this.$constants('SHOW')}`,
|
|
3104
3227
|
`${this.$constants('FIELDS')}`,
|
|
@@ -3106,10 +3229,8 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
|
|
|
3106
3229
|
`${this.$state.get('TABLE_NAME')}`
|
|
3107
3230
|
].join(' ');
|
|
3108
3231
|
const fields = yield this._queryStatement(sql);
|
|
3232
|
+
const fakers = [];
|
|
3109
3233
|
for (let row = 0; row < rows; row++) {
|
|
3110
|
-
if (this.$state.get('TABLE_NAME') === '' || this.$state.get('TABLE_NAME') == null) {
|
|
3111
|
-
throw new Error("Unknow this table name");
|
|
3112
|
-
}
|
|
3113
3234
|
let columnAndValue = {};
|
|
3114
3235
|
for (const { Field: field, Type: type } of fields) {
|
|
3115
3236
|
const passed = field.toLowerCase() === 'id' ||
|
|
@@ -3120,12 +3241,20 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
|
|
|
3120
3241
|
columnAndValue = Object.assign(Object.assign({}, columnAndValue), { [field]: this.$utils.faker(type) });
|
|
3121
3242
|
}
|
|
3122
3243
|
if (cb) {
|
|
3123
|
-
|
|
3244
|
+
fakers.push(cb(columnAndValue, row));
|
|
3124
3245
|
continue;
|
|
3125
3246
|
}
|
|
3126
|
-
|
|
3247
|
+
fakers.push(columnAndValue);
|
|
3127
3248
|
}
|
|
3128
|
-
|
|
3249
|
+
const chunkedData = this.$utils.chunkArray([...fakers], 500);
|
|
3250
|
+
const promises = [];
|
|
3251
|
+
const table = this.getTableName();
|
|
3252
|
+
for (const data of chunkedData) {
|
|
3253
|
+
promises.push(() => {
|
|
3254
|
+
return new DB_1.DB(table).createMultiple([...data]).void().save();
|
|
3255
|
+
});
|
|
3256
|
+
}
|
|
3257
|
+
yield Promise.allSettled(promises.map((v) => v()));
|
|
3129
3258
|
return;
|
|
3130
3259
|
});
|
|
3131
3260
|
}
|
|
@@ -3322,7 +3451,9 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
|
|
|
3322
3451
|
};
|
|
3323
3452
|
}
|
|
3324
3453
|
_resultHandler(data) {
|
|
3325
|
-
this.$state.
|
|
3454
|
+
if (!this.$state.get('VOID')) {
|
|
3455
|
+
this.$state.set('RESULT', data);
|
|
3456
|
+
}
|
|
3326
3457
|
this.$state.reset();
|
|
3327
3458
|
this.$logger.reset();
|
|
3328
3459
|
return data;
|
|
@@ -3406,7 +3537,6 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
|
|
|
3406
3537
|
.toString();
|
|
3407
3538
|
const data = yield this._queryStatement(sql);
|
|
3408
3539
|
const resultData = (data === null || data === void 0 ? void 0 : data.shift()) || null;
|
|
3409
|
-
this.$state.set('RESULT', resultData);
|
|
3410
3540
|
return this._resultHandler(resultData);
|
|
3411
3541
|
});
|
|
3412
3542
|
}
|
|
@@ -3691,9 +3821,9 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
|
|
|
3691
3821
|
this.$constants = (name) => {
|
|
3692
3822
|
if (name == null)
|
|
3693
3823
|
return constants_1.CONSTANTS;
|
|
3694
|
-
if (!constants_1.CONSTANTS.hasOwnProperty(name
|
|
3824
|
+
if (!constants_1.CONSTANTS.hasOwnProperty(name))
|
|
3695
3825
|
throw new Error(`Not found that constant : '${name}'`);
|
|
3696
|
-
return constants_1.CONSTANTS[name
|
|
3826
|
+
return constants_1.CONSTANTS[name];
|
|
3697
3827
|
};
|
|
3698
3828
|
}
|
|
3699
3829
|
}
|