tspace-mysql 1.5.2 → 1.5.4

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
@@ -2305,7 +2305,6 @@ class User extends Model<SchemaUserType, RelationUserType> {
2305
2305
  }
2306
2306
 
2307
2307
  export { User , SchemaUserType }
2308
- export default User
2309
2308
 
2310
2309
  +--------------------------------------------------------------------------+
2311
2310
 
@@ -2337,7 +2336,6 @@ class Phone extends Model<SchemaPhoneType,RelationPhoneType> {
2337
2336
  }
2338
2337
 
2339
2338
  export { Phone , SchemaPhoneType }
2340
- export default Phone
2341
2339
 
2342
2340
  +--------------------------------------------------------------------------+
2343
2341
 
@@ -2366,12 +2364,14 @@ const users = await new User()
2366
2364
  .findMany()
2367
2365
 
2368
2366
  for(const user of users) {
2369
- user.phone.user
2370
- user.phone.user.id
2371
- user.phone.userx
2372
- user.phone.user.idx
2373
- user.phones.map(phone =>phone.user.id)
2374
- user.phones.map(phone =>phone.user.idx) ❌
2367
+ user.phone.user
2368
+ user.phone?.user ✅
2369
+ user.phone?.user.id
2370
+ user.phone?.userx
2371
+ user.phone?.user.idx
2372
+ user.phones.map(phone =>phone?.user.id) ❌
2373
+ user.phones?.map(phone =>phone?.user.id) ✅
2374
+ user.phones?.map(phone =>phone?.user.idx) ❌
2375
2375
  }
2376
2376
 
