leoric 2.4.0 → 2.4.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 CHANGED
@@ -1,3 +1,12 @@
1
+ 2.4.1 / 2022-04-27
2
+ ==================
3
+
4
+ ## What's Changed
5
+ * fix: realm.Bone.DataTypes should be invokable, Invokable.TYPE.toSqlString() get wrong default length(1), DataType definitions by @JimmyDaddy in https://github.com/cyjake/leoric/pull/307
6
+
7
+
8
+ **Full Changelog**: https://github.com/cyjake/leoric/compare/v2.4.0...v2.4.1
9
+
1
10
  2.4.0 / 2022-04-24
2
11
  ==================
3
12
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "leoric",
3
- "version": "2.4.0",
3
+ "version": "2.4.1",
4
4
  "description": "JavaScript Object-relational mapping alchemy",
5
5
  "main": "index.js",
6
6
  "types": "types/index.d.ts",
package/src/bone.js CHANGED
@@ -138,6 +138,8 @@ function valuesValidate(values, attributes, ctx) {
138
138
  */
139
139
  class Bone {
140
140
 
141
+ static DataTypes = DataTypes.invokable;
142
+
141
143
  // private variables
142
144
  #raw = {};
143
145
  #rawSaved = {};
@@ -1712,6 +1714,4 @@ for (const getter of Spell_getters) {
1712
1714
  });
1713
1715
  }
1714
1716
 
1715
- Object.assign(Bone, { DataTypes });
1716
-
1717
1717
  module.exports = Bone;
