leoric 2.6.2 → 2.7.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.
package/src/data_types.js CHANGED
@@ -1,9 +1,17 @@
1
1
  'use strict';
2
-
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.invokable = exports.DataType = exports.LENGTH_VARIANTS = void 0;
3
4
  const util = require('util');
4
- const invokable = require('./utils/invokable');
5
+ const invokableFunc = require('./utils/invokable');
5
6
  const Raw = require('./raw');
6
-
7
+ var LENGTH_VARIANTS;
8
+ (function (LENGTH_VARIANTS) {
9
+ LENGTH_VARIANTS["tiny"] = "tiny";
10
+ LENGTH_VARIANTS["empty"] = "";
11
+ LENGTH_VARIANTS["medium"] = "medium";
12
+ LENGTH_VARIANTS["long"] = "long";
13
+ })(LENGTH_VARIANTS = exports.LENGTH_VARIANTS || (exports.LENGTH_VARIANTS = {}));
14
+ ;
7
15
  /**
8
16
  * @example
9
17
  * const { STRING, INTEGER, BIGINT, DATE, BOOLEAN } = app.model;
@@ -11,100 +19,24 @@ const Raw = require('./raw');
11
19
  * login: STRING,
12
20
  * });
13
21
  */
14
-
15
22
  class DataType {
16
- static findType(columnType) {
17
- const {
18
- STRING, TEXT,
19
- DATE, DATEONLY,
20
- TINYINT, SMALLINT, MEDIUMINT, INTEGER, BIGINT, DECIMAL,
21
- BOOLEAN,
22
- BINARY, VARBINARY, BLOB,
23
- } = this;
24
- const [ , dataType, ...matches ] = columnType.match(/(\w+)(?:\((\d+)(?:,(\d+))?\))?/);
25
- const params = [];
26
- for (let i = 0; i < matches.length; i++) {
27
- if (matches[i] != null) params[i] = parseInt(matches[i], 10);
28
- }
29
-
30
- switch (dataType) {
31
- case 'varchar':
32
- case 'char':
33
- return new STRING(...params);
34
- // longtext is only for MySQL
35
- case 'longtext':
36
- return new TEXT('long');
37
- case 'mediumtext':
38
- return new TEXT('medium');
39
- case 'text':
40
- return new TEXT();
41
- case 'date':
42
- return new DATEONLY();
43
- case 'datetime':
44
- case 'timestamp':
45
- // new DATE(precision)
46
- return new DATE(...params);
47
- case 'decimal':
48
- return new DECIMAL(...params);
49
- case 'int':
50
- case 'integer':
51
- case 'numeric':
52
- return new INTEGER(...params);
53
- case 'mediumint':
54
- return new MEDIUMINT(...params);
55
- case 'smallint':
56
- return new SMALLINT(...params);
57
- case 'tinyint':
58
- return new TINYINT(...params);
59
- case 'bigint':
60
- return new BIGINT(...params);
61
- case 'boolean':
62
- return new BOOLEAN();
63
- // mysql only
64
- case 'binary':
65
- // postgres only
66
- case 'bytea':
67
- return new BINARY(...params);
68
- // mysql only
69
- case 'varbinary':
70
- return new VARBINARY(...params);
71
- case 'longblob':
72
- return new BLOB('long');
73
- case 'mediumblob':
74
- return new BLOB('medium');
75
- case 'blob':
76
- return new BLOB();
77
- case 'tinyblob':
78
- return new BLOB('tiny');
79
- default:
80
- throw new Error(`Unexpected data type ${dataType}`);
81
- }
82
- }
83
-
84
- /**
85
- * Check if params is instance of DataType or not
86
- * @param {*} params
87
- * @returns {boolean}
88
- */
89
- static is(params) {
90
- return params instanceof DataType;
91
- }
92
-
93
- /**
94
- * cast raw data returned from data packet into js type
95
- */
96
- cast(value) {
97
- return value;
98
- }
99
-
100
- /**
101
- * uncast js value into database type with precision
102
- */
103
- uncast(value) {
104
- return value;
105
- }
23
+ constructor() {
24
+ this.dataType = '';
25
+ }
26
+ /**
27
+ * cast raw data returned from data packet into js type
28
+ */
29
+ cast(value) {
30
+ return value;
31
+ }
32
+ /**
33
+ * uncast js value into database type with precision
34
+ */
35
+ uncast(value, _strict) {
36
+ return value;
37
+ }
106
38
  }
