tspace-mysql 1.3.1 → 1.3.2

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 CHANGED
@@ -91,6 +91,8 @@ const selectQuery = await new DB('users').select('id','username').findOne()
91
91
  const selectQueries = await new DB('users').select('id','username').findMany()
92
92
  // selectQueries => [{ id : 1, username : 'tspace' } , { id : 2, username : 'tspace2'}]
93
93
 
94
+ const selectQueryRaw = await new DB('users').selectRaw('COUNT(id)').findMany()
95
+
94
96
  /**
95
97
  * @example except
96
98
  */
@@ -669,6 +671,7 @@ orWhereRaw(sql)
669
671
  orWhereIn(column , [])
670
672
  orWhereSubQuery(colmn , rawSQL)
671
673
  select(column1 ,column2 ,...N)
674
+ selectRaw(column1 ,column2 ,...N)
672
675
  except(column1 ,column2 ,...N)
673
676
  only(column1 ,column2 ,...N)
674
677
  hidden(column1 ,column2 ,...N)
@@ -677,10 +680,15 @@ rightJoin (primary key , table.foreign key)
677
680
  leftJoin (primary key , table.foreign key)
678
681
  limit (limit)
679
682
  having (condition)
683
+ havingRaw (condition)
680
684
  orderBy (column ,'ASC' || 'DSCE')
685
+ orderByRaw(column ,'ASC' || 'DSCE')
681
686
  latest (column)
687
+ latestRaw (column)
682
688
  oldest (column)
689
+ oldestRaw (column)
683
690
  groupBy (column)
691
+ groupByRaw (column)
684
692
  create(objects)
685
693
  createMultiple(array objects)
686
694
  update (objects)
@@ -32,10 +32,12 @@ declare abstract class AbstractModel extends Builder {
32
32
  abstract with(...nameRelations: string[]): this;
33
33
  abstract withQuery(nameRelations: string, callback: Function): this;
34
34
  abstract withExists(...nameRelations: string[]): this;
35
+ abstract withAndTrashed(...nameRelations: string[]): this;
35
36
  abstract has(...nameRelations: string[]): this;
36
37
  abstract relations(...nameRelations: string[]): this;
37
38
  abstract relationQuery(nameRelations: string, callback: Function): this;
38
39
  abstract relationsExists(...nameRelations: string[]): this;
40
+ abstract relationsAndTrashed(...nameRelations: string[]): this;
39
41
  }
40
42
  export { AbstractModel };
41
43
  export default AbstractModel;
