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/History.md +11 -0
- package/index.js +2 -1
- package/package.json +2 -1
- package/src/adapters/sequelize.js +6 -6
- package/src/bone.js +6 -7
- package/src/data_types.js +442 -428
- package/src/data_types.js.map +1 -0
- package/src/data_types.ts +608 -0
- package/src/decorators.js +29 -5
- package/src/decorators.js.map +1 -1
- package/src/decorators.ts +16 -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 +2 -2
- package/src/drivers/postgres/data_types.js +1 -1
- package/src/drivers/sqlite/data_types.js +1 -1
- package/src/realm.js +9 -6
- package/types/index.d.ts +12 -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.DataTypes = 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,42 @@ 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
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
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
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
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
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
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
|
-
|
|
160
|
-
|
|
161
|
-
|
|
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
|
-
|
|
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
|
-
|
|
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
|
-
|
|
229
|
-
|
|
230
|
-
|
|
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
|
-
|
|
244
|
-
|
|
245
|
-
|
|
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
|
-
|
|
259
|
-
|
|
260
|
-
|
|
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
|
-
|
|
275
|
-
|
|
276
|
-
|
|
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
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
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
|
-
|
|
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
|
-
|
|
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
|
-
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
|
|
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
|
-
|
|
397
|
-
|
|
398
|
-
|
|
399
|
-
|
|
400
|
-
|
|
401
|
-
|
|
402
|
-
|
|
403
|
-
|
|
404
|
-
|
|
405
|
-
|
|
406
|
-
|
|
407
|
-
|
|
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
|
-
|
|
415
|
-
|
|
416
|
-
|
|
417
|
-
|
|
418
|
-
|
|
419
|
-
|
|
420
|
-
|
|
421
|
-
|
|
422
|
-
|
|
423
|
-
|
|
424
|
-
|
|
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
|
-
|
|
430
|
-
|
|
431
|
-
|
|
432
|
-
|
|
433
|
-
|
|
434
|
-
|
|
435
|
-
|
|
436
|
-
|
|
437
|
-
|
|
438
|
-
|
|
439
|
-
|
|
440
|
-
|
|
441
|
-
|
|
442
|
-
|
|
443
|
-
|
|
444
|
-
|
|
445
|
-
|
|
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
|
|
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
|
-
|
|
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
|
|
482
|
-
|
|
483
|
-
|
|
484
|
-
|
|
485
|
-
|
|
486
|
-
|
|
487
|
-
|
|
488
|
-
|
|
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
|
-
|
|
494
|
-
|
|
495
|
-
|
|
496
|
-
|
|
497
|
-
|
|
498
|
-
|
|
499
|
-
|
|
500
|
-
|
|
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
|
-
|
|
505
|
-
|
|
506
|
-
|
|
507
|
-
|
|
508
|
-
|
|
509
|
-
|
|
510
|
-
|
|
511
|
-
|
|
512
|
-
|
|
513
|
-
|
|
514
|
-
|
|
515
|
-
|
|
516
|
-
|
|
517
|
-
|
|
518
|
-
|
|
519
|
-
|
|
520
|
-
|
|
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
|
-
|
|
525
|
-
|
|
526
|
-
|
|
527
|
-
|
|
528
|
-
|
|
529
|
-
|
|
530
|
-
|
|
531
|
-
|
|
532
|
-
|
|
533
|
-
|
|
534
|
-
|
|
535
|
-
}
|
|
536
|
-
|
|
537
|
-
|
|
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
|