tspace-mysql 1.4.6 → 1.4.7

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.
@@ -1,98 +1,78 @@
1
1
  import { AbstractDB } from './Abstract/AbstractDB';
2
2
  import { Connection, ConnectionOptions, ConnectionTransaction } from './Interface';
3
3
  /**
4
- * Assign table name
4
+ * 'DB' class is a component of the database system
5
5
  * @param {string?} table table name
6
+ * @example
7
+ * new DB('users').findMany().then(results => console.log(results))
6
8
  */
7
9
  declare class DB extends AbstractDB {
8
10
  constructor(table?: string);
9
11
  /**
10
- * Covert result to array
11
- * @param {any} result table name
12
- * @return {Array} array
13
- */
14
- makeArray(result: any): Array<any>;
15
- /**
16
- * Covert result to object or null
17
- * @param {any} result table name
18
- * @return {Record | null} object or null
19
- */
20
- makeObject(result: any): Record<string, any> | null;
21
- /**
22
- * Assign table name
12
+ * The 'table' method is used to define the table name.
23
13
  * @param {string} table table name
24
14
  * @return {this} this
25
15
  */
26
16
  table(table: string): this;
27
17
  /**
28
- * Get constant
29
- * @param {string} constant
30
- * @return {string | object} string || object
18
+ * The 'table' method is used to define the table name.
19
+ * @param {string} table table name
20
+ * @return {DB} DB
31
21
  */
32
- constants(constant?: string): string | {
33
- [key: string]: any;
34
- };
22
+ static table(table: string): DB;
35
23
  /**
36
- * cases query
37
- * @param {arrayObject} cases array object {when , then }
38
- * @param {string?} final else condition
24
+ * The 'jsonObject' method is used to specify select data to JSON objects.
25
+ * @param {string} object table name
26
+ * @param {string} alias
39
27
  * @return {string} string
40
28
  */
41
- caseUpdate(cases: {
42
- when: string;
43
- then: string;
44
- }[], final?: string): string | [];
29
+ jsonObject(object: Record<string, string>, alias: string): string;
45
30
  /**
46
- * generate UUID
31
+ * The 'jsonObject' method is used to specify select data to JSON objects.
32
+ * @static
33
+ * @param {string} object table name
34
+ * @param {string} alias
47
35
  * @return {string} string
48
36
  */
49
- generateUUID(): string;
37
+ static jsonObject(object: Record<string, string>, alias: string): string;
50
38
  /**
51
- * Assign raw query for schema validation
52
- * @param {string} sql
39
+ * The 'JSONObject' method is used to specify select data to JSON objects.
40
+ * @param {string} object table name
41
+ * @param {string} alias
53
42
  * @return {string} string
54
43
  */
55
- raw(sql: string): string;
44
+ JSONObject(object: Record<string, string>, alias: string): string;
56
45
  /**
57
- * Get a pool connection
58
- * @param {Object} options options for connection database with credentials
59
- * @param {string} option.host
60
- * @param {number} option.port
61
- * @param {string} option.database
62
- * @param {string} option.username
63
- * @param {string} option.password
64
- * @return {Connection}
65
- */
66
- getConnection(options?: ConnectionOptions): Promise<Connection>;
67
- /**
68
- * Get a connection
69
- * @return {ConnectionTransaction} object - Connection for the transaction
70
- * @type {object} connection
71
- * @property {function} connection.query - execute query sql then release connection to pool
72
- * @property {function} connection.startTransaction - start transaction of query
73
- * @property {function} connection.commit - commit transaction of query
74
- * @property {function} connection.rollback - rollback transaction of query
46
+ * The 'JSONObject' method is used to specify select data to JSON objects.
47
+ * @static
48
+ * @param {string} object table name
49
+ * @param {string} alias
50
+ * @return {string} string
75
51
  */
76
- beginTransaction(): Promise<ConnectionTransaction>;
52
+ static JSONObject(object: Record<string, string>, alias: string): string;
77
53
  /**
78
- * Covert result to array
79
- * @param {any} result table name
80
- * @return {Array} array
54
+ * The 'constants' method is used to return constants with key or none in 'DB' or 'Model'.
55
+ * @param {string} key
56
+ * @return {string | object} string || object
81
57
  */
82
- static makeArray(result: any): any[];
58
+ constants(key?: string): string | Record<string, any>;
83
59
  /**
84
- * Covert result to object | null
85
- * @param {any} result table name
86
- * @return {Record | null} object | null
60
+ * The 'constants' method is used to return constants with key or none in 'DB' or 'Model'.
61
+ * @static
62
+ * @param {string} key
63
+ * @return {string | object} string || object
87
64
  */
88
- static makeObject(result: any): Record<string, any> | null;
65
+ static constants(key?: string): string | Record<string, any>;
89
66
  /**
90
- * Assign table name
91
- * @static
92
- * @param {string} table table name
93
- * @return {DB} DB
67
+ * cases query
68
+ * @param {arrayObject} cases array object {when , then }
69
+ * @param {string?} final else condition
70
+ * @return {string} string
94
71
  */
95
- static table(table: string): DB;
72
+ caseUpdate(cases: {
73
+ when: string;
74
+ then: string;
75
+ }[], final?: string): string | [];
96
76
  /**
97
77
  * select by cases
98
78
  * @static
@@ -105,30 +85,58 @@ declare class DB extends AbstractDB {
105
85
  then: string;
106
86
  }[], final?: string): string | [];
107
87
  /**
108
- * Assign raw query for schema validation
109
- * @static
110
- * @param {string} sql
88
+ * The 'generateUUID' methid is used to generate a universal unique identifier.
111
89
  * @return {string} string
112
90
  */
113
- static raw(sql: string): string;
91
+ generateUUID(): string;
114
92
  /**
115
- * generate UUID
93
+ * The 'generateUUID' methid is used to generate a universal unique identifier.
116
94
  * @static
117
95
  * @return {string} string
118
96
  */
119
97
  static generateUUID(): string;
120
98
  /**
121
- * Get constant
122
- * @static
123
- * @param {string} constant
124
- * @return {string | object} string || object
99
+ * The 'raw' methid is used to allow for raw sql queries to some method in 'DB' or 'Model'.
100
+ * @param {string} sql
101
+ * @return {string} string
125
102
  */
126
- static constants(constant?: string): string | {
127
- [key: string]: any;
128
- };
103
+ raw(sql: string): string;
129
104
  /**
130
- * Get a connection
105
+ * The 'raw' methid is used to allow for raw sql queries to some method in 'DB' or 'Model'.
131
106
  * @static
107
+ * @param {string} sql
108
+ * @return {string} string
109
+ */
110
+ static raw(sql: string): string;
111
+ /**
112
+ * The 'getConnection' method is used to get a pool connection.
113
+ * @param {Object} options options for connection database with credentials
114
+ * @property {string} option.host
115
+ * @property {number} option.port
116
+ * @property {string} option.database
117
+ * @property {string} option.username
118
+ * @property {string} option.password
119
+ * @return {Connection}
120
+ */
121
+ getConnection(options?: ConnectionOptions): Promise<Connection>;
122
+ /**
123
+ * The 'getConnection' method is used to get a pool connection.
124
+ * @param {Object} options options for connection database with credentials
125
+ * @property {string} option.host
126
+ * @property {number} option.port
127
+ * @property {string} option.database
128
+ * @property {string} option.username
129
+ * @property {string} option.password
130
+ * @return {Connection}
131
+ */
132
+ static getConnection(options: ConnectionOptions): Promise<Connection>;
133
+ /**
134
+ * The 'beginTransaction' is a method used to initiate a database transaction within your application's code.
135
+ *
136
+ * A database transaction is a way to group multiple database operations (such as inserts, updates, or deletes) into a single unit of work.
137
+ *
138
+ * Transactions are typically used when you want to ensure that a series of database operations either all succeed or all fail together,
139
+ * ensuring data integrity.
132
140
  * @return {ConnectionTransaction} object - Connection for the transaction
133
141
  * @type {object} connection
134
142
  * @property {function} connection.query - execute query sql then release connection to pool
@@ -136,19 +144,23 @@ declare class DB extends AbstractDB {
136
144
  * @property {function} connection.commit - commit transaction of query
137
145
  * @property {function} connection.rollback - rollback transaction of query
138
146
  */
139
- static beginTransaction(): Promise<ConnectionTransaction>;
147
+ beginTransaction(): Promise<ConnectionTransaction>;
140
148
  /**
141
- * Get a pool connection
142
- * @param {Object} options options for connection database with credentials
143
- * @param {string} option.host
144
- * @param {number} option.port
145
- * @param {string} option.database
146
- * @param {string} option.username
147
- * @param {string} option.password
148
- * @return {Connection}
149
+ * The 'beginTransaction' is a method used to initiate a database transaction within your application's code.
150
+ *
151
+ * A database transaction is a way to group multiple database operations (such as inserts, updates, or deletes) into a single unit of work.
152
+ *
153
+ * Transactions are typically used when you want to ensure that a series of database operations either all succeed or all fail together,
154
+ * ensuring data integrity.
155
+ * @static
156
+ * @return {ConnectionTransaction} object - Connection for the transaction
157
+ * @type {object} connection
158
+ * @property {function} connection.query - execute query sql then release connection to pool
159
+ * @property {function} connection.startTransaction - start transaction of query
160
+ * @property {function} connection.commit - commit transaction of query
161
+ * @property {function} connection.rollback - rollback transaction of query
149
162
  */
150
- static getConnection(options: ConnectionOptions): Promise<Connection>;
151
- private _typeOf;
163
+ static beginTransaction(): Promise<ConnectionTransaction>;
152
164
  private _initialDB;
153
165
  }