@@ -50,14 +50,6 @@ declare class Builder extends AbstractBuilder {
50
50
  * @return {this} this
51
51
  */
52
52
  when(condition: string | number | undefined | null | Boolean, callback: Function): this;
53
- /**
54
- * if has 2 arguments default operator '='
55
- * @param {string} column
56
- * @param {string?} operator ['=', '<', '>' ,'!=', '!<', '!>' ,'LIKE']
57
- * @param {any?} value
58
- * @return {this}
59
- */
60
- resetWhere(): this;
61
53
  /**
62
54
  * if has 2 arguments default operator '='
63
55
  * @param {string} column if arguments is object
@@ -90,26 +90,28 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
90
90
  * @return {this} this
91
91
  */
92
92
  select(...columns) {
93
- let select = '*';
94
- if (columns === null || columns === void 0 ? void 0 : columns.length) {
95
- select = columns.map((column) => {
96
- if (column.includes(this.$constants('RAW')))
97
- return column === null || column === void 0 ? void 0 : column.replace(this.$constants('RAW'), '').replace(/'/g, '');
98
- return `\`${column}\``;
99
- }).join(', ');
100
- }
93
+ if (!columns.length)
94
+ return this;
95
+ const select = columns.map((column) => {
96
+ if (column === '*')
97
+ return column;
98
+ if (column.includes(this.$constants('RAW')))
99
+ return column === null || column === void 0 ? void 0 : column.replace(this.$constants('RAW'), '').replace(/'/g, '');
100
+ return `\`${column}\``;
101
+ }).join(', ');
101
102
  this.$state.set('SELECT', `${this.$constants('SELECT')} ${select}`);
102
103
  return this;
103
104
  }
104
105
  selectRaw(...columns) {
105
- let select = '*';
106
- if (columns === null || columns === void 0 ? void 0 : columns.length) {
107
- select = columns.map((column) => {
108
- if (column.includes(this.$constants('RAW')))
109
- return column === null || column === void 0 ? void 0 : column.replace(this.$constants('RAW'), '').replace(/'/g, '');
106
+ if (!columns.length)
107
+ return this;
108
+ const select = columns.map((column) => {
109
+ if (column === '*')
110
110
  return column;
111
- }).join(', ');
112
- }
111
+ if (column.includes(this.$constants('RAW')))
112
+ return column === null || column === void 0 ? void 0 : column.replace(this.$constants('RAW'), '').replace(/'/g, '');
113
+ return column;
114
+ }).join(', ');
113
115
  this.$state.set('SELECT', `${this.$constants('SELECT')} ${select}`);
114
116
  return this;
115
117
  }
@@ -132,17 +134,6 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
132
134
  callback(this);
133
135
  return this;
134
136
  }
135
- /**
136
- * if has 2 arguments default operator '='
137
- * @param {string} column
138
- * @param {string?} operator ['=', '<', '>' ,'!=', '!<', '!>' ,'LIKE']
139
- * @param {any?} value
140
- * @return {this}
141
- */
142
- resetWhere() {
143
- this.$state.set('WHERE', '');
144
- return this;
145
- }
146
137
  /**
147
138
  * if has 2 arguments default operator '='
148
139
  * @param {string} column if arguments is object
@@ -327,7 +318,7 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
327
318
  this._queryWhereIsExists()
328
319
  ? `${this.$state.get('WHERE')} ${this.$constants('AND')}`
329
320
  : `${this.$constants('WHERE')}`,
330
- `${this._bindTableAndColumnInQueryWhere(column)} ${this.$constants('IN')}`,
321
+ `${this._bindTableAndColumnInQueryWhere(column)}`,
331
322
  `${this.$constants('IN')}`,
332
323
  `(${values})`
333
324
  ].join(' '));
@@ -349,7 +340,7 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
349
340
  this._queryWhereIsExists()
350
341
  ? `${this.$state.get('WHERE')} ${this.$constants('OR')}`
351
342
  : `${this.$constants('WHERE')}`,
352
- `${this._bindTableAndColumnInQueryWhere(column)} ${this.$constants('IN')}`,
343
+ `${this._bindTableAndColumnInQueryWhere(column)}`,
353
344
  `${this.$constants('IN')}`,
354
345
  `(${values})`
355
346
  ].join(' '));
@@ -1771,7 +1762,8 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
1771
1762
  `*`,
1772
1763
  `${this.$constants('FROM')}`,
1773
1764
  `${this.$state.get('TABLE_NAME')}`,
1774
- `${this.$constants('WHERE')} id ${this.$constants('IN')}`,
1765
+ `${this.$constants('WHERE')} id`,
1766
+ `${this.$constants('IN')}`,
1775
1767
  `(${data.map((a) => `\'${a}\'`).join(',') || ['0']})`
1776
1768
  ].join(' ');
1777
1769
  const groups = yield this.queryStatement(sqlGroups);
@@ -2201,7 +2193,7 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
2201
2193
  `${this.$state.get('SELECT')}`,
2202
2194
  `${this.$state.get('FROM')}`,
2203
2195
  `${this.$state.get('TABLE_NAME')}`,
2204
- `${this.$constants('WHERE')} id = ${id}`
2196
+ `${this.$constants('WHERE')} ${this.$state.get('TABLE_NAME')}.\`id\` = ${id}`
2205
2197
  ].join(' ');
2206
2198
  const data = yield this.queryStatement(sql);
2207
2199
  const result = (data === null || data === void 0 ? void 0 : data.shift()) || null;
@@ -112,7 +112,7 @@ declare class Model extends AbstractModel {
112
112
  * exceptColumns for method except
113
113
  * @return {promise<string>} string
114
114
  */
115
- protected exceptColumns(): Promise<string>;
115
+ protected exceptColumns(): Promise<any>;
116
116
  /**
117
117
  * Build method for relation in model
118
118
  * @param {string} name name relation registry in your model
@@ -175,11 +175,11 @@ declare class Model extends AbstractModel {
175
175
  with(...nameRelations: Array<string>): this;
176
176
  /**
177
177
  *
178
- * Use relations in registry of model return ignore soft deleted
178
+ * Use relations in registry of model return normal and in trash
179
179
  * @param {...string} nameRelations if data exists return blank
180
180
  * @return {this} this
181
181
  */
182
- withTrashed(...nameRelations: Array<string>): this;
182
+ withAndTrashed(...nameRelations: Array<string>): this;
183
183
  /**
184
184
  *
185
185
  * Use relations in registry of model return only exists result of relation query
@@ -230,7 +230,7 @@ declare class Model extends AbstractModel {
230
230
  * @param {...string} nameRelations if data exists return blank
231
231
  * @return {this} this
232
232
  */
233
- relationTrashed(...nameRelations: Array<string>): this;
233
+ relationsAndTrashed(...nameRelations: Array<string>): this;
234
234
  /**
235
235
  * Assign the relation in model Objects
236
236
  * @param {object} relations registry relation in your model
@@ -212,7 +212,7 @@ class Model extends AbstractModel_1.AbstractModel {
212
212
  if (this.$state.get('SCHEMA')) {
213
213
  const columns = Object.keys(this.$state.get('SCHEMA'));
214
214
  const removeExcept = columns.filter((column) => !this.$state.get('EXCEPT').includes(column));
215
- return removeExcept.join(', ');
215
+ return removeExcept;
216
216
  }
217
217
  const rawColumns = yield this.queryStatement([
218
218
  `${this.$constants('SHOW')}`,
@@ -222,7 +222,7 @@ class Model extends AbstractModel_1.AbstractModel {
222
222
  ].join(' '));
223
223
  const columns = rawColumns.map((column) => column.Field);
224
224
  const removeExcept = columns.filter((column) => !this.$state.get('EXCEPT').includes(column));
225
- return removeExcept.join(', ');
225
+ return removeExcept;
226
226
  });
227
227
  }
228
228
  /**
@@ -336,11 +336,11 @@ class Model extends AbstractModel_1.AbstractModel {
336
336
  }
337
337
  /**
338
338
  *
339
- * Use relations in registry of model return ignore soft deleted
339
+ * Use relations in registry of model return normal and in trash
340
340
  * @param {...string} nameRelations if data exists return blank
341
341
  * @return {this} this
342
342
  */
343
- withTrashed(...nameRelations) {
343
+ withAndTrashed(...nameRelations) {
344
344
  const relations = this._handleRelations(nameRelations);
345
345
  relations.forEach(relation => relation.trashed = true);
346
346
  const setRelations = this.$state.get('RELATIONS').length
@@ -436,8 +436,8 @@ class Model extends AbstractModel_1.AbstractModel {
436
436
  * @param {...string} nameRelations if data exists return blank
437
437
  * @return {this} this
438
438
  */
439
- relationTrashed(...nameRelations) {
440
- return this.withTrashed(...nameRelations);
439
+ relationsAndTrashed(...nameRelations) {
440
+ return this.withAndTrashed(...nameRelations);
441
441
  }
442
442
  /**
443
443
  * Assign the relation in model Objects
@@ -773,7 +773,7 @@ class Model extends AbstractModel_1.AbstractModel {
773
773
  */
774
774
  toArray(column = 'id') {
775
775
  return __awaiter(this, void 0, void 0, function* () {
776
- this.select(column);
776
+ this.selectRaw(column);
777
777
  const sql = this._buildQueryModel();
778
778
  const result = yield this.queryStatement(sql);
779
779
  const toArray = result.map((data) => data[column]);
@@ -789,7 +789,7 @@ class Model extends AbstractModel_1.AbstractModel {
789
789
  avg(column = 'id') {
790
790
  var _a;
791
791
  return __awaiter(this, void 0, void 0, function* () {
792
- this.select(`${this.$constants('AVG')}(${column}) ${this.$constants('AS')} avg`);
792
+ this.selectRaw(`${this.$constants('AVG')}(${column}) ${this.$constants('AS')} avg`);
793
793
  const sql = this._buildQueryModel();
794
794
  const result = yield this.queryStatement(sql);
795
795
  return ((_a = result === null || result === void 0 ? void 0 : result.shift()) === null || _a === void 0 ? void 0 : _a.avg) || 0;
@@ -804,7 +804,7 @@ class Model extends AbstractModel_1.AbstractModel {
804
804
  sum(column = 'id') {
805
805
  var _a;
806
806
  return __awaiter(this, void 0, void 0, function* () {
807
- this.select(`${this.$constants('SUM')}(${column}) ${this.$constants('AS')} sum`);
807
+ this.selectRaw(`${this.$constants('SUM')}(${column}) ${this.$constants('AS')} sum`);
808
808
  const sql = this._buildQueryModel();
809
809
  const result = yield this.queryStatement(sql);
810
810
  return ((_a = result === null || result === void 0 ? void 0 : result.shift()) === null || _a === void 0 ? void 0 : _a.sum) || 0;
@@ -819,7 +819,7 @@ class Model extends AbstractModel_1.AbstractModel {
819
819
  max(column = 'id') {
820
820
  var _a;
821
821
  return __awaiter(this, void 0, void 0, function* () {
822
- this.select(`${this.$constants('MAX')}(${column}) ${this.$constants('AS')} max`);
822
+ this.selectRaw(`${this.$constants('MAX')}(${column}) ${this.$constants('AS')} max`);
823
823
  const sql = this._buildQueryModel();
824
824
  const result = yield this.queryStatement(sql);
825
825
  return ((_a = result === null || result === void 0 ? void 0 : result.shift()) === null || _a === void 0 ? void 0 : _a.max) || 0;
@@ -834,7 +834,7 @@ class Model extends AbstractModel_1.AbstractModel {
834
834
  min(column = 'id') {
835
835
  var _a;
836
836
  return __awaiter(this, void 0, void 0, function* () {
837
- this.select(`${this.$constants('MIN')}(${column}) ${this.$constants('AS')} min`);
837
+ this.selectRaw(`${this.$constants('MIN')}(${column}) ${this.$constants('AS')} min`);
838
838
  const sql = this._buildQueryModel();
839
839
  const result = yield this.queryStatement(sql);
840
840
  return ((_a = result === null || result === void 0 ? void 0 : result.shift()) === null || _a === void 0 ? void 0 : _a.min) || 0;
@@ -849,7 +849,7 @@ class Model extends AbstractModel_1.AbstractModel {
849
849
  count(column = 'id') {
850
850
  var _a;
851
851
  return __awaiter(this, void 0, void 0, function* () {
852
- this.select(`${this.$constants('COUNT')}(${column}) ${this.$constants('AS')} total`);
852
+ this.selectRaw(`${this.$constants('COUNT')}(${column}) ${this.$constants('AS')} total`);
853
853
  const sql = this._buildQueryModel();
854
854
  const result = yield this.queryStatement(sql);
855
855
  return ((_a = result === null || result === void 0 ? void 0 : result.shift()) === null || _a === void 0 ? void 0 : _a.total) || 0;
@@ -863,12 +863,12 @@ class Model extends AbstractModel_1.AbstractModel {
863
863
  exists() {
864
864
  var _a;
865
865
  return __awaiter(this, void 0, void 0, function* () {
866
- this.select(this.$state.get('PRIMARY_KEY'));
866
+ this.limit(1);
867
867
  const sql = this._buildQueryModel();
868
868
  const result = yield this.queryStatement([
869
869
  `${this.$constants('SELECT')}`,
870
870
  `${this.$constants('EXISTS')}`,
871
- `(${sql} ${this.$constants('LIMIT')} 1)`,
871
+ `(${sql})`,
872
872
  `${this.$constants('AS')} 'exists'`
873
873
  ].join(' '));
874
874
  return !!((_a = result === null || result === void 0 ? void 0 : result.shift()) === null || _a === void 0 ? void 0 : _a.exists) || false;
@@ -941,7 +941,7 @@ class Model extends AbstractModel_1.AbstractModel {
941
941
  if (this.$state.get('VOID'))
942
942
  return null;
943
943
  if ((_a = this.$state.get('EXCEPT')) === null || _a === void 0 ? void 0 : _a.length)
944
- this.select(yield this.exceptColumns());
944
+ this.select(...yield this.exceptColumns());
945
945
  this.limit(1);
946
946
  if (this.$state.get('RELATIONS_EXISTS')) {
947
947
  return yield this._execute({ sql: this._queryRelationsExists(), type: 'FIRST' });
@@ -969,7 +969,7 @@ class Model extends AbstractModel_1.AbstractModel {
969
969
  return __awaiter(this, void 0, void 0, function* () {
970
970
  this._validateMethod('firstOrError');
971
971
  if ((_a = this.$state.get('EXCEPT')) === null || _a === void 0 ? void 0 : _a.length)
972
- this.select(yield this.exceptColumns());
972
+ this.select(...yield this.exceptColumns());
973
973
  this.limit(1);
974
974
  if (this.$state.get('RELATIONS_EXISTS')) {
975
975
  return yield this._execute({ sql: this._queryRelationsExists(), type: 'FIRST_OR_ERROR', message, options });
@@ -1036,7 +1036,7 @@ class Model extends AbstractModel_1.AbstractModel {
1036
1036
  if (this.$state.get('VOID'))
1037
1037
  return [];
1038
1038
  if ((_a = this.$state.get('EXCEPT')) === null || _a === void 0 ? void 0 : _a.length)
1039
- this.select(yield this.exceptColumns());
1039
+ this.select(...yield this.exceptColumns());
1040
1040
  let sql = this._buildQueryModel();
1041
1041
  if (this.$state.get('RELATIONS_EXISTS'))
1042
1042
  sql = this._queryRelationsExists();
@@ -1072,7 +1072,7 @@ class Model extends AbstractModel_1.AbstractModel {
1072
1072
  page = (paginationOptions === null || paginationOptions === void 0 ? void 0 : paginationOptions.page) || page;
1073
1073
  }
1074
1074
  if ((_a = this.$state.get('EXCEPT')) === null || _a === void 0 ? void 0 : _a.length)
1075
- this.select(yield this.exceptColumns());
1075
+ this.select(...yield this.exceptColumns());
1076
1076
  const offset = (page - 1) * limit;
1077
1077
  this.$state.set('PER_PAGE', limit);
1078
1078
  this.$state.set('PAGE', page);
@@ -1107,7 +1107,7 @@ class Model extends AbstractModel_1.AbstractModel {
1107
1107
  var _a;
1108
1108
  return __awaiter(this, void 0, void 0, function* () {
1109
1109
  if ((_a = this.$state.get('EXCEPT')) === null || _a === void 0 ? void 0 : _a.length)
1110
- this.select(yield this.exceptColumns());
1110
+ this.select(...yield this.exceptColumns());
1111
1111
  this.$state.set('GROUP_BY', `${this.$constants('GROUP_BY')} ${column}`);
1112
1112
  this.$state.set('SELECT', [
1113
1113
  `${this.$state.get('SELECT')},`,
@@ -1812,7 +1812,6 @@ class Model extends AbstractModel_1.AbstractModel {
1812
1812
  .whereIn(foreignKey, dataPerentId)
1813
1813
  .debug(this.$state.get('DEBUG'))
1814
1814
  .when(relation.trashed, (query) => query.disableSoftDelete())
1815
- .when(relationIsHasOneOrBelongsTo, (query) => query.limit(1))
1816
1815
  .get();
1817
1816
  return dataFromRelation;
1818
1817
  });
@@ -1898,7 +1897,7 @@ class Model extends AbstractModel_1.AbstractModel {
1898
1897
  var _a, _b;
1899
1898
  return __awaiter(this, void 0, void 0, function* () {
1900
1899
  const currentPage = +(this.$state.get('PAGE'));
1901
- this.select([
1900
+ this.selectRaw([
1902
1901
  `${this.$constants('COUNT')}(${this.$state.get('PRIMARY_KEY')})`,
1903
1902
  `${this.$constants('AS')}`,
1904
1903
  `total`
@@ -2398,7 +2397,7 @@ class Model extends AbstractModel_1.AbstractModel {
2398
2397
  return __awaiter(this, void 0, void 0, function* () {
2399
2398
  this._assertError(!this.$state.get('WHERE'), "can not update [ update ] without where condition");
2400
2399
  const sql = this._buildQueryModel();
2401
- const [result] = yield this.actionStatement({ sql });
2400
+ const result = yield this.actionStatement({ sql });
2402
2401
  if (this.$state.get('VOID'))
2403
2402
  return null;
2404
2403
  if (result == null || !result)
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "tspace-mysql",
3
- "version": "1.3.1",
3
+ "version": "1.3.2",
4
4
  "description": "mysql query builder object relational mapping",
5
5
  "main": "dist/lib/index.js",
6
6
  "types": "dist/lib/index.d.ts",
@@ -40,7 +40,8 @@
40
40
  "dependencies": {
41
41
  "dotenv": "^16.0.3",
42
42
  "mysql2": "^2.3.3",
43
- "pluralize": "^8.0.0"
43
+ "pluralize": "^8.0.0",
44
+ "sql-formatter": "^10.7.1"
44
45
  },
45
46
  "homepage": "https://github.com/thanathip41",
46
47
  "scripts": {
@@ -49,7 +50,6 @@
49
50
  },
50
51
  "devDependencies": {
51
52
  "@types/mysql": "^2.15.19",
52
- "@types/pluralize": "0.0.29",
53
- "tspace-mysql": "^1.1.8"
53
+ "@types/pluralize": "^0.0.29"
54
54
  }
55
55
  }