sedentary 0.0.45 → 0.0.46

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,8 @@
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.Transaction = exports.DB = exports.Table = exports.Attribute = exports.Type = exports.EntryBase = exports.loaded = exports.actions = void 0;
4
+ exports.actions = Symbol("actions");
5
+ exports.loaded = Symbol("loaded");
4
6
  class EntryBase {
5
7
  constructor(from) {
6
8
  if (from === "load")
@@ -12,9 +14,15 @@ class EntryBase {
12
14
  }
13
15
  }
14
16
  construct() { }
17
+ // eslint-disable-next-line @typescript-eslint/no-unused-vars
18
+ postCommit(actions) { }
15
19
  postLoad() { }
16
- postRemove() { }
17
- postSave() { }
20
+ // eslint-disable-next-line @typescript-eslint/no-unused-vars
21
+ postRemove(deletedRecords) { }
22
+ // eslint-disable-next-line @typescript-eslint/no-unused-vars
23
+ postSave(savedRecords) { }
24
+ // eslint-disable-next-line @typescript-eslint/no-unused-vars
25
+ preCommit(actions) { }
18
26
  preLoad() { }
19
27
  preRemove() { }
20
28
  preSave() { }
@@ -101,13 +109,25 @@ class Transaction {
101
109
  }
102
110
  clean() {
103
111
  const { entries } = this;
104
- for (const entry of entries)
105
- Object.defineProperty(entry, "tx", { configurable: true, value: null });
112
+ for (const entry of entries) {
113
+ Object.defineProperty(entry, exports.actions, { configurable: true, value: undefined });
114
+ Object.defineProperty(entry, "tx", { configurable: true, value: undefined });
115
+ }
106
116
  this.entries = [];
107
117
  }
108
118
  async commit() {
119
+ const { entries } = this;
120
+ for (const entry of entries)
121
+ if (entry[exports.actions])
122
+ entry.postCommit(entry[exports.actions]);
109
123
  this.clean();
110
124
  }
125
+ preCommit() {
126
+ const { entries } = this;
127
+ for (const entry of entries)
128
+ if (entry[exports.actions])
129
+ entry.preCommit(entry[exports.actions]);
130
+ }
111
131
  async rollback() {
112
132
  this.clean();
113
133
  }
package/dist/cjs/index.js CHANGED
@@ -13,7 +13,26 @@ const operators = ["=", ">", "<", ">=", "<=", "<>", "IN", "IS NULL", "LIKE", "NO
13
13
  const allowedOption = ["indexes", "int8id", "parent", "primaryKey", "sync", "tableName"];
14
14
  const reservedNames = [
15
15
  ...["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"]
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
+ ]
17
36
  ];
18
37
  class Sedentary {
19
38
  constructor(options) {
@@ -496,19 +515,27 @@ class Sedentary {
496
515
  if (!this.loaded)
497
516
  throw new Error(`${modelName}.remove: Can't remove a never saved Entry`);
498
517
  this.preRemove();
499
- const ret = await remove.call(this);
500
- if (ret)
501
- this.postRemove();
502
- return ret;
518
+ const records = await remove.call(this);
519
+ 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
+ }
525
+ return records;
503
526
  };
504
527
  Object.defineProperty(ret.prototype.remove, "name", { value: modelName + ".remove" });
505
528
  const save = this.db.save(tableName, attr2field, pk);
506
529
  ret.prototype.save = async function () {
507
530
  this.preSave();
508
- const ret = await save.call(this);
509
- if (ret)
510
- this.postSave();
511
- return ret;
531
+ const records = await save.call(this);
532
+ 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
+ }
538
+ return records;
512
539
  };
513
540
  Object.defineProperty(ret.prototype.save, "name", { value: modelName + ".save" });
514
541
  for (const attribute of aArray)
package/dist/es/db.js CHANGED
@@ -1,3 +1,5 @@
1
+ export const actions = Symbol("actions");
2
+ export const loaded = Symbol("loaded");
1
3
  export class EntryBase {
2
4
  constructor(from) {
3
5
  if (from === "load")
@@ -9,9 +11,15 @@ export class EntryBase {
9
11
  }
10
12
  }
11
13
  construct() { }
14
+ // eslint-disable-next-line @typescript-eslint/no-unused-vars
15
+ postCommit(actions) { }
12
16
  postLoad() { }
13
- postRemove() { }
14
- postSave() { }
17
+ // eslint-disable-next-line @typescript-eslint/no-unused-vars
18
+ postRemove(deletedRecords) { }
19
+ // eslint-disable-next-line @typescript-eslint/no-unused-vars
20
+ postSave(savedRecords) { }
21
+ // eslint-disable-next-line @typescript-eslint/no-unused-vars
22
+ preCommit(actions) { }
15
23
  preLoad() { }
16
24
  preRemove() { }
17
25
  preSave() { }
@@ -97,13 +105,25 @@ export class Transaction {
97
105
  }
98
106
  clean() {
99
107
  const { entries } = this;
100
- for (const entry of entries)
101
- Object.defineProperty(entry, "tx", { configurable: true, value: null });
108
+ for (const entry of entries) {
109
+ Object.defineProperty(entry, actions, { configurable: true, value: undefined });
110
+ Object.defineProperty(entry, "tx", { configurable: true, value: undefined });
111
+ }
102
112
  this.entries = [];
103
113
  }
104
114
  async commit() {
115
+ const { entries } = this;
116
+ for (const entry of entries)
117
+ if (entry[actions])
118
+ entry.postCommit(entry[actions]);
105
119
  this.clean();
106
120
  }
121
+ preCommit() {
122
+ const { entries } = this;
123
+ for (const entry of entries)
124
+ if (entry[actions])
125
+ entry.preCommit(entry[actions]);
126
+ }
107
127
  async rollback() {
108
128
  this.clean();
109
129
  }
package/dist/es/index.js CHANGED
@@ -1,10 +1,29 @@
1
- import { Attribute, EntryBase, Table, Transaction, Type } from "./db";
1
+ import { actions, Attribute, EntryBase, Table, Transaction, Type } from "./db";
2
2
  export { Attribute, DB, 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
+ ...[
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
+ ]
8
27
  ];
9
28
  export class Sedentary {
10
29
  autoSync;
@@ -490,19 +509,27 @@ export class Sedentary {
490
509
  if (!this.loaded)
491
510
  throw new Error(`${modelName}.remove: Can't remove a never saved Entry`);
492
511
  this.preRemove();
493
- const ret = await remove.call(this);
494
- if (ret)
495
- this.postRemove();
496
- return ret;
512
+ const records = await remove.call(this);
513
+ 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
+ }
519
+ return records;
497
520
  };
498
521
  Object.defineProperty(ret.prototype.remove, "name", { value: modelName + ".remove" });
499
522
  const save = this.db.save(tableName, attr2field, pk);
500
523
  ret.prototype.save = async function () {
501
524
  this.preSave();
502
- const ret = await save.call(this);
503
- if (ret)
504
- this.postSave();
505
- return ret;
525
+ const records = await save.call(this);
526
+ 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
+ }
532
+ return records;
506
533
  };
507
534
  Object.defineProperty(ret.prototype.save, "name", { value: modelName + ".save" });
508
535
  for (const attribute of aArray)
@@ -1,9 +1,17 @@
1
+ export declare const actions: unique symbol;
2
+ export declare const loaded: unique symbol;
3
+ export interface Action {
4
+ action: "remove" | "save";
5
+ records: number | false;
6
+ }
1
7
  export declare class EntryBase {
2
8
  constructor(from?: Partial<EntryBase>);
3
9
  construct(): void;
10
+ postCommit(actions: Action[]): void;
4
11
  postLoad(): void;
5
- postRemove(): void;
6
- postSave(): void;
12
+ postRemove(deletedRecords: number): void;
13
+ postSave(savedRecords: number | false): void;
14
+ preCommit(actions: Action[]): void;
7
15
  preLoad(): void;
8
16
  preRemove(): void;
9
17
  preSave(): void;
@@ -88,8 +96,8 @@ export declare abstract class DB<T extends Transaction> {
88
96
  abstract cancel(tableName: string): (where: string, tx?: Transaction) => Promise<number>;
89
97
  abstract escape(value: unknown): string;
90
98
  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>;
99
+ abstract remove(tableName: string, pk: Attribute<unknown, unknown>): (this: EntryBase & Record<string, unknown>) => Promise<number>;
100
+ abstract save(tableName: string, attributes: Record<string, string>, pk: Attribute<unknown, unknown>): (this: EntryBase & Record<string, unknown>) => Promise<number | false>;
93
101
  abstract dropConstraints(table: Table): Promise<number[]>;
94
102
  abstract dropFields(table: Table): Promise<void>;
95
103
  abstract dropIndexes(table: Table, constraintIndexes: number[]): Promise<void>;
@@ -106,6 +114,7 @@ export declare class Transaction {
106
114
  addEntry(entry: EntryBase): void;
107
115
  clean(): void;
108
116
  commit(): Promise<void>;
117
+ protected preCommit(): void;
109
118
  rollback(): Promise<void>;
110
119
  }
111
120
  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, 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;
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.46"
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
- }