sedentary 0.0.29 → 0.0.32

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/README.md CHANGED
@@ -97,8 +97,8 @@ The full documentation is on [sedentary.readthedocs.io](https://sedentary.readth
97
97
 
98
98
  Requires:
99
99
 
100
- - Node.js: **v12**
101
- - TypeScript: **v4.1** (or none if used in a JavaScript project).
100
+ - Node.js: **v14**
101
+ - TypeScript: **v4.6** (or none if used in a JavaScript project).
102
102
 
103
103
  The package is tested under [all Node.js versions](https://app.travis-ci.com/github/iccicci/sedentary)
104
104
  currently supported accordingly to [Node.js Release](https://github.com/nodejs/Release#readme).
package/dist/cjs/db.js CHANGED
@@ -13,9 +13,14 @@ class EntryBase {
13
13
  }
14
14
  construct() { }
15
15
  postLoad() { }
16
+ postRemove() { }
16
17
  postSave() { }
17
18
  preLoad() { }
19
+ preRemove() { }
18
20
  preSave() { }
21
+ async remove() {
22
+ return false;
23
+ }
19
24
  async save() {
20
25
  return false;
21
26
  }
@@ -86,16 +91,18 @@ class DB {
86
91
  }
87
92
  exports.DB = DB;
88
93
  class Transaction {
89
- constructor() {
94
+ constructor(log) {
90
95
  this.entries = [];
96
+ this.log = log;
91
97
  }
92
98
  addEntry(entry) {
99
+ Object.defineProperty(entry, "tx", { configurable: true, value: this });
93
100
  this.entries.push(entry);
94
101
  }
95
102
  clean() {
96
103
  const { entries } = this;
97
104
  for (const entry of entries)
98
- delete entry.tx;
105
+ Object.defineProperty(entry, "tx", { configurable: true, value: null });
99
106
  this.entries = [];
100
107
  }
101
108
  async commit() {
package/dist/cjs/index.js CHANGED
@@ -419,19 +419,26 @@ class Sedentary {
419
419
  };
420
420
  checkParent(parent);
421
421
  const ret = class extends (parent || db_1.EntryBase) {
422
+ constructor(from, tx) {
423
+ super(from);
424
+ if (tx)
425
+ tx.addEntry(this);
426
+ }
422
427
  };
423
428
  const table = new db_1.Table({ autoIncrement, constraints, attributes: aarray, indexes: iarray, model: ret, parent, pk, sync, tableName });
424
429
  this.db.tables.push(table);
425
430
  const load_ = this.db.load(tableName, attr2field, pk, ret, table);
426
- const load = async (where, order, tx) => {
431
+ const load = async (where, order, tx, lock) => {
427
432
  if (order instanceof db_1.Transaction) {
433
+ if (typeof tx === "boolean" && tx)
434
+ lock = true;
428
435
  tx = order;
429
436
  order = undefined;
430
437
  }
431
438
  if (!this.checkOrderBy(order, attr2field, modelName))
432
439
  throw new Error(`${modelName}.load: 'order' argument: Wrong type, expected 'string[]'`);
433
440
  const [str] = this.createWhere(modelName, attr2field, where);
434
- const ret = await load_(str, order, tx);
441
+ const ret = await load_(str, order, tx, lock);
435
442
  return ret;
436
443
  };
437
444
  Object.defineProperty(load, "name", { value: modelName + ".load" });
@@ -442,6 +449,17 @@ class Sedentary {
442
449
  Object.defineProperty(ret, "foreignKeys", { value: foreignKeys });
443
450
  Object.defineProperty(ret, "methods", { value: methods });
444
451
  Object.assign(ret.prototype, methods);
452
+ const remove = this.db.remove(tableName, pk);
453
+ ret.prototype.remove = async function () {
454
+ if (!this.loaded)
455
+ throw new Error(`${modelName}.remove: Can't remove a never saved Entry`);
456
+ this.preRemove();
457
+ const ret = await remove.call(this);
458
+ if (ret)
459
+ this.postRemove();
460
+ return ret;
461
+ };
462
+ Object.defineProperty(ret.prototype.remove, "name", { value: modelName + ".remove" });
445
463
  const save = this.db.save(tableName, attr2field, pk);
446
464
  ret.prototype.save = async function () {
447
465
  this.preSave();
package/dist/es/db.js CHANGED
@@ -10,9 +10,14 @@ export class EntryBase {
10
10
  }
11
11
  construct() { }
12
12
  postLoad() { }
13
+ postRemove() { }
13
14
  postSave() { }
14
15
  preLoad() { }
16
+ preRemove() { }
15
17
  preSave() { }
18
+ async remove() {
19
+ return false;
20
+ }
16
21
  async save() {
17
22
  return false;
18
23
  }
@@ -82,13 +87,18 @@ export class DB {
82
87
  }
83
88
  export class Transaction {
84
89
  entries = [];
90
+ log;
91
+ constructor(log) {
92
+ this.log = log;
93
+ }
85
94
  addEntry(entry) {
95
+ Object.defineProperty(entry, "tx", { configurable: true, value: this });
86
96
  this.entries.push(entry);
87
97
  }
88
98
  clean() {
89
99
  const { entries } = this;
90
100
  for (const entry of entries)
91
- delete entry.tx;
101
+ Object.defineProperty(entry, "tx", { configurable: true, value: null });
92
102
  this.entries = [];
93
103
  }
94
104
  async commit() {
package/dist/es/index.js CHANGED
@@ -413,19 +413,26 @@ export class Sedentary {
413
413
  };
414
414
  checkParent(parent);
415
415
  const ret = class extends (parent || EntryBase) {
416
+ constructor(from, tx) {
417
+ super(from);
418
+ if (tx)
419
+ tx.addEntry(this);
420
+ }
416
421
  };
417
422
  const table = new Table({ autoIncrement, constraints, attributes: aarray, indexes: iarray, model: ret, parent, pk, sync, tableName });
418
423
  this.db.tables.push(table);
419
424
  const load_ = this.db.load(tableName, attr2field, pk, ret, table);
420
- const load = async (where, order, tx) => {
425
+ const load = async (where, order, tx, lock) => {
421
426
  if (order instanceof Transaction) {
427
+ if (typeof tx === "boolean" && tx)
428
+ lock = true;
422
429
  tx = order;
423
430
  order = undefined;
424
431
  }
425
432
  if (!this.checkOrderBy(order, attr2field, modelName))
426
433
  throw new Error(`${modelName}.load: 'order' argument: Wrong type, expected 'string[]'`);
427
434
  const [str] = this.createWhere(modelName, attr2field, where);
428
- const ret = await load_(str, order, tx);
435
+ const ret = await load_(str, order, tx, lock);
429
436
  return ret;
430
437
  };
431
438
  Object.defineProperty(load, "name", { value: modelName + ".load" });
@@ -436,6 +443,17 @@ export class Sedentary {
436
443
  Object.defineProperty(ret, "foreignKeys", { value: foreignKeys });
437
444
  Object.defineProperty(ret, "methods", { value: methods });
438
445
  Object.assign(ret.prototype, methods);
446
+ const remove = this.db.remove(tableName, pk);
447
+ ret.prototype.remove = async function () {
448
+ if (!this.loaded)
449
+ throw new Error(`${modelName}.remove: Can't remove a never saved Entry`);
450
+ this.preRemove();
451
+ const ret = await remove.call(this);
452
+ if (ret)
453
+ this.postRemove();
454
+ return ret;
455
+ };
456
+ Object.defineProperty(ret.prototype.remove, "name", { value: modelName + ".remove" });
439
457
  const save = this.db.save(tableName, attr2field, pk);
440
458
  ret.prototype.save = async function () {
441
459
  this.preSave();
@@ -3,9 +3,12 @@ export declare class EntryBase {
3
3
  constructor(from?: Partial<EntryBase>);
4
4
  construct(): void;
5
5
  postLoad(): void;
6
+ postRemove(): void;
6
7
  postSave(): void;
7
8
  preLoad(): void;
9
+ preRemove(): void;
8
10
  preSave(): void;
11
+ remove(): Promise<boolean>;
9
12
  save(): Promise<boolean>;
10
13
  }
11
14
  export declare type ForeignKeyActions = "cascade" | "no action" | "restrict" | "set default" | "set null";
@@ -84,7 +87,8 @@ export declare abstract class DB<T extends Transaction> {
84
87
  protected syncLog(message: string): void;
85
88
  abstract begin(): Promise<T>;
86
89
  abstract escape(value: Natural): string;
87
- abstract load(tableName: string, attributes: Record<string, string>, pk: Attribute<Natural, unknown>, model: new () => EntryBase, table: Table): (where: string, order?: string[], tx?: Transaction) => Promise<EntryBase[]>;
90
+ abstract load(tableName: string, attributes: Record<string, string>, pk: Attribute<Natural, unknown>, model: new () => EntryBase, table: Table): (where: string, order?: string[], tx?: Transaction, lock?: boolean) => Promise<EntryBase[]>;
91
+ abstract remove(tableName: string, pk: Attribute<Natural, unknown>): (this: EntryBase & Record<string, Natural>) => Promise<boolean>;
88
92
  abstract save(tableName: string, attributes: Record<string, string>, pk: Attribute<Natural, unknown>): (this: EntryBase & Record<string, Natural>) => Promise<boolean>;
89
93
  abstract dropConstraints(table: Table): Promise<number[]>;
90
94
  abstract dropFields(table: Table): Promise<void>;
@@ -97,6 +101,8 @@ export declare abstract class DB<T extends Transaction> {
97
101
  }
98
102
  export declare class Transaction {
99
103
  private entries;
104
+ protected log: (message: string) => void;
105
+ constructor(log: (message: string) => void);
100
106
  addEntry(entry: EntryBase): void;
101
107
  clean(): void;
102
108
  commit(): Promise<void>;
@@ -62,12 +62,12 @@ declare type ModelAttributesIf<A extends AttributesDefinition, T> = keyof A exte
62
62
  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"] : {
63
63
  id: Type<BaseKeyType<B>, unknown>;
64
64
  }>;
65
- interface ModelLoad<A extends AttributesDefinition, E extends EntryBase> {
65
+ export interface ModelLoad<A extends AttributesDefinition, E extends EntryBase> {
66
66
  attributes: A;
67
- load(where: Condition<A>, order?: Order<A>, tx?: Transaction): Promise<E[]>;
68
- load(where: Condition<A>, tx: Transaction): Promise<E[]>;
67
+ load(where: Condition<A>, order?: Order<A>, tx?: Transaction, lock?: boolean): Promise<E[]>;
68
+ load(where: Condition<A>, tx: Transaction, lock?: boolean): Promise<E[]>;
69
69
  }
70
- declare type ModelBase<N extends Natural, A extends AttributesDefinition, EA extends Record<string, Natural | undefined>, EM extends EntryBase, E extends EntryBase> = (new (from?: EA) => E) & Attribute<N, E> & {
70
+ declare type ModelBase<N extends Natural, A extends AttributesDefinition, EA extends Record<string, Natural | undefined>, EM extends EntryBase, E extends EntryBase> = (new (from?: EA, tx?: Transaction) => E) & Attribute<N, E> & {
71
71
  foreignKeys: Record<string, boolean>;
72
72
  methods: EM;
73
73
  parent?: ModelStd;
@@ -100,7 +100,7 @@ export declare class Sedentary<D extends DB<T>, T extends Transaction> {
100
100
  protected autoSync: boolean;
101
101
  protected db: D;
102
102
  protected doSync: boolean;
103
- protected log: (...data: unknown[]) => void;
103
+ protected log: (message: string) => void;
104
104
  private models;
105
105
  constructor(options?: SedentaryOptions);
106
106
  DATETIME(): Type<Date, unknown>;
package/package.json CHANGED
@@ -9,21 +9,21 @@
9
9
  "description": "The ORM which never needs to migrate",
10
10
  "devDependencies": {
11
11
  "@types/mocha": "9.1.0",
12
- "@types/node": "17.0.19",
12
+ "@types/node": "17.0.23",
13
13
  "@types/yamljs": "0.2.31",
14
- "@typescript-eslint/eslint-plugin": "5.12.1",
15
- "@typescript-eslint/parser": "5.12.1",
16
- "eslint": "8.9.0",
17
- "mocha": "9.2.1",
14
+ "@typescript-eslint/eslint-plugin": "5.17.0",
15
+ "@typescript-eslint/parser": "5.17.0",
16
+ "eslint": "8.12.0",
17
+ "mocha": "9.2.2",
18
18
  "nyc": "15.1.0",
19
- "prettier": "2.5.1",
20
- "ts-node": "10.5.0",
19
+ "prettier": "2.6.1",
20
+ "ts-node": "10.7.0",
21
21
  "tsd": "0.19.1",
22
- "typescript": "4.5.5",
22
+ "typescript": "4.6.3",
23
23
  "yamljs": "0.3.0"
24
24
  },
25
25
  "engines": {
26
- "node": ">=12.0"
26
+ "node": ">=14.0"
27
27
  },
28
28
  "funding": {
29
29
  "url": "https://blockchain.info/address/1Md9WFAHrXTb3yPBwQWmUfv2RmzrtbHioB"
@@ -79,5 +79,5 @@
79
79
  }
80
80
  },
81
81
  "types": "./dist/types/index.d.ts",
82
- "version": "0.0.29"
82
+ "version": "0.0.32"
83
83
  }