2377
2377
  ```
@@ -39,7 +39,7 @@ export interface RelationShip {
39
39
  belongsTo: string;
40
40
  belongsToMany: string;
41
41
  }
42
- export interface Pagination {
42
+ export interface Pagination<K = any[]> {
43
43
  meta: {
44
44
  total: number;
45
45
  limit: number;
@@ -49,7 +49,7 @@ export interface Pagination {
49
49
  next_page: number;
50
50
  prev_page: number;
51
51
  };
52
- data: any[];
52
+ data: K;
53
53
  }
54
54
  export interface Backup {
55
55
  database: string;
@@ -21,7 +21,7 @@ const environment = () => {
21
21
  dotenv_1.default.config({ path: environment() });
22
22
  const ENV = process.env;
23
23
  const env = {
24
- HOST: ENV.DB_HOST || ENV.TSPACE_HOST,
24
+ HOST: ENV.DB_HOST || ENV.TSPACE_HOST || 'localhost',
25
25
  PORT: ENV.DB_PORT || ENV.TSPACE_PORT || 3306,
26
26
  USERNAME: ENV.DB_USERNAME || ENV.TSPACE_USERNAME || ENV.DB_USER,
27
27
  PASSWORD: ENV.DB_PASSWORD || ENV.TSPACE_PASSWORD || '',
@@ -1,5 +1,3 @@
1
- declare const CONSTANTS: {
2
- [key: string]: string | Object;
3
- };
1
+ declare const CONSTANTS: Record<string, any>;
4
2
  export { CONSTANTS };
5
3
  export default CONSTANTS;
@@ -90,102 +90,6 @@ const CONSTANTS = Object.freeze({
90
90
  PATTERN: {
91
91
  snake_case: 'snake_case',
92
92
  camelCase: 'camelCase'
93
- },
94
- DEFAULT: {
95
- DEBUG: false,
96
- },
97
- DB: {
98
- PRIMARY_KEY: 'id',
99
- VOID: false,
100
- RESULT: null,
101
- DISTINCT: false,
102
- PLUCK: '',
103
- SAVE: '',
104
- DELETE: '',
105
- UPDATE: '',
106
- INSERT: '',
107
- SELECT: [],
108
- ONLY: [],
109
- EXCEPTS: [],
110
- CHUNK: 0,
111
- COUNT: '',
112
- FROM: 'FROM',
113
- JOIN: [],
114
- WHERE: [],
115
- GROUP_BY: '',
116
- ORDER_BY: [],
117
- LIMIT: '',
118
- OFFSET: '',
119
- HAVING: '',
120
- TABLE_NAME: '',
121
- UUID_CUSTOM: '',
122
- HIDDEN: [],
123
- DEBUG: false,
124
- UUID: false,
125
- PAGE: 1,
126
- PER_PAGE: 1,
127
- HOOKS: [],
128
- RETURN_TYPE: null
129
- },
130
- MODEL: {
131
- MODEL_NAME: 'MODEL',
132
- PRIMARY_KEY: 'id',
133
- VOID: false,
134
- SELECT: [],
135
- DELETE: '',
136
- UPDATE: '',
137
- INSERT: '',
138
- ONLY: [],
139
- EXCEPTS: [],
140
- CHUNK: 0,
141
- COUNT: '',
142
- FROM: 'FROM',
143
- JOIN: [],
144
- WHERE: [],
145
- GROUP_BY: '',
146
- ORDER_BY: [],
147
- LIMIT: '',
148
- OFFSET: '',
149
- HAVING: '',
150
- TABLE_NAME: '',
151
- UUID_FORMAT: 'uuid',
152
- HIDDEN: [],
153
- DEBUG: false,
154
- UUID: false,
155
- SOFT_DELETE: false,
156
- SOFT_DELETE_FORMAT: 'deleted_at',
157
- SOFT_DELETE_RELATIONS: false,
158
- PAGE: 1,
159
- PER_PAGE: 1,
160
- REGISTRY: {},
161
- RESULT: null,
162
- PATTERN: 'snake_case',
163
- DISTINCT: false,
164
- PLUCK: '',
165
- SAVE: '',
166
- HOOKS: [],
167
- RELATION: [],
168
- RELATIONS: [],
169
- RELATIONS_TRASHED: false,
170
- RELATIONS_EXISTS: false,
171
- RELATIONS_EXISTS_NOT_ID: [],
172
- TIMESTAMP: false,
173
- TIMESTAMP_FORMAT: {
174
- CREATED_AT: 'created_at',
175
- UPDATED_AT: 'updated_at'
176
- },
177
- LOGGER: false,
178
- LOGGER_OPTIONS: null,
179
- TABLE_LOGGER: '$loggers',
180
- VALIDATE_SCHEMA: false,
181
- VALIDATE_SCHEMA_DEFINED: null,
182
- FUNCTION_RELATION: false,
183
- SCHEMA_TABLE: null,
184
- RETRY: 0,
185
- OBSERVER: null,
186
- DATA: null,
187
- BEFORE_CREATING_TABLE: null,
188
- RETURN_TYPE: null
189
93
  }
190
94
  });
191
95
  exports.CONSTANTS = CONSTANTS;
@@ -100,7 +100,7 @@ declare abstract class AbstractBuilder {
100
100
  abstract findOne(): Promise<Record<string, any> | null>;
101
101
  abstract findMany(): Promise<any[]>;
102
102
  abstract getGroupBy(column: string): Promise<any[]>;
103
- abstract findManyGroupBy(column: string): Promise<any[]>;
103
+ abstract findGroupBy(column: string): Promise<any[]>;
104
104
  abstract toArray(column: string): Promise<any[]>;
105
105
  abstract toJSON(): Promise<string>;
106
106
  abstract toSQL(): string;
@@ -117,7 +117,7 @@ declare abstract class AbstractBuilder {
117
117
  abstract save(): Promise<Record<string, any> | any[] | null | undefined>;
118
118
  abstract increment(column: string, value: number): Promise<number>;
119
119
  abstract decrement(column: string, value: number): Promise<number>;
120
- abstract faker(round: number, cb?: Function): Promise<Record<string, any>[]>;
120
+ abstract faker(round: number, cb?: Function): Promise<void>;
121
121
  }
122
122
  export { AbstractBuilder };
123
123
  export default AbstractBuilder;
@@ -15,10 +15,7 @@ class AbstractBuilder {
15
15
  ];
16
16
  this.$utils = {};
17
17
  this.$constants = (name) => { };
18
- this.$state = new State_1.StateHandler({
19
- state: {},
20
- constants: {}
21
- });
18
+ this.$state = new State_1.StateHandler('default');
22
19
  this.$pool = {
23
20
  query: (sql) => { },
24
21
  set: (pool) => { },
@@ -1207,7 +1207,7 @@ declare class Builder extends AbstractBuilder {
1207
1207
  * @param {string} column
1208
1208
  * @return {promise<Array>}
1209
1209
  */
1210
- findManyGroupBy(column: string): Promise<any[]>;
1210
+ findGroupBy(column: string): Promise<any[]>;
1211
1211
  /**
1212
1212
  * The 'save' method is used to persist a new 'Model' or new 'DB' instance or update an existing model instance in the database.
1213
1213
  *
@@ -1289,7 +1289,7 @@ declare class Builder extends AbstractBuilder {
1289
1289
  * @param {number} rows number of rows
1290
1290
  * @return {promise<any>}
1291
1291
  */
1292
- faker(rows: number, cb?: Function): Promise<Record<string, any>[]>;
1292
+ faker(rows: number, cb?: Function): Promise<void>;
1293
1293
  /**
1294
1294
  *
1295
1295
  * truncate of table
@@ -1310,10 +1310,12 @@ declare class Builder extends AbstractBuilder {
1310
1310
  delete?: boolean;
1311
1311
  where?: boolean;
1312
1312
  limit?: boolean;
1313
- offset?: boolean;
1313
+ orderBy?: boolean;
1314
1314
  join?: boolean;
1315
+ offset?: boolean;
1315
1316
  groupBy?: boolean;
1316
1317
  select?: boolean;
1318
+ having?: boolean;
1317
1319
  }): Builder;
1318
1320
  protected _queryBuilder(): {
1319
1321
  select: () => string;
@@ -26,7 +26,6 @@ const utils_1 = require("../utils");
26
26
  const constants_1 = require("../constants");
27
27
  const DB_1 = require("./DB");
28
28
  const connection_1 = require("../connection");
29
- const State_1 = require("./Handlers/State");
30
29
  class Builder extends AbstractBuilder_1.AbstractBuilder {
31
30
  constructor() {
32
31
  super();
@@ -129,15 +128,15 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
129
128
  * @return {this} this
130
129
  */
131
130
  sleep(second) {
132
- const sql = `SELECT SLEEP(${second}) as delay`;
131
+ const sql = `SELECT SLEEP(${second}) as sleep`;
133
132
  this.$state.set('JOIN', [
134
- ...this.$state.get('JOIN'),
135
- [
133
+ ...this.$state.get('JOIN'), [
136
134
  `${this.$constants('INNER_JOIN')}`,
137
135
  `(${sql}) ${this.$constants('AS')} temp`,
138
136
  `${this.$constants('ON')}`,
139
137
  `1=1`
140
- ].join(' ')
138
+ ]
139
+ .join(' ')
141
140
  ]);
142
141
  return this;
143
142
  }
@@ -247,12 +246,18 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
247
246
  * @return {this}
248
247
  */
249
248
  where(column, operator, value) {
250
- if (typeof column === 'object' && column !== null && !Array.isArray(column)) {
249
+ if (typeof column === 'object') {
251
250
  return this.whereObject(column);
252
251
  }
253
252
  [value, operator] = this._valueAndOperator(value, operator, arguments.length === 2);
254
253
  value = this.$utils.escape(value);
255
254
  value = this._valueTrueFalse(value);
255
+ if (value === null) {
256
+ return this.whereNull(column);
257
+ }
258
+ if (Array.isArray(value)) {
259
+ return this.whereIn(column, value);
260
+ }
256
261
  this.$state.set('WHERE', [
257
262
  ...this.$state.get('WHERE'),
258
263
  [
@@ -279,6 +284,12 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
279
284
  [value, operator] = this._valueAndOperator(value, operator, arguments.length === 2);
280
285
  value = this.$utils.escape(value);
281
286
  value = this._valueTrueFalse(value);
287
+ if (value === null) {
288
+ return this.orWhereNull(column);
289
+ }
290
+ if (Array.isArray(value)) {
291
+ return this.orWhereIn(column, value);
292
+ }
282
293
  this.$state.set('WHERE', [
283
294
  ...this.$state.get('WHERE'),
284
295
  [
@@ -488,7 +499,7 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
488
499
  throw new Error(`This 'whereIn' method is required array only`);
489
500
  const values = array.length
490
501
  ? `${array.map((value) => this._checkValueHasRaw(this.$utils.escape(value))).join(',')}`
491
- : this.$constants('NULL');
502
+ : this.$constants(this.$constants('NULL'));
492
503
  this.$state.set('WHERE', [
493
504
  ...this.$state.get('WHERE'),
494
505
  [
@@ -513,7 +524,7 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
513
524
  throw new Error(`This 'orWhereIn' method is required array only`);
514
525
  const values = array.length
515
526
  ? `${array.map((value) => this._checkValueHasRaw(this.$utils.escape(value))).join(',')}`
516
- : this.$constants('NULL');
527
+ : this.$constants(this.$constants('NULL'));
517
528
  this.$state.set('WHERE', [
518
529
  ...this.$state.get('WHERE'),
519
530
  [
@@ -689,9 +700,9 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
689
700
  this.$state.get('WHERE').length ? `${this.$constants('AND')}` : '',
690
701
  `${this.bindColumn(column)}`,
691
702
  `${this.$constants('BETWEEN')}`,
692
- `${this.$constants('NULL')}`,
703
+ `${this.$constants(this.$constants('NULL'))}`,
693
704
  `${this.$constants('AND')}`,
694
- `${this.$constants('NULL')}`
705
+ `${this.$constants(this.$constants('NULL'))}`
695
706
  ].join(' ')
696
707
  ]);
697
708
  return this;
@@ -728,9 +739,9 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
728
739
  this.$state.get('WHERE').length ? `${this.$constants('OR')}` : '',
729
740
  `${this.bindColumn(column)}`,
730
741
  `${this.$constants('BETWEEN')}`,
731
- `${this.$constants('NULL')}`,
742
+ `${this.$constants(this.$constants('NULL'))}`,
732
743
  `${this.$constants('AND')}`,
733
- `${this.$constants('NULL')}`
744
+ `${this.$constants(this.$constants('NULL'))}`
734
745
  ].join(' ')
735
746
  ]);
736
747
  return this;
@@ -767,9 +778,9 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
767
778
  this.$state.get('WHERE').length ? `${this.$constants('AND')}` : '',
768
779
  `${this.bindColumn(column)}`,
769
780
  `${this.$constants('NOT_BETWEEN')}`,
770
- `${this.$constants('NULL')}`,
781
+ `${this.$constants(this.$constants('NULL'))}`,
771
782
  `${this.$constants('AND')}`,
772
- `${this.$constants('NULL')}`
783
+ `${this.$constants(this.$constants('NULL'))}`
773
784
  ].join(' ')
774
785
  ]);
775
786
  return this;
@@ -806,9 +817,9 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
806
817
  this.$state.get('WHERE').length ? `${this.$constants('OR')}` : '',
807
818
  `${this.bindColumn(column)}`,
808
819
  `${this.$constants('NOT_BETWEEN')}`,
809
- `${this.$constants('NULL')}`,
820
+ `${this.$constants(this.$constants('NULL'))}`,
810
821
  `${this.$constants('AND')}`,
811
- `${this.$constants('NULL')}`
822
+ `${this.$constants(this.$constants('NULL'))}`
812
823
  ].join(' ')
813
824
  ]);
814
825
  return this;
@@ -1502,7 +1513,10 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
1502
1513
  return `\`${column}\``;
