sedentary 0.0.42 → 0.0.45
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 +1 -1
- package/dist/cjs/db.d.ts +111 -0
- package/dist/cjs/index.d.ts +143 -0
- package/dist/cjs/index.js +38 -33
- package/dist/es/db.d.ts +111 -0
- package/dist/es/index.d.ts +143 -0
- package/dist/es/index.js +38 -33
- package/dist/types/db.d.ts +18 -18
- package/dist/types/index.d.ts +26 -24
- package/package.json +5 -4
package/README.md
CHANGED
package/dist/cjs/db.d.ts
ADDED
|
@@ -0,0 +1,111 @@
|
|
|
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 {};
|
|
@@ -0,0 +1,143 @@
|
|
|
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/cjs/index.js
CHANGED
|
@@ -39,30 +39,33 @@ class Sedentary {
|
|
|
39
39
|
this.log = log ? log : log === null ? () => { } : console.log;
|
|
40
40
|
this.doSync = sync;
|
|
41
41
|
}
|
|
42
|
-
|
|
42
|
+
Boolean() {
|
|
43
43
|
return new db_1.Type({ base: Boolean, type: "BOOLEAN" });
|
|
44
44
|
}
|
|
45
|
-
|
|
45
|
+
DateTime() {
|
|
46
46
|
return new db_1.Type({ base: Date, type: "DATETIME" });
|
|
47
47
|
}
|
|
48
|
-
|
|
48
|
+
FKey(attribute, options) {
|
|
49
49
|
const { attributeName, base, fieldName, size, tableName, type } = attribute;
|
|
50
50
|
return new db_1.Type({ base, foreignKey: { attributeName, fieldName, options, tableName }, size, type });
|
|
51
51
|
}
|
|
52
|
-
|
|
52
|
+
Int(size) {
|
|
53
53
|
const message = "Sedentary.INT: 'size' argument: Wrong value, expected 2 or 4";
|
|
54
54
|
size = size ? this.checkSize(size, message) : 4;
|
|
55
55
|
if (size !== 2 && size !== 4)
|
|
56
56
|
throw new Error(message);
|
|
57
57
|
return new db_1.Type({ base: Number, size, type: "INT" });
|
|
58
58
|
}
|
|
59
|
-
|
|
59
|
+
Int8() {
|
|
60
60
|
return new db_1.Type({ base: BigInt, size: 8, type: "INT8" });
|
|
61
61
|
}
|
|
62
|
-
|
|
62
|
+
JSON() {
|
|
63
|
+
return new db_1.Type({ base: Object, type: "JSON" });
|
|
64
|
+
}
|
|
65
|
+
Number() {
|
|
63
66
|
return new db_1.Type({ base: Number, type: "NUMBER" });
|
|
64
67
|
}
|
|
65
|
-
|
|
68
|
+
VarChar(size) {
|
|
66
69
|
const message = "Sedentary.VARCHAR: 'size' argument: Wrong value, expected positive integer";
|
|
67
70
|
size = size ? this.checkSize(size, message) : undefined;
|
|
68
71
|
return new db_1.Type({ base: String, size, type: "VARCHAR" });
|
|
@@ -195,7 +198,7 @@ class Sedentary {
|
|
|
195
198
|
await this.db.end();
|
|
196
199
|
this.log("Connection closed");
|
|
197
200
|
}
|
|
198
|
-
|
|
201
|
+
begin() {
|
|
199
202
|
return this.db.begin();
|
|
200
203
|
}
|
|
201
204
|
escape(value) {
|
|
@@ -227,12 +230,12 @@ class Sedentary {
|
|
|
227
230
|
throw new Error(`Sedentary.model: '${modelName}' model: 'parent' and 'primaryKey' options conflict each other`);
|
|
228
231
|
let autoIncrement = true;
|
|
229
232
|
const { indexes, int8id, parent, primaryKey, sync, tableName } = { sync: this.doSync, tableName: modelName, ...options };
|
|
230
|
-
let
|
|
231
|
-
? [new db_1.Attribute({ ...this.
|
|
232
|
-
: [new db_1.Attribute({ ...this.
|
|
233
|
-
let constraints = [{ attribute:
|
|
234
|
-
const
|
|
235
|
-
let pk =
|
|
233
|
+
let aArray = int8id
|
|
234
|
+
? [new db_1.Attribute({ ...this.Int8(), attributeName: "id", fieldName: "id", modelName, notNull: true, tableName, unique: true })]
|
|
235
|
+
: [new db_1.Attribute({ ...this.Int(4), attributeName: "id", fieldName: "id", modelName, notNull: true, tableName, unique: true })];
|
|
236
|
+
let constraints = [{ attribute: aArray[0], constraintName: `${tableName}_id_unique`, type: "u" }];
|
|
237
|
+
const iArray = [];
|
|
238
|
+
let pk = aArray[0];
|
|
236
239
|
let attr2field = { id: "id" };
|
|
237
240
|
if (!methods)
|
|
238
241
|
methods = {};
|
|
@@ -249,16 +252,16 @@ class Sedentary {
|
|
|
249
252
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
250
253
|
attr2field = parent ? { ...parent.attr2field } : {};
|
|
251
254
|
autoIncrement = false;
|
|
252
|
-
|
|
255
|
+
aArray = [];
|
|
253
256
|
constraints = [];
|
|
254
257
|
}
|
|
255
258
|
for (const attributeName of Object.keys(attributes).sort()) {
|
|
256
259
|
if (reservedNames.includes(attributeName))
|
|
257
260
|
throw new Error(`Sedentary.model: '${modelName}' model: '${attributeName}' attribute: Reserved name`);
|
|
258
261
|
const call = (defaultValue, fieldName, notNull, unique, func, message1, message2) => {
|
|
259
|
-
if (func === this.
|
|
260
|
-
throw new Error(`${message1} 'this.
|
|
261
|
-
if (![this.
|
|
262
|
+
if (func === this.FKey)
|
|
263
|
+
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))
|
|
262
265
|
throw new Error(`${message1} ${message2}`);
|
|
263
266
|
return new db_1.Attribute({ attributeName, defaultValue, fieldName, modelName, notNull, tableName, unique, ...func() });
|
|
264
267
|
};
|
|
@@ -305,17 +308,17 @@ class Sedentary {
|
|
|
305
308
|
if (foreignKey) {
|
|
306
309
|
const options = foreignKey.options || {};
|
|
307
310
|
if (foreignKey.options !== undefined && !(foreignKey.options instanceof Object))
|
|
308
|
-
throw new Error(`Sedentary.
|
|
311
|
+
throw new Error(`Sedentary.FKey: '${modelName}' model: '${attributeName}' attribute: Wrong options type, expected 'Object'`);
|
|
309
312
|
for (const k in options)
|
|
310
313
|
if (!["onDelete", "onUpdate"].includes(k))
|
|
311
|
-
throw new Error(`Sedentary.
|
|
314
|
+
throw new Error(`Sedentary.FKey: '${modelName}' model: '${attributeName}' attribute: Unknown option '${k}'`);
|
|
312
315
|
for (const onChange of ["onDelete", "onUpdate"]) {
|
|
313
316
|
const actions = ["cascade", "no action", "restrict", "set default", "set null"];
|
|
314
317
|
let action = options[onChange];
|
|
315
318
|
if (!action)
|
|
316
319
|
action = options[onChange] = "no action";
|
|
317
320
|
if (action && !actions.includes(action))
|
|
318
|
-
throw new Error(`Sedentary.
|
|
321
|
+
throw new Error(`Sedentary.FKey: '${modelName}' model: '${attributeName}' attribute: '${onChange}' option: Wrong value, expected ${actions.map(_ => `'${_}'`).join(" | ")}`);
|
|
319
322
|
}
|
|
320
323
|
foreignKey.options = options;
|
|
321
324
|
}
|
|
@@ -328,7 +331,7 @@ class Sedentary {
|
|
|
328
331
|
const attribute = new db_1.Attribute({ attributeName, base, defaultValue, fieldName, foreignKey, modelName, notNull, size, tableName, type, unique });
|
|
329
332
|
if (primaryKey === attributeName)
|
|
330
333
|
pk = attribute;
|
|
331
|
-
|
|
334
|
+
aArray.push(attribute);
|
|
332
335
|
attr2field[attributeName] = fieldName;
|
|
333
336
|
if (foreignKey)
|
|
334
337
|
constraints.push({ attribute, constraintName: `fkey_${fieldName}_${foreignKey.tableName}_${foreignKey.fieldName}`, type: "f" });
|
|
@@ -336,17 +339,17 @@ class Sedentary {
|
|
|
336
339
|
constraints.push({ attribute, constraintName: `${tableName}_${fieldName}_unique`, type: "u" });
|
|
337
340
|
}
|
|
338
341
|
if (indexes) {
|
|
339
|
-
const
|
|
342
|
+
const originalAttributes = attributes;
|
|
340
343
|
if (!(indexes instanceof Object))
|
|
341
344
|
throw new Error(`Sedentary.model: '${modelName}' model: 'indexes' option: Wrong type, expected 'Object'`);
|
|
342
345
|
for (const indexName in indexes) {
|
|
343
|
-
if (
|
|
346
|
+
if (aArray.some(({ fieldName, unique }) => unique && `${tableName}_${fieldName}_unique` === indexName))
|
|
344
347
|
throw new Error(`Sedentary.model: '${modelName}' model: '${indexName}' index: index name already inferred by the unique constraint on an attribute`);
|
|
345
348
|
const idx = indexes[indexName];
|
|
346
349
|
const checkAttribute = (attribute, l) => {
|
|
347
350
|
if (typeof attribute !== "string")
|
|
348
351
|
throw new Error(`Sedentary.model: '${modelName}' model: '${indexName}' index: #${l + 1} attribute: Wrong type, expected 'string'`);
|
|
349
|
-
if (!(attribute in
|
|
352
|
+
if (!(attribute in originalAttributes))
|
|
350
353
|
throw new Error(`Sedentary.model: '${modelName}' model: '${indexName}' index: #${l + 1} attribute: Unknown attribute '${attribute}'`);
|
|
351
354
|
};
|
|
352
355
|
let attributes;
|
|
@@ -384,11 +387,11 @@ class Sedentary {
|
|
|
384
387
|
}
|
|
385
388
|
else
|
|
386
389
|
throw new Error(`Sedentary.model: '${modelName}' model: '${indexName}' index: Wrong type, expected 'Object'`);
|
|
387
|
-
|
|
390
|
+
iArray.push({ fields: attributes, indexName, type, unique });
|
|
388
391
|
}
|
|
389
392
|
}
|
|
390
393
|
this.models[modelName] = true;
|
|
391
|
-
const foreignKeys =
|
|
394
|
+
const foreignKeys = aArray
|
|
392
395
|
.filter(_ => _.foreignKey)
|
|
393
396
|
.reduce((ret, curr) => {
|
|
394
397
|
ret[curr.attributeName] = true;
|
|
@@ -438,10 +441,13 @@ class Sedentary {
|
|
|
438
441
|
tx.addEntry(this);
|
|
439
442
|
}
|
|
440
443
|
};
|
|
441
|
-
const table = new db_1.Table({ autoIncrement, constraints,
|
|
444
|
+
const table = new db_1.Table({ attributes: aArray, autoIncrement, constraints, indexes: iArray, model: ret, parent, pk, sync, tableName });
|
|
442
445
|
this.db.tables.push(table);
|
|
446
|
+
const cancel_ = this.db.cancel(tableName);
|
|
447
|
+
const cancel = (where, tx) => cancel_(this.createWhere(modelName, attr2field, where)[0], tx);
|
|
448
|
+
Object.defineProperty(cancel, "name", { value: modelName + ".cancel" });
|
|
443
449
|
const load_ = this.db.load(tableName, attr2field, pk, ret, table);
|
|
444
|
-
const load =
|
|
450
|
+
const load = (where, ...args) => {
|
|
445
451
|
let order = undefined;
|
|
446
452
|
let limit = undefined;
|
|
447
453
|
let tx = undefined;
|
|
@@ -474,11 +480,10 @@ class Sedentary {
|
|
|
474
480
|
throw new Error(`${modelName}.load: 'order' argument: Wrong type, expected 'string | string[]'`);
|
|
475
481
|
if (tx && !(tx instanceof db_1.Transaction))
|
|
476
482
|
throw new Error(`${modelName}.load: 'tx' argument: Wrong type, expected 'Transaction'`);
|
|
477
|
-
|
|
478
|
-
const ret = await load_(str, order, limit, tx, lock);
|
|
479
|
-
return ret;
|
|
483
|
+
return load_(this.createWhere(modelName, attr2field, where)[0], order, limit, tx, lock);
|
|
480
484
|
};
|
|
481
485
|
Object.defineProperty(load, "name", { value: modelName + ".load" });
|
|
486
|
+
Object.defineProperty(ret, "cancel", { value: cancel });
|
|
482
487
|
Object.defineProperty(ret, "name", { value: modelName });
|
|
483
488
|
Object.defineProperty(ret, "load", { value: load });
|
|
484
489
|
Object.defineProperty(ret, "attr2field", { value: attr2field });
|
|
@@ -506,7 +511,7 @@ class Sedentary {
|
|
|
506
511
|
return ret;
|
|
507
512
|
};
|
|
508
513
|
Object.defineProperty(ret.prototype.save, "name", { value: modelName + ".save" });
|
|
509
|
-
for (const attribute of
|
|
514
|
+
for (const attribute of aArray)
|
|
510
515
|
Object.defineProperty(ret, attribute.attributeName, { value: attribute });
|
|
511
516
|
for (const key of ["attributeName", "base", "fieldName", "modelName", "size", "tableName", "type", "unique"])
|
|
512
517
|
Object.defineProperty(ret, key, { value: pk[key] });
|
package/dist/es/db.d.ts
ADDED
|
@@ -0,0 +1,111 @@
|
|
|
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 {};
|
|
@@ -0,0 +1,143 @@
|
|
|
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/index.js
CHANGED
|
@@ -33,30 +33,33 @@ export class Sedentary {
|
|
|
33
33
|
this.log = log ? log : log === null ? () => { } : console.log;
|
|
34
34
|
this.doSync = sync;
|
|
35
35
|
}
|
|
36
|
-
|
|
36
|
+
Boolean() {
|
|
37
37
|
return new Type({ base: Boolean, type: "BOOLEAN" });
|
|
38
38
|
}
|
|
39
|
-
|
|
39
|
+
DateTime() {
|
|
40
40
|
return new Type({ base: Date, type: "DATETIME" });
|
|
41
41
|
}
|
|
42
|
-
|
|
42
|
+
FKey(attribute, options) {
|
|
43
43
|
const { attributeName, base, fieldName, size, tableName, type } = attribute;
|
|
44
44
|
return new Type({ base, foreignKey: { attributeName, fieldName, options, tableName }, size, type });
|
|
45
45
|
}
|
|
46
|
-
|
|
46
|
+
Int(size) {
|
|
47
47
|
const message = "Sedentary.INT: 'size' argument: Wrong value, expected 2 or 4";
|
|
48
48
|
size = size ? this.checkSize(size, message) : 4;
|
|
49
49
|
if (size !== 2 && size !== 4)
|
|
50
50
|
throw new Error(message);
|
|
51
51
|
return new Type({ base: Number, size, type: "INT" });
|
|
52
52
|
}
|
|
53
|
-
|
|
53
|
+
Int8() {
|
|
54
54
|
return new Type({ base: BigInt, size: 8, type: "INT8" });
|
|
55
55
|
}
|
|
56
|
-
|
|
56
|
+
JSON() {
|
|
57
|
+
return new Type({ base: Object, type: "JSON" });
|
|
58
|
+
}
|
|
59
|
+
Number() {
|
|
57
60
|
return new Type({ base: Number, type: "NUMBER" });
|
|
58
61
|
}
|
|
59
|
-
|
|
62
|
+
VarChar(size) {
|
|
60
63
|
const message = "Sedentary.VARCHAR: 'size' argument: Wrong value, expected positive integer";
|
|
61
64
|
size = size ? this.checkSize(size, message) : undefined;
|
|
62
65
|
return new Type({ base: String, size, type: "VARCHAR" });
|
|
@@ -189,7 +192,7 @@ export class Sedentary {
|
|
|
189
192
|
await this.db.end();
|
|
190
193
|
this.log("Connection closed");
|
|
191
194
|
}
|
|
192
|
-
|
|
195
|
+
begin() {
|
|
193
196
|
return this.db.begin();
|
|
194
197
|
}
|
|
195
198
|
escape(value) {
|
|
@@ -221,12 +224,12 @@ export class Sedentary {
|
|
|
221
224
|
throw new Error(`Sedentary.model: '${modelName}' model: 'parent' and 'primaryKey' options conflict each other`);
|
|
222
225
|
let autoIncrement = true;
|
|
223
226
|
const { indexes, int8id, parent, primaryKey, sync, tableName } = { sync: this.doSync, tableName: modelName, ...options };
|
|
224
|
-
let
|
|
225
|
-
? [new Attribute({ ...this.
|
|
226
|
-
: [new Attribute({ ...this.
|
|
227
|
-
let constraints = [{ attribute:
|
|
228
|
-
const
|
|
229
|
-
let pk =
|
|
227
|
+
let aArray = int8id
|
|
228
|
+
? [new Attribute({ ...this.Int8(), attributeName: "id", fieldName: "id", modelName, notNull: true, tableName, unique: true })]
|
|
229
|
+
: [new Attribute({ ...this.Int(4), attributeName: "id", fieldName: "id", modelName, notNull: true, tableName, unique: true })];
|
|
230
|
+
let constraints = [{ attribute: aArray[0], constraintName: `${tableName}_id_unique`, type: "u" }];
|
|
231
|
+
const iArray = [];
|
|
232
|
+
let pk = aArray[0];
|
|
230
233
|
let attr2field = { id: "id" };
|
|
231
234
|
if (!methods)
|
|
232
235
|
methods = {};
|
|
@@ -243,16 +246,16 @@ export class Sedentary {
|
|
|
243
246
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
244
247
|
attr2field = parent ? { ...parent.attr2field } : {};
|
|
245
248
|
autoIncrement = false;
|
|
246
|
-
|
|
249
|
+
aArray = [];
|
|
247
250
|
constraints = [];
|
|
248
251
|
}
|
|
249
252
|
for (const attributeName of Object.keys(attributes).sort()) {
|
|
250
253
|
if (reservedNames.includes(attributeName))
|
|
251
254
|
throw new Error(`Sedentary.model: '${modelName}' model: '${attributeName}' attribute: Reserved name`);
|
|
252
255
|
const call = (defaultValue, fieldName, notNull, unique, func, message1, message2) => {
|
|
253
|
-
if (func === this.
|
|
254
|
-
throw new Error(`${message1} 'this.
|
|
255
|
-
if (![this.
|
|
256
|
+
if (func === this.FKey)
|
|
257
|
+
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))
|
|
256
259
|
throw new Error(`${message1} ${message2}`);
|
|
257
260
|
return new Attribute({ attributeName, defaultValue, fieldName, modelName, notNull, tableName, unique, ...func() });
|
|
258
261
|
};
|
|
@@ -299,17 +302,17 @@ export class Sedentary {
|
|
|
299
302
|
if (foreignKey) {
|
|
300
303
|
const options = foreignKey.options || {};
|
|
301
304
|
if (foreignKey.options !== undefined && !(foreignKey.options instanceof Object))
|
|
302
|
-
throw new Error(`Sedentary.
|
|
305
|
+
throw new Error(`Sedentary.FKey: '${modelName}' model: '${attributeName}' attribute: Wrong options type, expected 'Object'`);
|
|
303
306
|
for (const k in options)
|
|
304
307
|
if (!["onDelete", "onUpdate"].includes(k))
|
|
305
|
-
throw new Error(`Sedentary.
|
|
308
|
+
throw new Error(`Sedentary.FKey: '${modelName}' model: '${attributeName}' attribute: Unknown option '${k}'`);
|
|
306
309
|
for (const onChange of ["onDelete", "onUpdate"]) {
|
|
307
310
|
const actions = ["cascade", "no action", "restrict", "set default", "set null"];
|
|
308
311
|
let action = options[onChange];
|
|
309
312
|
if (!action)
|
|
310
313
|
action = options[onChange] = "no action";
|
|
311
314
|
if (action && !actions.includes(action))
|
|
312
|
-
throw new Error(`Sedentary.
|
|
315
|
+
throw new Error(`Sedentary.FKey: '${modelName}' model: '${attributeName}' attribute: '${onChange}' option: Wrong value, expected ${actions.map(_ => `'${_}'`).join(" | ")}`);
|
|
313
316
|
}
|
|
314
317
|
foreignKey.options = options;
|
|
315
318
|
}
|
|
@@ -322,7 +325,7 @@ export class Sedentary {
|
|
|
322
325
|
const attribute = new Attribute({ attributeName, base, defaultValue, fieldName, foreignKey, modelName, notNull, size, tableName, type, unique });
|
|
323
326
|
if (primaryKey === attributeName)
|
|
324
327
|
pk = attribute;
|
|
325
|
-
|
|
328
|
+
aArray.push(attribute);
|
|
326
329
|
attr2field[attributeName] = fieldName;
|
|
327
330
|
if (foreignKey)
|
|
328
331
|
constraints.push({ attribute, constraintName: `fkey_${fieldName}_${foreignKey.tableName}_${foreignKey.fieldName}`, type: "f" });
|
|
@@ -330,17 +333,17 @@ export class Sedentary {
|
|
|
330
333
|
constraints.push({ attribute, constraintName: `${tableName}_${fieldName}_unique`, type: "u" });
|
|
331
334
|
}
|
|
332
335
|
if (indexes) {
|
|
333
|
-
const
|
|
336
|
+
const originalAttributes = attributes;
|
|
334
337
|
if (!(indexes instanceof Object))
|
|
335
338
|
throw new Error(`Sedentary.model: '${modelName}' model: 'indexes' option: Wrong type, expected 'Object'`);
|
|
336
339
|
for (const indexName in indexes) {
|
|
337
|
-
if (
|
|
340
|
+
if (aArray.some(({ fieldName, unique }) => unique && `${tableName}_${fieldName}_unique` === indexName))
|
|
338
341
|
throw new Error(`Sedentary.model: '${modelName}' model: '${indexName}' index: index name already inferred by the unique constraint on an attribute`);
|
|
339
342
|
const idx = indexes[indexName];
|
|
340
343
|
const checkAttribute = (attribute, l) => {
|
|
341
344
|
if (typeof attribute !== "string")
|
|
342
345
|
throw new Error(`Sedentary.model: '${modelName}' model: '${indexName}' index: #${l + 1} attribute: Wrong type, expected 'string'`);
|
|
343
|
-
if (!(attribute in
|
|
346
|
+
if (!(attribute in originalAttributes))
|
|
344
347
|
throw new Error(`Sedentary.model: '${modelName}' model: '${indexName}' index: #${l + 1} attribute: Unknown attribute '${attribute}'`);
|
|
345
348
|
};
|
|
346
349
|
let attributes;
|
|
@@ -378,11 +381,11 @@ export class Sedentary {
|
|
|
378
381
|
}
|
|
379
382
|
else
|
|
380
383
|
throw new Error(`Sedentary.model: '${modelName}' model: '${indexName}' index: Wrong type, expected 'Object'`);
|
|
381
|
-
|
|
384
|
+
iArray.push({ fields: attributes, indexName, type, unique });
|
|
382
385
|
}
|
|
383
386
|
}
|
|
384
387
|
this.models[modelName] = true;
|
|
385
|
-
const foreignKeys =
|
|
388
|
+
const foreignKeys = aArray
|
|
386
389
|
.filter(_ => _.foreignKey)
|
|
387
390
|
.reduce((ret, curr) => {
|
|
388
391
|
ret[curr.attributeName] = true;
|
|
@@ -432,10 +435,13 @@ export class Sedentary {
|
|
|
432
435
|
tx.addEntry(this);
|
|
433
436
|
}
|
|
434
437
|
};
|
|
435
|
-
const table = new Table({ autoIncrement, constraints,
|
|
438
|
+
const table = new Table({ attributes: aArray, autoIncrement, constraints, indexes: iArray, model: ret, parent, pk, sync, tableName });
|
|
436
439
|
this.db.tables.push(table);
|
|
440
|
+
const cancel_ = this.db.cancel(tableName);
|
|
441
|
+
const cancel = (where, tx) => cancel_(this.createWhere(modelName, attr2field, where)[0], tx);
|
|
442
|
+
Object.defineProperty(cancel, "name", { value: modelName + ".cancel" });
|
|
437
443
|
const load_ = this.db.load(tableName, attr2field, pk, ret, table);
|
|
438
|
-
const load =
|
|
444
|
+
const load = (where, ...args) => {
|
|
439
445
|
let order = undefined;
|
|
440
446
|
let limit = undefined;
|
|
441
447
|
let tx = undefined;
|
|
@@ -468,11 +474,10 @@ export class Sedentary {
|
|
|
468
474
|
throw new Error(`${modelName}.load: 'order' argument: Wrong type, expected 'string | string[]'`);
|
|
469
475
|
if (tx && !(tx instanceof Transaction))
|
|
470
476
|
throw new Error(`${modelName}.load: 'tx' argument: Wrong type, expected 'Transaction'`);
|
|
471
|
-
|
|
472
|
-
const ret = await load_(str, order, limit, tx, lock);
|
|
473
|
-
return ret;
|
|
477
|
+
return load_(this.createWhere(modelName, attr2field, where)[0], order, limit, tx, lock);
|
|
474
478
|
};
|
|
475
479
|
Object.defineProperty(load, "name", { value: modelName + ".load" });
|
|
480
|
+
Object.defineProperty(ret, "cancel", { value: cancel });
|
|
476
481
|
Object.defineProperty(ret, "name", { value: modelName });
|
|
477
482
|
Object.defineProperty(ret, "load", { value: load });
|
|
478
483
|
Object.defineProperty(ret, "attr2field", { value: attr2field });
|
|
@@ -500,7 +505,7 @@ export class Sedentary {
|
|
|
500
505
|
return ret;
|
|
501
506
|
};
|
|
502
507
|
Object.defineProperty(ret.prototype.save, "name", { value: modelName + ".save" });
|
|
503
|
-
for (const attribute of
|
|
508
|
+
for (const attribute of aArray)
|
|
504
509
|
Object.defineProperty(ret, attribute.attributeName, { value: attribute });
|
|
505
510
|
for (const key of ["attributeName", "base", "fieldName", "modelName", "size", "tableName", "type", "unique"])
|
|
506
511
|
Object.defineProperty(ret, key, { value: pk[key] });
|
package/dist/types/db.d.ts
CHANGED
|
@@ -1,4 +1,3 @@
|
|
|
1
|
-
export declare type Natural = Date | Record<string, unknown> | bigint | boolean | number | string | null;
|
|
2
1
|
export declare class EntryBase {
|
|
3
2
|
constructor(from?: Partial<EntryBase>);
|
|
4
3
|
construct(): void;
|
|
@@ -16,10 +15,10 @@ export interface ForeignKeyOptions {
|
|
|
16
15
|
onDelete?: ForeignKeyActions;
|
|
17
16
|
onUpdate?: ForeignKeyActions;
|
|
18
17
|
}
|
|
19
|
-
export interface Type<
|
|
18
|
+
export interface Type<T, E> {
|
|
20
19
|
base: unknown;
|
|
21
20
|
entry?: E;
|
|
22
|
-
native?:
|
|
21
|
+
native?: T;
|
|
23
22
|
size?: number;
|
|
24
23
|
type: string;
|
|
25
24
|
foreignKey?: {
|
|
@@ -29,23 +28,23 @@ export interface Type<N extends Natural, E> {
|
|
|
29
28
|
tableName: string;
|
|
30
29
|
};
|
|
31
30
|
}
|
|
32
|
-
export declare class Type<
|
|
33
|
-
constructor(from: Type<
|
|
31
|
+
export declare class Type<T, E> {
|
|
32
|
+
constructor(from: Type<T, E>);
|
|
34
33
|
}
|
|
35
|
-
export interface Attribute<
|
|
34
|
+
export interface Attribute<T, E> extends Type<T, E> {
|
|
36
35
|
attributeName: string;
|
|
37
|
-
defaultValue?:
|
|
36
|
+
defaultValue?: unknown;
|
|
38
37
|
fieldName: string;
|
|
39
38
|
modelName: string;
|
|
40
39
|
notNull: boolean;
|
|
41
40
|
tableName: string;
|
|
42
41
|
unique?: boolean;
|
|
43
42
|
}
|
|
44
|
-
export declare class Attribute<
|
|
45
|
-
constructor(from: Attribute<
|
|
43
|
+
export declare class Attribute<T, E> extends Type<T, E> {
|
|
44
|
+
constructor(from: Attribute<T, E>);
|
|
46
45
|
}
|
|
47
46
|
export interface Constraint {
|
|
48
|
-
attribute: Attribute<
|
|
47
|
+
attribute: Attribute<unknown, unknown>;
|
|
49
48
|
constraintName: string;
|
|
50
49
|
type: "f" | "u";
|
|
51
50
|
}
|
|
@@ -56,15 +55,15 @@ export interface Index {
|
|
|
56
55
|
unique: boolean;
|
|
57
56
|
}
|
|
58
57
|
interface ITable {
|
|
59
|
-
attributes: Attribute<
|
|
58
|
+
attributes: Attribute<unknown, unknown>[];
|
|
60
59
|
autoIncrement: boolean;
|
|
61
60
|
constraints: Constraint[];
|
|
62
61
|
indexes: Index[];
|
|
63
62
|
model: {
|
|
64
63
|
load: (where: any, order?: string[], tx?: Transaction) => Promise<EntryBase[]>;
|
|
65
64
|
};
|
|
66
|
-
parent?: Attribute<
|
|
67
|
-
pk: Attribute<
|
|
65
|
+
parent?: Attribute<unknown, unknown>;
|
|
66
|
+
pk: Attribute<unknown, unknown>;
|
|
68
67
|
sync: boolean;
|
|
69
68
|
tableName: string;
|
|
70
69
|
}
|
|
@@ -72,7 +71,7 @@ declare const Table_base: new (defaults?: ITable | undefined) => ITable;
|
|
|
72
71
|
export declare class Table extends Table_base {
|
|
73
72
|
autoIncrementOwn?: boolean;
|
|
74
73
|
oid?: number;
|
|
75
|
-
findField(name: string): Attribute<
|
|
74
|
+
findField(name: string): Attribute<unknown, unknown>;
|
|
76
75
|
}
|
|
77
76
|
export declare abstract class DB<T extends Transaction> {
|
|
78
77
|
tables: Table[];
|
|
@@ -86,10 +85,11 @@ export declare abstract class DB<T extends Transaction> {
|
|
|
86
85
|
syncDataBase(): Promise<void>;
|
|
87
86
|
protected syncLog(message: string): void;
|
|
88
87
|
abstract begin(): Promise<T>;
|
|
89
|
-
abstract
|
|
90
|
-
abstract
|
|
91
|
-
abstract
|
|
92
|
-
abstract
|
|
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
93
|
abstract dropConstraints(table: Table): Promise<number[]>;
|
|
94
94
|
abstract dropFields(table: Table): Promise<void>;
|
|
95
95
|
abstract dropIndexes(table: Table, constraintIndexes: number[]): Promise<void>;
|
package/dist/types/index.d.ts
CHANGED
|
@@ -1,24 +1,24 @@
|
|
|
1
|
-
import { Attribute, DB, EntryBase, ForeignKeyOptions,
|
|
2
|
-
export { Attribute, DB, EntryBase, ForeignKeyActions, ForeignKeyOptions, Index,
|
|
3
|
-
export declare type TypeDefinition<
|
|
4
|
-
export interface AttributeOptions<
|
|
5
|
-
defaultValue?:
|
|
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
6
|
fieldName?: string;
|
|
7
7
|
notNull?: boolean;
|
|
8
|
-
type: TypeDefinition<
|
|
8
|
+
type: TypeDefinition<T, E>;
|
|
9
9
|
unique?: boolean;
|
|
10
10
|
}
|
|
11
|
-
export declare type AttributeDefinition<
|
|
11
|
+
export declare type AttributeDefinition<T, E> = TypeDefinition<T, E> | AttributeOptions<T, E>;
|
|
12
12
|
export declare type AttributesDefinition = {
|
|
13
|
-
[key: string]: AttributeDefinition<
|
|
13
|
+
[key: string]: AttributeDefinition<unknown, unknown>;
|
|
14
14
|
};
|
|
15
|
-
declare type ForeignKeysAttributes<T, k> = T extends AttributeDefinition<
|
|
15
|
+
declare type ForeignKeysAttributes<T, k> = T extends AttributeDefinition<unknown, infer E> ? (E extends EntryBase ? k : never) : never;
|
|
16
16
|
declare type ForeignKeys<A extends AttributesDefinition> = {
|
|
17
17
|
[a in keyof A]?: ForeignKeysAttributes<A[a], a>;
|
|
18
18
|
}[keyof A];
|
|
19
19
|
declare type Native___<T> = T extends Type<infer N, unknown> ? N : never;
|
|
20
20
|
declare type Native__<T> = T extends () => Type<infer N, infer E> ? Native___<Type<N, E>> : Native___<T>;
|
|
21
|
-
declare type Native_<T, N
|
|
21
|
+
declare type Native_<T, N, E> = T extends {
|
|
22
22
|
notNull: true;
|
|
23
23
|
} ? Native___<Type<N, E>> : Native___<Type<N, E>> | null;
|
|
24
24
|
declare type Native<T> = T extends AttributeOptions<infer N, infer E> ? Native_<T, N, E> : Native__<T> | null;
|
|
@@ -39,10 +39,10 @@ interface BaseModelOptions {
|
|
|
39
39
|
}
|
|
40
40
|
export interface ModelOptions extends BaseModelOptions {
|
|
41
41
|
int8id?: boolean;
|
|
42
|
-
parent?: Attribute<
|
|
42
|
+
parent?: Attribute<unknown, EntryBase>;
|
|
43
43
|
primaryKey?: string;
|
|
44
44
|
}
|
|
45
|
-
declare type ConditionAttribute<
|
|
45
|
+
declare type ConditionAttribute<T> = T | ["=" | ">" | "<" | ">=" | "<=" | "<>", T] | ["IN", T[]] | ["IS NULL"] | ["LIKE", string] | ["NOT"];
|
|
46
46
|
declare type ConditionBase<A extends AttributesDefinition> = string | {
|
|
47
47
|
[a in keyof A]?: ConditionAttribute<Native<A[a]>>;
|
|
48
48
|
};
|
|
@@ -53,7 +53,7 @@ declare type UnionToIntersection<U> = (U extends unknown ? (k: U) => void : neve
|
|
|
53
53
|
declare type IsUnion<T> = [T] extends [UnionToIntersection<T>] ? false : true;
|
|
54
54
|
declare type BaseKeyType<B extends boolean> = IsUnion<B> extends true ? number : B extends true ? string : number;
|
|
55
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<
|
|
56
|
+
declare type ForeignKey<A> = A extends AttributeDefinition<unknown, infer E> ? () => Promise<E> : never;
|
|
57
57
|
declare type EntryBaseAttributes<A extends AttributesDefinition> = {
|
|
58
58
|
[a in keyof A]: Native<A[a]>;
|
|
59
59
|
};
|
|
@@ -74,8 +74,9 @@ export interface ModelLoad<A extends AttributesDefinition, E extends EntryBase>
|
|
|
74
74
|
load(where: Condition<A>, order?: Order<A>, tx?: Transaction, lock?: boolean): Promise<E[]>;
|
|
75
75
|
load(where: Condition<A>, limit?: number, tx?: Transaction, lock?: boolean): Promise<E[]>;
|
|
76
76
|
load(where: Condition<A>, tx: Transaction, lock?: boolean): Promise<E[]>;
|
|
77
|
+
cancel(where: Condition<A>, tx?: Transaction): Promise<number>;
|
|
77
78
|
}
|
|
78
|
-
declare type ModelBase<
|
|
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> & {
|
|
79
80
|
attributes: A;
|
|
80
81
|
foreignKeys: Record<string, boolean>;
|
|
81
82
|
methods: EM;
|
|
@@ -84,8 +85,8 @@ declare type ModelBase<N extends Natural, A extends AttributesDefinition, EA ext
|
|
|
84
85
|
} & {
|
|
85
86
|
[a in keyof A]: Attribute<Native<A[a]>, E>;
|
|
86
87
|
} & ModelLoad<A, E>;
|
|
87
|
-
declare type Model<
|
|
88
|
-
declare type ModelStd = Attribute<
|
|
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> & {
|
|
89
90
|
attributes: AttributesDefinition;
|
|
90
91
|
foreignKeys: Record<string, boolean>;
|
|
91
92
|
methods: EntryBase;
|
|
@@ -112,13 +113,14 @@ export declare class Sedentary<D extends DB<T>, T extends Transaction> {
|
|
|
112
113
|
protected log: (message: string) => void;
|
|
113
114
|
private models;
|
|
114
115
|
constructor(options?: SedentaryOptions);
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
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>;
|
|
122
124
|
private checkDB;
|
|
123
125
|
private checkOrderBy;
|
|
124
126
|
private checkSize;
|
|
@@ -127,7 +129,7 @@ export declare class Sedentary<D extends DB<T>, T extends Transaction> {
|
|
|
127
129
|
sync(): Promise<void>;
|
|
128
130
|
end(): Promise<void>;
|
|
129
131
|
begin(): Promise<T>;
|
|
130
|
-
escape(value:
|
|
132
|
+
escape(value: unknown): string;
|
|
131
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 & {
|
|
132
134
|
int8id?: B;
|
|
133
135
|
parent?: P;
|
package/package.json
CHANGED
|
@@ -33,16 +33,17 @@
|
|
|
33
33
|
"repository": "https://github.com/iccicci/sedentary",
|
|
34
34
|
"scripts": {
|
|
35
35
|
"build": "make build",
|
|
36
|
-
"coverage": "jest --coverage --runInBand",
|
|
37
|
-
"deploy": "
|
|
36
|
+
"coverage": "jest --coverage --no-cache --runInBand",
|
|
37
|
+
"deploy": "make deploy",
|
|
38
38
|
"precoverage": "make pretest",
|
|
39
39
|
"preinstall": "if [ -f Makefile ] ; then make ; fi",
|
|
40
40
|
"pretest": "make pretest",
|
|
41
|
-
"test": "jest --runInBand"
|
|
41
|
+
"test": "jest --no-cache --runInBand"
|
|
42
42
|
},
|
|
43
43
|
"tsd": {
|
|
44
44
|
"compilerOptions": {
|
|
45
45
|
"alwaysStrict": true,
|
|
46
|
+
"composite": false,
|
|
46
47
|
"esModuleInterop": true,
|
|
47
48
|
"moduleResolution": "Node",
|
|
48
49
|
"noImplicitAny": true,
|
|
@@ -57,5 +58,5 @@
|
|
|
57
58
|
}
|
|
58
59
|
},
|
|
59
60
|
"types": "./dist/types/index.d.ts",
|
|
60
|
-
"version": "0.0.
|
|
61
|
+
"version": "0.0.45"
|
|
61
62
|
}
|