sedentary 0.0.48 → 0.0.51

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 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.deepDiff = exports.deepCopy = 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) {
@@ -135,7 +137,17 @@ class Transaction {
135
137
  }
136
138
  exports.Transaction = Transaction;
137
139
  const sortedEntries = (obj) => Object.entries(obj).sort((entryA, entryB) => (entryA[0] > entryB[0] ? -1 : 1));
138
- function differ(a, b) {
140
+ function deepCopy(o) {
141
+ if (!o || typeof o !== "object")
142
+ return o;
143
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
144
+ const [ret, entries] = o instanceof Array ? [new Array(o.length), o.entries()] : [{}, Object.entries(o)];
145
+ for (const [k, v] of entries)
146
+ ret[k] = deepCopy(v);
147
+ return ret;
148
+ }
149
+ exports.deepCopy = deepCopy;
150
+ function deepDiff(a, b) {
139
151
  if (typeof a !== "object")
140
152
  return a !== b;
141
153
  if (typeof b !== "object")
@@ -148,7 +160,7 @@ function differ(a, b) {
148
160
  if (!(b instanceof Array))
149
161
  return true;
150
162
  for (const [i, value] of a.entries())
151
- if (differ(value, b[i]))
163
+ if (deepDiff(value, b[i]))
152
164
  return true;
153
165
  return false;
154
166
  }
@@ -157,8 +169,8 @@ function differ(a, b) {
157
169
  if (entriesA.length !== entriesB.length)
158
170
  return true;
159
171
  for (const [i, [key, value]] of entriesA.entries())
160
- if (key !== entriesB[i][0] || differ(value, entriesB[i][1]))
172
+ if (key !== entriesB[i][0] || deepDiff(value, entriesB[i][1]))
161
173
  return true;
162
174
  return false;
163
175
  }
164
- exports.differ = differ;
176
+ exports.deepDiff = deepDiff;
package/dist/cjs/index.js CHANGED
@@ -1,20 +1,27 @@
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.deepDiff = exports.deepCopy = 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
- Object.defineProperty(exports, "differ", { enumerable: true, get: function () { return db_2.differ; } });
9
+ Object.defineProperty(exports, "deepCopy", { enumerable: true, get: function () { return db_2.deepCopy; } });
10
+ Object.defineProperty(exports, "deepDiff", { enumerable: true, get: function () { return db_2.deepDiff; } });
9
11
  Object.defineProperty(exports, "EntryBase", { enumerable: true, get: function () { return db_2.EntryBase; } });
12
+ Object.defineProperty(exports, "loaded", { enumerable: true, get: function () { return db_2.loaded; } });
13
+ Object.defineProperty(exports, "size", { enumerable: true, get: function () { return db_2.size; } });
10
14
  Object.defineProperty(exports, "Table", { enumerable: true, get: function () { return db_2.Table; } });
11
15
  Object.defineProperty(exports, "Transaction", { enumerable: true, get: function () { return db_2.Transaction; } });
16
+ Object.defineProperty(exports, "transaction", { enumerable: true, get: function () { return db_2.transaction; } });
12
17
  Object.defineProperty(exports, "Type", { enumerable: true, get: function () { return db_2.Type; } });
18
+ const attributes = Symbol("attributes");
19
+ const methods = Symbol("methods");
13
20
  const operators = ["=", ">", "<", ">=", "<=", "<>", "IN", "IS NULL", "LIKE", "NOT"];
14
21
  const allowedOption = ["indexes", "int8id", "parent", "primaryKey", "sync", "tableName"];
15
22
  const reservedNames = [
16
- ...["attr2field", "attributeName", "attributes", "base", "class", "construct", "constructor", "defaultValue", "entry", "fieldName", "foreignKeys", "load"],
17
- ...["methods", "name", "postCommit", "postLoad", "postRemove", "postSave", "preCommit", "preLoad", "preRemove", "preSave", "primaryKey", "prototype", "save", "size", "tableName", "type"]
23
+ ...["attr2field", "attributeName", "cancel", "class", "construct", "constructor", "defaultValue", "fieldName", "foreignKeys", "load", "name"],
24
+ ...["postCommit", "postLoad", "postRemove", "postSave", "preCommit", "preLoad", "preRemove", "preSave", "primaryKey", "prototype", "save", "tableName", "type"]
18
25
  ];
19
26
  class Sedentary {
20
27
  constructor(options) {
@@ -41,38 +48,38 @@ class Sedentary {
41
48
  this.doSync = sync;
42
49
  }
43
50
  Boolean() {
44
- return new db_1.Type({ base: Boolean, type: "BOOLEAN" });
51
+ return new db_1.Type({ [db_1.base]: Boolean, type: "BOOLEAN" });
45
52
  }
46
53
  DateTime() {
47
- return new db_1.Type({ base: Date, type: "DATETIME" });
54
+ return new db_1.Type({ [db_1.base]: Date, type: "DATETIME" });
48
55
  }
49
56
  FKey(attribute, options) {
50
- const { attributeName, base, fieldName, size, tableName, type } = attribute;
51
- return new db_1.Type({ base, foreignKey: { attributeName, fieldName, options, tableName }, size, type });
57
+ const { attributeName, fieldName, tableName, type, [db_1.base]: _base, [db_1.size]: _size } = attribute;
58
+ return new db_1.Type({ [db_1.base]: _base, foreignKey: { attributeName, fieldName, options, tableName }, [db_1.size]: _size, type });
52
59
  }
53
- Int(size) {
60
+ Int(_size) {
54
61
  const message = "Sedentary.INT: 'size' argument: Wrong value, expected 2 or 4";
55
- size = size ? this.checkSize(size, message) : 4;
56
- if (size !== 2 && size !== 4)
62
+ _size = _size ? this.checkSize(_size, message) : 4;
63
+ if (_size !== 2 && _size !== 4)
57
64
  throw new Error(message);
58
- return new db_1.Type({ base: Number, size, type: "INT" });
65
+ return new db_1.Type({ [db_1.base]: Number, [db_1.size]: _size, type: "INT" });
59
66
  }
60
67
  Int8() {
61
- return new db_1.Type({ base: BigInt, size: 8, type: "INT8" });
68
+ return new db_1.Type({ [db_1.base]: BigInt, [db_1.size]: 8, type: "INT8" });
62
69
  }
63
70
  JSON() {
64
- return new db_1.Type({ base: Object, type: "JSON" });
71
+ return new db_1.Type({ [db_1.base]: Object, type: "JSON" });
65
72
  }
66
73
  Number() {
67
- return new db_1.Type({ base: Number, type: "NUMBER" });
74
+ return new db_1.Type({ [db_1.base]: Number, type: "NUMBER" });
68
75
  }
69
76
  None() {
70
- return new db_1.Type({ base: undefined, type: "NONE" });
77
+ return new db_1.Type({ [db_1.base]: undefined, type: "NONE" });
71
78
  }
72
- VarChar(size) {
79
+ VarChar(_size) {
73
80
  const message = "Sedentary.VARCHAR: 'size' argument: Wrong value, expected positive integer";
74
- size = size ? this.checkSize(size, message) : undefined;
75
- return new db_1.Type({ base: String, size, type: "VARCHAR" });
81
+ _size = _size ? this.checkSize(_size, message) : undefined;
82
+ return new db_1.Type({ [db_1.base]: String, [db_1.size]: _size, type: "VARCHAR" });
76
83
  }
77
84
  checkDB() {
78
85
  if (!this.db)
@@ -209,15 +216,15 @@ class Sedentary {
209
216
  return this.db.escape(value);
210
217
  }
211
218
  /* eslint-enable @typescript-eslint/no-explicit-any */
212
- model(modelName, attributes, options, methods) {
219
+ model(modelName, _attributes, options, _methods) {
213
220
  this.checkDB();
214
221
  if (typeof modelName !== "string")
215
222
  throw new Error("Sedentary.model: 'name' argument: Wrong type, expected 'string'");
216
223
  if (this.models[modelName])
217
224
  throw new Error(`Sedentary.model: '${modelName}' model: Model already defined`);
218
- if (!attributes)
219
- attributes = {};
220
- if (!(attributes instanceof Object))
225
+ if (!_attributes)
226
+ _attributes = {};
227
+ if (!(_attributes instanceof Object))
221
228
  throw new Error(`Sedentary.model: '${modelName}' model: 'attributes' argument: Wrong type, expected 'Object'`);
222
229
  if (!options)
223
230
  options = {};
@@ -241,16 +248,16 @@ class Sedentary {
241
248
  const iArray = [];
242
249
  let pk = aArray[0];
243
250
  let attr2field = { id: "id" };
244
- if (!methods)
245
- methods = {};
246
- if (!(methods instanceof Object))
251
+ if (!_methods)
252
+ _methods = {};
253
+ if (!(_methods instanceof Object))
247
254
  throw new Error(`Sedentary.model: '${modelName}' model: 'methods' option: Wrong type, expected 'Object'`);
248
255
  if (parent)
249
- if (!parent.attributes)
256
+ if (!parent[attributes])
250
257
  throw new Error(`Sedentary.model: '${modelName}' model: 'parent' option: Wrong type, expected 'Model'`);
251
258
  if (primaryKey && typeof primaryKey !== "string")
252
259
  throw new Error(`Sedentary.model: '${modelName}' model: 'primaryKey' option: Wrong type, expected 'string'`);
253
- if (primaryKey && !Object.keys(attributes).includes(primaryKey))
260
+ if (primaryKey && !Object.keys(_attributes).includes(primaryKey))
254
261
  throw new Error(`Sedentary.model: '${modelName}' model: 'primaryKey' option: Attribute '${primaryKey}' does not exists`);
255
262
  if (parent || primaryKey) {
256
263
  // eslint-disable-next-line @typescript-eslint/no-explicit-any
@@ -259,7 +266,7 @@ class Sedentary {
259
266
  aArray = [];
260
267
  constraints = [];
261
268
  }
262
- for (const attributeName of Object.keys(attributes).sort()) {
269
+ for (const attributeName of Object.keys(_attributes).sort()) {
263
270
  if (reservedNames.includes(attributeName))
264
271
  throw new Error(`Sedentary.model: '${modelName}' model: '${attributeName}' attribute: Reserved name`);
265
272
  const call = (defaultValue, fieldName, notNull, unique, func, message1, message2) => {
@@ -269,8 +276,8 @@ class Sedentary {
269
276
  throw new Error(`${message1} ${message2}`);
270
277
  return new db_1.Attribute({ attributeName, defaultValue, fieldName, modelName, notNull, tableName, unique, ...func() });
271
278
  };
272
- const attributeDefinition = attributes[attributeName];
273
- let { base, defaultValue, fieldName, foreignKey, notNull, size, type, unique } = (() => {
279
+ const attributeDefinition = _attributes[attributeName];
280
+ let { [db_1.base]: _base, defaultValue, fieldName, foreignKey, notNull, [db_1.size]: _size, type, unique } = (() => {
274
281
  const ret = (() => {
275
282
  if (attributeDefinition instanceof db_1.Type)
276
283
  return new db_1.Attribute({ attributeName, fieldName: attributeName, modelName, notNull: false, tableName, ...attributeDefinition });
@@ -296,15 +303,15 @@ class Sedentary {
296
303
  return call(defaultValue, fieldName, notNull, unique, type, `Sedentary.model: '${modelName}' model: '${attributeName}' attribute: 'type' option:`, "Wrong type, expected 'Type'");
297
304
  throw new Error(`Sedentary.model: '${modelName}' model: '${attributeName}' attribute: 'type' option: Wrong type, expected 'Type'`);
298
305
  })();
299
- const { base, defaultValue } = ret;
306
+ const { [db_1.base]: _base, defaultValue } = ret;
300
307
  if (defaultValue !== undefined) {
301
- if (base === BigInt && typeof defaultValue !== "bigint")
308
+ if (_base === BigInt && typeof defaultValue !== "bigint")
302
309
  throw new Error(`Sedentary.model: '${modelName}' model: '${attributeName}' attribute: 'defaultValue' option: Wrong type, expected 'BigInt'`);
303
- if (base === Date && !(defaultValue instanceof Date))
310
+ if (_base === Date && !(defaultValue instanceof Date))
304
311
  throw new Error(`Sedentary.model: '${modelName}' model: '${attributeName}' attribute: 'defaultValue' option: Wrong type, expected 'Date'`);
305
- if (base === Number && typeof defaultValue !== "number")
312
+ if (_base === Number && typeof defaultValue !== "number")
306
313
  throw new Error(`Sedentary.model: '${modelName}' model: '${attributeName}' attribute: 'defaultValue' option: Wrong type, expected 'number'`);
307
- if (base === String && typeof defaultValue !== "string")
314
+ if (_base === String && typeof defaultValue !== "string")
308
315
  throw new Error(`Sedentary.model: '${modelName}' model: '${attributeName}' attribute: 'defaultValue' option: Wrong type, expected 'string'`);
309
316
  }
310
317
  return ret;
@@ -332,18 +339,19 @@ class Sedentary {
332
339
  }
333
340
  if (defaultValue)
334
341
  notNull = true;
335
- const attribute = new db_1.Attribute({ attributeName, base, defaultValue, fieldName, foreignKey, modelName, notNull, size, tableName, type, unique });
342
+ const attribute = new db_1.Attribute({ attributeName, [db_1.base]: _base, defaultValue, fieldName, foreignKey, modelName, notNull, [db_1.size]: _size, tableName, type, unique });
336
343
  if (primaryKey === attributeName)
337
344
  pk = attribute;
338
345
  aArray.push(attribute);
339
- attr2field[attributeName] = fieldName;
346
+ if (type !== "NONE")
347
+ attr2field[attributeName] = fieldName;
340
348
  if (foreignKey)
341
349
  constraints.push({ attribute, constraintName: `fkey_${fieldName}_${foreignKey.tableName}_${foreignKey.fieldName}`, type: "f" });
342
350
  if (unique)
343
351
  constraints.push({ attribute, constraintName: `${tableName}_${fieldName}_unique`, type: "u" });
344
352
  }
345
353
  if (indexes) {
346
- const originalAttributes = attributes;
354
+ const originalAttributes = _attributes;
347
355
  if (!(indexes instanceof Object))
348
356
  throw new Error(`Sedentary.model: '${modelName}' model: 'indexes' option: Wrong type, expected 'Object'`);
349
357
  for (const indexName in indexes) {
@@ -402,34 +410,34 @@ class Sedentary {
402
410
  return ret;
403
411
  }, {});
404
412
  for (const foreignKey in foreignKeys) {
405
- if (foreignKey + "Load" in attributes)
413
+ if (foreignKey + "Load" in _attributes)
406
414
  throw new Error(`Sedentary.model: '${modelName}' model: '${foreignKey}' attribute: '${foreignKey}Load' inferred methods conflicts with an attribute`);
407
- if (foreignKey + "Load" in methods)
415
+ if (foreignKey + "Load" in _methods)
408
416
  throw new Error(`Sedentary.model: '${modelName}' model: '${foreignKey}' attribute: '${foreignKey}Load' inferred methods conflicts with a method`);
409
417
  }
410
- for (const method in methods)
411
- if (method in attributes)
418
+ for (const method in _methods)
419
+ if (method in _attributes)
412
420
  throw new Error(`Sedentary.model: '${modelName}' model: '${method}' method: conflicts with an attribute`);
413
421
  const checkParent = (parent) => {
414
422
  if (!parent)
415
423
  return;
416
- for (const attribute in attributes) {
417
- if (attribute in parent.attributes)
424
+ for (const attribute in _attributes) {
425
+ if (attribute in parent[attributes])
418
426
  throw new Error(`Sedentary.model: '${modelName}' model: '${attribute}' attribute: conflicts with an attribute of '${parent.modelName}' model`);
419
- if (attribute in parent.methods)
427
+ if (attribute in parent[methods])
420
428
  throw new Error(`Sedentary.model: '${modelName}' model: '${attribute}' attribute: conflicts with a method of '${parent.modelName}' model`);
421
429
  for (const foreignKey in parent.foreignKeys)
422
430
  if (attribute === foreignKey + "Load")
423
431
  throw new Error(`Sedentary.model: '${modelName}' model: '${attribute}' attribute: conflicts with an inferred methods of '${parent.modelName}' model`);
424
432
  }
425
433
  for (const foreignKey in foreignKeys) {
426
- if (foreignKey + "Load" in parent.attributes)
434
+ if (foreignKey + "Load" in parent[attributes])
427
435
  throw new Error(`Sedentary.model: '${modelName}' model: '${foreignKey}' attribute: '${foreignKey}Load' inferred methods conflicts with an attribute of '${parent.modelName}' model`);
428
- if (foreignKey + "Load" in parent.methods)
436
+ if (foreignKey + "Load" in parent[methods])
429
437
  throw new Error(`Sedentary.model: '${modelName}' model: '${foreignKey}' attribute: '${foreignKey}Load' inferred methods conflicts with a method of '${parent.modelName}' model`);
430
438
  }
431
- for (const method in methods) {
432
- if (method in parent.attributes)
439
+ for (const method in _methods) {
440
+ if (method in parent[attributes])
433
441
  throw new Error(`Sedentary.model: '${modelName}' model: '${method}' method: conflicts with an attribute of '${parent.modelName}' model`);
434
442
  for (const foreignKey in parent.foreignKeys)
435
443
  if (foreignKey + "Load" === method)
@@ -491,10 +499,10 @@ class Sedentary {
491
499
  Object.defineProperty(ret, "name", { value: modelName });
492
500
  Object.defineProperty(ret, "load", { value: load });
493
501
  Object.defineProperty(ret, "attr2field", { value: attr2field });
494
- Object.defineProperty(ret, "attributes", { value: attributes });
502
+ Object.defineProperty(ret, attributes, { value: _attributes });
495
503
  Object.defineProperty(ret, "foreignKeys", { value: foreignKeys });
496
- Object.defineProperty(ret, "methods", { value: methods });
497
- Object.assign(ret.prototype, methods);
504
+ Object.defineProperty(ret, methods, { value: _methods });
505
+ Object.assign(ret.prototype, _methods);
498
506
  const ensureActions = (entry) => {
499
507
  if (!entry[db_1.actions])
500
508
  Object.defineProperty(entry, db_1.actions, { configurable: true, value: [] });
@@ -524,7 +532,7 @@ class Sedentary {
524
532
  Object.defineProperty(ret.prototype.save, "name", { value: modelName + ".save" });
525
533
  for (const attribute of aArray)
526
534
  Object.defineProperty(ret, attribute.attributeName, { value: attribute });
527
- for (const key of ["attributeName", "base", "fieldName", "modelName", "size", "tableName", "type", "unique"])
535
+ for (const key of ["attributeName", db_1.base, "fieldName", "modelName", db_1.size, "tableName", "type", "unique"])
528
536
  Object.defineProperty(ret, key, { value: pk[key] });
529
537
  return ret;
530
538
  }
package/dist/es/db.js CHANGED
@@ -1,5 +1,7 @@
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");
3
5
  export const transaction = Symbol("transaction");
4
6
  export class EntryBase {
5
7
  constructor(from) {
@@ -130,7 +132,16 @@ export class Transaction {
130
132
  }
131
133
  }
132
134
  const sortedEntries = (obj) => Object.entries(obj).sort((entryA, entryB) => (entryA[0] > entryB[0] ? -1 : 1));
133
- export function differ(a, b) {
135
+ export function deepCopy(o) {
136
+ if (!o || typeof o !== "object")
137
+ return o;
138
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
139
+ const [ret, entries] = o instanceof Array ? [new Array(o.length), o.entries()] : [{}, Object.entries(o)];
140
+ for (const [k, v] of entries)
141
+ ret[k] = deepCopy(v);
142
+ return ret;
143
+ }
144
+ export function deepDiff(a, b) {
134
145
  if (typeof a !== "object")
135
146
  return a !== b;
136
147
  if (typeof b !== "object")
@@ -143,7 +154,7 @@ export function differ(a, b) {
143
154
  if (!(b instanceof Array))
144
155
  return true;
145
156
  for (const [i, value] of a.entries())
146
- if (differ(value, b[i]))
157
+ if (deepDiff(value, b[i]))
147
158
  return true;
148
159
  return false;
149
160
  }
@@ -152,7 +163,7 @@ export function differ(a, b) {
152
163
  if (entriesA.length !== entriesB.length)
153
164
  return true;
154
165
  for (const [i, [key, value]] of entriesA.entries())
155
- if (key !== entriesB[i][0] || differ(value, entriesB[i][1]))
166
+ if (key !== entriesB[i][0] || deepDiff(value, entriesB[i][1]))
156
167
  return true;
157
168
  return false;
158
169
  }
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, Table, Transaction, Type } from "./db";
1
+ import { actions, Attribute, base, EntryBase, loaded, size, Table, Transaction, transaction, Type } from "./db";
2
+ export { Attribute, base, DB, deepCopy, deepDiff, 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", "attributes", "base", "class", "construct", "constructor", "defaultValue", "entry", "fieldName", "foreignKeys", "load"],
7
- ...["methods", "name", "postCommit", "postLoad", "postRemove", "postSave", "preCommit", "preLoad", "preRemove", "preSave", "primaryKey", "prototype", "save", "size", "tableName", "type"]
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, base, fieldName, size, tableName, type } = attribute;
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(size) {
48
+ Int(_size) {
47
49
  const message = "Sedentary.INT: 'size' argument: Wrong value, expected 2 or 4";
48
- size = size ? this.checkSize(size, message) : 4;
49
- if (size !== 2 && size !== 4)
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(size) {
67
+ VarChar(_size) {
66
68
  const message = "Sedentary.VARCHAR: 'size' argument: Wrong value, expected positive integer";
67
- size = size ? this.checkSize(size, message) : undefined;
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, attributes, options, methods) {
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 (!attributes)
212
- attributes = {};
213
- if (!(attributes instanceof Object))
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 (!methods)
238
- methods = {};
239
- if (!(methods instanceof Object))
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.attributes)
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(attributes).includes(primaryKey))
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(attributes).sort()) {
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 = attributes[attributeName];
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 (base === BigInt && typeof defaultValue !== "bigint")
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 (base === Date && !(defaultValue instanceof Date))
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 (base === Number && typeof defaultValue !== "number")
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 (base === String && typeof defaultValue !== "string")
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
- attr2field[attributeName] = fieldName;
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 = attributes;
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 attributes)
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 methods)
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 methods)
404
- if (method in attributes)
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 attributes) {
410
- if (attribute in parent.attributes)
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.methods)
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.attributes)
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.methods)
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 methods) {
425
- if (method in parent.attributes)
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, "attributes", { value: attributes });
490
+ Object.defineProperty(ret, attributes, { value: _attributes });
488
491
  Object.defineProperty(ret, "foreignKeys", { value: foreignKeys });
489
- Object.defineProperty(ret, "methods", { value: methods });
490
- Object.assign(ret.prototype, methods);
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", "base", "fieldName", "modelName", "size", "tableName", "type", "unique"])
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
  }
@@ -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, attributes: Record<string, string>, pk: Attribute<unknown, unknown>): (this: EntryBase & Record<string, unknown>) => Promise<number | false>;
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>;
@@ -118,5 +120,6 @@ export declare class Transaction {
118
120
  protected preCommit(): void;
119
121
  rollback(): Promise<void>;
120
122
  }
121
- export declare function differ(a: unknown, b: unknown): boolean;
123
+ export declare function deepCopy(o: unknown): any;
124
+ export declare function deepDiff(a: unknown, b: unknown): boolean;
122
125
  export {};
@@ -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, deepCopy, deepDiff, 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["methods"] : EntryBase;
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["attributes"] : {
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(size?: number): Type<number, unknown>;
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>(size?: number): Type<S, unknown>;
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
@@ -58,5 +58,5 @@
58
58
  }
59
59
  },
60
60
  "types": "./dist/types/index.d.ts",
61
- "version": "0.0.48"
61
+ "version": "0.0.51"
62
62
  }