107
-
39
+ exports.DataType = DataType;
108
40
  /**
109
41
  * @example
110
42
  * STRING
@@ -113,55 +45,51 @@ class DataType {
113
45
  * @param {number} dataLength
114
46
  */
115
47
  class STRING extends DataType {
116
- constructor(dataLength = 255) {
117
- super();
118
- this.dataType = 'varchar';
119
- this.dataLength = dataLength;
120
- }
121
-
122
- toSqlString() {
123
- const { dataLength } = this;
124
- const dataType = this.dataType.toUpperCase();
125
- const chunks = [];
126
- chunks.push(dataLength > 0 ? `${dataType}(${dataLength})` : dataType);
127
- return chunks.join(' ');
128
- }
129
-
130
- uncast(value) {
131
- if (value == null || value instanceof Raw) return value;
132
- return '' + value;
133
- }
48
+ constructor(dataLength = 255) {
49
+ super();
50
+ this.dataType = 'varchar';
51
+ this.dataLength = dataLength;
52
+ }
53
+ toSqlString() {
54
+ const { dataLength } = this;
55
+ const dataType = this.dataType.toUpperCase();
56
+ const chunks = [];
57
+ chunks.push(dataLength && dataLength > 0 ? `${dataType}(${dataLength})` : dataType);
58
+ return chunks.join(' ');
59
+ }
60
+ uncast(value) {
61
+ if (value == null || value instanceof Raw)
62
+ return value;
63
+ return '' + value;
64
+ }
134
65
  }
135
-
136
66
  class BINARY extends DataType {
137
- constructor(dataLength = 255) {
138
- super();
139
- this.dataLength = dataLength;
140
- this.dataType = 'binary';
141
- }
142
-
143
- toSqlString() {
144
- const { dataLength } = this;
145
- const dataType = this.dataType.toUpperCase();
146
- const chunks = [];
147
- chunks.push(dataLength > 0 ? `${dataType}(${dataLength})` : dataType);
148
- return chunks.join(' ');
149
- }
150
-
151
- cast(value) {
152
- if (value == null) return value;
153
- if (Buffer.isBuffer(value)) return value;
154
- return Buffer.from(value);
155
- }
67
+ constructor(dataLength = 255) {
68
+ super();
69
+ this.dataLength = dataLength;
70
+ this.dataType = 'binary';
71
+ }
72
+ toSqlString() {
73
+ const { dataLength } = this;
74
+ const dataType = this.dataType.toUpperCase();
75
+ const chunks = [];
76
+ chunks.push(dataLength && dataLength > 0 ? `${dataType}(${dataLength})` : dataType);
77
+ return chunks.join(' ');
78
+ }
79
+ cast(value) {
80
+ if (value == null)
81
+ return value;
82
+ if (Buffer.isBuffer(value))
83
+ return value;
84
+ return Buffer.from(value);
85
+ }
156
86
  }
157
-
158
87
  class VARBINARY extends BINARY {
159
- constructor(dataLength) {
160
- super(dataLength);
161
- this.dataType = 'varbinary';
162
- }
88
+ constructor(dataLength) {
89
+ super(dataLength);
90
+ this.dataType = 'varbinary';
91
+ }
163
92
  }
164
-
165
93
  /**
166
94
  * ZEROFILL is deprecated
167
95
  * - https://dev.mysql.com/doc/refman/8.0/en/integer-types.html
@@ -173,49 +101,49 @@ class VARBINARY extends BINARY {
173
101
  * @param {number} dataLength
174
102
  */
