sedentary 0.0.43 → 0.0.44

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.
@@ -0,0 +1,110 @@
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 escape(value: unknown): string;
89
+ 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[]>;
90
+ abstract remove(tableName: string, pk: Attribute<unknown, unknown>): (this: EntryBase & Record<string, unknown>) => Promise<boolean>;
91
+ abstract save(tableName: string, attributes: Record<string, string>, pk: Attribute<unknown, unknown>): (this: EntryBase & Record<string, unknown>) => Promise<boolean>;
92
+ abstract dropConstraints(table: Table): Promise<number[]>;
93
+ abstract dropFields(table: Table): Promise<void>;
94
+ abstract dropIndexes(table: Table, constraintIndexes: number[]): Promise<void>;
95
+ abstract syncConstraints(table: Table): Promise<void>;
96
+ abstract syncFields(table: Table): Promise<void>;
97
+ abstract syncIndexes(table: Table): Promise<void>;
98
+ abstract syncSequence(table: Table): Promise<void>;
99
+ abstract syncTable(table: Table): Promise<void>;
100
+ }
101
+ export declare class Transaction {
102
+ private entries;
103
+ protected log: (message: string) => void;
104
+ constructor(log: (message: string) => void);
105
+ addEntry(entry: EntryBase): void;
106
+ clean(): void;
107
+ commit(): Promise<void>;
108
+ rollback(): Promise<void>;
109
+ }
110
+ export {};
package/dist/cjs/db.js ADDED
@@ -0,0 +1,115 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.Transaction = exports.DB = exports.Table = exports.Attribute = exports.Type = exports.EntryBase = void 0;
4
+ class EntryBase {
5
+ constructor(from) {
6
+ if (from === "load")
7
+ this.preLoad();
8
+ else {
9
+ if (from)
10
+ Object.assign(this, from);
11
+ this.construct();
12
+ }
13
+ }
14
+ construct() { }
15
+ postLoad() { }
16
+ postRemove() { }
17
+ postSave() { }
18
+ preLoad() { }
19
+ preRemove() { }
20
+ preSave() { }
21
+ async remove() {
22
+ return false;
23
+ }
24
+ async save() {
25
+ return false;
26
+ }
27
+ }
28
+ exports.EntryBase = EntryBase;
29
+ class Type {
30
+ constructor(from) {
31
+ Object.assign(this, from);
32
+ }
33
+ }
34
+ exports.Type = Type;
35
+ class Attribute extends Type {
36
+ constructor(from) {
37
+ super(from);
38
+ }
39
+ }
40
+ exports.Attribute = Attribute;
41
+ function autoImplement() {
42
+ return class {
43
+ constructor(defaults) {
44
+ Object.assign(this, defaults);
45
+ }
46
+ };
47
+ }
48
+ class Table extends autoImplement() {
49
+ findField(name) {
50
+ return this.attributes.filter(_ => _.fieldName === name)[0];
51
+ }
52
+ }
53
+ exports.Table = Table;
54
+ class DB {
55
+ constructor(log) {
56
+ this.tables = [];
57
+ this.sync = true;
58
+ this.log = log;
59
+ }
60
+ findTable(name) {
61
+ return this.tables.filter(_ => _.tableName === name)[0];
62
+ }
63
+ indexesEq(a, b) {
64
+ if (a.fields.length !== b.fields.length)
65
+ return false;
66
+ for (const i in a.fields)
67
+ if (a.fields[i] !== b.fields[i])
68
+ return false;
69
+ if (a.type !== b.type)
70
+ return false;
71
+ if (a.unique !== b.unique)
72
+ return false;
73
+ return true;
74
+ }
75
+ async syncDataBase() {
76
+ for (const table of this.tables) {
77
+ this.sync = table.sync;
78
+ await this.syncTable(table);
79
+ const indexes = await this.dropConstraints(table);
80
+ await this.dropIndexes(table, indexes);
81
+ await this.dropFields(table);
82
+ await this.syncFields(table);
83
+ await this.syncSequence(table);
84
+ await this.syncConstraints(table);
85
+ await this.syncIndexes(table);
86
+ }
87
+ }
88
+ syncLog(message) {
89
+ this.log(this.sync ? message : "NOT SYNCING: " + message);
90
+ }
91
+ }
92
+ exports.DB = DB;
93
+ class Transaction {
94
+ constructor(log) {
95
+ this.entries = [];
96
+ this.log = log;
97
+ }
98
+ addEntry(entry) {
99
+ Object.defineProperty(entry, "tx", { configurable: true, value: this });
100
+ this.entries.push(entry);
101
+ }
102
+ clean() {
103
+ const { entries } = this;
104
+ for (const entry of entries)
105
+ Object.defineProperty(entry, "tx", { configurable: true, value: null });
106
+ this.entries = [];
107
+ }
108
+ async commit() {
109
+ this.clean();
110
+ }
111
+ async rollback() {
112
+ this.clean();
113
+ }
114
+ }
115
+ exports.Transaction = Transaction;
@@ -0,0 +1,142 @@
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
+ }
78
+ 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
+ attributes: A;
80
+ foreignKeys: Record<string, boolean>;
81
+ methods: EM;
82
+ parent?: ModelStd;
83
+ tableName: string;
84
+ } & {
85
+ [a in keyof A]: Attribute<Native<A[a]>, E>;
86
+ } & ModelLoad<A, E>;
87
+ declare type Model<T, A extends AttributesDefinition, EM extends EntryBase> = ModelBase<T, A, EntryBaseAttributes<A>, EM, EntryBaseAttributes<A> & EM>;
88
+ declare type ModelStd = Attribute<unknown, EntryBase> & {
89
+ attributes: AttributesDefinition;
90
+ foreignKeys: Record<string, boolean>;
91
+ methods: EntryBase;
92
+ parent?: ModelStd;
93
+ };
94
+ export declare type Entry<M> = M extends new () => infer E ? E : never;
95
+ export declare type OrderBy<M> = M extends {
96
+ load(where: unknown, order?: infer T): void;
97
+ load(where: unknown, limit?: number): void;
98
+ load(where: unknown, tx?: Transaction): void;
99
+ } ? Exclude<T, undefined> : never;
100
+ export declare type Where<M> = M extends {
101
+ load(where: infer T): void;
102
+ } ? T : never;
103
+ export interface SedentaryOptions {
104
+ autoSync?: boolean;
105
+ log?: ((message: string) => void) | null;
106
+ sync?: boolean;
107
+ }
108
+ export declare class Sedentary<D extends DB<T>, T extends Transaction> {
109
+ protected autoSync: boolean;
110
+ protected db: D;
111
+ protected doSync: boolean;
112
+ protected log: (message: string) => void;
113
+ private models;
114
+ constructor(options?: SedentaryOptions);
115
+ Boolean(): Type<boolean, unknown>;
116
+ DateTime(): Type<Date, unknown>;
117
+ FKey<T, E extends EntryBase>(attribute: Attribute<T, E>, options?: ForeignKeyOptions): Type<T, E>;
118
+ Int(size?: number): Type<number, unknown>;
119
+ Int8(): Type<bigint, unknown>;
120
+ JSON<T>(): Type<T, unknown>;
121
+ Number(): Type<number, unknown>;
122
+ VarChar(size?: number): Type<string, unknown>;
123
+ private checkDB;
124
+ private checkOrderBy;
125
+ private checkSize;
126
+ private createWhere;
127
+ connect(sync?: boolean): Promise<void>;
128
+ sync(): Promise<void>;
129
+ end(): Promise<void>;
130
+ begin(): Promise<T>;
131
+ escape(value: unknown): string;
132
+ model<A extends AttributesDefinition, B extends boolean, K extends string, P extends ModelStd, EM extends EntryMethods<A, P>>(modelName: string, attributes: A, options?: BaseModelOptions & {
133
+ int8id?: B;
134
+ parent?: P;
135
+ primaryKey?: K | keyof A;
136
+ }): Model<K extends keyof A ? Native<A[K]> : KeyType<B, P>, ModelAttributes<A, B, K, P>, EM>;
137
+ 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 & {
138
+ int8id?: B;
139
+ parent?: P;
140
+ primaryKey?: K | keyof A;
141
+ }, 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>;
142
+ }