sedentary 0.0.46 → 0.0.49

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,9 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- 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.loaded = exports.actions = void 0;
4
4
  exports.actions = Symbol("actions");
5
5
  exports.loaded = Symbol("loaded");
6
+ exports.transaction = Symbol("transaction");
6
7
  class EntryBase {
7
8
  constructor(from) {
8
9
  if (from === "load")
@@ -104,14 +105,14 @@ class Transaction {
104
105
  this.log = log;
105
106
  }
106
107
  addEntry(entry) {
107
- Object.defineProperty(entry, "tx", { configurable: true, value: this });
108
+ Object.defineProperty(entry, exports.transaction, { configurable: true, value: this });
108
109
  this.entries.push(entry);
109
110
  }
110
111
  clean() {
111
112
  const { entries } = this;
112
113
  for (const entry of entries) {
113
114
  Object.defineProperty(entry, exports.actions, { configurable: true, value: undefined });
114
- Object.defineProperty(entry, "tx", { configurable: true, value: undefined });
115
+ Object.defineProperty(entry, exports.transaction, { configurable: true, value: undefined });
115
116
  }
116
117
  this.entries = [];
117
118
  }
@@ -133,3 +134,31 @@ class Transaction {
133
134
  }
134
135
  }
135
136
  exports.Transaction = Transaction;
137
+ const sortedEntries = (obj) => Object.entries(obj).sort((entryA, entryB) => (entryA[0] > entryB[0] ? -1 : 1));
138
+ function differ(a, b) {
139
+ if (typeof a !== "object")
140
+ return a !== b;
141
+ if (typeof b !== "object")
142
+ return true;
143
+ if (a === null)
144
+ return b !== null;
145
+ if (b === null)
146
+ return true;
147
+ if (a instanceof Array) {
148
+ if (!(b instanceof Array))
149
+ return true;
150
+ for (const [i, value] of a.entries())
151
+ if (differ(value, b[i]))
152
+ return true;
153
+ return false;
154
+ }
155
+ const entriesA = sortedEntries(a);
156
+ const entriesB = sortedEntries(b);
157
+ if (entriesA.length !== entriesB.length)
158
+ return true;
159
+ for (const [i, [key, value]] of entriesA.entries())
160
+ if (key !== entriesB[i][0] || differ(value, entriesB[i][1]))
161
+ return true;
162
+ return false;
163
+ }
164
+ exports.differ = differ;
package/dist/cjs/index.js CHANGED
@@ -1,38 +1,22 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.Sedentary = exports.Type = exports.Transaction = exports.Table = exports.EntryBase = exports.DB = exports.Attribute = void 0;
3
+ exports.Sedentary = exports.Type = exports.transaction = exports.Transaction = exports.Table = exports.loaded = exports.EntryBase = exports.differ = exports.DB = 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
7
  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; } });
8
9
  Object.defineProperty(exports, "EntryBase", { enumerable: true, get: function () { return db_2.EntryBase; } });
10
+ Object.defineProperty(exports, "loaded", { enumerable: true, get: function () { return db_2.loaded; } });
9
11
  Object.defineProperty(exports, "Table", { enumerable: true, get: function () { return db_2.Table; } });
10
12
  Object.defineProperty(exports, "Transaction", { enumerable: true, get: function () { return db_2.Transaction; } });
13
+ Object.defineProperty(exports, "transaction", { enumerable: true, get: function () { return db_2.transaction; } });
11
14
  Object.defineProperty(exports, "Type", { enumerable: true, get: function () { return db_2.Type; } });
12
15
  const operators = ["=", ">", "<", ">=", "<=", "<>", "IN", "IS NULL", "LIKE", "NOT"];
13
16
  const allowedOption = ["indexes", "int8id", "parent", "primaryKey", "sync", "tableName"];