175
103
  class INTEGER extends DataType {
176
- constructor(dataLength) {
177
- super();
178
- this.dataLength = dataLength;
179
- this.dataType = 'integer';
180
- }
181
-
182
- get UNSIGNED() {
183
- this.unsigned = true;
184
- return this;
185
- }
186
-
187
- get ZEROFILL() {
188
- this.zerofill = true;
189
- return this;
190
- }
191
-
192
- toSqlString() {
193
- const { dataLength, unsigned, zerofill } = this;
194
- const dataType = this.dataType.toUpperCase();
195
- const chunks = [];
196
- chunks.push(dataLength > 0 ? `${dataType}(${dataLength})` : dataType);
197
- if (unsigned) chunks.push('UNSIGNED');
198
- if (zerofill) chunks.push('ZEROFILL');
199
- return chunks.join(' ');
200
- }
201
-
202
- cast(value) {
203
- if (value == null || isNaN(value)) return value;
204
- return Number(value);
205
- }
206
-
207
- uncast(value, strict = true) {
208
- const originValue = value;
209
- if (value == null || value instanceof Raw) return value;
210
- if (typeof value === 'string') value = parseInt(value, 10);
211
- if (isNaN(value)) {
212
- if (strict) throw new Error(util.format('invalid integer: %s', originValue));
213
- return originValue;
214
- }
215
- return value;
216
- }
104
+ constructor(dataLength) {
105
+ super();
106
+ this.dataLength = dataLength;
107
+ this.dataType = 'integer';
108
+ }
109
+ get UNSIGNED() {
110
+ this.unsigned = true;
111
+ return this;
112
+ }
113
+ get ZEROFILL() {
114
+ this.zerofill = true;
115
+ return this;
116
+ }
117
+ toSqlString() {
118
+ const { dataLength, unsigned, zerofill } = this;
119
+ const dataType = this.dataType.toUpperCase();
120
+ const chunks = [];
121
+ chunks.push(dataLength && dataLength > 0 ? `${dataType}(${dataLength})` : dataType);
122
+ if (unsigned)
123
+ chunks.push('UNSIGNED');
124
+ if (zerofill)
125
+ chunks.push('ZEROFILL');
126
+ return chunks.join(' ');
127
+ }
128
+ cast(value) {
129
+ if (value == null || isNaN(value))
130
+ return value;
131
+ return Number(value);
132
+ }
133
+ uncast(value, strict = true) {
134
+ const originValue = value;
135
+ if (value == null || value instanceof Raw)
136
+ return value;
137
+ if (typeof value === 'string')
138
+ value = parseInt(value, 10);
139
+ if (isNaN(value)) {
140
+ if (strict)
141
+ throw new Error(util.format('invalid integer: %s', originValue));
142
+ return originValue;
143
+ }
144
+ return value;
145
+ }
217
146
  }
218
-
219
147
  /**
220
148
  * 8 bit integer
221
149
  * @example
@@ -225,12 +153,11 @@ class INTEGER extends DataType {
225
153
  * @param {number} dataLength
226
154
  */
227
155
  class TINYINT extends INTEGER {
228
- constructor(dataLength) {
229
- super(dataLength);
230
- this.dataType = 'tinyint';
231
- }
156
+ constructor(dataLength) {
157
+ super(dataLength);
158
+ this.dataType = 'tinyint';
159
+ }
232
160
  }
233
-
234
161
  /**
235
162
  * 16 bit integer
236
163
  * @example
@@ -240,12 +167,11 @@ class TINYINT extends INTEGER {
240
167
  * @param {number} dataLength
241
168
  */
242
169
  class SMALLINT extends INTEGER {
243
- constructor(dataLength) {
244
- super(dataLength);
245
- this.dataType = 'smallint';
246
- }
170
+ constructor(dataLength) {
171
+ super(dataLength);
172
+ this.dataType = 'smallint';
173
+ }
247
174
  }
248
-
249
175
  /**
250
176
  * 24 bit integer
251
177
  * @example
@@ -255,13 +181,11 @@ class SMALLINT extends INTEGER {
255
181
  * @param {number} dataLength
256
182
  */
