sedentary 0.0.53 → 0.1.0

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.
@@ -1,146 +0,0 @@
1
- import { Attribute, DB, EntryBase, ForeignKeyOptions, Transaction, Type } from "./db";
2
- export { Action, Attribute, base, DB, deepCopy, deepDiff, EntryBase, ForeignKeyActions, ForeignKeyOptions, Index, loaded, size, Table, Transaction, transaction, Type } from "./db";
3
- export 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 type AttributeDefinition<T, E> = TypeDefinition<T, E> | AttributeOptions<T, E>;
12
- export type AttributesDefinition = {
13
- [key: string]: AttributeDefinition<unknown, unknown>;
14
- };
15
- declare const attributes: unique symbol;
16
- declare const methods: unique symbol;
17
- type ForeignKeysAttributes<T, k> = T extends AttributeDefinition<unknown, infer E> ? (E extends EntryBase ? k : never) : never;
18
- type ForeignKeys<A extends AttributesDefinition> = {
19
- [a in keyof A]?: ForeignKeysAttributes<A[a], a>;
20
- }[keyof A];
21
- type Native___<T> = T extends Type<infer N, unknown> ? N : never;
22
- type Native__<T> = T extends () => Type<infer N, infer E> ? Native___<Type<N, E>> : Native___<T>;
23
- type Native_<T, N, E> = T extends {
24
- notNull: true;
25
- } ? Native___<Type<N, E>> : Native___<Type<N, E>> | null;
26
- type Native<T> = T extends AttributeOptions<infer N, infer E> ? Native_<T, N, E> : Native__<T> | null;
27
- export type IndexAttributes = string[] | string;
28
- export interface IndexOptions {
29
- attributes: IndexAttributes;
30
- type?: "btree" | "hash";
31
- unique?: boolean;
32
- }
33
- export type IndexDefinition = IndexAttributes | IndexOptions;
34
- export type IndexesDefinition = {
35
- [key: string]: IndexDefinition;
36
- };
37
- interface BaseModelOptions {
38
- indexes?: IndexesDefinition;
39
- sync?: boolean;
40
- tableName?: string;
41
- }
42
- export interface ModelOptions extends BaseModelOptions {
43
- int8id?: boolean;
44
- parent?: Attribute<unknown, EntryBase>;
45
- primaryKey?: string;
46
- }
47
- type ConditionAttribute<T> = T | ["=" | ">" | "<" | ">=" | "<=" | "<>", T] | ["IN", T[]] | ["IS NULL"] | ["LIKE", string] | ["NOT"];
48
- type ConditionBase<A extends AttributesDefinition> = string | {
49
- [a in keyof A]?: ConditionAttribute<Native<A[a]>>;
50
- };
51
- type Condition<A extends AttributesDefinition> = ConditionBase<A> | ["NOT", Condition<A>] | ["AND", ...Condition<A>[]] | ["OR", ...Condition<A>[]];
52
- type Order_<A extends AttributesDefinition> = keyof A | `-${string & keyof A}`;
53
- type Order<A extends AttributesDefinition> = Order_<A> | Order_<A>[];
54
- type UnionToIntersection<U> = (U extends unknown ? (k: U) => void : never) extends (k: infer I) => void ? I : never;
55
- type IsUnion<T> = [T] extends [UnionToIntersection<T>] ? false : true;
56
- type BaseKeyType<B extends boolean> = IsUnion<B> extends true ? number : B extends true ? string : number;
57
- type KeyType<B extends boolean, P extends ModelStd> = P extends new () => EntryBase ? (P extends Attribute<infer T, EntryBase> ? T : never) : BaseKeyType<B>;
58
- type ForeignKey<A> = A extends AttributeDefinition<unknown, infer E> ? () => Promise<E> : never;
59
- type EntryBaseAttributes<A extends AttributesDefinition> = {
60
- [a in keyof A]: Native<A[a]>;
61
- };
62
- type EntryMethodsBase<P extends ModelStd> = P extends new () => EntryBase ? P[typeof methods] : EntryBase;
63
- type EntryMethodsFK<A extends AttributesDefinition> = {
64
- [a in ForeignKeys<A> & string as `${a}Load`]: ForeignKey<A[a]>;
65
- };
66
- type EntryMethods<A extends AttributesDefinition, P extends ModelStd> = keyof EntryMethodsFK<A> extends never ? EntryMethodsBase<P> : EntryMethodsBase<P> & EntryMethodsFK<A>;
67
- type ModelAttributesIf<A extends AttributesDefinition, T> = keyof A extends never ? T : T & A;
68
- 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] : {
69
- id: {
70
- notNull: true;
71
- type: Type<BaseKeyType<B>, unknown>;
72
- };
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, 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]>, 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, 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
- export declare class Sedentary<D extends DB<T>, T extends Transaction> {
112
- protected autoSync: boolean;
113
- protected db: D;
114
- protected doSync: boolean;
115
- protected log: (message: string) => void;
116
- private models;
117
- constructor(options?: SedentaryOptions);
118
- Boolean(): Type<boolean, unknown>;
119
- DateTime(): Type<Date, unknown>;
120
- FKey<T, E extends EntryBase>(attribute: Attribute<T, E>, options?: ForeignKeyOptions): Type<T, E>;
121
- Int(_size?: number): Type<number, unknown>;
122
- Int8(): Type<bigint, unknown>;
123
- JSON<T>(): Type<T, unknown>;
124
- Number(): Type<number, unknown>;
125
- None<T>(): Type<T, unknown>;
126
- VarChar<S extends string>(_size?: number): Type<S, unknown>;
127
- private checkDB;
128
- private checkOrderBy;
129
- private checkSize;
130
- private createWhere;
131
- connect(sync?: boolean): Promise<void>;
132
- sync(): Promise<void>;
133
- end(): Promise<void>;
134
- begin(): Promise<T>;
135
- escape(value: unknown): string;
136
- model<A extends AttributesDefinition, B extends boolean, K extends string, P extends ModelStd, EM extends EntryMethods<A, P>>(modelName: string, attributes: A, options?: BaseModelOptions & {
137
- int8id?: B;
138
- parent?: P;
139
- primaryKey?: K | keyof A;
140
- }): Model<K extends keyof A ? Native<A[K]> : KeyType<B, P>, ModelAttributes<A, B, K, P>, EM>;
141
- 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 & {
142
- int8id?: B;
143
- parent?: P;
144
- primaryKey?: K | keyof A;
145
- }, 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>;
146
- }