14
17
  const reservedNames = [
15
18
  ...["attr2field", "attributeName", "attributes", "base", "class", "construct", "constructor", "defaultValue", "entry", "fieldName", "foreignKeys", "load"],
16
- ...[
17
- "loaded",
18
- "methods",
19
- "name",
20
- "postCommit",
21
- "postLoad",
22
- "postRemove",
23
- "postSave",
24
- "preCommit",
25
- "preLoad",
26
- "preRemove",
27
- "preSave",
28
- "primaryKey",
29
- "prototype",
30
- "save",
31
- "size",
32
- "tableName",
33
- "tx",
34
- "type"
35
- ]
19
+ ...["methods", "name", "postCommit", "postLoad", "postRemove", "postSave", "preCommit", "preLoad", "preRemove", "preSave", "primaryKey", "prototype", "save", "size", "tableName", "type"]
36
20
  ];
37
21
  class Sedentary {
38
22
  constructor(options) {
@@ -84,6 +68,9 @@ class Sedentary {
84
68
  Number() {
85
69
  return new db_1.Type({ base: Number, type: "NUMBER" });
86
70
  }
71
+ None() {
72
+ return new db_1.Type({ base: undefined, type: "NONE" });
73
+ }
87
74
  VarChar(size) {
88
75
  const message = "Sedentary.VARCHAR: 'size' argument: Wrong value, expected positive integer";
89
76
  size = size ? this.checkSize(size, message) : undefined;
@@ -280,7 +267,7 @@ class Sedentary {
280
267
  const call = (defaultValue, fieldName, notNull, unique, func, message1, message2) => {
281
268
  if (func === this.FKey)
282
269
  throw new Error(`${message1} 'this.FKey' can't be used directly`);
283
- if (![this.Boolean, this.DateTime, this.Int, this.JSON, this.Int8, this.Number, this.VarChar].includes(func))
270
+ if (![this.Boolean, this.DateTime, this.Int, this.JSON, this.Int8, this.None, this.Number, this.VarChar].includes(func))
284
271
  throw new Error(`${message1} ${message2}`);
285
272
  return new db_1.Attribute({ attributeName, defaultValue, fieldName, modelName, notNull, tableName, unique, ...func() });
286
273
  };
@@ -510,18 +497,20 @@ class Sedentary {
510
497
  Object.defineProperty(ret, "foreignKeys", { value: foreignKeys });
511
498
  Object.defineProperty(ret, "methods", { value: methods });
512
499
  Object.assign(ret.prototype, methods);
500
+ const ensureActions = (entry) => {
501
+ if (!entry[db_1.actions])
502
+ Object.defineProperty(entry, db_1.actions, { configurable: true, value: [] });
503
+ return entry[db_1.actions];
504
+ };
513
505
  const remove = this.db.remove(tableName, pk);
514
506
  ret.prototype.remove = async function () {
515
- if (!this.loaded)
507
+ if (!this[db_1.loaded])
516
508
  throw new Error(`${modelName}.remove: Can't remove a never saved Entry`);
517
509
  this.preRemove();
518
510
  const records = await remove.call(this);
519
511
  this.postRemove(records);
520
- if (this.tx) {
521
- if (!this[db_1.actions])
522
- Object.defineProperty(this, db_1.actions, { configurable: true, value: [] });
523
- this[db_1.actions].push({ action: "remove", records });
524
- }
512
+ if (this[db_1.transaction])
513
+ ensureActions(this).push({ action: "remove", records });
525
514
  return records;
526
515
  };
527
516
  Object.defineProperty(ret.prototype.remove, "name", { value: modelName + ".remove" });
@@ -530,11 +519,8 @@ class Sedentary {
530
519
  this.preSave();
531
520
  const records = await save.call(this);
532
521
  this.postSave(records);
533
- if (this.tx) {
534
- if (!this[db_1.actions])
535
- Object.defineProperty(this, db_1.actions, { configurable: true, value: [] });
536
- this[db_1.actions].push({ action: "save", records });
537
- }
522
+ if (this[db_1.transaction])
523
+ ensureActions(this).push({ action: "save", records });
538
524
  return records;
539
525
  };
540
526
  Object.defineProperty(ret.prototype.save, "name", { value: modelName + ".save" });
package/dist/es/db.js CHANGED
@@ -1,5 +1,6 @@
1
1
  export const actions = Symbol("actions");
2
2
  export const loaded = Symbol("loaded");
3
+ export const transaction = Symbol("transaction");
3
4
  export class EntryBase {
4
5
  constructor(from) {
5
6
  if (from === "load")
@@ -100,14 +101,14 @@ export class Transaction {
100
101
  this.log = log;
101
102
  }
102
103
  addEntry(entry) {
103
- Object.defineProperty(entry, "tx", { configurable: true, value: this });
104
+ Object.defineProperty(entry, transaction, { configurable: true, value: this });
104
105
  this.entries.push(entry);
105
106
  }
106
107
  clean() {
107
108
  const { entries } = this;
108
109
  for (const entry of entries) {
109
110
  Object.defineProperty(entry, actions, { configurable: true, value: undefined });
110
- Object.defineProperty(entry, "tx", { configurable: true, value: undefined });
111
+ Object.defineProperty(entry, transaction, { configurable: true, value: undefined });
111
112
  }
112
113
  this.entries = [];
113
114
  }
@@ -128,3 +129,30 @@ export class Transaction {
128
129
  this.clean();
129
130
  }
130
131
  }
132
+ const sortedEntries = (obj) => Object.entries(obj).sort((entryA, entryB) => (entryA[0] > entryB[0] ? -1 : 1));
133
+ export function differ(a, b) {
134
+ if (typeof a !== "object")
135
+ return a !== b;
136
+ if (typeof b !== "object")
137
+ return true;
138
+ if (a === null)
139
+ return b !== null;
140
+ if (b === null)
141
+ return true;
142
+ if (a instanceof Array) {
143
+ if (!(b instanceof Array))
144
+ return true;
145
+ for (const [i, value] of a.entries())
146
+ if (differ(value, b[i]))
147
+ return true;
148
+ return false;
149
+ }
150
+ const entriesA = sortedEntries(a);
151
+ const entriesB = sortedEntries(b);
152
+ if (entriesA.length !== entriesB.length)
153
+ return true;
154
+ for (const [i, [key, value]] of entriesA.entries())
155
+ if (key !== entriesB[i][0] || differ(value, entriesB[i][1]))
156
+ return true;
157
+ return false;
158
+ }
package/dist/es/index.js CHANGED
@@ -1,29 +1,10 @@
1
- import { actions, Attribute, EntryBase, Table, Transaction, Type } from "./db";
2
- export { Attribute, DB, EntryBase, Table, Transaction, Type } from "./db";
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";
3
3
  const operators = ["=", ">", "<", ">=", "<=", "<>", "IN", "IS NULL", "LIKE", "NOT"];
4
4
  const allowedOption = ["indexes", "int8id", "parent", "primaryKey", "sync", "tableName"];
5
5
  const reservedNames = [
6
6
  ...["attr2field", "attributeName", "attributes", "base", "class", "construct", "constructor", "defaultValue", "entry", "fieldName", "foreignKeys", "load"],
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
- ]
7
+ ...["methods", "name", "postCommit", "postLoad", "postRemove", "postSave", "preCommit", "preLoad", "preRemove", "preSave", "primaryKey", "prototype", "save", "size", "tableName", "type"]
27
8
  ];
28
9
  export class Sedentary {
29
10
  autoSync;
@@ -78,6 +59,9 @@ export class Sedentary {
78
59
  Number() {
79
60
  return new Type({ base: Number, type: "NUMBER" });
80
61
  }
62
+ None() {
63
+ return new Type({ base: undefined, type: "NONE" });
64
+ }
81
65
  VarChar(size) {
82
66
  const message = "Sedentary.VARCHAR: 'size' argument: Wrong value, expected positive integer";
83
67
  size = size ? this.checkSize(size, message) : undefined;
@@ -274,7 +258,7 @@ export class Sedentary {
274
258
  const call = (defaultValue, fieldName, notNull, unique, func, message1, message2) => {
275
259
  if (func === this.FKey)
276
260
  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))
261
+ if (![this.Boolean, this.DateTime, this.Int, this.JSON, this.Int8, this.None, this.Number, this.VarChar].includes(func))
278
262
  throw new Error(`${message1} ${message2}`);
279
263
  return new Attribute({ attributeName, defaultValue, fieldName, modelName, notNull, tableName, unique, ...func() });
280
264
  };
@@ -504,18 +488,20 @@ export class Sedentary {
504
488
  Object.defineProperty(ret, "foreignKeys", { value: foreignKeys });
505
489
  Object.defineProperty(ret, "methods", { value: methods });
506
490
  Object.assign(ret.prototype, methods);
491
+ const ensureActions = (entry) => {
492
+ if (!entry[actions])
493
+ Object.defineProperty(entry, actions, { configurable: true, value: [] });
494
+ return entry[actions];
495
+ };
507
496
  const remove = this.db.remove(tableName, pk);
508
497
  ret.prototype.remove = async function () {
509
- if (!this.loaded)
498
+ if (!this[loaded])
510
499
  throw new Error(`${modelName}.remove: Can't remove a never saved Entry`);
511
500
  this.preRemove();
512
501
  const records = await remove.call(this);
513
502
  this.postRemove(records);
514
- if (this.tx) {
515
- if (!this[actions])
516
- Object.defineProperty(this, actions, { configurable: true, value: [] });
517
- this[actions].push({ action: "remove", records });
518
- }
503
+ if (this[transaction])
504
+ ensureActions(this).push({ action: "remove", records });
519
505
  return records;
520
506
  };
521
507
  Object.defineProperty(ret.prototype.remove, "name", { value: modelName + ".remove" });
@@ -524,11 +510,8 @@ export class Sedentary {
524
510
  this.preSave();
525
511
  const records = await save.call(this);
526
512
  this.postSave(records);
527
- if (this.tx) {
528
- if (!this[actions])
529
- Object.defineProperty(this, actions, { configurable: true, value: [] });
530
- this[actions].push({ action: "save", records });
531
- }
513
+ if (this[transaction])
514
+ ensureActions(this).push({ action: "save", records });
532
515
  return records;
533
516
  };
534
517
  Object.defineProperty(ret.prototype.save, "name", { value: modelName + ".save" });
@@ -1,5 +1,6 @@
1
1
  export declare const actions: unique symbol;
2
2
  export declare const loaded: unique symbol;
3
+ export declare const transaction: unique symbol;
3
4
  export interface Action {
4
5
  action: "remove" | "save";
5
6
  records: number | false;
@@ -117,4 +118,5 @@ export declare class Transaction {
117
118
  protected preCommit(): void;
118
119
  rollback(): Promise<void>;
119
120
  }
121
+ export declare function differ(a: unknown, b: unknown): boolean;
120
122
  export {};
@@ -1,5 +1,5 @@
1
1
  import { Attribute, DB, EntryBase, ForeignKeyOptions, Transaction, Type } from "./db";
2
- export { Action, Attribute, DB, EntryBase, ForeignKeyActions, ForeignKeyOptions, Index, Table, Transaction, Type } from "./db";
2
+ export { Action, Attribute, DB, differ, EntryBase, ForeignKeyActions, ForeignKeyOptions, Index, loaded, 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;
@@ -120,6 +120,7 @@ export declare class Sedentary<D extends DB<T>, T extends Transaction> {
120
120
  Int8(): Type<bigint, unknown>;
121
121
  JSON<T>(): Type<T, unknown>;
122
122
  Number(): Type<number, unknown>;
123
+ None<T>(): Type<T, unknown>;
123
124
  VarChar<S extends string>(size?: number): Type<S, unknown>;
124
125
  private checkDB;
125
126
  private checkOrderBy;
package/package.json CHANGED
@@ -58,5 +58,5 @@
58
58
  }
59
59
  },
60
60
  "types": "./dist/types/index.d.ts",
61
- "version": "0.0.46"
61
+ "version": "0.0.49"
62
62
  }