257
183
  class MEDIUMINT extends INTEGER {
258
- constructor(dataLength) {
259
- super(dataLength);
260
- this.dataType = 'mediumint';
261
- }
184
+ constructor(dataLength) {
185
+ super(dataLength);
186
+ this.dataType = 'mediumint';
187
+ }
262
188
  }
263
-
264
-
265
189
  /**
266
190
  * 64 bit integer
267
191
  * @example
@@ -271,12 +195,11 @@ class MEDIUMINT extends INTEGER {
271
195
  * @param {number} dataLength
272
196
  */
273
197
  class BIGINT extends INTEGER {
274
- constructor(dataLength) {
275
- super(dataLength);
276
- this.dataType = 'bigint';
277
- }
198
+ constructor(dataLength) {
199
+ super(dataLength);
200
+ this.dataType = 'bigint';
201
+ }
278
202
  }
279
-
280
203
  /**
281
204
  * fixed-point decimal types
282
205
  * @example
@@ -288,250 +211,337 @@ class BIGINT extends INTEGER {
288
211
  * - https://dev.mysql.com/doc/refman/8.0/en/fixed-point-types.html
289
212
  */
290
213
  class DECIMAL extends INTEGER {
291
- constructor(precision, scale) {
292
- super();
293
- this.dataType = 'decimal';
294
- this.precision = precision;
295
- this.scale = scale;
296
- }
297
-
298
- toSqlString() {
299
- const { precision, scale, unsigned, zerofill } = this;
300
- const dataType = this.dataType.toUpperCase();
301
- const chunks = [];
302
- if (precision > 0 && scale >= 0) {
303
- chunks.push(`${dataType}(${precision},${scale})`);
304
- } else if (precision > 0) {
305
- chunks.push(`${dataType}(${precision})`);
306
- } else {
307
- chunks.push(dataType);
308
- }
309
- if (unsigned) chunks.push('UNSIGNED');
310
- if (zerofill) chunks.push('ZEROFILL');
311
- return chunks.join(' ');
312
- }
214
+ constructor(precision, scale) {
215
+ super();
216
+ this.dataType = 'decimal';
217
+ this.precision = precision;
218
+ this.scale = scale;
219
+ }
220
+ toSqlString() {
221
+ const { precision, scale, unsigned, zerofill } = this;
222
+ const dataType = this.dataType.toUpperCase();
223
+ const chunks = [];
224
+ if (precision && precision > 0 && scale != null && scale >= 0) {
225
+ chunks.push(`${dataType}(${precision},${scale})`);
226
+ }
227
+ else if (precision && precision > 0) {
228
+ chunks.push(`${dataType}(${precision})`);
229
+ }
230
+ else {
231
+ chunks.push(dataType);
232
+ }
233
+ if (unsigned)
234
+ chunks.push('UNSIGNED');
235
+ if (zerofill)
236
+ chunks.push('ZEROFILL');
237
+ return chunks.join(' ');
238
+ }
313
239
  }
314
-
315
240
  const rDateFormat = /^\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}(?:[,.]\d{3,6}){0,1}$/;
