sedentary 0.0.49 → 0.0.50
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/dist/cjs/db.js +3 -1
- package/dist/cjs/index.js +58 -53
- package/dist/es/db.js +2 -0
- package/dist/es/index.js +57 -54
- package/dist/types/db.d.ts +5 -3
- package/dist/types/index.d.ts +11 -9
- package/package.json +1 -1
package/dist/cjs/db.js
CHANGED
|
@@ -1,8 +1,10 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.differ = exports.Transaction = exports.DB = exports.Table = exports.Attribute = exports.Type = exports.EntryBase = exports.transaction = exports.loaded = exports.actions = void 0;
|
|
3
|
+
exports.differ = exports.Transaction = exports.DB = exports.Table = exports.Attribute = exports.Type = exports.EntryBase = exports.transaction = exports.size = exports.loaded = exports.base = exports.actions = void 0;
|
|
4
4
|
exports.actions = Symbol("actions");
|
|
5
|
+
exports.base = Symbol("base");
|
|
5
6
|
exports.loaded = Symbol("loaded");
|
|
7
|
+
exports.size = Symbol("size");
|
|
6
8
|
exports.transaction = Symbol("transaction");
|
|
7
9
|
class EntryBase {
|
|
8
10
|
constructor(from) {
|
package/dist/cjs/index.js
CHANGED
|
@@ -1,22 +1,26 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.Sedentary = exports.Type = exports.transaction = exports.Transaction = exports.Table = exports.loaded = exports.EntryBase = exports.differ = exports.DB = exports.Attribute = void 0;
|
|
3
|
+
exports.Sedentary = exports.Type = exports.transaction = exports.Transaction = exports.Table = exports.size = exports.loaded = exports.EntryBase = exports.differ = exports.DB = exports.base = exports.Attribute = void 0;
|
|
4
4
|
const db_1 = require("./db");
|
|
5
5
|
var db_2 = require("./db");
|
|
6
6
|
Object.defineProperty(exports, "Attribute", { enumerable: true, get: function () { return db_2.Attribute; } });
|
|
7
|
+
Object.defineProperty(exports, "base", { enumerable: true, get: function () { return db_2.base; } });
|
|
7
8
|
Object.defineProperty(exports, "DB", { enumerable: true, get: function () { return db_2.DB; } });
|
|
8
9
|
Object.defineProperty(exports, "differ", { enumerable: true, get: function () { return db_2.differ; } });
|
|
9
10
|
Object.defineProperty(exports, "EntryBase", { enumerable: true, get: function () { return db_2.EntryBase; } });
|
|
10
11
|
Object.defineProperty(exports, "loaded", { enumerable: true, get: function () { return db_2.loaded; } });
|
|
12
|
+
Object.defineProperty(exports, "size", { enumerable: true, get: function () { return db_2.size; } });
|
|
11
13
|
Object.defineProperty(exports, "Table", { enumerable: true, get: function () { return db_2.Table; } });
|
|
12
14
|
Object.defineProperty(exports, "Transaction", { enumerable: true, get: function () { return db_2.Transaction; } });
|
|
13
15
|
Object.defineProperty(exports, "transaction", { enumerable: true, get: function () { return db_2.transaction; } });
|
|
14
16
|
Object.defineProperty(exports, "Type", { enumerable: true, get: function () { return db_2.Type; } });
|
|
17
|
+
const attributes = Symbol("attributes");
|
|
18
|
+
const methods = Symbol("methods");
|
|
15
19
|
const operators = ["=", ">", "<", ">=", "<=", "<>", "IN", "IS NULL", "LIKE", "NOT"];
|
|
16
20
|
const allowedOption = ["indexes", "int8id", "parent", "primaryKey", "sync", "tableName"];
|
|
17
21
|
const reservedNames = [
|
|
18
|
-
...["attr2field", "attributeName", "
|
|
19
|
-
...["
|
|
22
|
+
...["attr2field", "attributeName", "cancel", "class", "construct", "constructor", "defaultValue", "fieldName", "foreignKeys", "load", "name"],
|
|
23
|
+
...["postCommit", "postLoad", "postRemove", "postSave", "preCommit", "preLoad", "preRemove", "preSave", "primaryKey", "prototype", "save", "tableName", "type"]
|
|
20
24
|
];
|
|
21
25
|
class Sedentary {
|
|
22
26
|
constructor(options) {
|
|
@@ -43,38 +47,38 @@ class Sedentary {
|
|
|
43
47
|
this.doSync = sync;
|
|
44
48
|
}
|
|
45
49
|
Boolean() {
|
|
46
|
-
return new db_1.Type({ base: Boolean, type: "BOOLEAN" });
|
|
50
|
+
return new db_1.Type({ [db_1.base]: Boolean, type: "BOOLEAN" });
|
|
47
51
|
}
|
|
48
52
|
DateTime() {
|
|
49
|
-
return new db_1.Type({ base: Date, type: "DATETIME" });
|
|
53
|
+
return new db_1.Type({ [db_1.base]: Date, type: "DATETIME" });
|
|
50
54
|
}
|
|
51
55
|
FKey(attribute, options) {
|
|
52
|
-
const { attributeName,
|
|
53
|
-
return new db_1.Type({ base, foreignKey: { attributeName, fieldName, options, tableName }, size, type });
|
|
56
|
+
const { attributeName, fieldName, tableName, type, [db_1.base]: _base, [db_1.size]: _size } = attribute;
|
|
57
|
+
return new db_1.Type({ [db_1.base]: _base, foreignKey: { attributeName, fieldName, options, tableName }, [db_1.size]: _size, type });
|
|
54
58
|
}
|
|
55
|
-
Int(
|
|
59
|
+
Int(_size) {
|
|
56
60
|
const message = "Sedentary.INT: 'size' argument: Wrong value, expected 2 or 4";
|
|
57
|
-
|
|
58
|
-
if (
|
|
61
|
+
_size = _size ? this.checkSize(_size, message) : 4;
|
|
62
|
+
if (_size !== 2 && _size !== 4)
|
|
59
63
|
throw new Error(message);
|
|
60
|
-
return new db_1.Type({ base: Number, size, type: "INT" });
|
|
64
|
+
return new db_1.Type({ [db_1.base]: Number, [db_1.size]: _size, type: "INT" });
|
|
61
65
|
}
|
|
62
66
|
Int8() {
|
|
63
|
-
return new db_1.Type({ base: BigInt, size: 8, type: "INT8" });
|
|
67
|
+
return new db_1.Type({ [db_1.base]: BigInt, [db_1.size]: 8, type: "INT8" });
|
|
64
68
|
}
|
|
65
69
|
JSON() {
|
|
66
|
-
return new db_1.Type({ base: Object, type: "JSON" });
|
|
70
|
+
return new db_1.Type({ [db_1.base]: Object, type: "JSON" });
|
|
67
71
|
}
|
|
68
72
|
Number() {
|
|
69
|
-
return new db_1.Type({ base: Number, type: "NUMBER" });
|
|
73
|
+
return new db_1.Type({ [db_1.base]: Number, type: "NUMBER" });
|
|
70
74
|
}
|
|
71
75
|
None() {
|
|
72
|
-
return new db_1.Type({ base: undefined, type: "NONE" });
|
|
76
|
+
return new db_1.Type({ [db_1.base]: undefined, type: "NONE" });
|
|
73
77
|
}
|
|
74
|
-
VarChar(
|
|
78
|
+
VarChar(_size) {
|
|
75
79
|
const message = "Sedentary.VARCHAR: 'size' argument: Wrong value, expected positive integer";
|
|
76
|
-
|
|
77
|
-
return new db_1.Type({ base: String, size, type: "VARCHAR" });
|
|
80
|
+
_size = _size ? this.checkSize(_size, message) : undefined;
|
|
81
|
+
return new db_1.Type({ [db_1.base]: String, [db_1.size]: _size, type: "VARCHAR" });
|
|
78
82
|
}
|
|
79
83
|
checkDB() {
|
|
80
84
|
if (!this.db)
|
|
@@ -211,15 +215,15 @@ class Sedentary {
|
|
|
211
215
|
return this.db.escape(value);
|
|
212
216
|
}
|
|
213
217
|
/* eslint-enable @typescript-eslint/no-explicit-any */
|
|
214
|
-
model(modelName,
|
|
218
|
+
model(modelName, _attributes, options, _methods) {
|
|
215
219
|
this.checkDB();
|
|
216
220
|
if (typeof modelName !== "string")
|
|
217
221
|
throw new Error("Sedentary.model: 'name' argument: Wrong type, expected 'string'");
|
|
218
222
|
if (this.models[modelName])
|
|
219
223
|
throw new Error(`Sedentary.model: '${modelName}' model: Model already defined`);
|
|
220
|
-
if (!
|
|
221
|
-
|
|
222
|
-
if (!(
|
|
224
|
+
if (!_attributes)
|
|
225
|
+
_attributes = {};
|
|
226
|
+
if (!(_attributes instanceof Object))
|
|
223
227
|
throw new Error(`Sedentary.model: '${modelName}' model: 'attributes' argument: Wrong type, expected 'Object'`);
|
|
224
228
|
if (!options)
|
|
225
229
|
options = {};
|
|
@@ -243,16 +247,16 @@ class Sedentary {
|
|
|
243
247
|
const iArray = [];
|
|
244
248
|
let pk = aArray[0];
|
|
245
249
|
let attr2field = { id: "id" };
|
|
246
|
-
if (!
|
|
247
|
-
|
|
248
|
-
if (!(
|
|
250
|
+
if (!_methods)
|
|
251
|
+
_methods = {};
|
|
252
|
+
if (!(_methods instanceof Object))
|
|
249
253
|
throw new Error(`Sedentary.model: '${modelName}' model: 'methods' option: Wrong type, expected 'Object'`);
|
|
250
254
|
if (parent)
|
|
251
|
-
if (!parent
|
|
255
|
+
if (!parent[attributes])
|
|
252
256
|
throw new Error(`Sedentary.model: '${modelName}' model: 'parent' option: Wrong type, expected 'Model'`);
|
|
253
257
|
if (primaryKey && typeof primaryKey !== "string")
|
|
254
258
|
throw new Error(`Sedentary.model: '${modelName}' model: 'primaryKey' option: Wrong type, expected 'string'`);
|
|
255
|
-
if (primaryKey && !Object.keys(
|
|
259
|
+
if (primaryKey && !Object.keys(_attributes).includes(primaryKey))
|
|
256
260
|
throw new Error(`Sedentary.model: '${modelName}' model: 'primaryKey' option: Attribute '${primaryKey}' does not exists`);
|
|
257
261
|
if (parent || primaryKey) {
|
|
258
262
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
@@ -261,7 +265,7 @@ class Sedentary {
|
|
|
261
265
|
aArray = [];
|
|
262
266
|
constraints = [];
|
|
263
267
|
}
|
|
264
|
-
for (const attributeName of Object.keys(
|
|
268
|
+
for (const attributeName of Object.keys(_attributes).sort()) {
|
|
265
269
|
if (reservedNames.includes(attributeName))
|
|
266
270
|
throw new Error(`Sedentary.model: '${modelName}' model: '${attributeName}' attribute: Reserved name`);
|
|
267
271
|
const call = (defaultValue, fieldName, notNull, unique, func, message1, message2) => {
|
|
@@ -271,8 +275,8 @@ class Sedentary {
|
|
|
271
275
|
throw new Error(`${message1} ${message2}`);
|
|
272
276
|
return new db_1.Attribute({ attributeName, defaultValue, fieldName, modelName, notNull, tableName, unique, ...func() });
|
|
273
277
|
};
|
|
274
|
-
const attributeDefinition =
|
|
275
|
-
let { base, defaultValue, fieldName, foreignKey, notNull, size, type, unique } = (() => {
|
|
278
|
+
const attributeDefinition = _attributes[attributeName];
|
|
279
|
+
let { [db_1.base]: _base, defaultValue, fieldName, foreignKey, notNull, [db_1.size]: _size, type, unique } = (() => {
|
|
276
280
|
const ret = (() => {
|
|
277
281
|
if (attributeDefinition instanceof db_1.Type)
|
|
278
282
|
return new db_1.Attribute({ attributeName, fieldName: attributeName, modelName, notNull: false, tableName, ...attributeDefinition });
|
|
@@ -298,15 +302,15 @@ class Sedentary {
|
|
|
298
302
|
return call(defaultValue, fieldName, notNull, unique, type, `Sedentary.model: '${modelName}' model: '${attributeName}' attribute: 'type' option:`, "Wrong type, expected 'Type'");
|
|
299
303
|
throw new Error(`Sedentary.model: '${modelName}' model: '${attributeName}' attribute: 'type' option: Wrong type, expected 'Type'`);
|
|
300
304
|
})();
|
|
301
|
-
const { base, defaultValue } = ret;
|
|
305
|
+
const { [db_1.base]: _base, defaultValue } = ret;
|
|
302
306
|
if (defaultValue !== undefined) {
|
|
303
|
-
if (
|
|
307
|
+
if (_base === BigInt && typeof defaultValue !== "bigint")
|
|
304
308
|
throw new Error(`Sedentary.model: '${modelName}' model: '${attributeName}' attribute: 'defaultValue' option: Wrong type, expected 'BigInt'`);
|
|
305
|
-
if (
|
|
309
|
+
if (_base === Date && !(defaultValue instanceof Date))
|
|
306
310
|
throw new Error(`Sedentary.model: '${modelName}' model: '${attributeName}' attribute: 'defaultValue' option: Wrong type, expected 'Date'`);
|
|
307
|
-
if (
|
|
311
|
+
if (_base === Number && typeof defaultValue !== "number")
|
|
308
312
|
throw new Error(`Sedentary.model: '${modelName}' model: '${attributeName}' attribute: 'defaultValue' option: Wrong type, expected 'number'`);
|
|
309
|
-
if (
|
|
313
|
+
if (_base === String && typeof defaultValue !== "string")
|
|
310
314
|
throw new Error(`Sedentary.model: '${modelName}' model: '${attributeName}' attribute: 'defaultValue' option: Wrong type, expected 'string'`);
|
|
311
315
|
}
|
|
312
316
|
return ret;
|
|
@@ -334,18 +338,19 @@ class Sedentary {
|
|
|
334
338
|
}
|
|
335
339
|
if (defaultValue)
|
|
336
340
|
notNull = true;
|
|
337
|
-
const attribute = new db_1.Attribute({ attributeName, base, defaultValue, fieldName, foreignKey, modelName, notNull, size, tableName, type, unique });
|
|
341
|
+
const attribute = new db_1.Attribute({ attributeName, [db_1.base]: _base, defaultValue, fieldName, foreignKey, modelName, notNull, [db_1.size]: _size, tableName, type, unique });
|
|
338
342
|
if (primaryKey === attributeName)
|
|
339
343
|
pk = attribute;
|
|
340
344
|
aArray.push(attribute);
|
|
341
|
-
|
|
345
|
+
if (type !== "NONE")
|
|
346
|
+
attr2field[attributeName] = fieldName;
|
|
342
347
|
if (foreignKey)
|
|
343
348
|
constraints.push({ attribute, constraintName: `fkey_${fieldName}_${foreignKey.tableName}_${foreignKey.fieldName}`, type: "f" });
|
|
344
349
|
if (unique)
|
|
345
350
|
constraints.push({ attribute, constraintName: `${tableName}_${fieldName}_unique`, type: "u" });
|
|
346
351
|
}
|
|
347
352
|
if (indexes) {
|
|
348
|
-
const originalAttributes =
|
|
353
|
+
const originalAttributes = _attributes;
|
|
349
354
|
if (!(indexes instanceof Object))
|
|
350
355
|
throw new Error(`Sedentary.model: '${modelName}' model: 'indexes' option: Wrong type, expected 'Object'`);
|
|
351
356
|
for (const indexName in indexes) {
|
|
@@ -404,34 +409,34 @@ class Sedentary {
|
|
|
404
409
|
return ret;
|
|
405
410
|
}, {});
|
|
406
411
|
for (const foreignKey in foreignKeys) {
|
|
407
|
-
if (foreignKey + "Load" in
|
|
412
|
+
if (foreignKey + "Load" in _attributes)
|
|
408
413
|
throw new Error(`Sedentary.model: '${modelName}' model: '${foreignKey}' attribute: '${foreignKey}Load' inferred methods conflicts with an attribute`);
|
|
409
|
-
if (foreignKey + "Load" in
|
|
414
|
+
if (foreignKey + "Load" in _methods)
|
|
410
415
|
throw new Error(`Sedentary.model: '${modelName}' model: '${foreignKey}' attribute: '${foreignKey}Load' inferred methods conflicts with a method`);
|
|
411
416
|
}
|
|
412
|
-
for (const method in
|
|
413
|
-
if (method in
|
|
417
|
+
for (const method in _methods)
|
|
418
|
+
if (method in _attributes)
|
|
414
419
|
throw new Error(`Sedentary.model: '${modelName}' model: '${method}' method: conflicts with an attribute`);
|
|
415
420
|
const checkParent = (parent) => {
|
|
416
421
|
if (!parent)
|
|
417
422
|
return;
|
|
418
|
-
for (const attribute in
|
|
419
|
-
if (attribute in parent
|
|
423
|
+
for (const attribute in _attributes) {
|
|
424
|
+
if (attribute in parent[attributes])
|
|
420
425
|
throw new Error(`Sedentary.model: '${modelName}' model: '${attribute}' attribute: conflicts with an attribute of '${parent.modelName}' model`);
|
|
421
|
-
if (attribute in parent
|
|
426
|
+
if (attribute in parent[methods])
|
|
422
427
|
throw new Error(`Sedentary.model: '${modelName}' model: '${attribute}' attribute: conflicts with a method of '${parent.modelName}' model`);
|
|
423
428
|
for (const foreignKey in parent.foreignKeys)
|
|
424
429
|
if (attribute === foreignKey + "Load")
|
|
425
430
|
throw new Error(`Sedentary.model: '${modelName}' model: '${attribute}' attribute: conflicts with an inferred methods of '${parent.modelName}' model`);
|
|
426
431
|
}
|
|
427
432
|
for (const foreignKey in foreignKeys) {
|
|
428
|
-
if (foreignKey + "Load" in parent
|
|
433
|
+
if (foreignKey + "Load" in parent[attributes])
|
|
429
434
|
throw new Error(`Sedentary.model: '${modelName}' model: '${foreignKey}' attribute: '${foreignKey}Load' inferred methods conflicts with an attribute of '${parent.modelName}' model`);
|
|
430
|
-
if (foreignKey + "Load" in parent
|
|
435
|
+
if (foreignKey + "Load" in parent[methods])
|
|
431
436
|
throw new Error(`Sedentary.model: '${modelName}' model: '${foreignKey}' attribute: '${foreignKey}Load' inferred methods conflicts with a method of '${parent.modelName}' model`);
|
|
432
437
|
}
|
|
433
|
-
for (const method in
|
|
434
|
-
if (method in parent
|
|
438
|
+
for (const method in _methods) {
|
|
439
|
+
if (method in parent[attributes])
|
|
435
440
|
throw new Error(`Sedentary.model: '${modelName}' model: '${method}' method: conflicts with an attribute of '${parent.modelName}' model`);
|
|
436
441
|
for (const foreignKey in parent.foreignKeys)
|
|
437
442
|
if (foreignKey + "Load" === method)
|
|
@@ -493,10 +498,10 @@ class Sedentary {
|
|
|
493
498
|
Object.defineProperty(ret, "name", { value: modelName });
|
|
494
499
|
Object.defineProperty(ret, "load", { value: load });
|
|
495
500
|
Object.defineProperty(ret, "attr2field", { value: attr2field });
|
|
496
|
-
Object.defineProperty(ret,
|
|
501
|
+
Object.defineProperty(ret, attributes, { value: _attributes });
|
|
497
502
|
Object.defineProperty(ret, "foreignKeys", { value: foreignKeys });
|
|
498
|
-
Object.defineProperty(ret,
|
|
499
|
-
Object.assign(ret.prototype,
|
|
503
|
+
Object.defineProperty(ret, methods, { value: _methods });
|
|
504
|
+
Object.assign(ret.prototype, _methods);
|
|
500
505
|
const ensureActions = (entry) => {
|
|
501
506
|
if (!entry[db_1.actions])
|
|
502
507
|
Object.defineProperty(entry, db_1.actions, { configurable: true, value: [] });
|
|
@@ -526,7 +531,7 @@ class Sedentary {
|
|
|
526
531
|
Object.defineProperty(ret.prototype.save, "name", { value: modelName + ".save" });
|
|
527
532
|
for (const attribute of aArray)
|
|
528
533
|
Object.defineProperty(ret, attribute.attributeName, { value: attribute });
|
|
529
|
-
for (const key of ["attributeName",
|
|
534
|
+
for (const key of ["attributeName", db_1.base, "fieldName", "modelName", db_1.size, "tableName", "type", "unique"])
|
|
530
535
|
Object.defineProperty(ret, key, { value: pk[key] });
|
|
531
536
|
return ret;
|
|
532
537
|
}
|
package/dist/es/db.js
CHANGED
package/dist/es/index.js
CHANGED
|
@@ -1,10 +1,12 @@
|
|
|
1
|
-
import { actions, Attribute, EntryBase, loaded, Table, Transaction, transaction, Type } from "./db";
|
|
2
|
-
export { Attribute, DB, differ, EntryBase, loaded, Table, Transaction, transaction, Type } from "./db";
|
|
1
|
+
import { actions, Attribute, base, EntryBase, loaded, size, Table, Transaction, transaction, Type } from "./db";
|
|
2
|
+
export { Attribute, base, DB, differ, EntryBase, loaded, size, Table, Transaction, transaction, Type } from "./db";
|
|
3
|
+
const attributes = Symbol("attributes");
|
|
4
|
+
const methods = Symbol("methods");
|
|
3
5
|
const operators = ["=", ">", "<", ">=", "<=", "<>", "IN", "IS NULL", "LIKE", "NOT"];
|
|
4
6
|
const allowedOption = ["indexes", "int8id", "parent", "primaryKey", "sync", "tableName"];
|
|
5
7
|
const reservedNames = [
|
|
6
|
-
...["attr2field", "attributeName", "
|
|
7
|
-
...["
|
|
8
|
+
...["attr2field", "attributeName", "cancel", "class", "construct", "constructor", "defaultValue", "fieldName", "foreignKeys", "load", "name"],
|
|
9
|
+
...["postCommit", "postLoad", "postRemove", "postSave", "preCommit", "preLoad", "preRemove", "preSave", "primaryKey", "prototype", "save", "tableName", "type"]
|
|
8
10
|
];
|
|
9
11
|
export class Sedentary {
|
|
10
12
|
autoSync;
|
|
@@ -34,38 +36,38 @@ export class Sedentary {
|
|
|
34
36
|
this.doSync = sync;
|
|
35
37
|
}
|
|
36
38
|
Boolean() {
|
|
37
|
-
return new Type({ base: Boolean, type: "BOOLEAN" });
|
|
39
|
+
return new Type({ [base]: Boolean, type: "BOOLEAN" });
|
|
38
40
|
}
|
|
39
41
|
DateTime() {
|
|
40
|
-
return new Type({ base: Date, type: "DATETIME" });
|
|
42
|
+
return new Type({ [base]: Date, type: "DATETIME" });
|
|
41
43
|
}
|
|
42
44
|
FKey(attribute, options) {
|
|
43
|
-
const { attributeName,
|
|
44
|
-
return new Type({ base, foreignKey: { attributeName, fieldName, options, tableName }, size, type });
|
|
45
|
+
const { attributeName, fieldName, tableName, type, [base]: _base, [size]: _size } = attribute;
|
|
46
|
+
return new Type({ [base]: _base, foreignKey: { attributeName, fieldName, options, tableName }, [size]: _size, type });
|
|
45
47
|
}
|
|
46
|
-
Int(
|
|
48
|
+
Int(_size) {
|
|
47
49
|
const message = "Sedentary.INT: 'size' argument: Wrong value, expected 2 or 4";
|
|
48
|
-
|
|
49
|
-
if (
|
|
50
|
+
_size = _size ? this.checkSize(_size, message) : 4;
|
|
51
|
+
if (_size !== 2 && _size !== 4)
|
|
50
52
|
throw new Error(message);
|
|
51
|
-
return new Type({ base: Number, size, type: "INT" });
|
|
53
|
+
return new Type({ [base]: Number, [size]: _size, type: "INT" });
|
|
52
54
|
}
|
|
53
55
|
Int8() {
|
|
54
|
-
return new Type({ base: BigInt, size: 8, type: "INT8" });
|
|
56
|
+
return new Type({ [base]: BigInt, [size]: 8, type: "INT8" });
|
|
55
57
|
}
|
|
56
58
|
JSON() {
|
|
57
|
-
return new Type({ base: Object, type: "JSON" });
|
|
59
|
+
return new Type({ [base]: Object, type: "JSON" });
|
|
58
60
|
}
|
|
59
61
|
Number() {
|
|
60
|
-
return new Type({ base: Number, type: "NUMBER" });
|
|
62
|
+
return new Type({ [base]: Number, type: "NUMBER" });
|
|
61
63
|
}
|
|
62
64
|
None() {
|
|
63
|
-
return new Type({ base: undefined, type: "NONE" });
|
|
65
|
+
return new Type({ [base]: undefined, type: "NONE" });
|
|
64
66
|
}
|
|
65
|
-
VarChar(
|
|
67
|
+
VarChar(_size) {
|
|
66
68
|
const message = "Sedentary.VARCHAR: 'size' argument: Wrong value, expected positive integer";
|
|
67
|
-
|
|
68
|
-
return new Type({ base: String, size, type: "VARCHAR" });
|
|
69
|
+
_size = _size ? this.checkSize(_size, message) : undefined;
|
|
70
|
+
return new Type({ [base]: String, [size]: _size, type: "VARCHAR" });
|
|
69
71
|
}
|
|
70
72
|
checkDB() {
|
|
71
73
|
if (!this.db)
|
|
@@ -202,15 +204,15 @@ export class Sedentary {
|
|
|
202
204
|
return this.db.escape(value);
|
|
203
205
|
}
|
|
204
206
|
/* eslint-enable @typescript-eslint/no-explicit-any */
|
|
205
|
-
model(modelName,
|
|
207
|
+
model(modelName, _attributes, options, _methods) {
|
|
206
208
|
this.checkDB();
|
|
207
209
|
if (typeof modelName !== "string")
|
|
208
210
|
throw new Error("Sedentary.model: 'name' argument: Wrong type, expected 'string'");
|
|
209
211
|
if (this.models[modelName])
|
|
210
212
|
throw new Error(`Sedentary.model: '${modelName}' model: Model already defined`);
|
|
211
|
-
if (!
|
|
212
|
-
|
|
213
|
-
if (!(
|
|
213
|
+
if (!_attributes)
|
|
214
|
+
_attributes = {};
|
|
215
|
+
if (!(_attributes instanceof Object))
|
|
214
216
|
throw new Error(`Sedentary.model: '${modelName}' model: 'attributes' argument: Wrong type, expected 'Object'`);
|
|
215
217
|
if (!options)
|
|
216
218
|
options = {};
|
|
@@ -234,16 +236,16 @@ export class Sedentary {
|
|
|
234
236
|
const iArray = [];
|
|
235
237
|
let pk = aArray[0];
|
|
236
238
|
let attr2field = { id: "id" };
|
|
237
|
-
if (!
|
|
238
|
-
|
|
239
|
-
if (!(
|
|
239
|
+
if (!_methods)
|
|
240
|
+
_methods = {};
|
|
241
|
+
if (!(_methods instanceof Object))
|
|
240
242
|
throw new Error(`Sedentary.model: '${modelName}' model: 'methods' option: Wrong type, expected 'Object'`);
|
|
241
243
|
if (parent)
|
|
242
|
-
if (!parent
|
|
244
|
+
if (!parent[attributes])
|
|
243
245
|
throw new Error(`Sedentary.model: '${modelName}' model: 'parent' option: Wrong type, expected 'Model'`);
|
|
244
246
|
if (primaryKey && typeof primaryKey !== "string")
|
|
245
247
|
throw new Error(`Sedentary.model: '${modelName}' model: 'primaryKey' option: Wrong type, expected 'string'`);
|
|
246
|
-
if (primaryKey && !Object.keys(
|
|
248
|
+
if (primaryKey && !Object.keys(_attributes).includes(primaryKey))
|
|
247
249
|
throw new Error(`Sedentary.model: '${modelName}' model: 'primaryKey' option: Attribute '${primaryKey}' does not exists`);
|
|
248
250
|
if (parent || primaryKey) {
|
|
249
251
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
@@ -252,7 +254,7 @@ export class Sedentary {
|
|
|
252
254
|
aArray = [];
|
|
253
255
|
constraints = [];
|
|
254
256
|
}
|
|
255
|
-
for (const attributeName of Object.keys(
|
|
257
|
+
for (const attributeName of Object.keys(_attributes).sort()) {
|
|
256
258
|
if (reservedNames.includes(attributeName))
|
|
257
259
|
throw new Error(`Sedentary.model: '${modelName}' model: '${attributeName}' attribute: Reserved name`);
|
|
258
260
|
const call = (defaultValue, fieldName, notNull, unique, func, message1, message2) => {
|
|
@@ -262,8 +264,8 @@ export class Sedentary {
|
|
|
262
264
|
throw new Error(`${message1} ${message2}`);
|
|
263
265
|
return new Attribute({ attributeName, defaultValue, fieldName, modelName, notNull, tableName, unique, ...func() });
|
|
264
266
|
};
|
|
265
|
-
const attributeDefinition =
|
|
266
|
-
let { base, defaultValue, fieldName, foreignKey, notNull, size, type, unique } = (() => {
|
|
267
|
+
const attributeDefinition = _attributes[attributeName];
|
|
268
|
+
let { [base]: _base, defaultValue, fieldName, foreignKey, notNull, [size]: _size, type, unique } = (() => {
|
|
267
269
|
const ret = (() => {
|
|
268
270
|
if (attributeDefinition instanceof Type)
|
|
269
271
|
return new Attribute({ attributeName, fieldName: attributeName, modelName, notNull: false, tableName, ...attributeDefinition });
|
|
@@ -289,15 +291,15 @@ export class Sedentary {
|
|
|
289
291
|
return call(defaultValue, fieldName, notNull, unique, type, `Sedentary.model: '${modelName}' model: '${attributeName}' attribute: 'type' option:`, "Wrong type, expected 'Type'");
|
|
290
292
|
throw new Error(`Sedentary.model: '${modelName}' model: '${attributeName}' attribute: 'type' option: Wrong type, expected 'Type'`);
|
|
291
293
|
})();
|
|
292
|
-
const { base, defaultValue } = ret;
|
|
294
|
+
const { [base]: _base, defaultValue } = ret;
|
|
293
295
|
if (defaultValue !== undefined) {
|
|
294
|
-
if (
|
|
296
|
+
if (_base === BigInt && typeof defaultValue !== "bigint")
|
|
295
297
|
throw new Error(`Sedentary.model: '${modelName}' model: '${attributeName}' attribute: 'defaultValue' option: Wrong type, expected 'BigInt'`);
|
|
296
|
-
if (
|
|
298
|
+
if (_base === Date && !(defaultValue instanceof Date))
|
|
297
299
|
throw new Error(`Sedentary.model: '${modelName}' model: '${attributeName}' attribute: 'defaultValue' option: Wrong type, expected 'Date'`);
|
|
298
|
-
if (
|
|
300
|
+
if (_base === Number && typeof defaultValue !== "number")
|
|
299
301
|
throw new Error(`Sedentary.model: '${modelName}' model: '${attributeName}' attribute: 'defaultValue' option: Wrong type, expected 'number'`);
|
|
300
|
-
if (
|
|
302
|
+
if (_base === String && typeof defaultValue !== "string")
|
|
301
303
|
throw new Error(`Sedentary.model: '${modelName}' model: '${attributeName}' attribute: 'defaultValue' option: Wrong type, expected 'string'`);
|
|
302
304
|
}
|
|
303
305
|
return ret;
|
|
@@ -325,18 +327,19 @@ export class Sedentary {
|
|
|
325
327
|
}
|
|
326
328
|
if (defaultValue)
|
|
327
329
|
notNull = true;
|
|
328
|
-
const attribute = new Attribute({ attributeName, base, defaultValue, fieldName, foreignKey, modelName, notNull, size, tableName, type, unique });
|
|
330
|
+
const attribute = new Attribute({ attributeName, [base]: _base, defaultValue, fieldName, foreignKey, modelName, notNull, [size]: _size, tableName, type, unique });
|
|
329
331
|
if (primaryKey === attributeName)
|
|
330
332
|
pk = attribute;
|
|
331
333
|
aArray.push(attribute);
|
|
332
|
-
|
|
334
|
+
if (type !== "NONE")
|
|
335
|
+
attr2field[attributeName] = fieldName;
|
|
333
336
|
if (foreignKey)
|
|
334
337
|
constraints.push({ attribute, constraintName: `fkey_${fieldName}_${foreignKey.tableName}_${foreignKey.fieldName}`, type: "f" });
|
|
335
338
|
if (unique)
|
|
336
339
|
constraints.push({ attribute, constraintName: `${tableName}_${fieldName}_unique`, type: "u" });
|
|
337
340
|
}
|
|
338
341
|
if (indexes) {
|
|
339
|
-
const originalAttributes =
|
|
342
|
+
const originalAttributes = _attributes;
|
|
340
343
|
if (!(indexes instanceof Object))
|
|
341
344
|
throw new Error(`Sedentary.model: '${modelName}' model: 'indexes' option: Wrong type, expected 'Object'`);
|
|
342
345
|
for (const indexName in indexes) {
|
|
@@ -395,34 +398,34 @@ export class Sedentary {
|
|
|
395
398
|
return ret;
|
|
396
399
|
}, {});
|
|
397
400
|
for (const foreignKey in foreignKeys) {
|
|
398
|
-
if (foreignKey + "Load" in
|
|
401
|
+
if (foreignKey + "Load" in _attributes)
|
|
399
402
|
throw new Error(`Sedentary.model: '${modelName}' model: '${foreignKey}' attribute: '${foreignKey}Load' inferred methods conflicts with an attribute`);
|
|
400
|
-
if (foreignKey + "Load" in
|
|
403
|
+
if (foreignKey + "Load" in _methods)
|
|
401
404
|
throw new Error(`Sedentary.model: '${modelName}' model: '${foreignKey}' attribute: '${foreignKey}Load' inferred methods conflicts with a method`);
|
|
402
405
|
}
|
|
403
|
-
for (const method in
|
|
404
|
-
if (method in
|
|
406
|
+
for (const method in _methods)
|
|
407
|
+
if (method in _attributes)
|
|
405
408
|
throw new Error(`Sedentary.model: '${modelName}' model: '${method}' method: conflicts with an attribute`);
|
|
406
409
|
const checkParent = (parent) => {
|
|
407
410
|
if (!parent)
|
|
408
411
|
return;
|
|
409
|
-
for (const attribute in
|
|
410
|
-
if (attribute in parent
|
|
412
|
+
for (const attribute in _attributes) {
|
|
413
|
+
if (attribute in parent[attributes])
|
|
411
414
|
throw new Error(`Sedentary.model: '${modelName}' model: '${attribute}' attribute: conflicts with an attribute of '${parent.modelName}' model`);
|
|
412
|
-
if (attribute in parent
|
|
415
|
+
if (attribute in parent[methods])
|
|
413
416
|
throw new Error(`Sedentary.model: '${modelName}' model: '${attribute}' attribute: conflicts with a method of '${parent.modelName}' model`);
|
|
414
417
|
for (const foreignKey in parent.foreignKeys)
|
|
415
418
|
if (attribute === foreignKey + "Load")
|
|
416
419
|
throw new Error(`Sedentary.model: '${modelName}' model: '${attribute}' attribute: conflicts with an inferred methods of '${parent.modelName}' model`);
|
|
417
420
|
}
|
|
418
421
|
for (const foreignKey in foreignKeys) {
|
|
419
|
-
if (foreignKey + "Load" in parent
|
|
422
|
+
if (foreignKey + "Load" in parent[attributes])
|
|
420
423
|
throw new Error(`Sedentary.model: '${modelName}' model: '${foreignKey}' attribute: '${foreignKey}Load' inferred methods conflicts with an attribute of '${parent.modelName}' model`);
|
|
421
|
-
if (foreignKey + "Load" in parent
|
|
424
|
+
if (foreignKey + "Load" in parent[methods])
|
|
422
425
|
throw new Error(`Sedentary.model: '${modelName}' model: '${foreignKey}' attribute: '${foreignKey}Load' inferred methods conflicts with a method of '${parent.modelName}' model`);
|
|
423
426
|
}
|
|
424
|
-
for (const method in
|
|
425
|
-
if (method in parent
|
|
427
|
+
for (const method in _methods) {
|
|
428
|
+
if (method in parent[attributes])
|
|
426
429
|
throw new Error(`Sedentary.model: '${modelName}' model: '${method}' method: conflicts with an attribute of '${parent.modelName}' model`);
|
|
427
430
|
for (const foreignKey in parent.foreignKeys)
|
|
428
431
|
if (foreignKey + "Load" === method)
|
|
@@ -484,10 +487,10 @@ export class Sedentary {
|
|
|
484
487
|
Object.defineProperty(ret, "name", { value: modelName });
|
|
485
488
|
Object.defineProperty(ret, "load", { value: load });
|
|
486
489
|
Object.defineProperty(ret, "attr2field", { value: attr2field });
|
|
487
|
-
Object.defineProperty(ret,
|
|
490
|
+
Object.defineProperty(ret, attributes, { value: _attributes });
|
|
488
491
|
Object.defineProperty(ret, "foreignKeys", { value: foreignKeys });
|
|
489
|
-
Object.defineProperty(ret,
|
|
490
|
-
Object.assign(ret.prototype,
|
|
492
|
+
Object.defineProperty(ret, methods, { value: _methods });
|
|
493
|
+
Object.assign(ret.prototype, _methods);
|
|
491
494
|
const ensureActions = (entry) => {
|
|
492
495
|
if (!entry[actions])
|
|
493
496
|
Object.defineProperty(entry, actions, { configurable: true, value: [] });
|
|
@@ -517,7 +520,7 @@ export class Sedentary {
|
|
|
517
520
|
Object.defineProperty(ret.prototype.save, "name", { value: modelName + ".save" });
|
|
518
521
|
for (const attribute of aArray)
|
|
519
522
|
Object.defineProperty(ret, attribute.attributeName, { value: attribute });
|
|
520
|
-
for (const key of ["attributeName",
|
|
523
|
+
for (const key of ["attributeName", base, "fieldName", "modelName", size, "tableName", "type", "unique"])
|
|
521
524
|
Object.defineProperty(ret, key, { value: pk[key] });
|
|
522
525
|
return ret;
|
|
523
526
|
}
|
package/dist/types/db.d.ts
CHANGED
|
@@ -1,5 +1,7 @@
|
|
|
1
1
|
export declare const actions: unique symbol;
|
|
2
|
+
export declare const base: unique symbol;
|
|
2
3
|
export declare const loaded: unique symbol;
|
|
4
|
+
export declare const size: unique symbol;
|
|
3
5
|
export declare const transaction: unique symbol;
|
|
4
6
|
export interface Action {
|
|
5
7
|
action: "remove" | "save";
|
|
@@ -25,10 +27,10 @@ export interface ForeignKeyOptions {
|
|
|
25
27
|
onUpdate?: ForeignKeyActions;
|
|
26
28
|
}
|
|
27
29
|
export interface Type<T, E> {
|
|
28
|
-
base: unknown;
|
|
30
|
+
[base]: unknown;
|
|
29
31
|
entry?: E;
|
|
30
32
|
native?: T;
|
|
31
|
-
size?: number;
|
|
33
|
+
[size]?: number;
|
|
32
34
|
type: string;
|
|
33
35
|
foreignKey?: {
|
|
34
36
|
attributeName: string;
|
|
@@ -98,7 +100,7 @@ export declare abstract class DB<T extends Transaction> {
|
|
|
98
100
|
abstract escape(value: unknown): string;
|
|
99
101
|
abstract load(tableName: string, attributes: Record<string, string>, pk: Attribute<unknown, unknown>, model: new () => EntryBase, table: Table): (where: string, order?: string | string[], limit?: number, tx?: Transaction, lock?: boolean) => Promise<EntryBase[]>;
|
|
100
102
|
abstract remove(tableName: string, pk: Attribute<unknown, unknown>): (this: EntryBase & Record<string, unknown>) => Promise<number>;
|
|
101
|
-
abstract save(tableName: string,
|
|
103
|
+
abstract save(tableName: string, attr2field: Record<string, string>, pk: Attribute<unknown, unknown>): (this: EntryBase & Record<string, unknown>) => Promise<number | false>;
|
|
102
104
|
abstract dropConstraints(table: Table): Promise<number[]>;
|
|
103
105
|
abstract dropFields(table: Table): Promise<void>;
|
|
104
106
|
abstract dropIndexes(table: Table, constraintIndexes: number[]): Promise<void>;
|
package/dist/types/index.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { Attribute, DB, EntryBase, ForeignKeyOptions, Transaction, Type } from "./db";
|
|
2
|
-
export { Action, Attribute, DB, differ, EntryBase, ForeignKeyActions, ForeignKeyOptions, Index, loaded, Table, Transaction, transaction, Type } from "./db";
|
|
2
|
+
export { Action, Attribute, base, DB, differ, EntryBase, ForeignKeyActions, ForeignKeyOptions, Index, loaded, size, Table, Transaction, transaction, Type } from "./db";
|
|
3
3
|
export declare type TypeDefinition<T, E> = (() => Type<T, E>) | Type<T, E>;
|
|
4
4
|
export interface AttributeOptions<T, E> {
|
|
5
5
|
defaultValue?: T;
|
|
@@ -12,6 +12,8 @@ export declare type AttributeDefinition<T, E> = TypeDefinition<T, E> | Attribute
|
|
|
12
12
|
export declare type AttributesDefinition = {
|
|
13
13
|
[key: string]: AttributeDefinition<unknown, unknown>;
|
|
14
14
|
};
|
|
15
|
+
declare const attributes: unique symbol;
|
|
16
|
+
declare const methods: unique symbol;
|
|
15
17
|
declare type ForeignKeysAttributes<T, k> = T extends AttributeDefinition<unknown, infer E> ? (E extends EntryBase ? k : never) : never;
|
|
16
18
|
declare type ForeignKeys<A extends AttributesDefinition> = {
|
|
17
19
|
[a in keyof A]?: ForeignKeysAttributes<A[a], a>;
|
|
@@ -57,13 +59,13 @@ declare type ForeignKey<A> = A extends AttributeDefinition<unknown, infer E> ? (
|
|
|
57
59
|
declare type EntryBaseAttributes<A extends AttributesDefinition> = {
|
|
58
60
|
[a in keyof A]: Native<A[a]>;
|
|
59
61
|
};
|
|
60
|
-
declare type EntryMethodsBase<P extends ModelStd> = P extends new () => EntryBase ? P[
|
|
62
|
+
declare type EntryMethodsBase<P extends ModelStd> = P extends new () => EntryBase ? P[typeof methods] : EntryBase;
|
|
61
63
|
declare type EntryMethodsFK<A extends AttributesDefinition> = {
|
|
62
64
|
[a in ForeignKeys<A> & string as `${a}Load`]: ForeignKey<A[a]>;
|
|
63
65
|
};
|
|
64
66
|
declare type EntryMethods<A extends AttributesDefinition, P extends ModelStd> = keyof EntryMethodsFK<A> extends never ? EntryMethodsBase<P> : EntryMethodsBase<P> & EntryMethodsFK<A>;
|
|
65
67
|
declare type ModelAttributesIf<A extends AttributesDefinition, T> = keyof A extends never ? T : T & A;
|
|
66
|
-
declare type ModelAttributes<A extends AttributesDefinition, B extends boolean, K extends string, P extends ModelStd> = K extends keyof A ? A : ModelAttributesIf<A, P extends new () => EntryBase ? P[
|
|
68
|
+
declare type ModelAttributes<A extends AttributesDefinition, B extends boolean, K extends string, P extends ModelStd> = K extends keyof A ? A : ModelAttributesIf<A, P extends new () => EntryBase ? P[typeof attributes] : {
|
|
67
69
|
id: {
|
|
68
70
|
notNull: true;
|
|
69
71
|
type: Type<BaseKeyType<B>, unknown>;
|
|
@@ -77,9 +79,9 @@ export interface ModelLoad<A extends AttributesDefinition, E extends EntryBase>
|
|
|
77
79
|
cancel(where: Condition<A>, tx?: Transaction): Promise<number>;
|
|
78
80
|
}
|
|
79
81
|
declare type ModelBase<T, A extends AttributesDefinition, EA extends Record<string, unknown>, EM extends EntryBase, E extends EntryBase> = (new (from?: Partial<EA>, tx?: Transaction) => E) & Attribute<T, E> & {
|
|
80
|
-
attributes: A;
|
|
82
|
+
[attributes]: A;
|
|
81
83
|
foreignKeys: Record<string, boolean>;
|
|
82
|
-
methods: EM;
|
|
84
|
+
[methods]: EM;
|
|
83
85
|
parent?: ModelStd;
|
|
84
86
|
tableName: string;
|
|
85
87
|
} & {
|
|
@@ -87,9 +89,9 @@ declare type ModelBase<T, A extends AttributesDefinition, EA extends Record<stri
|
|
|
87
89
|
} & ModelLoad<A, E>;
|
|
88
90
|
declare type Model<T, A extends AttributesDefinition, EM extends EntryBase> = ModelBase<T, A, EntryBaseAttributes<A>, EM, EntryBaseAttributes<A> & EM>;
|
|
89
91
|
declare type ModelStd = Attribute<unknown, EntryBase> & {
|
|
90
|
-
attributes: AttributesDefinition;
|
|
92
|
+
[attributes]: AttributesDefinition;
|
|
91
93
|
foreignKeys: Record<string, boolean>;
|
|
92
|
-
methods: EntryBase;
|
|
94
|
+
[methods]: EntryBase;
|
|
93
95
|
parent?: ModelStd;
|
|
94
96
|
};
|
|
95
97
|
export declare type Entry<M> = M extends new () => infer E ? E : never;
|
|
@@ -116,12 +118,12 @@ export declare class Sedentary<D extends DB<T>, T extends Transaction> {
|
|
|
116
118
|
Boolean(): Type<boolean, unknown>;
|
|
117
119
|
DateTime(): Type<Date, unknown>;
|
|
118
120
|
FKey<T, E extends EntryBase>(attribute: Attribute<T, E>, options?: ForeignKeyOptions): Type<T, E>;
|
|
119
|
-
Int(
|
|
121
|
+
Int(_size?: number): Type<number, unknown>;
|
|
120
122
|
Int8(): Type<bigint, unknown>;
|
|
121
123
|
JSON<T>(): Type<T, unknown>;
|
|
122
124
|
Number(): Type<number, unknown>;
|
|
123
125
|
None<T>(): Type<T, unknown>;
|
|
124
|
-
VarChar<S extends string>(
|
|
126
|
+
VarChar<S extends string>(_size?: number): Type<S, unknown>;
|
|
125
127
|
private checkDB;
|
|
126
128
|
private checkOrderBy;
|
|
127
129
|
private checkSize;
|
package/package.json
CHANGED