leoric 2.6.3 → 2.7.0

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