316
241
  class DATE extends DataType {
317
- constructor(precision, timezone = true) {
318
- super();
319
- this.dataType = 'datetime';
320
- this.precision = precision;
321
- // PostgreSQL enables timestamp with or without time zone
322
- // - https://www.postgresql.org/docs/9.5/datatype-datetime.html
323
- this.timezone = timezone;
324
- }
325
-
326
- toSqlString() {
327
- const { precision } = this;
328
- const dataType = this.dataType.toUpperCase();
329
- if (precision != null && precision >= 0) return `${dataType}(${precision})`;
330
- return dataType;
331
- }
332
-
333
- _round(value) {
334
- const { precision } = this;
335
- if (precision != null && precision < 3 && value instanceof Date) {
336
- const divider = 10 ** (3 - precision);
337
- return new Date(Math.round(value.getTime() / divider) * divider);
338
- }
339
- return value;
340
- }
341
-
342
- cast(value) {
343
- const original = value;
344
- if (value == null) return value;
345
- if (!(value instanceof Date)) value = new Date(value);
346
- if (isNaN(value.getTime())) return original;
347
- return this._round(value);
348
- }
349
-
350
- uncast(value) {
351
- const originValue = value;
352
-
353
- if (value == null || value instanceof Raw) return value;
354
- if (typeof value.toDate === 'function') {
355
- value = value.toDate();
356
- }
357
-
358
- // @deprecated
359
- // vaguely standard date formats such as 2021-10-15 15:50:02,548
360
- if (typeof value === 'string' && rDateFormat.test(value)) {
361
- // 2021-10-15 15:50:02,548 => 2021-10-15T15:50:02,548,
362
- // 2021-10-15 15:50:02 => 2021-10-15T15:50:02.000
363
- value = new Date(`${value.replace(' ', 'T').replace(',', '.')}`);
364
- }
365
-
366
- // 1634611135776
367
- // '2021-10-15T08:38:43.877Z'
368
- if (!(value instanceof Date)) value = new Date(value);
369
- if (isNaN(value)) throw new Error(util.format('invalid date: %s', originValue));
370
-
371
- return this._round(value);
372
- }
242
+ constructor(precision, timezone = true) {
243
+ super();
244
+ this.timezone = true;
245
+ this.dataType = 'datetime';
246
+ this.precision = precision;
247
+ // PostgreSQL enables timestamp with or without time zone
248
+ // - https://www.postgresql.org/docs/9.5/datatype-datetime.html
249
+ this.timezone = timezone;
250
+ }
251
+ toSqlString() {
252
+ const { precision } = this;
253
+ const dataType = this.dataType.toUpperCase();
254
+ if (precision != null && precision >= 0)
255
+ return `${dataType}(${precision})`;
256
+ return dataType;
257
+ }
258
+ _round(value) {
259
+ const { precision } = this;
260
+ if (precision != null && precision < 3 && value instanceof Date) {
261
+ const divider = 10 ** (3 - precision);
262
+ return new Date(Math.round(value.getTime() / divider) * divider);
263
+ }
264
+ return value;
265
+ }
266
+ cast(value) {
267
+ const original = value;
268
+ if (value == null)
269
+ return value;
270
+ if (!(value instanceof Date))
271
+ value = new Date(value);
272
+ if (isNaN(value.getTime()))
273
+ return original;
274
+ return this._round(value);
275
+ }
276
+ uncast(value, _strict) {
277
+ const originValue = value;
278
+ if (value == null || value instanceof Raw)
279
+ return value;
280
+ if (typeof value.toDate === 'function') {
281
+ value = value.toDate();
282
+ }
283
+ // @deprecated
284
+ // vaguely standard date formats such as 2021-10-15 15:50:02,548
285
+ if (typeof value === 'string' && rDateFormat.test(value)) {
286
+ // 2021-10-15 15:50:02,548 => 2021-10-15T15:50:02,548,
287
+ // 2021-10-15 15:50:02 => 2021-10-15T15:50:02.000
288
+ value = new Date(`${value.replace(' ', 'T').replace(',', '.')}`);
289
+ }
290
+ // 1634611135776
291
+ // '2021-10-15T08:38:43.877Z'
292
+ if (!(value instanceof Date))
293
+ value = new Date(value);
294
+ if (isNaN(value))
295
+ throw new Error(util.format('invalid date: %s', originValue));
296
+ return this._round(value);
297
+ }
373
298
  }
374
-
375
299
  class DATEONLY extends DATE {
376
- constructor() {
377
- super();
378
- this.dataType = 'date';
379
- this.precision = null;
380
- this.timezone = false;
381
- }
382
-
383
- toSqlString() {
384
- return this.dataType.toUpperCase();
385
- }
386
-
387
- _round(value) {
388
- if (value instanceof Date) {
389
- return new Date(value.getFullYear(), value.getMonth(), value.getDate());
390
- }
391
- return value;
392
- }
300
+ constructor() {
301
+ super();
302
+ this.dataType = 'date';
303
+ this.precision = null;
304
+ this.timezone = false;
305
+ }
306
+ toSqlString() {
307
+ return this.dataType.toUpperCase();
308
+ }
309
+ _round(value) {
310
+ if (value instanceof Date) {
311
+ return new Date(value.getFullYear(), value.getMonth(), value.getDate());
312
+ }
313
+ return value;
314
+ }
393
315
  }
