tspace-mysql 1.6.4 → 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.
Files changed (39) hide show
  1. package/README.md +0 -9
  2. package/build/lib/connection/index.d.ts +1 -0
  3. package/build/lib/connection/index.js +26 -17
  4. package/build/lib/connection/index.js.map +1 -1
  5. package/build/lib/connection/options.js +17 -15
  6. package/build/lib/connection/options.js.map +1 -1
  7. package/build/lib/constants/index.d.ts +96 -1
  8. package/build/lib/constants/index.js +3 -0
  9. package/build/lib/constants/index.js.map +1 -1
  10. package/build/lib/core/Abstracts/AbstractBuilder.d.ts +5 -7
  11. package/build/lib/core/Abstracts/AbstractBuilder.js +0 -5
  12. package/build/lib/core/Abstracts/AbstractBuilder.js.map +1 -1
  13. package/build/lib/core/Abstracts/AbstractModel.d.ts +16 -16
  14. package/build/lib/core/Builder.d.ts +51 -3
  15. package/build/lib/core/Builder.js +162 -24
  16. package/build/lib/core/Builder.js.map +1 -1
  17. package/build/lib/core/DB.d.ts +15 -2
  18. package/build/lib/core/DB.js +17 -0
  19. package/build/lib/core/DB.js.map +1 -1
  20. package/build/lib/core/Handlers/Relation.d.ts +3 -3
  21. package/build/lib/core/Handlers/Relation.js +75 -54
  22. package/build/lib/core/Handlers/Relation.js.map +1 -1
  23. package/build/lib/core/Handlers/State.d.ts +130 -2
  24. package/build/lib/core/Handlers/State.js +3 -3
  25. package/build/lib/core/Handlers/State.js.map +1 -1
  26. package/build/lib/core/Model.d.ts +73 -17
  27. package/build/lib/core/Model.js +282 -203
  28. package/build/lib/core/Model.js.map +1 -1
  29. package/build/lib/core/Schema.js +1 -1
  30. package/build/lib/types.d.ts +3 -3
  31. package/build/lib/utils/index.d.ts +4 -1
  32. package/build/lib/utils/index.js +29 -4
  33. package/build/lib/utils/index.js.map +1 -1
  34. package/build/tests/03-Model.test.js +83 -14
  35. package/build/tests/03-Model.test.js.map +1 -1
  36. package/build/tests/schema-spec.d.ts +19 -2
  37. package/build/tests/schema-spec.js +12 -11
  38. package/build/tests/schema-spec.js.map +1 -1
  39. 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 = `${this.$constants('JSON_OBJECT')}(${maping.join(' , ')}) ${this.$constants('AS')} \`${alias}\``;
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
  *
@@ -1400,13 +1498,17 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
1400
1498
  * @returns {this}
1401
1499
  */
