sedentary 0.0.49 → 0.0.52

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