394
-
395
316
  class BOOLEAN extends DataType {
396
- constructor() {
397
- super();
398
- this.dataType = 'boolean';
399
- }
400
-
401
- toSqlString() {
402
- return this.dataType.toUpperCase();
403
- }
404
-
405
- cast(value) {
406
- if (value == null) return value;
407
- return Boolean(value);
408
- }
317
+ constructor() {
318
+ super();
319
+ this.dataType = 'boolean';
320
+ }
321
+ toSqlString() {
322
+ return this.dataType.toUpperCase();
323
+ }
324
+ cast(value) {
325
+ if (value == null)
326
+ return value;
327
+ return Boolean(value);
328
+ }
409
329
  }
410
-
411
- const LENGTH_VARIANTS = [ 'tiny', '', 'medium', 'long' ];
412
-
413
330
  class TEXT extends DataType {
414
- constructor(length = '') {
415
- if (!LENGTH_VARIANTS.includes(length)) {
416
- throw new Error(`invalid text length: ${length}`);
417
- }
418
- super();
419
- this.dataType = 'text';
420
- this.dataLength = length;
421
- }
422
-
423
- toSqlString() {
424
- return [ this.dataLength, this.dataType ].join('').toUpperCase();
425
- }
331
+ constructor(length = LENGTH_VARIANTS.empty) {
332
+ if (!Object.values(LENGTH_VARIANTS).includes(length)) {
333
+ throw new Error(`invalid text length: ${length}`);
334
+ }
335
+ super();
336
+ this.dataType = 'text';
337
+ this.dataLength = length;
338
+ }
339
+ toSqlString() {
340
+ return [this.dataLength, this.dataType].join('').toUpperCase();
341
+ }
426
342
  }
427
-
428
343
  class BLOB extends DataType {
429
- constructor(length = '') {
430
- if (!LENGTH_VARIANTS.includes(length)) {
431
- throw new Error(`invalid blob length: ${length}`);
432
- }
433
- super();
434
- this.dataType = 'blob';
435
- this.dataLength = length;
436
- }
437
-
438
- toSqlString() {
439
- return [ this.dataLength, this.dataType ].join('').toUpperCase();
440
- }
441
-
442
- cast(value) {
443
- if (value == null) return value;
444
- if (Buffer.isBuffer(value)) return value;
445
- return Buffer.from(value);
446
- }
344
+ constructor(length = LENGTH_VARIANTS.empty) {
345
+ if (!Object.values(LENGTH_VARIANTS).includes(length)) {
346
+ throw new Error(`invalid blob length: ${length}`);
347
+ }
348
+ super();
349
+ this.dataType = 'blob';
350
+ this.dataLength = length;
351
+ }
352
+ toSqlString() {
353
+ return [this.dataLength, this.dataType].join('').toUpperCase();
354
+ }
355
+ cast(value) {
356
+ if (value == null)
357
+ return value;
358
+ if (Buffer.isBuffer(value))
359
+ return value;
360
+ return Buffer.from(value);
361
+ }
447
362
  }
448
-
449
363
  // JSON text type
