sedentary 0.0.45 → 0.0.48

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,6 +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 = 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
+ exports.actions = Symbol("actions");
5
+ exports.loaded = Symbol("loaded");
6
+ exports.transaction = Symbol("transaction");
4
7
  class EntryBase {
5
8
  constructor(from) {
6
9
  if (from === "load")
@@ -12,9 +15,15 @@ class EntryBase {
12
15
  }
13
16
  }
14
17
  construct() { }
18
+ // eslint-disable-next-line @typescript-eslint/no-unused-vars
19
+ postCommit(actions) { }
15
20
  postLoad() { }
16
- postRemove() { }
17
- postSave() { }
21
+ // eslint-disable-next-line @typescript-eslint/no-unused-vars
22
+ postRemove(deletedRecords) { }
23
+ // eslint-disable-next-line @typescript-eslint/no-unused-vars
24
+ postSave(savedRecords) { }
25
+ // eslint-disable-next-line @typescript-eslint/no-unused-vars
26
+ preCommit(actions) { }
18
27
  preLoad() { }
19
28
  preRemove() { }
20
29
  preSave() { }
@@ -96,20 +105,60 @@ class Transaction {
96
105
  this.log = log;
97
106
  }
98
107
  addEntry(entry) {
99
- Object.defineProperty(entry, "tx", { configurable: true, value: this });
108
+ Object.defineProperty(entry, exports.transaction, { configurable: true, value: this });
100
109
  this.entries.push(entry);
101
110
  }
102
111
  clean() {
103
112
  const { entries } = this;
104
- for (const entry of entries)
105
- Object.defineProperty(entry, "tx", { configurable: true, value: null });
113
+ for (const entry of entries) {
114
+ Object.defineProperty(entry, exports.actions, { configurable: true, value: undefined });
115
+ Object.defineProperty(entry, exports.transaction, { configurable: true, value: undefined });
116
+ }
106
117
  this.entries = [];
107
118
  }
108
119
  async commit() {
120
+ const { entries } = this;
121
+ for (const entry of entries)
122
+ if (entry[exports.actions])
123
+ entry.postCommit(entry[exports.actions]);
109
124
  this.clean();
110
125
  }
126
+ preCommit() {
127
+ const { entries } = this;
128
+ for (const entry of entries)
129
+ if (entry[exports.actions])
130
+ entry.preCommit(entry[exports.actions]);
131
+ }
111
132
  async rollback() {
112
133
  this.clean();
113
134
  }
114
135
  }
115
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,10 +1,11 @@
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.Table = 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; } });
9
10
  Object.defineProperty(exports, "Table", { enumerable: true, get: function () { return db_2.Table; } });
10
11
  Object.defineProperty(exports, "Transaction", { enumerable: true, get: function () { return db_2.Transaction; } });