1503
1514
  }).join(', ');
1504
1515
  }
1505
- this.$state.set('GROUP_BY', `${this.$constants('GROUP_BY')} ${groupBy}`);
1516
+ this.$state.set('GROUP_BY', [
1517
+ ...this.$state.get('GROUP_BY'),
1518
+ `${groupBy}`
1519
+ ]);
1506
1520
  return this;
1507
1521
  }
1508
1522
  /**
@@ -1525,7 +1539,10 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
1525
1539
  return column;
1526
1540
  }).join(', ');
1527
1541
  }
1528
- this.$state.set('GROUP_BY', `${this.$constants('GROUP_BY')} ${groupBy}`);
1542
+ this.$state.set('GROUP_BY', [
1543
+ ...this.$state.get('GROUP_BY'),
1544
+ `${groupBy}`
1545
+ ]);
1529
1546
  return this;
1530
1547
  }
1531
1548
  /**
@@ -1944,8 +1961,8 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
1944
1961
  if (typeof value === 'string' && !(value.includes(this.$constants('RAW')))) {
1945
1962
  value = this.$utils.escapeActions(value);
1946
1963
  }
1947
- return `${this.bindColumn(column)} = ${value == null || value === 'NULL'
1948
- ? 'NULL'
1964
+ return `${this.bindColumn(column)} = ${value == null || value === this.$constants('NULL')
1965
+ ? this.$constants('NULL')
1949
1966
  : typeof value === 'string' && value.includes(this.$constants('RAW'))
1950
1967
  ? `${this.$utils.covertBooleanToNumber(value)}`.replace(this.$constants('RAW'), '')
1951
1968
  : `'${this.$utils.covertBooleanToNumber(value)}'`}`;
@@ -2238,13 +2255,14 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
2238
2255
  */