450
- class JSON extends DataType {
451
- constructor() {
452
- super();
453
- this.dataType = 'text';
454
- }
455
-
456
- toSqlString() {
457
- return 'TEXT';
458
- }
459
-
460
- cast(value) {
461
- if (!value) return value;
462
- // type === JSONB
463
- if (typeof value === 'object') return value;
464
- try {
465
- return global.JSON.parse(value);
466
- } catch (err) {
467
- console.error(new Error(`unable to cast ${value} to JSON`));
468
- return value;
469
- }
470
- }
471
-
472
- uncast(value) {
473
- if (value == null || value instanceof Raw) return value;
474
- return global.JSON.stringify(value);
475
- }
364
+ class MYJSON extends DataType {
365
+ constructor() {
366
+ super();
367
+ this.dataType = 'text';
368
+ }
369
+ toSqlString() {
370
+ return 'TEXT';
371
+ }
372
+ static toSqlString() {
373
+ return 'TEXT';
374
+ }
375
+ cast(value) {
376
+ if (!value)
377
+ return value;
378
+ // type === JSONB
379
+ if (typeof value === 'object')
380
+ return value;
381
+ try {
382
+ return global.JSON.parse(value);
383
+ }
384
+ catch (err) {
385
+ console.error(new Error(`unable to cast ${value} to JSON`));
386
+ return value;
387
+ }
388
+ }
389
+ uncast(value) {
390
+ if (value == null || value instanceof Raw)
391
+ return value;
392
+ return global.JSON.stringify(value);
393
+ }
476
394
  }
477
-
478
395
  // JSON binary type, available in postgreSQL or mySQL 5.7 +
479
396
  // - https://dev.mysql.com/doc/refman/8.0/en/json.html
480
397
  // - https://www.postgresql.org/docs/9.4/datatype-json.html
481
- class JSONB extends JSON {
482
- constructor() {
483
- super();
484
- this.dataType = 'json';
485
- }
486
-
487
- toSqlString() {
488
- return 'JSON';
489
- }
398
+ class JSONB extends MYJSON {
399
+ constructor() {
400
+ super();
401
+ this.dataType = 'json';
402
+ }
403
+ toSqlString() {
404
+ return 'JSON';
405
+ }
406
+ static toSqlString() {
407
+ return 'JSON';
408
+ }
490
409
  }
491
-
492
410
  class VIRTUAL extends DataType {
493
- constructor() {
494
- super();
495
- this.dataType = 'virtual';
496
- this.virtual = true;
497
- }
498
-
499
- toSqlString() {
500
- return 'VIRTUAL';
501
- }
411
+ constructor() {
412
+ super();
413
+ this.virtual = true;
414
+ this.dataType = 'virtual';
415
+ this.virtual = true;
416
+ }
417
+ toSqlString() {
418
+ return 'VIRTUAL';
419
+ }
420
+ static toSqlString() {
421
+ return 'VIRTUAL';
422
+ }
502
423
  }
503
-
504
- const DataTypes = {
505
- STRING,
506
- TINYINT,
507
- SMALLINT,
508
- MEDIUMINT,
509
- INTEGER,
510
- BIGINT,
511
- DECIMAL,
512
- DATE,
513
- DATEONLY,
514
- BOOLEAN,
515
- TEXT,
516
- BLOB,
517
- JSON,
518
- JSONB,
519
- BINARY,
520
- VARBINARY,
521
- VIRTUAL,
424
+ const AllDataTypes = {
425
+ STRING,
426
+ TINYINT,
427
+ SMALLINT,
428
+ MEDIUMINT,
429
+ INTEGER,
430
+ BIGINT,
431
+ DECIMAL,
432
+ DATE,
433
+ DATEONLY,
434
+ BOOLEAN,
435
+ TEXT,
436
+ BLOB,
437
+ JSON: MYJSON,
438
+ JSONB,
439
+ BINARY,
440
+ VARBINARY,
441
+ VIRTUAL,
522
442
  };
523
-
524
- Object.assign(DataType, DataTypes);
525
- Object.defineProperty(DataType, 'invokable', {
526
- get() {
527
- return new Proxy(this, {
528
- get(target, p) {
529
- const value = target[p];
530
- if (DataTypes.hasOwnProperty(p)) return invokable(value);
531
- return value;
532
- }
533
- });
534
- },
535
- });
536
-
537
- module.exports = DataType;
443
+ class DataTypes {
444
+ static findType(columnType) {
445
+ const { STRING, TEXT, DATE, DATEONLY, TINYINT, SMALLINT, MEDIUMINT, INTEGER, BIGINT, DECIMAL, BOOLEAN, BINARY, VARBINARY, BLOB, } = this;
446
+ const res = columnType === null || columnType === void 0 ? void 0 : columnType.match(/(\w+)(?:\((\d+)(?:,(\d+))?\))?/);
447
+ if (!res) {
448
+ throw new Error(`Unknown columnType ${columnType}`);
449
+ }
450
+ const [, dataType, ...matches] = res;
451
+ const params = [];
452
+ for (let i = 0; i < matches.length; i++) {
453
+ if (matches[i] != null)
454
+ params[i] = parseInt(matches[i], 10);
455
+ }
456
+ switch (dataType) {
457
+ case 'varchar':
458
+ case 'char':
459
+ return new STRING(...params);
460
+ // longtext is only for MySQL
461
+ case 'longtext':
462
+ return new TEXT(LENGTH_VARIANTS.long);
463
+ case 'mediumtext':
464
+ return new TEXT(LENGTH_VARIANTS.medium);
465
+ case 'text':
466
+ return new TEXT();
467
+ case 'date':
468
+ return new DATEONLY();
469
+ case 'datetime':
470
+ case 'timestamp':
471
+ // new DATE(precision)
472
+ return new DATE(...params);
473
+ case 'decimal':
474
+ return new DECIMAL(...params);
475
+ case 'int':
476
+ case 'integer':
477
+ case 'numeric':
478
+ return new INTEGER(...params);
479
+ case 'mediumint':
480
+ return new MEDIUMINT(...params);
481
+ case 'smallint':
482
+ return new SMALLINT(...params);
483
+ case 'tinyint':
484
+ return new TINYINT(...params);
485
+ case 'bigint':
486
+ return new BIGINT(...params);
487
+ case 'boolean':
488
+ return new BOOLEAN();
489
+ // mysql only
490
+ case 'binary':
491
+ // postgres only
492
+ case 'bytea':
493
+ return new BINARY(...params);
494
+ // mysql only
495
+ case 'varbinary':
496
+ return new VARBINARY(...params);
497
+ case 'longblob':
498
+ return new BLOB(LENGTH_VARIANTS.long);
499
+ case 'mediumblob':
500
+ return new BLOB(LENGTH_VARIANTS.medium);
501
+ case 'blob':
502
+ return new BLOB();
503
+ case 'tinyblob':
504
+ return new BLOB(LENGTH_VARIANTS.tiny);
505
+ default:
506
+ throw new Error(`Unexpected data type ${dataType}`);
507
+ }
508
+ }
509
+ static get invokable() {
510
+ return new Proxy(this, {
511
+ get(target, p) {
512
+ const value = target[p];
513
+ if (AllDataTypes.hasOwnProperty(p))
514
+ return invokableFunc(value);
515
+ return value;
516
+ }
517
+ });
518
+ }
519
+ /**
520
+ * Check if params is instance of DataType or not
521
+ * @param {*} params
522
+ * @returns {boolean}
523
+ */
524
+ static is(params) {
525
+ return params instanceof DataType;
526
+ }
527
+ }
528
+ DataTypes.STRING = STRING;
529
+ DataTypes.TINYINT = TINYINT;
530
+ DataTypes.SMALLINT = SMALLINT;
531
+ DataTypes.MEDIUMINT = MEDIUMINT;
532
+ DataTypes.INTEGER = INTEGER;
533
+ DataTypes.BIGINT = BIGINT;
534
+ DataTypes.DECIMAL = DECIMAL;
535
+ DataTypes.DATE = DATE;
536
+ DataTypes.TEXT = TEXT;
537
+ DataTypes.BLOB = BLOB;
538
+ DataTypes.JSON = MYJSON;
539
+ DataTypes.JSONB = JSONB;
540
+ DataTypes.BINARY = BINARY;
541
+ DataTypes.VARBINARY = VARBINARY;
542
+ DataTypes.VIRTUAL = VIRTUAL;
543
+ DataTypes.DATEONLY = DATEONLY;
544
+ DataTypes.BOOLEAN = BOOLEAN;
545
+ exports.invokable = DataTypes.invokable;
546
+ exports.default = DataTypes;
547
+ //# sourceMappingURL=data_types.js.map