@@ -13,7 +14,7 @@ const operators = ["=", ">", "<", ">=", "<=", "<>", "IN", "IS NULL", "LIKE", "NO
13
14
  const allowedOption = ["indexes", "int8id", "parent", "primaryKey", "sync", "tableName"];
14
15
  const reservedNames = [
15
16
  ...["attr2field", "attributeName", "attributes", "base", "class", "construct", "constructor", "defaultValue", "entry", "fieldName", "foreignKeys", "load"],
16
- ...["loaded", "methods", "name", "postLoad", "postSave", "preLoad", "preSave", "primaryKey", "prototype", "save", "size", "tableName", "tx", "type"]
17
+ ...["methods", "name", "postCommit", "postLoad", "postRemove", "postSave", "preCommit", "preLoad", "preRemove", "preSave", "primaryKey", "prototype", "save", "size", "tableName", "type"]
17
18
  ];
18
19
  class Sedentary {
19
20
  constructor(options) {
@@ -65,6 +66,9 @@ class Sedentary {
65
66
  Number() {
66
67
  return new db_1.Type({ base: Number, type: "NUMBER" });
67
68
  }
69
+ None() {
70
+ return new db_1.Type({ base: undefined, type: "NONE" });
71
+ }
68
72
  VarChar(size) {
69
73
  const message = "Sedentary.VARCHAR: 'size' argument: Wrong value, expected positive integer";
70
74
  size = size ? this.checkSize(size, message) : undefined;
@@ -261,7 +265,7 @@ class Sedentary {
261
265
  const call = (defaultValue, fieldName, notNull, unique, func, message1, message2) => {
262
266
  if (func === this.FKey)
263
267
  throw new Error(`${message1} 'this.FKey' can't be used directly`);
264
- if (![this.Boolean, this.DateTime, this.Int, this.JSON, this.Int8, this.Number, this.VarChar].includes(func))
268
+ if (![this.Boolean, this.DateTime, this.Int, this.JSON, this.Int8, this.None, this.Number, this.VarChar].includes(func))
265
269
  throw new Error(`${message1} ${message2}`);
266
270
  return new db_1.Attribute({ attributeName, defaultValue, fieldName, modelName, notNull, tableName, unique, ...func() });
267
271
  };
@@ -491,24 +495,31 @@ class Sedentary {
491
495
  Object.defineProperty(ret, "foreignKeys", { value: foreignKeys });
492
496
  Object.defineProperty(ret, "methods", { value: methods });
493
497
  Object.assign(ret.prototype, methods);
498
+ const ensureActions = (entry) => {
499
+ if (!entry[db_1.actions])
500
+ Object.defineProperty(entry, db_1.actions, { configurable: true, value: [] });
501
+ return entry[db_1.actions];
502
+ };
494
503
  const remove = this.db.remove(tableName, pk);
495
504
  ret.prototype.remove = async function () {
496
- if (!this.loaded)
505
+ if (!this[db_1.loaded])
497
506
  throw new Error(`${modelName}.remove: Can't remove a never saved Entry`);
498
507
  this.preRemove();
499
- const ret = await remove.call(this);
500
- if (ret)
501
- this.postRemove();
502
- return ret;
508
+ const records = await remove.call(this);
509
+ this.postRemove(records);
510
+ if (this[db_1.transaction])
511
+ ensureActions(this).push({ action: "remove", records });
512
+ return records;
503
513
  };
504
514
  Object.defineProperty(ret.prototype.remove, "name", { value: modelName + ".remove" });
505
515
  const save = this.db.save(tableName, attr2field, pk);
506
516
  ret.prototype.save = async function () {
507
517
  this.preSave();
508
- const ret = await save.call(this);
509
- if (ret)
510
- this.postSave();
511
- return ret;
518
+ const records = await save.call(this);
519
+ this.postSave(records);
520
+ if (this[db_1.transaction])
521
+ ensureActions(this).push({ action: "save", records });
522
+ return records;
512
523
  };
513
524
  Object.defineProperty(ret.prototype.save, "name", { value: modelName + ".save" });
514
525
  for (const attribute of aArray)
package/dist/es/db.js CHANGED
@@ -1,3 +1,6 @@
1
+ export const actions = Symbol("actions");
2
+ export const loaded = Symbol("loaded");
3
+ export const transaction = Symbol("transaction");
1
4
  export class EntryBase {
2
5
  constructor(from) {
3
6
  if (from === "load")
@@ -9,9 +12,15 @@ export class EntryBase {
9
12
  }
10
13
  }
11
14
  construct() { }
15
+ // eslint-disable-next-line @typescript-eslint/no-unused-vars
16
+ postCommit(actions) { }
12
17
  postLoad() { }
13
- postRemove() { }
14
- postSave() { }
18
+ // eslint-disable-next-line @typescript-eslint/no-unused-vars
19
+ postRemove(deletedRecords) { }
20
+ // eslint-disable-next-line @typescript-eslint/no-unused-vars
21
+ postSave(savedRecords) { }
22
+ // eslint-disable-next-line @typescript-eslint/no-unused-vars
23
+ preCommit(actions) { }
15
24
  preLoad() { }
16
25
  preRemove() { }
17
26
  preSave() { }
@@ -92,19 +101,58 @@ export class Transaction {
92
101
  this.log = log;
93
102
  }
94
103
  addEntry(entry) {
95
- Object.defineProperty(entry, "tx", { configurable: true, value: this });
104
+ Object.defineProperty(entry, transaction, { configurable: true, value: this });
96
105
  this.entries.push(entry);
97
106
  }
98
107
  clean() {
99
108
  const { entries } = this;
100
- for (const entry of entries)
101
- Object.defineProperty(entry, "tx", { configurable: true, value: null });
109
+ for (const entry of entries) {
110
+ Object.defineProperty(entry, actions, { configurable: true, value: undefined });
111
+ Object.defineProperty(entry, transaction, { configurable: true, value: undefined });
112
+ }
102
113
  this.entries = [];
103
114
  }
104
115
  async commit() {
116
+ const { entries } = this;
117
+ for (const entry of entries)
118
+ if (entry[actions])
119
+ entry.postCommit(entry[actions]);
105
120
  this.clean();
106
121
  }
122
+ preCommit() {
123
+ const { entries } = this;
124
+ for (const entry of entries)
125
+ if (entry[actions])
126
+ entry.preCommit(entry[actions]);
127
+ }
107
128
  async rollback() {
108
129
  this.clean();
109
130
  }
110
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,10 +1,10 @@
1
- import { 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, Table, 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
- ...["loaded", "methods", "name", "postLoad", "postSave", "preLoad", "preSave", "primaryKey", "prototype", "save", "size", "tableName", "tx", "type"]
7
+ ...["methods", "name", "postCommit", "postLoad", "postRemove", "postSave", "preCommit", "preLoad", "preRemove", "preSave", "primaryKey", "prototype", "save", "size", "tableName", "type"]
8
8
  ];
9
9
  export class Sedentary {
10
10
  autoSync;
@@ -59,6 +59,9 @@ export class Sedentary {
59
59
  Number() {
60
60
  return new Type({ base: Number, type: "NUMBER" });
61
61
  }
62
+ None() {
63
+ return new Type({ base: undefined, type: "NONE" });
64
+ }
62
65
  VarChar(size) {
63
66
  const message = "Sedentary.VARCHAR: 'size' argument: Wrong value, expected positive integer";
64
67
  size = size ? this.checkSize(size, message) : undefined;
@@ -255,7 +258,7 @@ export class Sedentary {
255
258
  const call = (defaultValue, fieldName, notNull, unique, func, message1, message2) => {
256
259
  if (func === this.FKey)
257
260
  throw new Error(`${message1} 'this.FKey' can't be used directly`);
258
- 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))
259
262
  throw new Error(`${message1} ${message2}`);
260
263
  return new Attribute({ attributeName, defaultValue, fieldName, modelName, notNull, tableName, unique, ...func() });
261
264
  };
@@ -485,24 +488,31 @@ export class Sedentary {
485
488
  Object.defineProperty(ret, "foreignKeys", { value: foreignKeys });
486
489
  Object.defineProperty(ret, "methods", { value: methods });
487
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
+ };
488
496
  const remove = this.db.remove(tableName, pk);
489
497
  ret.prototype.remove = async function () {
490
- if (!this.loaded)
498
+ if (!this[loaded])
491
499
  throw new Error(`${modelName}.remove: Can't remove a never saved Entry`);
492
500
  this.preRemove();
493
- const ret = await remove.call(this);
494
- if (ret)
495
- this.postRemove();
496
- return ret;
501
+ const records = await remove.call(this);
502
+ this.postRemove(records);
503
+ if (this[transaction])
504
+ ensureActions(this).push({ action: "remove", records });
505
+ return records;
497
506
  };
498
507
  Object.defineProperty(ret.prototype.remove, "name", { value: modelName + ".remove" });
499
508
  const save = this.db.save(tableName, attr2field, pk);
500
509
  ret.prototype.save = async function () {
501
510
  this.preSave();
502
- const ret = await save.call(this);
503
- if (ret)
504
- this.postSave();
505
- return ret;
511
+ const records = await save.call(this);
512
+ this.postSave(records);
513
+ if (this[transaction])
514
+ ensureActions(this).push({ action: "save", records });
515
+ return records;
506
516
  };
507
517
  Object.defineProperty(ret.prototype.save, "name", { value: modelName + ".save" });
508
518
  for (const attribute of aArray)
@@ -1,9 +1,18 @@
1
+ export declare const actions: unique symbol;
2
+ export declare const loaded: unique symbol;
3
+ export declare const transaction: unique symbol;
4
+ export interface Action {
5
+ action: "remove" | "save";
6
+ records: number | false;
7
+ }
1
8
  export declare class EntryBase {
2
9
  constructor(from?: Partial<EntryBase>);
3
10
  construct(): void;
11
+ postCommit(actions: Action[]): void;
4
12
  postLoad(): void;
5
- postRemove(): void;
6
- postSave(): void;
13
+ postRemove(deletedRecords: number): void;
14
+ postSave(savedRecords: number | false): void;
15
+ preCommit(actions: Action[]): void;
7
16
  preLoad(): void;
8
17
  preRemove(): void;
9
18
  preSave(): void;
@@ -88,8 +97,8 @@ export declare abstract class DB<T extends Transaction> {
88
97
  abstract cancel(tableName: string): (where: string, tx?: Transaction) => Promise<number>;
89
98
  abstract escape(value: unknown): string;
90
99
  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[]>;
91
- abstract remove(tableName: string, pk: Attribute<unknown, unknown>): (this: EntryBase & Record<string, unknown>) => Promise<boolean>;
92
- abstract save(tableName: string, attributes: Record<string, string>, pk: Attribute<unknown, unknown>): (this: EntryBase & Record<string, unknown>) => Promise<boolean>;
100
+ 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>;
93
102
  abstract dropConstraints(table: Table): Promise<number[]>;
94
103
  abstract dropFields(table: Table): Promise<void>;
95
104
  abstract dropIndexes(table: Table, constraintIndexes: number[]): Promise<void>;
@@ -106,6 +115,8 @@ export declare class Transaction {
106
115
  addEntry(entry: EntryBase): void;
107
116
  clean(): void;
108
117
  commit(): Promise<void>;
118
+ protected preCommit(): void;
109
119
  rollback(): Promise<void>;
110
120
  }
121
+ export declare function differ(a: unknown, b: unknown): boolean;
111
122
  export {};
@@ -1,5 +1,5 @@
1
1
  import { Attribute, DB, EntryBase, ForeignKeyOptions, Transaction, Type } from "./db";
2
- export { Attribute, DB, EntryBase, ForeignKeyActions, ForeignKeyOptions, Index, Table, Transaction, Type } from "./db";
2
+ export { Action, Attribute, DB, differ, EntryBase, ForeignKeyActions, ForeignKeyOptions, Index, Table, 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.45"
61
+ "version": "0.0.48"
62
62
  }
package/dist/cjs/db.d.ts DELETED
@@ -1,111 +0,0 @@
1
- export declare class EntryBase {
2
- constructor(from?: Partial<EntryBase>);
3
- construct(): void;
4
- postLoad(): void;
5
- postRemove(): void;
6
- postSave(): void;
7
- preLoad(): void;
8
- preRemove(): void;
9
- preSave(): void;
10
- remove(): Promise<boolean>;
11
- save(): Promise<boolean>;
12
- }
13
- export declare type ForeignKeyActions = "cascade" | "no action" | "restrict" | "set default" | "set null";
14
- export interface ForeignKeyOptions {
15
- onDelete?: ForeignKeyActions;
16
- onUpdate?: ForeignKeyActions;
17
- }
18
- export interface Type<T, E> {
19
- base: unknown;
20
- entry?: E;
21
- native?: T;
22
- size?: number;
23
- type: string;
24
- foreignKey?: {
25
- attributeName: string;
26
- fieldName: string;
27
- options?: ForeignKeyOptions;
28
- tableName: string;
29
- };
30
- }
31
- export declare class Type<T, E> {
32
- constructor(from: Type<T, E>);
33
- }
34
- export interface Attribute<T, E> extends Type<T, E> {
35
- attributeName: string;
36
- defaultValue?: unknown;
37
- fieldName: string;
38
- modelName: string;
39
- notNull: boolean;
40
- tableName: string;
41
- unique?: boolean;
42
- }
43
- export declare class Attribute<T, E> extends Type<T, E> {
44
- constructor(from: Attribute<T, E>);
45
- }
46
- export interface Constraint {
47
- attribute: Attribute<unknown, unknown>;
48
- constraintName: string;
49
- type: "f" | "u";
50
- }
51
- export interface Index {
52
- fields: string[];
53
- indexName: string;
54
- type: "btree" | "hash";
55
- unique: boolean;
56
- }
57
- interface ITable {
58
- attributes: Attribute<unknown, unknown>[];
59
- autoIncrement: boolean;
60
- constraints: Constraint[];
61
- indexes: Index[];
62
- model: {
63
- load: (where: any, order?: string[], tx?: Transaction) => Promise<EntryBase[]>;
64
- };
65
- parent?: Attribute<unknown, unknown>;
66
- pk: Attribute<unknown, unknown>;
67
- sync: boolean;
68
- tableName: string;
69
- }
70
- declare const Table_base: new (defaults?: ITable | undefined) => ITable;
71
- export declare class Table extends Table_base {
72
- autoIncrementOwn?: boolean;
73
- oid?: number;
74
- findField(name: string): Attribute<unknown, unknown>;
75
- }
76
- export declare abstract class DB<T extends Transaction> {
77
- tables: Table[];
78
- protected log: (message: string) => void;
79
- protected sync: boolean;
80
- abstract connect(): Promise<void>;
81
- abstract end(): Promise<void>;
82
- constructor(log: (message: string) => void);
83
- findTable(name: string): Table;
84
- protected indexesEq(a: Index, b: Index): boolean;
85
- syncDataBase(): Promise<void>;
86
- protected syncLog(message: string): void;
87
- abstract begin(): Promise<T>;
88
- abstract cancel(tableName: string): (where: string, tx?: Transaction) => Promise<number>;
89
- abstract escape(value: unknown): string;
90
- 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[]>;
91
- abstract remove(tableName: string, pk: Attribute<unknown, unknown>): (this: EntryBase & Record<string, unknown>) => Promise<boolean>;
92
- abstract save(tableName: string, attributes: Record<string, string>, pk: Attribute<unknown, unknown>): (this: EntryBase & Record<string, unknown>) => Promise<boolean>;
93
- abstract dropConstraints(table: Table): Promise<number[]>;
94
- abstract dropFields(table: Table): Promise<void>;
95
- abstract dropIndexes(table: Table, constraintIndexes: number[]): Promise<void>;
96
- abstract syncConstraints(table: Table): Promise<void>;
97
- abstract syncFields(table: Table): Promise<void>;
98
- abstract syncIndexes(table: Table): Promise<void>;
99
- abstract syncSequence(table: Table): Promise<void>;
100
- abstract syncTable(table: Table): Promise<void>;
101
- }
102
- export declare class Transaction {
103
- private entries;
104
- protected log: (message: string) => void;
105
- constructor(log: (message: string) => void);
106
- addEntry(entry: EntryBase): void;
107
- clean(): void;
108
- commit(): Promise<void>;
109
- rollback(): Promise<void>;
110
- }
111
- export {};
@@ -1,143 +0,0 @@
1
- import { Attribute, DB, EntryBase, ForeignKeyOptions, Transaction, Type } from "./db";
2
- export { Attribute, DB, EntryBase, ForeignKeyActions, ForeignKeyOptions, Index, Table, Transaction, Type } from "./db";
3
- export declare type TypeDefinition<T, E> = (() => Type<T, E>) | Type<T, E>;
4
- export interface AttributeOptions<T, E> {
5
- defaultValue?: T;
6
- fieldName?: string;
7
- notNull?: boolean;
8
- type: TypeDefinition<T, E>;
9
- unique?: boolean;
10
- }
11
- export declare type AttributeDefinition<T, E> = TypeDefinition<T, E> | AttributeOptions<T, E>;
12
- export declare type AttributesDefinition = {
13
- [key: string]: AttributeDefinition<unknown, unknown>;
14
- };
15
- declare type ForeignKeysAttributes<T, k> = T extends AttributeDefinition<unknown, infer E> ? (E extends EntryBase ? k : never) : never;
16
- declare type ForeignKeys<A extends AttributesDefinition> = {
17
- [a in keyof A]?: ForeignKeysAttributes<A[a], a>;
18
- }[keyof A];
19
- declare type Native___<T> = T extends Type<infer N, unknown> ? N : never;
20
- declare type Native__<T> = T extends () => Type<infer N, infer E> ? Native___<Type<N, E>> : Native___<T>;
21
- declare type Native_<T, N, E> = T extends {
22
- notNull: true;
23
- } ? Native___<Type<N, E>> : Native___<Type<N, E>> | null;
24
- declare type Native<T> = T extends AttributeOptions<infer N, infer E> ? Native_<T, N, E> : Native__<T> | null;
25
- export declare type IndexAttributes = string[] | string;
26
- export interface IndexOptions {
27
- attributes: IndexAttributes;
28
- type?: "btree" | "hash";
29
- unique?: boolean;
30
- }
31
- export declare type IndexDefinition = IndexAttributes | IndexOptions;
32
- export declare type IndexesDefinition = {
33
- [key: string]: IndexDefinition;
34
- };
35
- interface BaseModelOptions {
36
- indexes?: IndexesDefinition;
37
- sync?: boolean;
38
- tableName?: string;
39
- }
40
- export interface ModelOptions extends BaseModelOptions {
41
- int8id?: boolean;
42
- parent?: Attribute<unknown, EntryBase>;
43
- primaryKey?: string;
44
- }
45
- declare type ConditionAttribute<T> = T | ["=" | ">" | "<" | ">=" | "<=" | "<>", T] | ["IN", T[]] | ["IS NULL"] | ["LIKE", string] | ["NOT"];
46
- declare type ConditionBase<A extends AttributesDefinition> = string | {
47
- [a in keyof A]?: ConditionAttribute<Native<A[a]>>;
48
- };
49
- declare type Condition<A extends AttributesDefinition> = ConditionBase<A> | ["NOT", Condition<A>] | ["AND", ...Condition<A>[]] | ["OR", ...Condition<A>[]];
50
- declare type Order_<A extends AttributesDefinition> = keyof A | `-${string & keyof A}`;
51
- declare type Order<A extends AttributesDefinition> = Order_<A> | Order_<A>[];
52
- declare type UnionToIntersection<U> = (U extends unknown ? (k: U) => void : never) extends (k: infer I) => void ? I : never;
53
- declare type IsUnion<T> = [T] extends [UnionToIntersection<T>] ? false : true;
54
- declare type BaseKeyType<B extends boolean> = IsUnion<B> extends true ? number : B extends true ? string : number;
55
- declare type KeyType<B extends boolean, P extends ModelStd> = P extends new () => EntryBase ? (P extends Attribute<infer T, EntryBase> ? T : never) : BaseKeyType<B>;
56
- declare type ForeignKey<A> = A extends AttributeDefinition<unknown, infer E> ? () => Promise<E> : never;
57
- declare type EntryBaseAttributes<A extends AttributesDefinition> = {
58
- [a in keyof A]: Native<A[a]>;
59
- };
60
- declare type EntryMethodsBase<P extends ModelStd> = P extends new () => EntryBase ? P["methods"] : EntryBase;
61
- declare type EntryMethodsFK<A extends AttributesDefinition> = {
62
- [a in ForeignKeys<A> & string as `${a}Load`]: ForeignKey<A[a]>;
63
- };
64
- declare type EntryMethods<A extends AttributesDefinition, P extends ModelStd> = keyof EntryMethodsFK<A> extends never ? EntryMethodsBase<P> : EntryMethodsBase<P> & EntryMethodsFK<A>;
65
- 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"] : {
67
- id: {
68
- notNull: true;
69
- type: Type<BaseKeyType<B>, unknown>;
70
- };
71
- }>;
72
- export interface ModelLoad<A extends AttributesDefinition, E extends EntryBase> {
73
- load(where: Condition<A>, order?: Order<A>, limit?: number, tx?: Transaction, lock?: boolean): Promise<E[]>;
74
- load(where: Condition<A>, order?: Order<A>, tx?: Transaction, lock?: boolean): Promise<E[]>;
75
- load(where: Condition<A>, limit?: number, tx?: Transaction, lock?: boolean): Promise<E[]>;
76
- load(where: Condition<A>, tx: Transaction, lock?: boolean): Promise<E[]>;
77
- cancel(where: Condition<A>, tx?: Transaction): Promise<number>;
78
- }
79
- 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;
81
- foreignKeys: Record<string, boolean>;
82
- methods: EM;
83
- parent?: ModelStd;
84
- tableName: string;
85
- } & {
86
- [a in keyof A]: Attribute<Native<A[a]>, E>;
87
- } & ModelLoad<A, E>;
88
- declare type Model<T, A extends AttributesDefinition, EM extends EntryBase> = ModelBase<T, A, EntryBaseAttributes<A>, EM, EntryBaseAttributes<A> & EM>;
89
- declare type ModelStd = Attribute<unknown, EntryBase> & {
90
- attributes: AttributesDefinition;
91
- foreignKeys: Record<string, boolean>;
92
- methods: EntryBase;
93
- parent?: ModelStd;
94
- };
95
- export declare type Entry<M> = M extends new () => infer E ? E : never;
96
- export declare type OrderBy<M> = M extends {
97
- load(where: unknown, order?: infer T): void;
98
- load(where: unknown, limit?: number): void;
99
- load(where: unknown, tx?: Transaction): void;
100
- } ? Exclude<T, undefined> : never;
101
- export declare type Where<M> = M extends {
102
- load(where: infer T): void;
103
- } ? T : never;
104
- export interface SedentaryOptions {
105
- autoSync?: boolean;
106
- log?: ((message: string) => void) | null;
107
- sync?: boolean;
108
- }
109
- export declare class Sedentary<D extends DB<T>, T extends Transaction> {
110
- protected autoSync: boolean;
111
- protected db: D;
112
- protected doSync: boolean;
113
- protected log: (message: string) => void;
114
- private models;
115
- constructor(options?: SedentaryOptions);
116
- Boolean(): Type<boolean, unknown>;
117
- DateTime(): Type<Date, unknown>;
118
- FKey<T, E extends EntryBase>(attribute: Attribute<T, E>, options?: ForeignKeyOptions): Type<T, E>;
119
- Int(size?: number): Type<number, unknown>;
120
- Int8(): Type<bigint, unknown>;
121
- JSON<T>(): Type<T, unknown>;
122
- Number(): Type<number, unknown>;
123
- VarChar<S extends string>(size?: number): Type<S, unknown>;
124
- private checkDB;
125
- private checkOrderBy;
126
- private checkSize;
127
- private createWhere;
128
- connect(sync?: boolean): Promise<void>;
129
- sync(): Promise<void>;
130
- end(): Promise<void>;
131
- begin(): Promise<T>;
132
- escape(value: unknown): string;
133
- model<A extends AttributesDefinition, B extends boolean, K extends string, P extends ModelStd, EM extends EntryMethods<A, P>>(modelName: string, attributes: A, options?: BaseModelOptions & {
134
- int8id?: B;
135
- parent?: P;
136
- primaryKey?: K | keyof A;
137
- }): Model<K extends keyof A ? Native<A[K]> : KeyType<B, P>, ModelAttributes<A, B, K, P>, EM>;
138
- model<A extends AttributesDefinition, B extends boolean, K extends string, P extends ModelStd, EA extends EntryBaseAttributes<ModelAttributes<A, B, K, P>>, EM extends EntryMethods<A, P>, M extends Record<string, <S extends M>(this: EA & EM & S, ...args: any[]) => void>>(modelName: string, attributes: A, options: BaseModelOptions & {
139
- int8id?: B;
140
- parent?: P;
141
- primaryKey?: K | keyof A;
142
- }, methods: M & Record<keyof M, (this: EA & EM & M, ...args: any[]) => void>): Model<K extends keyof A ? Native<A[K]> : KeyType<B, P>, ModelAttributes<A, B, K, P>, EM & M>;
143
- }
package/dist/es/db.d.ts DELETED
@@ -1,111 +0,0 @@
1
- export declare class EntryBase {
2
- constructor(from?: Partial<EntryBase>);
3
- construct(): void;
4
- postLoad(): void;
5
- postRemove(): void;
6
- postSave(): void;
7
- preLoad(): void;
8
- preRemove(): void;
9
- preSave(): void;
10
- remove(): Promise<boolean>;
11
- save(): Promise<boolean>;
12
- }
13
- export declare type ForeignKeyActions = "cascade" | "no action" | "restrict" | "set default" | "set null";
14
- export interface ForeignKeyOptions {
15
- onDelete?: ForeignKeyActions;
16
- onUpdate?: ForeignKeyActions;
17
- }
18
- export interface Type<T, E> {
19
- base: unknown;
20
- entry?: E;
21
- native?: T;
22
- size?: number;
23
- type: string;
24
- foreignKey?: {
25
- attributeName: string;
26
- fieldName: string;
27
- options?: ForeignKeyOptions;
28
- tableName: string;
29
- };
30
- }
31
- export declare class Type<T, E> {
32
- constructor(from: Type<T, E>);
33
- }
34
- export interface Attribute<T, E> extends Type<T, E> {
35
- attributeName: string;
36
- defaultValue?: unknown;
37
- fieldName: string;
38
- modelName: string;
39
- notNull: boolean;
40
- tableName: string;
41
- unique?: boolean;
42
- }
43
- export declare class Attribute<T, E> extends Type<T, E> {
44
- constructor(from: Attribute<T, E>);
45
- }
46
- export interface Constraint {
47
- attribute: Attribute<unknown, unknown>;
48
- constraintName: string;
49
- type: "f" | "u";
50
- }
51
- export interface Index {
52
- fields: string[];
53
- indexName: string;
54
- type: "btree" | "hash";
55
- unique: boolean;
56
- }
57
- interface ITable {
58
- attributes: Attribute<unknown, unknown>[];
59
- autoIncrement: boolean;
60
- constraints: Constraint[];
61
- indexes: Index[];
62
- model: {
63
- load: (where: any, order?: string[], tx?: Transaction) => Promise<EntryBase[]>;
64
- };
65
- parent?: Attribute<unknown, unknown>;
66
- pk: Attribute<unknown, unknown>;
67
- sync: boolean;
68
- tableName: string;
69
- }
70
- declare const Table_base: new (defaults?: ITable | undefined) => ITable;
71
- export declare class Table extends Table_base {
72
- autoIncrementOwn?: boolean;
73
- oid?: number;
74
- findField(name: string): Attribute<unknown, unknown>;
75
- }
76
- export declare abstract class DB<T extends Transaction> {
77
- tables: Table[];
78
- protected log: (message: string) => void;
79
- protected sync: boolean;
80
- abstract connect(): Promise<void>;
81
- abstract end(): Promise<void>;
82
- constructor(log: (message: string) => void);
83
- findTable(name: string): Table;
84
- protected indexesEq(a: Index, b: Index): boolean;
85
- syncDataBase(): Promise<void>;
86
- protected syncLog(message: string): void;
87
- abstract begin(): Promise<T>;
88
- abstract cancel(tableName: string): (where: string, tx?: Transaction) => Promise<number>;
89
- abstract escape(value: unknown): string;
90
- 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[]>;
91
- abstract remove(tableName: string, pk: Attribute<unknown, unknown>): (this: EntryBase & Record<string, unknown>) => Promise<boolean>;
92
- abstract save(tableName: string, attributes: Record<string, string>, pk: Attribute<unknown, unknown>): (this: EntryBase & Record<string, unknown>) => Promise<boolean>;
93
- abstract dropConstraints(table: Table): Promise<number[]>;
94
- abstract dropFields(table: Table): Promise<void>;
95
- abstract dropIndexes(table: Table, constraintIndexes: number[]): Promise<void>;
96
- abstract syncConstraints(table: Table): Promise<void>;
97
- abstract syncFields(table: Table): Promise<void>;
98
- abstract syncIndexes(table: Table): Promise<void>;
99
- abstract syncSequence(table: Table): Promise<void>;
100
- abstract syncTable(table: Table): Promise<void>;
101
- }
102
- export declare class Transaction {
103
- private entries;
104
- protected log: (message: string) => void;
105
- constructor(log: (message: string) => void);
106
- addEntry(entry: EntryBase): void;
107
- clean(): void;
108
- commit(): Promise<void>;
109
- rollback(): Promise<void>;
110
- }
111
- export {};
@@ -1,143 +0,0 @@
1
- import { Attribute, DB, EntryBase, ForeignKeyOptions, Transaction, Type } from "./db";
2
- export { Attribute, DB, EntryBase, ForeignKeyActions, ForeignKeyOptions, Index, Table, Transaction, Type } from "./db";
3
- export declare type TypeDefinition<T, E> = (() => Type<T, E>) | Type<T, E>;
4
- export interface AttributeOptions<T, E> {
5
- defaultValue?: T;
6
- fieldName?: string;
7
- notNull?: boolean;
8
- type: TypeDefinition<T, E>;
9
- unique?: boolean;
10
- }
11
- export declare type AttributeDefinition<T, E> = TypeDefinition<T, E> | AttributeOptions<T, E>;
12
- export declare type AttributesDefinition = {
13
- [key: string]: AttributeDefinition<unknown, unknown>;
14
- };
15
- declare type ForeignKeysAttributes<T, k> = T extends AttributeDefinition<unknown, infer E> ? (E extends EntryBase ? k : never) : never;
16
- declare type ForeignKeys<A extends AttributesDefinition> = {
17
- [a in keyof A]?: ForeignKeysAttributes<A[a], a>;
18
- }[keyof A];
19
- declare type Native___<T> = T extends Type<infer N, unknown> ? N : never;
20
- declare type Native__<T> = T extends () => Type<infer N, infer E> ? Native___<Type<N, E>> : Native___<T>;
21
- declare type Native_<T, N, E> = T extends {
22
- notNull: true;
23
- } ? Native___<Type<N, E>> : Native___<Type<N, E>> | null;
24
- declare type Native<T> = T extends AttributeOptions<infer N, infer E> ? Native_<T, N, E> : Native__<T> | null;
25
- export declare type IndexAttributes = string[] | string;
26
- export interface IndexOptions {
27
- attributes: IndexAttributes;
28
- type?: "btree" | "hash";
29
- unique?: boolean;
30
- }
31
- export declare type IndexDefinition = IndexAttributes | IndexOptions;
32
- export declare type IndexesDefinition = {
33
- [key: string]: IndexDefinition;
34
- };
35
- interface BaseModelOptions {
36
- indexes?: IndexesDefinition;
37
- sync?: boolean;
38
- tableName?: string;
39
- }
40
- export interface ModelOptions extends BaseModelOptions {
41
- int8id?: boolean;
42
- parent?: Attribute<unknown, EntryBase>;
43
- primaryKey?: string;
44
- }
45
- declare type ConditionAttribute<T> = T | ["=" | ">" | "<" | ">=" | "<=" | "<>", T] | ["IN", T[]] | ["IS NULL"] | ["LIKE", string] | ["NOT"];
46
- declare type ConditionBase<A extends AttributesDefinition> = string | {
47
- [a in keyof A]?: ConditionAttribute<Native<A[a]>>;
48
- };
49
- declare type Condition<A extends AttributesDefinition> = ConditionBase<A> | ["NOT", Condition<A>] | ["AND", ...Condition<A>[]] | ["OR", ...Condition<A>[]];
50
- declare type Order_<A extends AttributesDefinition> = keyof A | `-${string & keyof A}`;
51
- declare type Order<A extends AttributesDefinition> = Order_<A> | Order_<A>[];
52
- declare type UnionToIntersection<U> = (U extends unknown ? (k: U) => void : never) extends (k: infer I) => void ? I : never;
53
- declare type IsUnion<T> = [T] extends [UnionToIntersection<T>] ? false : true;
54
- declare type BaseKeyType<B extends boolean> = IsUnion<B> extends true ? number : B extends true ? string : number;
55
- declare type KeyType<B extends boolean, P extends ModelStd> = P extends new () => EntryBase ? (P extends Attribute<infer T, EntryBase> ? T : never) : BaseKeyType<B>;
56
- declare type ForeignKey<A> = A extends AttributeDefinition<unknown, infer E> ? () => Promise<E> : never;
57
- declare type EntryBaseAttributes<A extends AttributesDefinition> = {
58
- [a in keyof A]: Native<A[a]>;
59
- };
60
- declare type EntryMethodsBase<P extends ModelStd> = P extends new () => EntryBase ? P["methods"] : EntryBase;
61
- declare type EntryMethodsFK<A extends AttributesDefinition> = {
62
- [a in ForeignKeys<A> & string as `${a}Load`]: ForeignKey<A[a]>;
63
- };
64
- declare type EntryMethods<A extends AttributesDefinition, P extends ModelStd> = keyof EntryMethodsFK<A> extends never ? EntryMethodsBase<P> : EntryMethodsBase<P> & EntryMethodsFK<A>;
65
- 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"] : {
67
- id: {
68
- notNull: true;
69
- type: Type<BaseKeyType<B>, unknown>;
70
- };
71
- }>;
72
- export interface ModelLoad<A extends AttributesDefinition, E extends EntryBase> {
73
- load(where: Condition<A>, order?: Order<A>, limit?: number, tx?: Transaction, lock?: boolean): Promise<E[]>;
74
- load(where: Condition<A>, order?: Order<A>, tx?: Transaction, lock?: boolean): Promise<E[]>;
75
- load(where: Condition<A>, limit?: number, tx?: Transaction, lock?: boolean): Promise<E[]>;
76
- load(where: Condition<A>, tx: Transaction, lock?: boolean): Promise<E[]>;
77
- cancel(where: Condition<A>, tx?: Transaction): Promise<number>;
78
- }
79
- 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;
81
- foreignKeys: Record<string, boolean>;
82
- methods: EM;
83
- parent?: ModelStd;
84
- tableName: string;
85
- } & {
86
- [a in keyof A]: Attribute<Native<A[a]>, E>;
87
- } & ModelLoad<A, E>;
88
- declare type Model<T, A extends AttributesDefinition, EM extends EntryBase> = ModelBase<T, A, EntryBaseAttributes<A>, EM, EntryBaseAttributes<A> & EM>;
89
- declare type ModelStd = Attribute<unknown, EntryBase> & {
90
- attributes: AttributesDefinition;
91
- foreignKeys: Record<string, boolean>;
92
- methods: EntryBase;
93
- parent?: ModelStd;
94
- };
95
- export declare type Entry<M> = M extends new () => infer E ? E : never;
96
- export declare type OrderBy<M> = M extends {
97
- load(where: unknown, order?: infer T): void;
98
- load(where: unknown, limit?: number): void;
99
- load(where: unknown, tx?: Transaction): void;
100
- } ? Exclude<T, undefined> : never;
101
- export declare type Where<M> = M extends {
102
- load(where: infer T): void;
103
- } ? T : never;
104
- export interface SedentaryOptions {
105
- autoSync?: boolean;
106
- log?: ((message: string) => void) | null;
107
- sync?: boolean;
108
- }
109
- export declare class Sedentary<D extends DB<T>, T extends Transaction> {
110
- protected autoSync: boolean;
111
- protected db: D;
112
- protected doSync: boolean;
113
- protected log: (message: string) => void;
114
- private models;
115
- constructor(options?: SedentaryOptions);
116
- Boolean(): Type<boolean, unknown>;
117
- DateTime(): Type<Date, unknown>;
118
- FKey<T, E extends EntryBase>(attribute: Attribute<T, E>, options?: ForeignKeyOptions): Type<T, E>;
119
- Int(size?: number): Type<number, unknown>;
120
- Int8(): Type<bigint, unknown>;
121
- JSON<T>(): Type<T, unknown>;
122
- Number(): Type<number, unknown>;
123
- VarChar<S extends string>(size?: number): Type<S, unknown>;
124
- private checkDB;
125
- private checkOrderBy;
126
- private checkSize;
127
- private createWhere;
128
- connect(sync?: boolean): Promise<void>;
129
- sync(): Promise<void>;
130
- end(): Promise<void>;
131
- begin(): Promise<T>;
132
- escape(value: unknown): string;
133
- model<A extends AttributesDefinition, B extends boolean, K extends string, P extends ModelStd, EM extends EntryMethods<A, P>>(modelName: string, attributes: A, options?: BaseModelOptions & {
134
- int8id?: B;
135
- parent?: P;
136
- primaryKey?: K | keyof A;
137
- }): Model<K extends keyof A ? Native<A[K]> : KeyType<B, P>, ModelAttributes<A, B, K, P>, EM>;
138
- model<A extends AttributesDefinition, B extends boolean, K extends string, P extends ModelStd, EA extends EntryBaseAttributes<ModelAttributes<A, B, K, P>>, EM extends EntryMethods<A, P>, M extends Record<string, <S extends M>(this: EA & EM & S, ...args: any[]) => void>>(modelName: string, attributes: A, options: BaseModelOptions & {
139
- int8id?: B;
140
- parent?: P;
141
- primaryKey?: K | keyof A;
142
- }, methods: M & Record<keyof M, (this: EA & EM & M, ...args: any[]) => void>): Model<K extends keyof A ? Native<A[K]> : KeyType<B, P>, ModelAttributes<A, B, K, P>, EM & M>;
143
- }