package/src/data_types.js CHANGED
@@ -110,20 +110,20 @@ class DataType {
110
110
  * STRING
111
111
  * STRING(127)
112
112
  * STRING.BINARY
113
- * @param {number} length
113
+ * @param {number} dataLength
114
114
  */
115
115
  class STRING extends DataType {
116
- constructor(length = 255) {
116
+ constructor(dataLength = 255) {
117
117
  super();
118
118
  this.dataType = 'varchar';
119
- this.length = length;
119
+ this.dataLength = dataLength;
120
120
  }
121
121
 
122
122
  toSqlString() {
123
- const { length } = this;
123
+ const { dataLength } = this;
124
124
  const dataType = this.dataType.toUpperCase();
125
125
  const chunks = [];
126
- chunks.push(length > 0 ? `${dataType}(${length})` : dataType);
126
+ chunks.push(dataLength > 0 ? `${dataType}(${dataLength})` : dataType);
127
127
  return chunks.join(' ');
128
128
  }
129
129
 
@@ -134,17 +134,17 @@ class STRING extends DataType {
134
134
  }
135
135
 
136
136
  class BINARY extends DataType {
137
- constructor(length = 255) {
137
+ constructor(dataLength = 255) {
138
138
  super();
139
- this.length = length;
139
+ this.dataLength = dataLength;
140
140
  this.dataType = 'binary';
141
141
  }
142
142
 
143
143
  toSqlString() {
144
- const { length } = this;
144
+ const { dataLength } = this;
145
145
  const dataType = this.dataType.toUpperCase();
146
146
  const chunks = [];
147
- chunks.push(length > 0 ? `${dataType}(${length})` : dataType);
147
+ chunks.push(dataLength > 0 ? `${dataType}(${dataLength})` : dataType);
148
148
  return chunks.join(' ');
149
149
  }
150
150
 
@@ -156,8 +156,8 @@ class BINARY extends DataType {
156
156
  }
157
157
 
158
158
  class VARBINARY extends BINARY {
159
- constructor(length) {
160
- super(length);
159
+ constructor(dataLength) {
160
+ super(dataLength);
161
161
  this.dataType = 'varbinary';
162
162
  }
163
163
  }
@@ -170,12 +170,12 @@ class VARBINARY extends BINARY {
170
170
  * INTEGER.UNSIGNED
171
171
  * INTEGER.UNSIGNED.ZEROFILL
172
172
  * INTEGER(10)
173
- * @param {number} length
173
+ * @param {number} dataLength
174
174
  */
175
175
  class INTEGER extends DataType {
176
- constructor(length) {
176
+ constructor(dataLength) {
177
177
  super();
178
- this.length = length;
178
+ this.dataLength = dataLength;
179
179
  this.dataType = 'integer';
180
180
  }
181
181
 
@@ -190,10 +190,10 @@ class INTEGER extends DataType {
190
190
  }
191
191
 
192
192
  toSqlString() {
193
- const { length, unsigned, zerofill } = this;
193
+ const { dataLength, unsigned, zerofill } = this;
194
194
  const dataType = this.dataType.toUpperCase();
195
195
  const chunks = [];
196
- chunks.push(length > 0 ? `${dataType}(${length})` : dataType);
196
+ chunks.push(dataLength > 0 ? `${dataType}(${dataLength})` : dataType);
197
197
  if (unsigned) chunks.push('UNSIGNED');
198
198
  if (zerofill) chunks.push('ZEROFILL');
199
199
  return chunks.join(' ');
@@ -222,11 +222,11 @@ class INTEGER extends DataType {
222
222
  * TINYINT
223
223
  * TINYINT.UNSIGNED
224
224
  * TINYINT(1)
225
- * @param {number} length
225
+ * @param {number} dataLength
226
226
  */
227
227
  class TINYINT extends INTEGER {
228
- constructor(length) {
229
- super(length);
228
+ constructor(dataLength) {
229
+ super(dataLength);
230
230
  this.dataType = 'tinyint';
231
231
  }
232
232
  }
@@ -237,11 +237,11 @@ class TINYINT extends INTEGER {
237
237
  * SMALLINT
238
238
  * SMALLINT.UNSIGNED
239
239
  * SMALLINT(2)
240
- * @param {number} length
240
+ * @param {number} dataLength
241
241
  */
242
242
  class SMALLINT extends INTEGER {
243
- constructor(length) {
244
- super(length);
243
+ constructor(dataLength) {
244
+ super(dataLength);
245
245
  this.dataType = 'smallint';
246
246
  }
247
247
  }
@@ -252,11 +252,11 @@ class SMALLINT extends INTEGER {
252
252
  * MEDIUMINT
253
253
  * MEDIUMINT.UNSIGNED
254
254
  * MEDIUMINT(3)
255
- * @param {number} length
255
+ * @param {number} dataLength
256
256
  */
257
257
  class MEDIUMINT extends INTEGER {
258
- constructor(length) {
259
- super(length);
258
+ constructor(dataLength) {
259
+ super(dataLength);
260
260
  this.dataType = 'mediumint';
261
261
  }
262
262
  }
@@ -268,11 +268,11 @@ class MEDIUMINT extends INTEGER {
268
268
  * BIGINT
269
269
  * BIGINT.UNSIGNED
270
270
  * BIGINT(8)
271
- * @param {number} length
271
+ * @param {number} dataLength
272
272
  */
273
273
  class BIGINT extends INTEGER {
274
- constructor(length) {
275
- super(length);
274
+ constructor(dataLength) {
275
+ super(dataLength);
276
276
  this.dataType = 'bigint';
277
277
  }
278
278
  }
@@ -442,11 +442,11 @@ class TEXT extends DataType {
442
442
  }
443
443
  super();
444
444
  this.dataType = 'text';
445
- this.length = length;
445
+ this.dataLength = length;
446
446
  }
447
447
 
448
448
  toSqlString() {
449
- return [ this.length, this.dataType ].join('').toUpperCase();
449
+ return [ this.dataLength, this.dataType ].join('').toUpperCase();
450
450
  }
451
451
  }
452
452
 
@@ -457,11 +457,11 @@ class BLOB extends DataType {
457
457
  }
458
458
  super();
459
459
  this.dataType = 'blob';
460
- this.length = length;
460
+ this.dataLength = length;
461
461
  }
462
462
 
463
463
  toSqlString() {
464
- return [ this.length, this.dataType ].join('').toUpperCase();
464
+ return [ this.dataLength, this.dataType ].join('').toUpperCase();
465
465
  }
466
466
 
467
467
  cast(value) {
@@ -77,7 +77,7 @@ function createType(DataTypes, params) {
77
77
  case 'VARBINARY':
78
78
  case 'CHAR':
79
79
  case 'VARCHAR':
80
- return new DataType(type.length);
80
+ return new DataType(type.dataLength);
81
81
  default:
82
82
  return new DataType();
83
83
  }
@@ -41,8 +41,8 @@ class Postgres_BINARY extends DataTypes {
41
41
  }
42
42
 
43
43
  class Postgres_INTEGER extends DataTypes.INTEGER {
44
- constructor(length) {
45
- super(length);
44
+ constructor(dataLength) {
45
+ super(dataLength);
46
46
  }
47
47
 
48
48
  uncast(value) {
@@ -34,8 +34,8 @@ class Sqlite_DATEONLY extends DataTypes.DATEONLY {
34
34
  }
35
35
 
36
36
  class Sqlite_INTEGER extends DataTypes.INTEGER {
37
- constructor(length) {
38
- super(length);
37
+ constructor(dataLength) {
38
+ super(dataLength);
39
39
  }
40
40
 
41
41
  uncast(value) {
@@ -54,24 +54,24 @@ class Sqlite_BIGINT extends DataTypes.BIGINT {
54
54
  }
55
55
  }
56
56
  class Sqlite_BINARY extends DataTypes {
57
- constructor(length = 255) {
58
- super(length);
59
- this.length = length;
57
+ constructor(dataLength = 255) {
58
+ super(dataLength);
59
+ this.dataLength = dataLength;
60
60
  this.dataType = 'binary';
61
61
  }
62
62
 
63
63
  toSqlString() {
64
- const { length } = this;
64
+ const { dataLength } = this;
65
65
  const dataType = this.dataType.toUpperCase();
66
66
  const chunks = [];
67
67
  chunks.push('VARCHAR');
68
- chunks.push(length > 0 ? `${dataType}(${length})` : dataType);
68
+ chunks.push(dataLength > 0 ? `${dataType}(${dataLength})` : dataType);
69
69
  return chunks.join(' ');
70
70
  }
71
71
  }
72
72
  class Sqlite_VARBINARY extends Sqlite_BINARY {
73
- constructor(length) {
74
- super(length);
73
+ constructor(dataLength) {
74
+ super(dataLength);
75
75
  this.dataType = 'varbinary';
76
76
  }
77
77
  }
@@ -18,6 +18,9 @@ function invokable(DataType) {
18
18
 
19
19
  // INTEGER.UNSIGNED
20
20
  get(target, p) {
21
+ // ref: https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/Function/length
22
+ // The length property indicates the number of parameters expected by the function.
23
+ // invokable INTEGER.toSqlString() will default to return "INTEGER(1)"
21
24
  return target.hasOwnProperty(p) ? target[p] : new target()[p];
22
25
  }
23
26
  });
@@ -1,65 +1,68 @@
1
1
  type LENGTH_VARIANTS = 'tiny' | '' | 'medium' | 'long';
2
2
 
3
- interface INVOKABLE<T> {
4
- (length: LENGTH_VARIANTS): T;
5
- (length: number): T;
3
+ interface INVOKABLE<T> extends DataType {
4
+ (dataLength?: LENGTH_VARIANTS): T;
5
+ (dataLength?: number): T;
6
6
  }
7
7
 
8
8
  export default class DataType {
9
9
  toSqlString(): string;
10
10
 
11
- static STRING: typeof STRING & INVOKABLE<STRING>;
12
- static INTEGER: typeof INTEGER & INVOKABLE<INTEGER>;
13
- static BIGINT: typeof BIGINT & INVOKABLE<BIGINT>;
14
- static DECIMAL: typeof DECIMAL & INVOKABLE<DECIMAL>;
15
- static TEXT: typeof TEXT & INVOKABLE<TEXT>;
16
- static BLOB: typeof BLOB & INVOKABLE<BLOB>;
17
- static JSON: typeof JSON & INVOKABLE<JSON>;
18
- static JSONB: typeof JSONB & INVOKABLE<JSONB>;
19
- static BINARY: typeof BINARY & INVOKABLE<BINARY>;
20
- static VARBINARY: typeof VARBINARY & INVOKABLE<VARBINARY>;
21
- static DATE: typeof DATE & INVOKABLE<DATE>;
22
- static DATEONLY: typeof DATEONLY & INVOKABLE<DATEONLY>;
23
- static BOOLEAN: typeof BOOLEAN & INVOKABLE<BOOLEAN>;
24
- static VIRTUAL: typeof VIRTUAL & INVOKABLE<VIRTUAL>;
11
+ static STRING: INVOKABLE<STRING>;
12
+ static INTEGER: INTEGER & INVOKABLE<INTEGER>;
13
+ static BIGINT: BIGINT & INVOKABLE<BIGINT>;
14
+ static DECIMAL: DECIMAL & INVOKABLE<DECIMAL>;
15
+ static TEXT: INVOKABLE<TEXT>;
16
+ static BLOB: INVOKABLE<BLOB>;
17
+ static JSON: JSON;
18
+ static JSONB: JSONB;
19
+ static BINARY: BINARY & INVOKABLE<BINARY>;
20
+ static VARBINARY: VARBINARY & INVOKABLE<VARBINARY>;
21
+ static DATE: DATE & INVOKABLE<DATE>;
22
+ static DATEONLY: DATEONLY;
23
+ static BOOLEAN: BOOLEAN;
24
+ static VIRTUAL: VIRTUAL;
25
25
 
26
26
  }
27
27
 
28
28
  declare class STRING extends DataType {
29
29
  dataType: 'varchar';
30
- length: number;
31
- constructor(length: number);
30
+ dataLength: number;
31
+ constructor(dataLength: number);
32
32
  }
33
33
 
34
34
  declare class INTEGER extends DataType {
35
35
  dataType: 'integer' | 'bigint' | 'decimal';
36
- length: number;
37
- constructor(length: number);
38
- get UNSIGNED(): this;
39
- get ZEROFILL(): this;
36
+ dataLength: number;
37
+ constructor(dataLength: number);
38
+ // avoid INTEGER.UNSIGNED.ZEROFILL.UNSIGNED.UNSIGNED
39
+ get UNSIGNED(): Omit<this, 'UNSIGNED' | 'ZEROFILL'>;
40
+ get ZEROFILL(): Omit<this, 'UNSIGNED' | 'ZEROFILL'>;
40
41
  }
41
42
 
42
43
  declare class BIGINT extends INTEGER {
43
44
  dataType: 'bigint';
44
45
  }
45
46
 
46
- declare class DECIMAL extends INTEGER {
47
+ declare class DECIMAL_INNER extends INTEGER {
47
48
  dataType: 'decimal';
48
49
  precision: number;
49
50
  scale: number;
50
51
  constructor(precision: number, scale: number);
51
52
  }
52
53
 
54
+ declare type DECIMAL = Omit<DECIMAL_INNER, 'dataLength'>;
55
+
53
56
  declare class TEXT extends DataType {
54
57
  dataType: 'text';
55
- length: LENGTH_VARIANTS;
56
- constructor(length: LENGTH_VARIANTS);
58
+ dataLength: LENGTH_VARIANTS;
59
+ constructor(dataLength: LENGTH_VARIANTS);
57
60
  }
58
61
 
59
62
  declare class BLOB extends DataType {
60
63
  dataType: 'blob';
61
- length: LENGTH_VARIANTS;
62
- constructor(length: LENGTH_VARIANTS)
64
+ dataLength: LENGTH_VARIANTS;
65
+ constructor(dataLength: LENGTH_VARIANTS)
63
66
  }
64
67
 
65
68
  declare class JSON extends DataType {
@@ -72,14 +75,14 @@ declare class JSONB extends JSON {
72
75
 
73
76
  declare class BINARY extends DataType {
74
77
  dataType: 'binary';
75
- length: number;
76
- constructor(length: number);
78
+ dataLength: number;
79
+ constructor(dataLength: number);
77
80
  }
78
81
 
79
82
  declare class VARBINARY extends DataType {
80
83
  dataType: 'varbinary';
81
- length: number;
82
- constructor(length: number);
84
+ dataLength: number;
85
+ constructor(dataLength: number);
83
86
  }
84
87
 
85
88
  declare class DATE extends DataType {
package/types/index.d.ts CHANGED
@@ -179,11 +179,17 @@ export interface ColumnMeta {
179
179
  comment?: string;
180
180
  datetimePrecision?: string;
181
181
  }
182
+
183
+ declare type validator = Literal | Function | Array<Literal | Literal[]>;
184
+
182
185
  export interface AttributeMeta extends ColumnMeta {
183
186
  jsType?: Literal;
184
187
  type: DataType;
185
188
  virtual?: boolean,
186
189
  toSqlString: () => string;
190
+ validate: {
191
+ [key: string]: validator;
192
+ }
187
193
  }
188
194
 
189
195
  interface RelateOptions {
@@ -442,7 +448,7 @@ export class Collection<T extends Bone> extends Array<T> {
442
448
  }
443
449
 
444
450
  export class Bone {
445
- static DataTypes: DataType;
451
+ static DataTypes: typeof DataType;
446
452
 
447
453
  /**
448
454
  * get the connection pool of the driver