1402
1500
  orderBy(column, order = 'ASC') {
1403
- if (typeof column !== 'string')
1404
- return this;
1405
- if (column.includes(this.$constants('RAW')) || /\./.test(column)) {
1406
- column = column === null || column === void 0 ? void 0 : column.replace(this.$constants('RAW'), '');
1407
- if (/\./.test(column))
1408
- column = this.bindColumn(column);
1409
- }
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
+ ]);
1410
1512
  this.$state.set('ORDER_BY', [
1411
1513
  ...this.$state.get('ORDER_BY'),
1412
1514
  `\`${column}\` ${order.toUpperCase()}`
@@ -1561,10 +1663,12 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
1561
1663
  groupBy(...columns) {
1562
1664
  let groupBy = 'id';
1563
1665
  if (columns === null || columns === void 0 ? void 0 : columns.length) {
1564
- groupBy = columns.map(column => {
1565
- if (column.includes(this.$constants('RAW')))
1566
- return column === null || column === void 0 ? void 0 : column.replace(this.$constants('RAW'), '');
1567
- return `\`${column}\``;
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);
1568
1672
  }).join(', ');
1569
1673
  }
1570
1674
  this.$state.set('GROUP_BY', [
@@ -2070,6 +2174,23 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
2070
2174
  getTableName() {
2071
2175
  return this.$state.get('TABLE_NAME').replace(/\`/g, '');
2072
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
+ }
2073
2194
  /**
2074
2195
  * The 'getSchema' method is used to get schema information
2075
2196
  * @returns {this} this this
@@ -2214,6 +2335,15 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
2214
2335
  return yield this._queryStatement(sql);
2215
2336
  });
2216
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
+ }
2217
2347
  /**
2218
2348
  *
2219
2349
  * plus value then update
@@ -2310,7 +2440,6 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
2310
2440
  limit = (paginationOptions === null || paginationOptions === void 0 ? void 0 : paginationOptions.limit) || limit;
2311
2441
  page = (paginationOptions === null || paginationOptions === void 0 ? void 0 : paginationOptions.page) || page;
2312
2442
  }
2313
- limit = limit > 1000 ? 1000 : limit;
2314
2443
  const currentPage = page;
2315
2444
  const nextPage = currentPage + 1;
2316
2445
  const prevPage = currentPage - 1 === 0 ? 1 : currentPage - 1;
@@ -3090,7 +3219,9 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
3090
3219
  */
3091
3220
  faker(rows, cb) {
3092
3221
  return __awaiter(this, void 0, void 0, function* () {
3093
- let data = [];
3222
+ if (this.$state.get('TABLE_NAME') === '' || this.$state.get('TABLE_NAME') == null) {
3223
+ throw new Error("Unknow this table name");
3224
+ }
3094
3225
  const sql = [
3095
3226
  `${this.$constants('SHOW')}`,
3096
3227
  `${this.$constants('FIELDS')}`,
@@ -3098,10 +3229,8 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
3098
3229
  `${this.$state.get('TABLE_NAME')}`
3099
3230
  ].join(' ');
3100
3231
  const fields = yield this._queryStatement(sql);
3232
+ const fakers = [];
3101
3233
  for (let row = 0; row < rows; row++) {
3102
- if (this.$state.get('TABLE_NAME') === '' || this.$state.get('TABLE_NAME') == null) {
3103
- throw new Error("Unknow this table name");
3104
- }
3105
3234
  let columnAndValue = {};
3106
3235
  for (const { Field: field, Type: type } of fields) {
3107
3236
  const passed = field.toLowerCase() === 'id' ||
@@ -3112,12 +3241,20 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
3112
3241
  columnAndValue = Object.assign(Object.assign({}, columnAndValue), { [field]: this.$utils.faker(type) });
3113
3242
  }
3114
3243
  if (cb) {
3115
- data = [...data, cb(columnAndValue, row)];
3244
+ fakers.push(cb(columnAndValue, row));
3116
3245
  continue;
3117
3246
  }
3118
- data = [...data, columnAndValue];
3247
+ fakers.push(columnAndValue);
3119
3248
  }
3120
- yield this.createMultiple(data).save();
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()));
3121
3258
  return;
3122
3259
  });
3123
3260
  }
@@ -3314,7 +3451,9 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
3314
3451
  };
3315
3452
  }
3316
3453
  _resultHandler(data) {
3317
- this.$state.set('RESULT', data);
3454
+ if (!this.$state.get('VOID')) {
3455
+ this.$state.set('RESULT', data);
3456
+ }
3318
3457
  this.$state.reset();
3319
3458
  this.$logger.reset();
3320
3459
  return data;
@@ -3398,7 +3537,6 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
3398
3537
  .toString();
3399
3538
  const data = yield this._queryStatement(sql);
3400
3539
  const resultData = (data === null || data === void 0 ? void 0 : data.shift()) || null;
3401
- this.$state.set('RESULT', resultData);
3402
3540
  return this._resultHandler(resultData);
3403
3541
  });
3404
3542
  }
@@ -3683,9 +3821,9 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
3683
3821
  this.$constants = (name) => {
3684
3822
  if (name == null)
3685
3823
  return constants_1.CONSTANTS;
3686
- if (!constants_1.CONSTANTS.hasOwnProperty(name.toUpperCase()))
3824
+ if (!constants_1.CONSTANTS.hasOwnProperty(name))
3687
3825
  throw new Error(`Not found that constant : '${name}'`);
3688
- return constants_1.CONSTANTS[name.toUpperCase()];
3826
+ return constants_1.CONSTANTS[name];
3689
3827
  };
3690
3828
  }
3691
3829
  }