sedentary 0.1.0 → 0.1.1

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,130 @@
1
+ export declare const actions: unique symbol;
2
+ export declare const base: unique symbol;
3
+ export declare const loaded: unique symbol;
4
+ export declare const size: unique symbol;
5
+ export declare const transaction: unique symbol;
6
+ export type TxAction = {
7
+ action: "remove";
8
+ records: number;
9
+ } | {
10
+ action: "save";
11
+ records: number | false;
12
+ };
13
+ export declare class EntryBase {
14
+ constructor(from?: Partial<EntryBase>);
15
+ construct(): void;
16
+ postCommit(actions: TxAction[]): void;
17
+ postLoad(): void;
18
+ postRemove(deletedRecords: number): void;
19
+ postSave(savedRecords: number | false): void;
20
+ preCommit(actions: TxAction[]): void;
21
+ preLoad(): void;
22
+ preRemove(): void;
23
+ preSave(): void;
24
+ remove(): Promise<boolean>;
25
+ save(): Promise<number | false>;
26
+ }
27
+ export type ForeignKeyActions = "cascade" | "no action" | "restrict" | "set default" | "set null";
28
+ export interface ForeignKeyOptions {
29
+ onDelete?: ForeignKeyActions;
30
+ onUpdate?: ForeignKeyActions;
31
+ }
32
+ export interface Type<T, N extends boolean, E> {
33
+ [base]: unknown;
34
+ entry?: E;
35
+ native?: T;
36
+ notNull?: N;
37
+ [size]?: number;
38
+ type: string;
39
+ foreignKey?: {
40
+ attributeName: string;
41
+ fieldName: string;
42
+ options?: ForeignKeyOptions;
43
+ tableName: string;
44
+ };
45
+ }
46
+ export declare class Type<T, N extends boolean, E> {
47
+ constructor(from: Type<T, N, E>);
48
+ }
49
+ export interface Attribute<T, N extends boolean, E> extends Type<T, N, E> {
50
+ attributeName: string;
51
+ defaultValue?: unknown;
52
+ fieldName: string;
53
+ modelName: string;
54
+ notNull: N;
55
+ tableName: string;
56
+ unique?: boolean;
57
+ }
58
+ export declare class Attribute<T, N extends boolean, E> extends Type<T, N, E> {
59
+ constructor(from: Attribute<T, N, E>);
60
+ }
61
+ export interface Constraint {
62
+ attribute: Attribute<unknown, boolean, unknown>;
63
+ constraintName: string;
64
+ type: "f" | "u";
65
+ }
66
+ export interface Index {
67
+ fields: string[];
68
+ indexName: string;
69
+ type: "btree" | "hash";
70
+ unique: boolean;
71
+ }
72
+ interface ITable {
73
+ attributes: Attribute<unknown, boolean, unknown>[];
74
+ autoIncrement: boolean;
75
+ constraints: Constraint[];
76
+ indexes: Index[];
77
+ model: {
78
+ load: (where: any, order?: any, tx?: Transaction) => Promise<EntryBase[]>;
79
+ };
80
+ parent?: Attribute<unknown, boolean, unknown>;
81
+ pk: Attribute<unknown, boolean, unknown>;
82
+ sync: boolean;
83
+ tableName: string;
84
+ }
85
+ declare const Table_base: new (defaults?: ITable | undefined) => ITable;
86
+ export declare class Table extends Table_base {
87
+ autoIncrementOwn?: boolean;
88
+ oid?: number;
89
+ findAttribute(name: string): Attribute<unknown, boolean, unknown>;
90
+ findField(name: string): Attribute<unknown, boolean, unknown>;
91
+ }
92
+ export declare abstract class DB<T extends Transaction> {
93
+ tables: Table[];
94
+ protected log: (message: string) => void;
95
+ protected sync: boolean;
96
+ abstract connect(): Promise<void>;
97
+ abstract end(): Promise<void>;
98
+ constructor(log: (message: string) => void);
99
+ findTable(name: string): Table;
100
+ protected indexesEq(a: Index, b: Index): boolean;
101
+ syncDataBase(): Promise<void>;
102
+ protected syncLog(message: string): void;
103
+ abstract begin(): Promise<T>;
104
+ abstract cancel(tableName: string): (where: string, tx?: Transaction) => Promise<number>;
105
+ abstract escape(value: unknown): string;
106
+ abstract load(tableName: string, attributes: Record<string, string>, pk: Attribute<unknown, boolean, unknown>, model: new () => EntryBase, table: Table): (where: string, order?: string | string[], limit?: number, tx?: Transaction, lock?: boolean) => Promise<EntryBase[]>;
107
+ abstract remove(tableName: string, pk: Attribute<unknown, boolean, unknown>): (this: EntryBase & Record<string, unknown>) => Promise<number>;
108
+ abstract save(tableName: string, attr2field: Record<string, string>, pk: Attribute<unknown, boolean, unknown>): (this: EntryBase & Record<string, unknown>) => Promise<number | false>;
109
+ abstract dropConstraints(table: Table): Promise<number[]>;
110
+ abstract dropFields(table: Table): Promise<void>;
111
+ abstract dropIndexes(table: Table, constraintIndexes: number[]): Promise<void>;
112
+ abstract syncConstraints(table: Table): Promise<void>;
113
+ abstract syncFields(table: Table): Promise<void>;
114
+ abstract syncIndexes(table: Table): Promise<void>;
115
+ abstract syncSequence(table: Table): Promise<void>;
116
+ abstract syncTable(table: Table): Promise<void>;
117
+ }
118
+ export declare class Transaction {
119
+ private entries;
120
+ protected log: (message: string) => void;
121
+ constructor(log: (message: string) => void);
122
+ addEntry(entry: EntryBase): void;
123
+ clean(): void;
124
+ commit(): Promise<void>;
125
+ protected preCommit(): void;
126
+ rollback(): Promise<void>;
127
+ }
128
+ export declare function deepCopy(o: unknown): any;
129
+ export declare function deepDiff(a: unknown, b: unknown): boolean;
130
+ export {};
@@ -0,0 +1,165 @@
1
+ import { Attribute, DB, EntryBase, ForeignKeyOptions, Transaction, Type } from "./db";
2
+ export { Attribute, base, DB, deepCopy, deepDiff, EntryBase, ForeignKeyActions, ForeignKeyOptions, Index, loaded, size, Table, Transaction, transaction, TxAction, Type } from "./db";
3
+ export interface AttributeOptions<T, N extends boolean> {
4
+ defaultValue?: T;
5
+ fieldName?: string;
6
+ notNull?: N;
7
+ unique?: boolean;
8
+ }
9
+ export interface AttributeOptionsSize<T, N extends boolean> extends AttributeOptions<T, N> {
10
+ size?: number;
11
+ }
12
+ type NotNull<O> = O extends {
13
+ notNull: true;
14
+ } ? true : false;
15
+ type AttributesDefinitionBase = {
16
+ [key: string]: [unknown, boolean, unknown];
17
+ };
18
+ export type AttributesDefinition<N extends AttributesDefinitionBase = AttributesDefinitionBase> = keyof N extends never ? {
19
+ [s: string]: Type<unknown, boolean, unknown>;
20
+ } : {
21
+ [key in keyof N]: Type<N[key][0], N[key][1], N[key][2]>;
22
+ };
23
+ declare const attributes: unique symbol;
24
+ declare const methods: unique symbol;
25
+ type ForeignKeysAttributes<T, k> = T extends Type<unknown, boolean, infer E> ? (E extends EntryBase ? k : never) : never;
26
+ type ForeignKeys<A extends AttributesDefinition> = Exclude<{
27
+ [a in keyof A]?: ForeignKeysAttributes<A[a], a>;
28
+ }[keyof A], undefined>;
29
+ type Native<T> = T extends Type<infer N, infer NN, unknown> ? (NN extends true ? N : N | null) : never;
30
+ export type IndexAttributes = string[] | string;
31
+ export interface IndexOptions {
32
+ attributes: IndexAttributes;
33
+ type?: "btree" | "hash";
34
+ unique?: boolean;
35
+ }
36
+ export type IndexDefinition = IndexAttributes | IndexOptions;
37
+ export type IndexesDefinition = {
38
+ [key: string]: IndexDefinition;
39
+ };
40
+ interface BaseModelOptions {
41
+ indexes?: IndexesDefinition;
42
+ sync?: boolean;
43
+ tableName?: string;
44
+ }
45
+ export interface ModelOptions extends BaseModelOptions {
46
+ int8id?: boolean;
47
+ parent?: Attribute<unknown, boolean, EntryBase>;
48
+ primaryKey?: string;
49
+ }
50
+ type ConditionAttribute<T> = T | ["=" | ">" | "<" | ">=" | "<=" | "<>", T] | ["IN", T[]] | ["IS NULL"] | ["LIKE", string] | ["NOT"];
51
+ type ConditionBase<A extends AttributesDefinition> = string | {
52
+ [a in keyof A]?: ConditionAttribute<Native<A[a]>>;
53
+ };
54
+ type Condition<A extends AttributesDefinition> = ConditionBase<A> | ["NOT", Condition<A>] | ["AND", ...Condition<A>[]] | ["OR", ...Condition<A>[]];
55
+ type Order_<A extends AttributesDefinition> = keyof A | `-${string & keyof A}`;
56
+ type Order<A extends AttributesDefinition> = Order_<A> | Order_<A>[];
57
+ type UnionToIntersection<U> = (U extends unknown ? (k: U) => void : never) extends (k: infer I) => void ? I : never;
58
+ type IsUnion<T> = [T] extends [UnionToIntersection<T>] ? false : true;
59
+ type BaseKeyType<B extends boolean> = IsUnion<B> extends true ? number : B extends true ? string : number;
60
+ type KeyType<B extends boolean, P extends ModelStd> = P extends new () => EntryBase ? (P extends Attribute<infer T, boolean, EntryBase> ? T : never) : BaseKeyType<B>;
61
+ type ForeignKey<A> = A extends Type<unknown, boolean, infer E> ? () => Promise<E> : never;
62
+ type EntryBaseAttributes<A extends AttributesDefinition> = {
63
+ [a in keyof A]: Native<A[a]>;
64
+ };
65
+ type EntryMethodsBase<P extends ModelStd> = P extends new () => EntryBase ? P[typeof methods] : EntryBase;
66
+ type EntryMethodsFK<A extends AttributesDefinition> = {
67
+ [a in ForeignKeys<A> & string as `${a}Load`]: ForeignKey<A[a]>;
68
+ };
69
+ type EntryMethods<A extends AttributesDefinition, P extends ModelStd> = keyof EntryMethodsFK<A> extends never ? EntryMethodsBase<P> : EntryMethodsBase<P> & EntryMethodsFK<A>;
70
+ type ModelAttributesIf<A extends AttributesDefinition, T> = keyof A extends never ? T : T & A;
71
+ 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[typeof attributes] : {
72
+ id: Type<BaseKeyType<B>, true, unknown>;
73
+ }>;
74
+ export interface ModelLoad<A extends AttributesDefinition, E extends EntryBase> {
75
+ load(where: Condition<A>, order?: Order<A>, limit?: number, tx?: Transaction, lock?: boolean): Promise<E[]>;
76
+ load(where: Condition<A>, order?: Order<A>, tx?: Transaction, lock?: boolean): Promise<E[]>;
77
+ load(where: Condition<A>, limit?: number, tx?: Transaction, lock?: boolean): Promise<E[]>;
78
+ load(where: Condition<A>, tx: Transaction, lock?: boolean): Promise<E[]>;
79
+ cancel(where: Condition<A>, tx?: Transaction): Promise<number>;
80
+ }
81
+ 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, true, E> & {
82
+ [attributes]: A;
83
+ foreignKeys: Record<string, boolean>;
84
+ [methods]: EM;
85
+ parent?: ModelStd;
86
+ tableName: string;
87
+ } & {
88
+ [a in keyof A]: Attribute<Native<A[a]>, boolean, E>;
89
+ } & ModelLoad<A, E>;
90
+ type Model<T, A extends AttributesDefinition, EM extends EntryBase> = ModelBase<T, A, EntryBaseAttributes<A>, EM, EntryBaseAttributes<A> & EM>;
91
+ type ModelStd = Attribute<unknown, true, EntryBase> & {
92
+ [attributes]: AttributesDefinition;
93
+ foreignKeys: Record<string, boolean>;
94
+ [methods]: EntryBase;
95
+ parent?: ModelStd;
96
+ };
97
+ export type Entry<M> = M extends new () => infer E ? E : never;
98
+ export type OrderBy<M> = M extends {
99
+ load(where: unknown, order?: infer T): void;
100
+ load(where: unknown, limit?: number): void;
101
+ load(where: unknown, tx?: Transaction): void;
102
+ } ? Exclude<T, undefined> : never;
103
+ export type Where<M> = M extends {
104
+ load(where: infer T): void;
105
+ } ? T : never;
106
+ export interface SedentaryOptions {
107
+ autoSync?: boolean;
108
+ log?: ((message: string) => void) | null;
109
+ sync?: boolean;
110
+ }
111
+ /**
112
+ * Converts a JavaScript name to the default SQL name (snake_case).
113
+ * Uppercase letters become lowercase preceded by underscore, except the first character
114
+ * which is only lowercased without underscore.
115
+ */
116
+ export declare function toSqlName(name: string): string;
117
+ export declare class Sedentary<D extends DB<T>, T extends Transaction> {
118
+ protected autoSync: boolean;
119
+ protected db: D;
120
+ protected doSync: boolean;
121
+ protected log: (message: string) => void;
122
+ private models;
123
+ constructor(options?: SedentaryOptions);
124
+ Boolean<O extends AttributeOptions<boolean, boolean> | undefined = undefined>(options?: O): Type<boolean, NotNull<O>, unknown>;
125
+ DateTime<O extends AttributeOptions<Date, boolean> | undefined = undefined>(options?: O): Type<Date, NotNull<O>, unknown>;
126
+ FKey<T, E extends EntryBase>(attribute: Attribute<T, boolean, E>, options?: ForeignKeyOptions): Type<T, boolean, E>;
127
+ Float<O extends AttributeOptionsSize<number, boolean> | undefined = undefined>(options?: O): Type<number, NotNull<O>, unknown>;
128
+ Int<O extends AttributeOptionsSize<number, boolean> | undefined = undefined>(options?: O): Type<number, NotNull<O>, unknown>;
129
+ Int8<O extends AttributeOptions<bigint, boolean> | undefined = undefined>(options?: O): Type<bigint, NotNull<O>, unknown>;
130
+ JSON<T = unknown>(): Type<T, false, unknown>;
131
+ JSON<T = unknown>(options: {
132
+ notNull: true;
133
+ } & AttributeOptions<T, boolean>): Type<T, true, unknown>;
134
+ JSON<T = unknown>(options: AttributeOptions<T, boolean>): Type<T, boolean, unknown>;
135
+ Number<O extends AttributeOptions<number, boolean> | undefined = undefined>(options?: O): Type<number, NotNull<O>, unknown>;
136
+ None<T = unknown>(): Type<T, false, unknown>;
137
+ None<T = unknown>(options: {
138
+ notNull: true;
139
+ } & AttributeOptions<unknown, boolean>): Type<T, true, unknown>;
140
+ None<T = unknown>(options: AttributeOptions<unknown, boolean>): Type<T, boolean, unknown>;
141
+ VarChar<S extends string = string>(): Type<S, false, unknown>;
142
+ VarChar<S extends string = string>(options: {
143
+ notNull: true;
144
+ } & AttributeOptionsSize<S, boolean>): Type<S, true, unknown>;
145
+ VarChar<S extends string = string>(options: AttributeOptionsSize<S, boolean>): Type<S, boolean, unknown>;
146
+ private checkDB;
147
+ private checkOrderBy;
148
+ private checkSize;
149
+ private createWhere;
150
+ connect(sync?: boolean): Promise<void>;
151
+ sync(): Promise<void>;
152
+ end(): Promise<void>;
153
+ begin(): Promise<T>;
154
+ escape(value: unknown): string;
155
+ model<A extends AttributesDefinition, B extends boolean, K extends string, P extends ModelStd, EM extends EntryMethods<A, P>>(modelName: string, attributes: A, options?: BaseModelOptions & {
156
+ int8id?: B;
157
+ parent?: P;
158
+ primaryKey?: K | keyof A;
159
+ }): Model<K extends keyof A ? Native<A[K]> : KeyType<B, P>, ModelAttributes<A, B, K, P>, EM>;
160
+ 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 & {
161
+ int8id?: B;
162
+ parent?: P;
163
+ primaryKey?: K | keyof A;
164
+ }, 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>;
165
+ }
package/package.json CHANGED
@@ -9,6 +9,9 @@
9
9
  "engines": {
10
10
  "node": ">=20.19"
11
11
  },
12
+ "files": [
13
+ "dist"
14
+ ],
12
15
  "funding": {
13
16
  "url": "https://blockchain.info/address/1Md9WFAHrXTb3yPBwQWmUfv2RmzrtbHioB"
14
17
  },
@@ -34,23 +37,6 @@
34
37
  "deploy": "make deploy",
35
38
  "preinstall": "if [ -f Makefile ] ; then make ; fi"
36
39
  },
37
- "tsd": {
38
- "compilerOptions": {
39
- "alwaysStrict": true,
40
- "composite": false,
41
- "esModuleInterop": true,
42
- "moduleResolution": "Node",
43
- "noImplicitAny": true,
44
- "noImplicitReturns": true,
45
- "noImplicitThis": true,
46
- "strict": true,
47
- "strictBindCallApply": true,
48
- "strictFunctionTypes": true,
49
- "strictNullChecks": true,
50
- "strictPropertyInitialization": true,
51
- "target": "ESNext"
52
- }
53
- },
54
40
  "types": "./dist/types/index.d.ts",
55
- "version": "0.1.0"
41
+ "version": "0.1.1"
56
42
  }