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