154
166
  export { DB };
@@ -29,8 +29,10 @@ const ProxyHandler_1 = require("./ProxyHandler");
29
29
  const connection_1 = require("../connection");
30
30
  const StateHandler_1 = __importDefault(require("./StateHandler"));
31
31
  /**
32
- * Assign table name
32
+ * 'DB' class is a component of the database system
33
33
  * @param {string?} table table name
34
+ * @example
35
+ * new DB('users').findMany().then(results => console.log(results))
34
36
  */
35
37
  class DB extends AbstractDB_1.AbstractDB {
36
38
  constructor(table) {
@@ -41,71 +43,87 @@ class DB extends AbstractDB_1.AbstractDB {
41
43
  return new Proxy(this, ProxyHandler_1.proxyHandler);
42
44
  }
43
45
  /**
44
- * Covert result to array
45
- * @param {any} result table name
46
- * @return {Array} array
46
+ * The 'table' method is used to define the table name.
47
+ * @param {string} table table name
48
+ * @return {this} this
47
49
  */
48
- makeArray(result) {
49
- switch (this._typeOf(result)) {
50
- case 'object': {
51
- if (Object.keys(result).length)
52
- return [result];
53
- return [];
54
- }
55
- case 'array': {
56
- return result;
57
- }
58
- case 'undefined':
59
- case 'null': {
60
- return [];
61
- }
62
- default: return [].concat(result);
63
- }
50
+ table(table) {
51
+ this._setState('TABLE_NAME', `\`${table}\``);
52
+ return this;
64
53
  }
65
54
  /**
66
- * Covert result to object or null
67
- * @param {any} result table name
68
- * @return {Record | null} object or null
55
+ * The 'table' method is used to define the table name.
56
+ * @param {string} table table name
57
+ * @return {DB} DB
69
58
  */
70
- makeObject(result) {
71
- switch (this._typeOf(result)) {
72
- case 'object': {
73
- if (Object.keys(result).length)
74
- return result;
75
- return null;
76
- }
77
- case 'array': {
78
- if (result[0] == null)
79
- return null;
80
- return result[0];
81
- }
82
- case 'undefined':
83
- case 'null': {
84
- return null;
59
+ static table(table) {
60
+ return new this().table(table);
61
+ }
62
+ /**
63
+ * The 'jsonObject' method is used to specify select data to JSON objects.
64
+ * @param {string} object table name
65
+ * @param {string} alias
66
+ * @return {string} string
67
+ */
68
+ jsonObject(object, alias) {
69
+ if (!Object.keys(object).length)
70
+ throw new Error("The method 'jsonObject' is not supported for empty object");
71
+ let maping = [];
72
+ for (const [key, value] of Object.entries(object)) {
73
+ if (/\./.test(value)) {
74
+ const [table, c] = value.split('.');
75
+ maping = [...maping, `'${key}'`, `\`${table}\`.\`${c}\``];
76
+ continue;
85
77
  }
86
- default: return {
87
- "0": result
88
- };
78
+ maping = [...maping, `'${key}'`, `\`${this.getTableName()}\`.\`${value}\``];
89
79
  }
80
+ return `${this.$constants('JSON_OBJECT')}(${maping.join(' , ')}) ${this.$constants('AS')} \`${alias}\``;
90
81
  }
91
82
  /**
92
- * Assign table name
93
- * @param {string} table table name
94
- * @return {this} this
83
+ * The 'jsonObject' method is used to specify select data to JSON objects.
84
+ * @static
85
+ * @param {string} object table name
86
+ * @param {string} alias
87
+ * @return {string} string
95
88
  */
96
- table(table) {
97
- this.$state.set('SELECT', `${this.$constants('SELECT')} *`);
98
- this.$state.set('TABLE_NAME', `\`${table}\``);
99
- this.$state.set('FROM', `${this.$constants('FROM')}`);
100
- return this;
89
+ static jsonObject(object, alias) {
90
+ return new this().jsonObject(object, alias);
91
+ }
92
+ /**
93
+ * The 'JSONObject' method is used to specify select data to JSON objects.
94
+ * @param {string} object table name
95
+ * @param {string} alias
96
+ * @return {string} string
97
+ */
98
+ JSONObject(object, alias) {
99
+ return this.jsonObject(object, alias);
100
+ }
101
+ /**
102
+ * The 'JSONObject' method is used to specify select data to JSON objects.
103
+ * @static
104
+ * @param {string} object table name
105
+ * @param {string} alias
106
+ * @return {string} string
107
+ */
108
+ static JSONObject(object, alias) {
109
+ return new this().jsonObject(object, alias);
101
110
  }
102
111
  /**
103
- * Get constant
104
- * @param {string} constant
112
+ * The 'constants' method is used to return constants with key or none in 'DB' or 'Model'.
113
+ * @param {string} key
114
+ * @return {string | object} string || object
115
+ */
116
+ constants(key) {
117
+ return this.$constants(key);
118
+ }
119
+ /**
120
+ * The 'constants' method is used to return constants with key or none in 'DB' or 'Model'.
121
+ * @static
122
+ * @param {string} key
105
123
  * @return {string | object} string || object
106
124
  */
107
- constants(constant) {
108
- return this.$constants(constant);
125
+ static constants(key) {
126
+ return new this().constants(key);
109
127
  }
110
128
  /**
111
129
  * cases query
@@ -136,14 +154,32 @@ class DB extends AbstractDB_1.AbstractDB {
136
154
  ].join(' ');
137
155
  }
138
156
  /**
139
- * generate UUID
157
+ * select by cases
158
+ * @static
159
+ * @param {arrayObject} cases array object {when , then }
160
+ * @param {string?} final else condition
161
+ * @return {this}
162
+ */
163
+ static caseUpdate(cases, final) {
164
+ return new this().caseUpdate(cases, final);
165
+ }
166
+ /**
167
+ * The 'generateUUID' methid is used to generate a universal unique identifier.
140
168
  * @return {string} string
141
169
  */
142
170
  generateUUID() {
143
171
  return this.$utils.generateUUID();
144
172
  }
145
173
  /**
146
- * Assign raw query for schema validation
174
+ * The 'generateUUID' methid is used to generate a universal unique identifier.
175
+ * @static
176
+ * @return {string} string
177
+ */
178
+ static generateUUID() {
179
+ return new this().generateUUID();
180
+ }
181
+ /**
182
+ * The 'raw' methid is used to allow for raw sql queries to some method in 'DB' or 'Model'.
147
183
  * @param {string} sql
148
184
  * @return {string} string
149
185
  */
@@ -151,13 +187,22 @@ class DB extends AbstractDB_1.AbstractDB {
151
187
  return `${this.$constants('RAW')} ${sql}`;
152
188
  }
153
189
  /**
154
- * Get a pool connection
190
+ * The 'raw' methid is used to allow for raw sql queries to some method in 'DB' or 'Model'.
191
+ * @static
192
+ * @param {string} sql
193
+ * @return {string} string
194
+ */
195
+ static raw(sql) {
196
+ return `${new this().raw(sql)}`;
197
+ }
198
+ /**
199
+ * The 'getConnection' method is used to get a pool connection.
155
200
  * @param {Object} options options for connection database with credentials
156
- * @param {string} option.host
157
- * @param {number} option.port
158
- * @param {string} option.database
159
- * @param {string} option.username
160
- * @param {string} option.password
201
+ * @property {string} option.host
202
+ * @property {number} option.port
203
+ * @property {string} option.database
204
+ * @property {string} option.username
205
+ * @property {string} option.password
161
206
  * @return {Connection}
162
207
  */
163
208
  getConnection(options) {
@@ -176,7 +221,27 @@ class DB extends AbstractDB_1.AbstractDB {
176
221
  });
177
222
  }
178
223
  /**
179
- * Get a connection
224
+ * The 'getConnection' method is used to get a pool connection.
225
+ * @param {Object} options options for connection database with credentials
226
+ * @property {string} option.host
227
+ * @property {number} option.port
228
+ * @property {string} option.database
229
+ * @property {string} option.username
230
+ * @property {string} option.password
231
+ * @return {Connection}
232
+ */
233
+ static getConnection(options) {
234
+ return __awaiter(this, void 0, void 0, function* () {
235
+ return new this().getConnection(options);
236
+ });
237
+ }
238
+ /**
239
+ * The 'beginTransaction' is a method used to initiate a database transaction within your application's code.
240
+ *
241
+ * A database transaction is a way to group multiple database operations (such as inserts, updates, or deletes) into a single unit of work.
242
+ *
243
+ * Transactions are typically used when you want to ensure that a series of database operations either all succeed or all fail together,
244
+ * ensuring data integrity.
180
245
  * @return {ConnectionTransaction} object - Connection for the transaction
181
246
  * @type {object} connection
182
247
  * @property {function} connection.query - execute query sql then release connection to pool
@@ -191,124 +256,12 @@ class DB extends AbstractDB_1.AbstractDB {
191
256
  });
192
257
  }
193
258
  /**
194
- * Covert result to array
195
- * @param {any} result table name
196
- * @return {Array} array
197
- */
198
- static makeArray(result) {
199
- switch (new this()._typeOf(result)) {
200
- case 'object': {
201
- if (Object.keys(result).length)
202
- return [result];
203
- return [];
204
- }
205
- case 'array': {
206
- return result;
207
- }
208
- case 'undefined':
209
- case 'null': {
210
- return [];
211
- }
212
- default: return [].concat(result);
213
- }
214
- }
215
- /**
216
- * Covert result to object | null
217
- * @param {any} result table name
218
- * @return {Record | null} object | null
219
- */
220
- static makeObject(result) {
221
- switch (new this()._typeOf(result)) {
222
- case 'object': {
223
- if (Object.keys(result).length)
224
- return result;
225
- return null;
226
- }
227
- case 'array': {
228
- if (result[0] == null)
229
- return null;
230
- return result[0];
231
- }
232
- case 'undefined':
233
- case 'null': {
234
- return null;
235
- }
236
- default: return {
237
- "0": result
238
- };
239
- }
240
- }
241
- /**
242
- * Assign table name
243
- * @static
244
- * @param {string} table table name
245
- * @return {DB} DB
246
- */
247
- static table(table) {
248
- const self = new this();
249
- self.$state.set('SELECT', `${self.$constants('SELECT')} *`);
250
- self.$state.set('TABLE_NAME', `\`${table}\``);
251
- self.$state.set('FROM', `${self.$constants('FROM')}`);
252
- return self;
253
- }
254
- /**
255
- * select by cases
256
- * @static
257
- * @param {arrayObject} cases array object {when , then }
258
- * @param {string?} final else condition
259
- * @return {this}
260
- */
261
- static caseUpdate(cases, final) {
262
- if (!cases.length)
263
- return [];
264
- const self = new this();
265
- let query = [];
266
- for (const c of cases) {
267
- if (c.when == null)
268
- throw new Error(`can't find when condition`);
269
- if (c.then == null)
270
- throw new Error(`can't find then condition`);
271
- query = [
272
- ...query,
273
- `${self.$constants('WHEN')} ${c.when} ${self.$constants('THEN')} ${c.then}`
274
- ];
275
- }
276
- return [
277
- self.$constants('RAW'),
278
- self.$constants('CASE'),
279
- query.join(' '),
280
- final == null ? '' : `ELSE ${final}`,
281
- self.$constants('END'),
282
- ].join(' ');
283
- }
284
- /**
285
- * Assign raw query for schema validation
286
- * @static
287
- * @param {string} sql
288
- * @return {string} string
289
- */
290
- static raw(sql) {
291
- return `${new this().$constants('RAW')} ${sql}`;
292
- }
293
- /**
294
- * generate UUID
295
- * @static
296
- * @return {string} string
297
- */
298
- static generateUUID() {
299
- return new this().$utils.generateUUID();
300
- }
301
- /**
302
- * Get constant
303
- * @static
304
- * @param {string} constant
305
- * @return {string | object} string || object
306
- */
307
- static constants(constant) {
308
- return new this().$constants(constant);
309
- }
310
- /**
311
- * Get a connection
259
+ * The 'beginTransaction' is a method used to initiate a database transaction within your application's code.
260
+ *
261
+ * A database transaction is a way to group multiple database operations (such as inserts, updates, or deletes) into a single unit of work.
262
+ *
263
+ * Transactions are typically used when you want to ensure that a series of database operations either all succeed or all fail together,
264
+ * ensuring data integrity.
312
265
  * @static
313
266
  * @return {ConnectionTransaction} object - Connection for the transaction
314
267
  * @type {object} connection
@@ -319,40 +272,9 @@ class DB extends AbstractDB_1.AbstractDB {
319
272
  */
320
273
  static beginTransaction() {
321
274
  return __awaiter(this, void 0, void 0, function* () {
322
- const self = new this();
323
- const pool = yield self.$pool.get();
324
- return yield pool.connection();
325
- });
326
- }
327
- /**
328
- * Get a pool connection
329
- * @param {Object} options options for connection database with credentials
330
- * @param {string} option.host
331
- * @param {number} option.port
332
- * @param {string} option.database
333
- * @param {string} option.username
334
- * @param {string} option.password
335
- * @return {Connection}
336
- */
337
- static getConnection(options) {
338
- return __awaiter(this, void 0, void 0, function* () {
339
- if (options == null) {
340
- const self = new this();
341
- const pool = yield self.$pool.get();
342
- return yield pool.connection();
343
- }
344
- const { host, port, database, username: user, password } = options, others = __rest(options, ["host", "port", "database", "username", "password"]);
345
- const pool = new connection_1.PoolConnection(Object.assign({ host,
346
- port,
347
- database,
348
- user,
349
- password }, others));
350
- return pool.connection();
275
+ return yield new this().beginTransaction();
351
276
  });
352
277
  }
353
- _typeOf(data) {
354
- return Object.prototype.toString.apply(data).slice(8, -1).toLocaleLowerCase();
355
- }
356
278
  _initialDB() {
357
279
  this.$state = new StateHandler_1.default(this.$constants('DB'));
358
280
  return this;