2239
2256
  pagination(paginationOptions) {
2240
2257
  return __awaiter(this, void 0, void 0, function* () {
2241
- var _a, _b, _c;
2258
+ var _a, _b;
2242
2259
  let limit = 15;
2243
2260
  let page = 1;
2244
2261
  if (paginationOptions != null) {
2245
2262
  limit = (paginationOptions === null || paginationOptions === void 0 ? void 0 : paginationOptions.limit) || limit;
2246
2263
  page = (paginationOptions === null || paginationOptions === void 0 ? void 0 : paginationOptions.page) || page;
2247
2264
  }
2265
+ limit = limit > 1000 ? 1000 : limit;
2248
2266
  const currentPage = page;
2249
2267
  const nextPage = currentPage + 1;
2250
2268
  const prevPage = currentPage - 1 === 0 ? 1 : currentPage - 1;
@@ -2268,22 +2286,14 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
2268
2286
  },
2269
2287
  data: []
2270
2288
  };
2271
- const sqlCount = [
2272
- `${this.$constants('SELECT')}`,
2273
- `${this.$constants('COUNT')}(*)`,
2274
- `${this.$constants('AS')} total`
2275
- ].join(' ');
2276
- const sqlTotal = [
2277
- sqlCount,
2278
- this.$state.get('FROM'),
2279
- this.$state.get('TABLE_NAME'),
2280
- this._queryBuilder().where()
2281
- ].join(' ');
2282
- const count = yield this._queryStatement(sqlTotal);
2283
- const total = ((_b = count === null || count === void 0 ? void 0 : count.shift()) === null || _b === void 0 ? void 0 : _b.total) || 0;
2289
+ const total = yield new DB_1.DB()
2290
+ .copyBuilder(this, { where: true, join: true })
2291
+ .bind(this.$pool.get())
2292
+ .debug(this.$state.get('DEBUG'))
2293
+ .count();
2284
2294
  let lastPage = Math.ceil(total / limit) || 0;
2285
2295
  lastPage = lastPage > 1 ? lastPage : 1;
2286
- const totalPage = (_c = result === null || result === void 0 ? void 0 : result.length) !== null && _c !== void 0 ? _c : 0;
2296
+ const totalPage = (_b = result === null || result === void 0 ? void 0 : result.length) !== null && _b !== void 0 ? _b : 0;
2287
2297
  return {
2288
2298
  meta: {
2289
2299
  total: total,
@@ -2573,9 +2583,11 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
2573
2583
  count() {
2574
2584
  return __awaiter(this, arguments, void 0, function* (column = 'id') {
2575
2585
  const distinct = this.$state.get('DISTINCT');
2576
- column = distinct
2577
- ? `${this.$constants('DISTINCT')} ${this.bindColumn(column)}`
2578
- : `${this.bindColumn(column)}`;
2586
+ column = column === '*'
2587
+ ? '*'
2588
+ : distinct
2589
+ ? `${this.$constants('DISTINCT')} ${this.bindColumn(column)}`
2590
+ : `${this.bindColumn(column)}`;
2579
2591
  this.selectRaw(`${this.$constants('COUNT')}(${column}) ${this.$constants('AS')} \`aggregate\``);
2580
2592
  const sql = this._queryBuilder().select();
2581
2593
  const result = yield this._queryStatement(sql);
@@ -2661,7 +2673,7 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
2661
2673
  delete() {
2662
2674
  return __awaiter(this, void 0, void 0, function* () {
2663
2675
  var _a;
2664
- if (!this.$state.get('where').length) {
2676
+ if (!this.$state.get('WHERE').length) {
2665
2677
  throw new Error("can't delete without where condition");
2666
2678
  }
2667
2679
  this.limit(1);
@@ -2685,7 +2697,7 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
2685
2697
  deleteMany() {
2686
2698
  return __awaiter(this, void 0, void 0, function* () {
2687
2699
  var _a;
2688
- if (!this.$state.get('where').length) {
2700
+ if (!this.$state.get('WHERE').length) {
2689
2701
  throw new Error("can't delete without where condition");
2690
2702
  }
2691
2703
  this.$state.set('DELETE', [
@@ -2778,7 +2790,7 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
2778
2790
  * @param {string} column
2779
2791
  * @return {promise<Array>}
2780
2792
  */
2781
- findManyGroupBy(column) {
2793
+ findGroupBy(column) {
2782
2794
  return __awaiter(this, void 0, void 0, function* () {
2783
2795
  return yield this.getGroupBy(column);
2784
2796
  });
@@ -3014,7 +3026,7 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
3014
3026
  return `(${Object.values(value).map((v) => {
3015
3027
  if (typeof v === 'object' && v != null && !Array.isArray(v))
3016
3028
  return `'${JSON.stringify(v)}'`;
3017
- return v == null ? 'NULL' : `'${v}'`;
3029
+ return v == null ? this.$constants('NULL') : `'${v}'`;
3018
3030
  }).join(', ')})`;
3019
3031
  });
3020
3032
  return values;
@@ -3057,7 +3069,8 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
3057
3069
  }
3058
3070
  data = [...data, columnAndValue];
3059
3071
  }
3060
- return yield this.createMultiple(data).save();
3072
+ yield this.createMultiple(data).save();
3073
+ return;
3061
3074
  });
3062
3075
  }
3063
3076
  /**
@@ -3141,24 +3154,29 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
3141
3154
  const newInstance = new Builder();
3142
3155
  newInstance.$state.clone(copy);
3143
3156
  newInstance.$state.set('SAVE', '');
3144
- if ((options === null || options === void 0 ? void 0 : options.insert) == null)
3157
+ newInstance.$state.set('DEBUG', false);
3158
+ if ((options === null || options === void 0 ? void 0 : options.insert) == null || !options.insert)
3145
3159
  newInstance.$state.set('INSERT', '');
3146
- if ((options === null || options === void 0 ? void 0 : options.update) == null)
3160
+ if ((options === null || options === void 0 ? void 0 : options.update) == null || !options.update)
3147
3161
  newInstance.$state.set('UPDATE', '');
3148
- if ((options === null || options === void 0 ? void 0 : options.delete) == null)
3162
+ if ((options === null || options === void 0 ? void 0 : options.delete) == null || !options.delete)
3149
3163
  newInstance.$state.set('DELETE', '');
3150
- if ((options === null || options === void 0 ? void 0 : options.where) == null)
3151
- newInstance.$state.set('WHERE', '');
3152
- if ((options === null || options === void 0 ? void 0 : options.limit) == null)
3164
+ if ((options === null || options === void 0 ? void 0 : options.where) == null || !options.where)
3165
+ newInstance.$state.set('WHERE', []);
3166
+ if ((options === null || options === void 0 ? void 0 : options.limit) == null || !options.limit)
3153
3167
  newInstance.$state.set('LIMIT', '');
3154
- if ((options === null || options === void 0 ? void 0 : options.offset) == null)
3168
+ if ((options === null || options === void 0 ? void 0 : options.offset) == null || !options.offset)
3155
3169
  newInstance.$state.set('OFFSET', '');
3156
- if ((options === null || options === void 0 ? void 0 : options.groupBy) == null)
3170
+ if ((options === null || options === void 0 ? void 0 : options.groupBy) == null || !options.groupBy)
3157
3171
  newInstance.$state.set('GROUP_BY', '');
3158
- if ((options === null || options === void 0 ? void 0 : options.select) == null)
3159
- newInstance.$state.set('SELECT', '');
3160
- if ((options === null || options === void 0 ? void 0 : options.join) == null)
3161
- newInstance.$state.set('JOIN', '');
3172
+ if ((options === null || options === void 0 ? void 0 : options.orderBy) == null || !options.orderBy)
3173
+ newInstance.$state.set('ORDER_BY', []);
3174
+ if ((options === null || options === void 0 ? void 0 : options.select) == null || !options.select)
3175
+ newInstance.$state.set('SELECT', []);
3176
+ if ((options === null || options === void 0 ? void 0 : options.join) == null || !options.join)
3177
+ newInstance.$state.set('JOIN', []);
3178
+ if ((options === null || options === void 0 ? void 0 : options.having) == null || !options.having)
3179
+ newInstance.$state.set('HAVING', '');
3162
3180
  return newInstance;
3163
3181
  }
3164
3182
  _queryBuilder() {
@@ -3194,6 +3212,11 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
3194
3212
  return null;
3195
3213
  return `${this.$constants('ORDER_BY')} ${values.map(v => v.replace(/^\s/, '').replace(/\s+/g, ' ')).join(', ')}`;
3196
3214
  };
3215
+ const bindGroupBy = (values) => {
3216
+ if (!Array.isArray(values) || !values.length)
3217
+ return null;
3218
+ return `${this.$constants('GROUP_BY')} ${values.map(v => v.replace(/^\s/, '').replace(/\s+/g, ' ')).join(', ')}`;
3219
+ };
3197
3220
  const select = () => {
3198
3221
  return buildSQL([
3199
3222
  bindSelect(this.$state.get('SELECT')),
@@ -3201,7 +3224,7 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
3201
3224
  this.$state.get('TABLE_NAME'),
3202
3225
  bindJoin(this.$state.get('JOIN')),
3203
3226
  bindWhere(this.$state.get('WHERE')),
3204
- this.$state.get('GROUP_BY'),
3227
+ bindGroupBy(this.$state.get('GROUP_BY')),
3205
3228
  this.$state.get('HAVING'),
3206
3229
  bindOrderBy(this.$state.get('ORDER_BY')),
3207
3230
  this.$state.get('LIMIT'),
@@ -3280,7 +3303,7 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
3280
3303
  }
3281
3304
  _insertNotExists() {
3282
3305
  return __awaiter(this, void 0, void 0, function* () {
3283
- if (!this.$state.get('where').length)
3306
+ if (!this.$state.get('WHERE').length)
3284
3307
  throw new Error("Can't insert not exists without where condition");
3285
3308
  let sql = [
3286
3309
  `${this.$constants('SELECT')}`,
@@ -3356,7 +3379,7 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
3356
3379
  }
3357
3380
  _insertOrSelect() {
3358
3381
  return __awaiter(this, void 0, void 0, function* () {
3359
- if (!this.$state.get('where').length) {
3382
+ if (!this.$state.get('WHERE').length) {
3360
3383
  throw new Error("Can't create or select without where condition");
3361
3384
  }
3362
3385
  let sql = [
@@ -3410,7 +3433,7 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
3410
3433
  }
3411
3434
  _updateOrInsert() {
3412
3435
  return __awaiter(this, void 0, void 0, function* () {
3413
- if (!this.$state.get('where').length) {
3436
+ if (!this.$state.get('WHERE').length) {
3414
3437
  throw new Error("Can't update or insert without where condition");
3415
3438
  }
3416
3439
  let sql = [
@@ -3466,7 +3489,7 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
3466
3489
  }
3467
3490
  _update() {
3468
3491
  return __awaiter(this, arguments, void 0, function* (ignoreWhere = false) {
3469
- if (!this.$state.get('where').length && !ignoreWhere)
3492
+ if (!this.$state.get('WHERE').length && !ignoreWhere)
3470
3493
  throw new Error("can't update without where condition");
3471
3494
  const result = yield this._actionStatement({
3472
3495
  sql: this._queryBuilder().update()
@@ -3499,8 +3522,8 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
3499
3522
  if (typeof value === 'string' && !(value.includes(this.$constants('RAW')))) {
3500
3523
  value = this.$utils.escapeActions(value);
3501
3524
  }
3502
- return `${this.bindColumn(column)} = ${value == null || value === 'NULL'
3503
- ? 'NULL'
3525
+ return `${this.bindColumn(column)} = ${value == null || value === this.$constants('NULL')
3526
+ ? this.$constants('NULL')
3504
3527
  : this._checkValueHasRaw(value)}`;
3505
3528
  });
3506
3529
  return `${this.$constants('SET')} ${values}`;
@@ -3512,8 +3535,8 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
3512
3535
  if (typeof value === 'string' && !(value.includes(this.$constants('RAW')))) {
3513
3536
  value = this.$utils.escapeActions(value);
3514
3537
  }
3515
- return `${value == null || value === 'NULL'
3516
- ? 'NULL'
3538
+ return `${value == null || value === this.$constants('NULL')
3539
+ ? this.$constants('NULL')
3517
3540
  : this._checkValueHasRaw(value)}`;
3518
3541
  });
3519
3542
  return [
@@ -3531,8 +3554,8 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
3531
3554
  if (typeof value === 'string' && !(value.includes(this.$constants('RAW')))) {
3532
3555
  value = this.$utils.escapeActions(value);
3533
3556
  }
3534
- return `${value == null || value === 'NULL'
3535
- ? 'NULL'
3557
+ return `${value == null || value === this.$constants('NULL')
3558
+ ? this.$constants('NULL')
3536
3559
  : this._checkValueHasRaw(value)}`;
3537
3560
  });
3538
3561
  values.push(`(${vals.join(',')})`);
@@ -3548,7 +3571,7 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
3548
3571
  if (useDefault)
3549
3572
  return [operator, '='];
3550
3573
  if (operator == null)
3551
- throw new Error('Bad arguments');
3574
+ throw new Error(`The arguments are required. Please check the your arguments in method 'where' or etc methods.`);
3552
3575
  if (operator.toUpperCase() === this.$constants('LIKE')) {
3553
3576
  operator = operator.toUpperCase();
3554
3577
  }
@@ -3593,10 +3616,9 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
3593
3616
  if (name == null)
3594
3617
  return constants_1.CONSTANTS;
3595
3618
  if (!constants_1.CONSTANTS.hasOwnProperty(name.toUpperCase()))
3596
- throw new Error(`Not found constants : ${name}`);
3619
+ throw new Error(`Not found that constant : '${name}'`);
3597
3620
  return constants_1.CONSTANTS[name.toUpperCase()];
3598
3621
  };
3599
- this.$state = new State_1.StateHandler(this.$constants('DEFAULT'));
3600
3622
  }
3601
3623
  }
3602
3624
  exports.Builder = Builder;
@@ -792,7 +792,7 @@ class DB extends AbstractDB_1.AbstractDB {
792
792
  });
793
793
  }
794
794
  _initialDB() {
795
- this.$state = new State_1.default(this.$constants('DB'));
795
+ this.$state = new State_1.default('db');
796
796
  return this;
797
797
  }
798
798
  }