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/History.md +783 -0
- package/index.js +4 -1
- package/package.json +2 -1
- package/src/adapters/sequelize.js +6 -6
- package/src/bone.js +9 -7
- package/src/collection.js +0 -8
- package/src/constants.js +3 -0
- package/src/data_types.js +438 -428
- package/src/data_types.js.map +1 -0
- package/src/data_types.ts +600 -0
- package/src/decorators.js +29 -5
- package/src/decorators.js.map +1 -1
- package/src/decorators.ts +20 -9
- package/src/drivers/abstract/index.js +8 -8
- package/src/drivers/abstract/spellbook.js +10 -8
- package/src/drivers/mysql/data_types.js +1 -1
- package/src/drivers/mysql/index.js +4 -2
- package/src/drivers/postgres/data_types.js +2 -2
- package/src/drivers/postgres/index.js +6 -2
- package/src/drivers/postgres/type_parser.js +7 -0
- package/src/drivers/sqlite/data_types.js +2 -2
- package/src/realm.js +11 -7
- package/src/spell.js +2 -2
- package/src/utils/index.js +8 -0
- package/types/index.d.ts +16 -16
- package/types/data_types.d.ts +0 -105
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
|
|
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
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
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
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
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
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
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
|
-
|
|
160
|
-
|
|
161
|
-
|
|
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
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
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
|
-
|
|
229
|
-
|
|
230
|
-
|
|
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
|
-
|
|
244
|
-
|
|
245
|
-
|
|
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
|
-
|
|
259
|
-
|
|
260
|
-
|
|
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
|
-
|
|
275
|
-
|
|
276
|
-
|
|
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
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
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
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
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
|
-
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
|
|
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
|
-
|
|
397
|
-
|
|
398
|
-
|
|
399
|
-
|
|
400
|
-
|
|
401
|
-
|
|
402
|
-
|
|
403
|
-
|
|
404
|
-
|
|
405
|
-
|
|
406
|
-
|
|
407
|
-
|
|
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
|
-
|
|
415
|
-
|
|
416
|
-
|
|
417
|
-
|
|
418
|
-
|
|
419
|
-
|
|
420
|
-
|
|
421
|
-
|
|
422
|
-
|
|
423
|
-
|
|
424
|
-
|
|
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
|
-
|
|
430
|
-
|
|
431
|
-
|
|
432
|
-
|
|
433
|
-
|
|
434
|
-
|
|
435
|
-
|
|
436
|
-
|
|
437
|
-
|
|
438
|
-
|
|
439
|
-
|
|
440
|
-
|
|
441
|
-
|
|
442
|
-
|
|
443
|
-
|
|
444
|
-
|
|
445
|
-
|
|
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
|
|
451
|
-
|
|
452
|
-
|
|
453
|
-
|
|
454
|
-
|
|
455
|
-
|
|
456
|
-
|
|
457
|
-
|
|
458
|
-
|
|
459
|
-
|
|
460
|
-
|
|
461
|
-
|
|
462
|
-
|
|
463
|
-
|
|
464
|
-
|
|
465
|
-
|
|
466
|
-
|
|
467
|
-
|
|
468
|
-
|
|
469
|
-
|
|
470
|
-
|
|
471
|
-
|
|
472
|
-
|
|
473
|
-
|
|
474
|
-
|
|
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
|
|
482
|
-
|
|
483
|
-
|
|
484
|
-
|
|
485
|
-
|
|
486
|
-
|
|
487
|
-
|
|
488
|
-
|
|
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
|
-
|
|
494
|
-
|
|
495
|
-
|
|
496
|
-
|
|
497
|
-
|
|
498
|
-
|
|
499
|
-
|
|
500
|
-
|
|
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
|
-
|
|
505
|
-
|
|
506
|
-
|
|
507
|
-
|
|
508
|
-
|
|
509
|
-
|
|
510
|
-
|
|
511
|
-
|
|
512
|
-
|
|
513
|
-
|
|
514
|
-
|
|
515
|
-
|
|
516
|
-
|
|
517
|
-
|
|
518
|
-
|
|
519
|
-
|
|
520
|
-
|
|
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
|
-
|
|
525
|
-
|
|
526
|
-
|
|
527
|
-
|
|
528
|
-
|
|
529
|
-
|
|
530
|
-
|
|
531
|
-
|
|
532
|
-
|
|
533
|
-
|
|
534
|
-
|
|
535
|
-
}
|
|
536
|
-
|
|
537
|
-
|
|
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
|