tspace-mysql 1.5.2 → 1.5.3

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
  ```
@@ -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;
@@ -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) => { },
@@ -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
@@ -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
  [
@@ -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
  /**
@@ -2661,7 +2678,7 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
2661
2678
  delete() {
2662
2679
  return __awaiter(this, void 0, void 0, function* () {
2663
2680
  var _a;
2664
- if (!this.$state.get('where').length) {
2681
+ if (!this.$state.get('WHERE').length) {
2665
2682
  throw new Error("can't delete without where condition");
2666
2683
  }
2667
2684
  this.limit(1);
@@ -2685,7 +2702,7 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
2685
2702
  deleteMany() {
2686
2703
  return __awaiter(this, void 0, void 0, function* () {
2687
2704
  var _a;
2688
- if (!this.$state.get('where').length) {
2705
+ if (!this.$state.get('WHERE').length) {
2689
2706
  throw new Error("can't delete without where condition");
2690
2707
  }
2691
2708
  this.$state.set('DELETE', [
@@ -3057,7 +3074,8 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
3057
3074
  }
3058
3075
  data = [...data, columnAndValue];
3059
3076
  }
3060
- return yield this.createMultiple(data).save();
3077
+ yield this.createMultiple(data).save();
3078
+ return;
3061
3079
  });
3062
3080
  }
3063
3081
  /**
@@ -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,8 @@ 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
+ // this.$state.get('GROUP_BY'),
3228
+ bindGroupBy(this.$state.get('GROUP_BY')),
3205
3229
  this.$state.get('HAVING'),
3206
3230
  bindOrderBy(this.$state.get('ORDER_BY')),
3207
3231
  this.$state.get('LIMIT'),
@@ -3280,7 +3304,7 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
3280
3304
  }
3281
3305
  _insertNotExists() {
3282
3306
  return __awaiter(this, void 0, void 0, function* () {
3283
- if (!this.$state.get('where').length)
3307
+ if (!this.$state.get('WHERE').length)
3284
3308
  throw new Error("Can't insert not exists without where condition");
3285
3309
  let sql = [
3286
3310
  `${this.$constants('SELECT')}`,
@@ -3356,7 +3380,7 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
3356
3380
  }
3357
3381
  _insertOrSelect() {
3358
3382
  return __awaiter(this, void 0, void 0, function* () {
3359
- if (!this.$state.get('where').length) {
3383
+ if (!this.$state.get('WHERE').length) {
3360
3384
  throw new Error("Can't create or select without where condition");
3361
3385
  }
3362
3386
  let sql = [
@@ -3410,7 +3434,7 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
3410
3434
  }
3411
3435
  _updateOrInsert() {
3412
3436
  return __awaiter(this, void 0, void 0, function* () {
3413
- if (!this.$state.get('where').length) {
3437
+ if (!this.$state.get('WHERE').length) {
3414
3438
  throw new Error("Can't update or insert without where condition");
3415
3439
  }
3416
3440
  let sql = [
@@ -3466,7 +3490,7 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
3466
3490
  }
3467
3491
  _update() {
3468
3492
  return __awaiter(this, arguments, void 0, function* (ignoreWhere = false) {
3469
- if (!this.$state.get('where').length && !ignoreWhere)
3493
+ if (!this.$state.get('WHERE').length && !ignoreWhere)
3470
3494
  throw new Error("can't update without where condition");
3471
3495
  const result = yield this._actionStatement({
3472
3496
  sql: this._queryBuilder().update()
@@ -3593,10 +3617,9 @@ class Builder extends AbstractBuilder_1.AbstractBuilder {
3593
3617
  if (name == null)
3594
3618
  return constants_1.CONSTANTS;
3595
3619
  if (!constants_1.CONSTANTS.hasOwnProperty(name.toUpperCase()))
3596
- throw new Error(`Not found constants : ${name}`);
3620
+ throw new Error(`Not found that constant : '${name}'`);
3597
3621
  return constants_1.CONSTANTS[name.toUpperCase()];
3598
3622
  };
3599
- this.$state = new State_1.StateHandler(this.$constants('DEFAULT'));
3600
3623
  }
3601
3624
  }
3602
3625
  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
  }
@@ -1,8 +1,8 @@
1
1
  declare class StateHandler {
2
2
  private STATE;
3
- constructor(constant: Record<string, any>);
3
+ constructor(state: 'model' | 'db' | 'default');
4
4
  original(): Map<string, any>;
5
- get(key?: string | null): any;
5
+ get(key?: string): any;
6
6
  set(key: string, value: any): void;
7
7
  clone(data: any): void;
8
8
  reset(): void;
@@ -1,18 +1,126 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.StateHandler = void 0;
4
+ const STATE_DEFAULT = {};
5
+ const STATE_DB = {
6
+ PRIMARY_KEY: 'id',
7
+ VOID: false,
8
+ RESULT: null,
9
+ DISTINCT: false,
10
+ PLUCK: '',
11
+ SAVE: '',
12
+ DELETE: '',
13
+ UPDATE: '',
14
+ INSERT: '',
15
+ SELECT: [],
16
+ ONLY: [],
17
+ EXCEPTS: [],
18
+ CHUNK: 0,
19
+ COUNT: '',
20
+ FROM: 'FROM',
21
+ JOIN: [],
22
+ WHERE: [],
23
+ GROUP_BY: [],
24
+ ORDER_BY: [],
25
+ LIMIT: '',
26
+ OFFSET: '',
27
+ HAVING: '',
28
+ TABLE_NAME: '',
29
+ UUID_CUSTOM: '',
30
+ HIDDEN: [],
31
+ DEBUG: false,
32
+ UUID: false,
33
+ PAGE: 1,
34
+ PER_PAGE: 1,
35
+ HOOKS: [],
36
+ RETURN_TYPE: null
37
+ };
38
+ const STATE_MODEL = {
39
+ MODEL_NAME: 'MODEL',
40
+ PRIMARY_KEY: 'id',
41
+ VOID: false,
42
+ SELECT: [],
43
+ DELETE: '',
44
+ UPDATE: '',
45
+ INSERT: '',
46
+ ONLY: [],
47
+ EXCEPTS: [],
48
+ CHUNK: 0,
49
+ COUNT: '',
50
+ FROM: 'FROM',
51
+ JOIN: [],
52
+ WHERE: [],
53
+ GROUP_BY: [],
54
+ ORDER_BY: [],
55
+ LIMIT: '',
56
+ OFFSET: '',
57
+ HAVING: '',
58
+ TABLE_NAME: '',
59
+ UUID_FORMAT: 'uuid',
60
+ HIDDEN: [],
61
+ DEBUG: false,
62
+ UUID: false,
63
+ SOFT_DELETE: false,
64
+ SOFT_DELETE_FORMAT: 'deleted_at',
65
+ SOFT_DELETE_RELATIONS: false,
66
+ PAGE: 1,
67
+ PER_PAGE: 1,
68
+ REGISTRY: {},
69
+ RESULT: null,
70
+ PATTERN: 'snake_case',
71
+ DISTINCT: false,
72
+ PLUCK: '',
73
+ SAVE: '',
74
+ HOOKS: [],
75
+ RELATION: [],
76
+ RELATIONS: [],
77
+ RELATIONS_TRASHED: false,
78
+ RELATIONS_EXISTS: false,
79
+ TIMESTAMP: false,
80
+ TIMESTAMP_FORMAT: {
81
+ CREATED_AT: 'created_at',
82
+ UPDATED_AT: 'updated_at'
83
+ },
84
+ LOGGER: false,
85
+ LOGGER_OPTIONS: null,
86
+ TABLE_LOGGER: '$loggers',
87
+ VALIDATE_SCHEMA: false,
88
+ VALIDATE_SCHEMA_DEFINED: null,
89
+ FUNCTION_RELATION: false,
90
+ SCHEMA_TABLE: null,
91
+ RETRY: 0,
92
+ OBSERVER: null,
93
+ DATA: null,
94
+ BEFORE_CREATING_TABLE: null,
95
+ RETURN_TYPE: null
96
+ };
4
97
  class StateHandler {
5
- constructor(constant) {
98
+ constructor(state) {
6
99
  this.STATE = {
7
100
  currentState: new Map(),
8
101
  defaultState: new Map()
9
102
  };
10
- const currentState = new Map(Object.entries(Object.assign({}, constant)));
11
- const defaultState = new Map(Object.entries(Object.assign({}, constant)));
12
- this.STATE = {
13
- currentState,
14
- defaultState
15
- };
103
+ switch (state) {
104
+ case 'db': {
105
+ const currentState = new Map(Object.entries(Object.assign({}, STATE_DB)));
106
+ const defaultState = new Map(Object.entries(Object.assign({}, STATE_DB)));
107
+ this.STATE = { currentState, defaultState };
108
+ return this;
109
+ }
110
+ case 'model': {
111
+ const currentState = new Map(Object.entries(Object.assign({}, STATE_MODEL)));
112
+ const defaultState = new Map(Object.entries(Object.assign({}, STATE_MODEL)));
113
+ this.STATE = { currentState, defaultState };
114
+ return this;
115
+ }
116
+ case 'default': {
117
+ const currentState = new Map(Object.entries(Object.assign({}, STATE_DEFAULT)));
118
+ const defaultState = new Map(Object.entries(Object.assign({}, STATE_DEFAULT)));
119
+ this.STATE = { currentState, defaultState };
120
+ return this;
121
+ }
122
+ default: throw new Error(`Unknown the state : '${state}'`);
123
+ }
16
124
  }
17
125
  original() {
18
126
  return this.STATE.defaultState;
@@ -20,12 +128,18 @@ class StateHandler {
20
128
  get(key) {
21
129
  if (key == null)
22
130
  return this.STATE.currentState;
23
- this._assertError(!this.STATE.currentState.has(key) && key !== 'DEBUG', `Can't get this [ ${key} ]`);
24
- return this.STATE.currentState.get(key.toUpperCase());
131
+ key = key.toUpperCase();
132
+ if (!this.STATE.currentState.has(key) && key !== 'DEBUG') {
133
+ return this._assertError('DEBUG', `This state does not have that key '${key}'`);
134
+ }
135
+ return this.STATE.currentState.get(key);
25
136
  }
26
137
  set(key, value) {
27
- this._assertError(!this.STATE.currentState.has(key), `Can't set this [ ${key} ]`);
28
- this.STATE.currentState.set(key.toUpperCase(), value);
138
+ key = key.toUpperCase();
139
+ if (!this.STATE.currentState.has(key)) {
140
+ return this._assertError(`That key '${key}' can't be set in the state`);
141
+ }
142
+ this.STATE.currentState.set(key, value);
29
143
  return;
30
144
  }
31
145
  clone(data) {
@@ -40,7 +154,7 @@ class StateHandler {
40
154
  this.STATE.currentState.set('LIMIT', '');
41
155
  this.STATE.currentState.set('OFFSET', '');
42
156
  this.STATE.currentState.set('SELECT', []);
43
- this.STATE.currentState.set('GROUP_BY', '');
157
+ this.STATE.currentState.set('GROUP_BY', []);
44
158
  this.STATE.currentState.set('ORDER_BY', []);
45
159
  this.STATE.currentState.set('HAVING', '');
46
160
  this.STATE.currentState.set('JOIN', []);