@idb-orm/core 1.0.3

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 ADDED
@@ -0,0 +1,38 @@
1
+ # IDB-ORM
2
+
3
+ ## Roadmap
4
+
5
+ - [x] "Include" query field
6
+ - [x] Enforce that either "include" xor "select" is in the query object
7
+ - [x] modify the query object so that on relations it is recursive
8
+ - [ ] Complete update action
9
+ - [ ] Finish `updateSingleton` action
10
+ - [ ] Unique model fields: Use "unique" indexes to enforce this.
11
+ - [ ] Optimize batch `add` editing with cursor functionality
12
+ - [ ] Make sure non-optional and non-array relations do not have the `SetNull` onDelete action
13
+ - [ ] Dump database to different formats:
14
+ - [ ] JSON
15
+ - [ ] CSV
16
+ - [ ] YAML
17
+ - [ ] Add extra object syntax to "where" object (i.e. `in`/`ne`/`gt`/...)
18
+ - [ ] On bulk add/puts/deletes, only wait for the last IDBRequest object
19
+ - [ ] addMany
20
+ - [ ] deleteMany
21
+ - [ ] findMany
22
+ - [ ] updateMany
23
+ - [ ] Convert internal string unions to enums
24
+ - [ ] Remove external dependencies
25
+ - [ ] deep-equal
26
+ - [x] type-fest
27
+ - [ ] uuid
28
+ - [ ] zod
29
+ - [ ] Make subpackages for adapters for different validation languages
30
+ - [ ] Zod
31
+ - [ ] Yup
32
+ - [ ] Joi
33
+ - [ ] schema.js
34
+
35
+ ### Roadmap - Maybe
36
+
37
+ - [ ] Discriminated union models: Be able to differentiate subtypes of a model by a discriminator key
38
+ - [ ] Error Handling: Instead of needing to type `tx.abort(...)` just use the `throw new ...` syntax and catch the error and automatically abort the transaction. This will require actions to be wrapped in some kind of try-catch block.
@@ -0,0 +1,25 @@
1
+ import { Model, type CollectionZodSchema } from "./model";
2
+ import { DbClient } from "./client";
3
+ import { PrimaryKey, type ValidValue } from "./field";
4
+ import type { Dict } from "./types/common";
5
+ export type CollectionObject<Names extends string> = {
6
+ [K in Names]: Model<K, any>;
7
+ };
8
+ export declare class Builder<Name extends string, Names extends string> {
9
+ readonly name: Name;
10
+ readonly names: Names[];
11
+ private models;
12
+ constructor(name: Name, names: Names[]);
13
+ defineModel<N extends Names, T extends Dict<ValidValue<Names>>>(name: N, values: T): Model<N, T, Extract<{ [K in keyof T]: T[K] extends PrimaryKey<any, any> ? K : never; }[keyof T], string>>;
14
+ compile<M extends CollectionObject<Names>>(models: M): CompiledDb<Name, Names, M>;
15
+ }
16
+ export declare class CompiledDb<Name extends string, Names extends string, C extends CollectionObject<Names>> {
17
+ readonly name: Name;
18
+ private readonly models;
19
+ readonly schemas: CollectionZodSchema<C>;
20
+ private readonly modelKeys;
21
+ constructor(name: Name, models: C);
22
+ getModel<N extends Names>(name: N): C[N];
23
+ createClient(version?: number): Promise<DbClient<Name, Names, C>>;
24
+ keys(): Names[];
25
+ }
@@ -0,0 +1,23 @@
1
+ import type { CollectionObject } from "../builder";
2
+ import type { DbClient } from "./index";
3
+ import type { FindInput, FindOutput } from "./types/find";
4
+ export declare class CompiledQuery<
5
+ Stores extends string,
6
+ Models extends CollectionObject<string>,
7
+ Db extends DbClient<string, Stores, Models>,
8
+ Input extends FindInput<Stores, Models[Stores], Models> = FindInput<
9
+ Stores,
10
+ Models[Stores],
11
+ Models
12
+ >,
13
+ Output = FindOutput<Stores, Models[Stores], Models, Input>
14
+ > {
15
+ private readonly client;
16
+ private readonly name;
17
+ private readonly accessedStores;
18
+ private readonly selectClause;
19
+ constructor(client: Db, name: Stores, input: Input);
20
+ find(): Promise<Output[]>;
21
+ findFirst(): Promise<Output>;
22
+ private _find;
23
+ }
@@ -0,0 +1,11 @@
1
+ import type { Dict } from "../types/common.js";
2
+ import type { DbClient } from "./index.ts";
3
+ import type { CollectionObject } from "../builder.ts";
4
+ import type { QueryInput } from "./types/find.ts";
5
+ import type { Transaction } from "../transaction.js";
6
+ import type { Promisable } from "type-fest";
7
+ export declare function generateWhereClause(where?: Dict): (obj: unknown) => boolean;
8
+ export declare function generateSelector<ModelNames extends string, Models extends CollectionObject<ModelNames>, Db extends DbClient<string, ModelNames, Models>, Q extends QueryInput<ModelNames, any, Models> = QueryInput<ModelNames, any, Models>>(name: ModelNames, client: Db, query?: Q, initTx?: Transaction<IDBTransactionMode, ModelNames>): (item: Dict, tx: Transaction<IDBTransactionMode, ModelNames>) => Promisable<Dict | undefined>;
9
+ export declare function getAccessedStores<ModelNames extends string, Models extends CollectionObject<ModelNames>>(name: ModelNames, query: Dict, type: "mutation" | "query", client: DbClient<string, ModelNames, Models>): Set<ModelNames>;
10
+ export declare function getSearchableQuery(q: QueryInput<any, any, any>): import("./types/find.ts").SelectObject<any, any, any>;
11
+ export declare function promiseCatch(tx: Transaction<IDBTransactionMode, string>): (error: any) => never;
@@ -0,0 +1,61 @@
1
+ import type { Arrayable } from "type-fest";
2
+ import type { CollectionObject, CompiledDb } from "../builder";
3
+ import { Transaction, type TransactionOptions } from "../transaction";
4
+ import type { InterfaceMap } from "./types/index";
5
+ export declare class DbClient<
6
+ Name extends string,
7
+ ModelNames extends string,
8
+ Models extends CollectionObject<ModelNames>
9
+ > {
10
+ private readonly db;
11
+ private readonly models;
12
+ readonly name: Name;
13
+ readonly version: number;
14
+ readonly stores: InterfaceMap<ModelNames, Models>;
15
+ constructor(db: IDBDatabase, models: CompiledDb<Name, ModelNames, Models>);
16
+ getStore<Name extends ModelNames>(name: Name): (typeof this.stores)[Name];
17
+ createTransaction<
18
+ Mode extends IDBTransactionMode,
19
+ Names extends ModelNames
20
+ >(
21
+ mode: Mode,
22
+ stores: Arrayable<Names>,
23
+ options?: TransactionOptions
24
+ ): Transaction<Mode, Names>;
25
+ deleteDb(): Promise<void>;
26
+ deleteAllStores(): void;
27
+ deleteStore(storeNames: Arrayable<ModelNames>): void;
28
+ getModel<N extends ModelNames>(name: N): Models[N];
29
+ private getAccessedStores;
30
+ private createInterface;
31
+ private add;
32
+ private clear;
33
+ /**
34
+ * Deletes a single document from a store given its Primary key value
35
+ * @param name Name of the store
36
+ * @param key Primary key value
37
+ * @param _state Optional state for multi-stage actions
38
+ * @returns Boolean indicating whether or not an item was removed
39
+ */
40
+ private deleteSingleton;
41
+ /**
42
+ * Deletes documents from the store that match the given filter.
43
+ * @param name Name of the store
44
+ * @param where Optional filter
45
+ * @param stopOnFirst Flag to stop after one successful deletion
46
+ * @param _state Optional state for multi-stage actions
47
+ * @returns The number of documents deleted
48
+ */
49
+ private delete;
50
+ /**
51
+ * Finds documents from the store that match the filter
52
+ * @param name Name of the store
53
+ * @param item Object containing the filter and the selection query
54
+ * @param stopOnFirst Flag to stop after one successful find
55
+ * @param _state Optional state for mutli-stage actions
56
+ * @returns Transformed selection item
57
+ */
58
+ private find;
59
+ private update;
60
+ private updateSingleton;
61
+ }
@@ -0,0 +1,107 @@
1
+ import type { Dict, If, Keyof, RemoveNeverValues } from "../../types/common.js";
2
+ import type {
3
+ ExtractFields,
4
+ ModelStructure,
5
+ RelationlessModelStructure,
6
+ } from "../../model/model-types.js";
7
+ import type { Model } from "../../model/index";
8
+ import type {
9
+ BaseRelation,
10
+ Field,
11
+ PrimaryKey,
12
+ RelationOutputStructure,
13
+ ValidValue,
14
+ } from "../../field/index";
15
+ import type { CollectionObject } from "../../builder";
16
+ export type FilterFn<Input> = (item: Input) => boolean;
17
+ export type WhereObject<Fields extends Dict<ValidValue>> = Partial<
18
+ RemoveNeverValues<{
19
+ [K in keyof Fields]: Fields[K] extends Field<infer Output, any>
20
+ ? Output | FilterFn<Output>
21
+ : Fields[K] extends PrimaryKey<any, infer Type>
22
+ ? Type | FilterFn<Type>
23
+ : never;
24
+ }>
25
+ >;
26
+ export type SelectObject<
27
+ All extends string,
28
+ Fields extends Dict<ValidValue>,
29
+ C extends CollectionObject<All>
30
+ > = {
31
+ [K in keyof Fields]?: Fields[K] extends Field<any, any>
32
+ ? true
33
+ : Fields[K] extends PrimaryKey<any, any>
34
+ ? true
35
+ : Fields[K] extends BaseRelation<infer To, any>
36
+ ?
37
+ | true
38
+ | (To extends Keyof<C>
39
+ ? C[To] extends Model<any, infer SubFields, any>
40
+ ? QueryInput<All, SubFields, C>
41
+ : never
42
+ : never)
43
+ : never;
44
+ };
45
+ export interface QueryInput<
46
+ All extends string,
47
+ Fields extends Dict<ValidValue>,
48
+ C extends CollectionObject<All>
49
+ > {
50
+ where?: WhereObject<Fields>;
51
+ select?: SelectObject<All, Fields, C>;
52
+ include?: SelectObject<All, Fields, C>;
53
+ }
54
+ export type FindInput<
55
+ All extends string,
56
+ Struct extends object,
57
+ C extends CollectionObject<All>
58
+ > = Struct extends Model<any, infer Fields, any>
59
+ ? QueryInput<All, Fields, C>
60
+ : never;
61
+ type _FindOutput<
62
+ All extends string,
63
+ Select extends Dict<Dict | true>,
64
+ Fields extends Dict<ValidValue>,
65
+ C extends CollectionObject<All>
66
+ > =
67
+ | {
68
+ [K in Keyof<Select>]: Fields[K] extends BaseRelation<infer To, any>
69
+ ? To extends Keyof<C>
70
+ ? C[To] extends Model<any, infer Sub, any>
71
+ ? If<
72
+ Select[K] extends true ? true : false,
73
+ RelationOutputStructure<
74
+ Fields[K],
75
+ RelationlessModelStructure<C[To]>
76
+ >,
77
+ Select[K] extends Dict<Dict | true>
78
+ ? RelationOutputStructure<
79
+ Fields[K],
80
+ _FindOutput<All, Select[K], Sub, C>
81
+ >
82
+ : never
83
+ >
84
+ : never
85
+ : never
86
+ : Fields[K] extends PrimaryKey<any, infer Type>
87
+ ? Type
88
+ : Fields[K] extends Field<infer Type, any>
89
+ ? Type
90
+ : never;
91
+ }
92
+ | undefined;
93
+ export type FindOutput<
94
+ All extends string,
95
+ Struct extends Model<any, any, any>,
96
+ C extends CollectionObject<All>,
97
+ F extends FindInput<All, Struct, C>
98
+ > = Struct extends Model<any, infer Fields, any>
99
+ ? F extends object
100
+ ?
101
+ | (F["select"] extends Dict<true | Dict>
102
+ ? _FindOutput<All, F["select"], Fields, C>
103
+ : ModelStructure<ExtractFields<Struct>, C>)
104
+ | undefined
105
+ : never
106
+ : never;
107
+ export {};
@@ -0,0 +1,98 @@
1
+ import type { CollectionObject } from "../../builder";
2
+ import type { Dict, ValidKey } from "../../types/common.js";
3
+ import type {
4
+ ExtractFields,
5
+ Model,
6
+ ModelStructure,
7
+ PrimaryKeyType,
8
+ } from "../../model/index";
9
+ import type { AddMutation, UpdateMutation } from "./mutation";
10
+ import type { FindInput, FindOutput, WhereObject } from "./find";
11
+ import type { CompiledQuery } from "../compiled-query";
12
+ import type { DbClient } from "../index";
13
+ import { Transaction } from "../../transaction.js";
14
+ import { BaseRelation } from "../../field/index";
15
+ export type InsertMutation<
16
+ N extends string,
17
+ C extends Dict
18
+ > = C[N] extends Model<N, infer F, any> ? ModelStructure<F, C> : never;
19
+ export interface StoreInterface<
20
+ Name extends Names,
21
+ Names extends string,
22
+ C extends CollectionObject<Names>,
23
+ KeyType = PrimaryKeyType<C[Name]>,
24
+ Add = AddMutation<Name, Names, C[Name], C>,
25
+ Update = UpdateMutation<Name, Names, C[Name], C>
26
+ > {
27
+ add(
28
+ mutation: Add,
29
+ transaction?: Transaction<"readwrite", Names>
30
+ ): Promise<KeyType>;
31
+ addMany(
32
+ mutations: Add[],
33
+ transaction?: Transaction<"readwrite", Names>
34
+ ): Promise<KeyType[]>;
35
+ find<T extends FindInput<Names, C[Name], C>>(
36
+ query: T,
37
+ transaction?: Transaction<IDBTransactionMode, Names>
38
+ ): Promise<FindOutput<Names, C[Name], C, T>[]>;
39
+ findFirst<T extends FindInput<Names, C[Name], C>>(
40
+ query: T,
41
+ transaction?: Transaction<IDBTransactionMode, Names>
42
+ ): Promise<FindOutput<Names, C[Name], C, T>>;
43
+ put(): Promise<void>;
44
+ insert(
45
+ item: InsertMutation<Name, C>,
46
+ transaction?: Transaction<"readwrite", Names>
47
+ ): Promise<KeyType>;
48
+ updateFirst(
49
+ item: Update,
50
+ transaction?: Transaction<"readwrite", Names>
51
+ ): Promise<KeyType | undefined>;
52
+ updateMany(
53
+ item: Update,
54
+ transaction?: Transaction<"readwrite", Names>
55
+ ): Promise<KeyType[]>;
56
+ delete(key: KeyType): Promise<boolean>;
57
+ deleteFirst(where?: WhereObject<ExtractFields<C[Name]>>): Promise<boolean>;
58
+ deleteMany(where: WhereObject<ExtractFields<C[Name]>>): Promise<number>;
59
+ /**
60
+ * Clears a store (does not update any relations)
61
+ */
62
+ clear(transaction?: Transaction<"readwrite", Names>): Promise<void>;
63
+ compileQuery<T extends FindInput<Names, C[Name], C>>(
64
+ query: T
65
+ ): CompiledQuery<Names, C, DbClient<string, Names, C>, T>;
66
+ get(key: KeyType): Promise<any>;
67
+ }
68
+ export type InterfaceMap<
69
+ Names extends string,
70
+ C extends CollectionObject<Names>
71
+ > = {
72
+ [K in Names]: StoreInterface<K, Names, C>;
73
+ };
74
+ export interface QueryState<Names extends string> {
75
+ tx?: Transaction<IDBTransactionMode, Names>;
76
+ }
77
+ export interface MutationState<Names extends string>
78
+ extends Partial<{
79
+ tx: Transaction<"readwrite", Names>;
80
+ relation: {
81
+ id: ValidKey;
82
+ key: string;
83
+ };
84
+ }> {}
85
+ export type KeyObject<Index = string> = {
86
+ isFun: boolean;
87
+ key: Index;
88
+ } & (
89
+ | {
90
+ isRelation: true;
91
+ relation: BaseRelation<any, any>;
92
+ }
93
+ | {
94
+ isRelation: false;
95
+ relation?: undefined;
96
+ }
97
+ );
98
+ export type { AddMutation, UpdateMutation };
@@ -0,0 +1,225 @@
1
+ import type {
2
+ MakeArrayable,
3
+ MakeOptional,
4
+ RemoveNeverValues,
5
+ PartialOnUndefined,
6
+ } from "../../types/common.js";
7
+ import type {
8
+ BaseRelation,
9
+ Field,
10
+ OptionalRelation,
11
+ PrimaryKey,
12
+ RelationArray,
13
+ } from "../../field/index";
14
+ import type { CollectionObject } from "../../builder";
15
+ import { WhereObject } from "./find.js";
16
+ import { Model, FindRelationKey, RelationValue } from "../../model/index";
17
+ export type MutationActions =
18
+ | "$connect"
19
+ | "$connectMany"
20
+ | "$create"
21
+ | "$createMany"
22
+ | "$update"
23
+ | "$updateMany"
24
+ | "$delete"
25
+ | "$deleteMany"
26
+ | "$deleteAll"
27
+ | "$disconnect"
28
+ | "$disconnectMany"
29
+ | "$disconnectAll";
30
+ export type Mutation<
31
+ This extends All,
32
+ All extends string,
33
+ Struct extends object,
34
+ C extends CollectionObject<All>,
35
+ MutType extends string = "add"
36
+ > = PartialOnUndefined<
37
+ RemoveNeverValues<
38
+ Struct extends Model<any, infer Fields, any>
39
+ ? {
40
+ [K in keyof Fields]: Fields[K] extends Field<
41
+ infer Type,
42
+ infer HasDefault
43
+ >
44
+ ? MutType extends "update"
45
+ ? Type | undefined | ((value: Type) => Type)
46
+ : HasDefault extends true
47
+ ? Type | undefined
48
+ : Type
49
+ : Fields[K] extends PrimaryKey<infer IsAuto, infer Type>
50
+ ? MutType extends "update"
51
+ ? never
52
+ : IsAuto extends true
53
+ ? never
54
+ : Type
55
+ : Fields[K] extends BaseRelation<infer To, infer Name>
56
+ ? To extends All
57
+ ? MakeOptional<
58
+ Fields[K] extends OptionalRelation<any, any>
59
+ ? true
60
+ : Fields[K] extends RelationArray<any, any>
61
+ ? true
62
+ : MutType extends "update"
63
+ ? true
64
+ : false,
65
+ | MakeArrayable<
66
+ Fields[K] extends RelationArray<any, any>
67
+ ? true
68
+ : false,
69
+ | {
70
+ $connect: RelationValue<To, C>;
71
+ }
72
+ | {
73
+ $create: Omit<
74
+ Mutation<
75
+ To,
76
+ All,
77
+ C[To],
78
+ C,
79
+ "add"
80
+ >,
81
+ FindRelationKey<
82
+ This,
83
+ Name,
84
+ C[To]
85
+ >
86
+ >;
87
+ }
88
+ | (MutType extends "update"
89
+ ?
90
+ | {
91
+ $update: Fields[K] extends RelationArray<
92
+ any,
93
+ any
94
+ >
95
+ ? {
96
+ where?: WhereSelection<
97
+ C[To]
98
+ >;
99
+ data: Mutation<
100
+ To,
101
+ All,
102
+ C[To],
103
+ C,
104
+ MutType
105
+ >;
106
+ }
107
+ : Mutation<
108
+ To,
109
+ All,
110
+ C[To],
111
+ C,
112
+ MutType
113
+ >;
114
+ }
115
+ | {
116
+ $delete: Fields[K] extends RelationArray<
117
+ any,
118
+ any
119
+ >
120
+ ? RelationValue<
121
+ To,
122
+ C
123
+ >
124
+ : true;
125
+ }
126
+ | {
127
+ $disconnect: Fields[K] extends RelationArray<
128
+ any,
129
+ any
130
+ >
131
+ ? RelationValue<
132
+ To,
133
+ C
134
+ >
135
+ : true;
136
+ }
137
+ : never)
138
+ >
139
+ | (Fields[K] extends RelationArray<any, any>
140
+ ?
141
+ | {
142
+ $connectMany: RelationValue<
143
+ To,
144
+ C
145
+ >[];
146
+ }
147
+ | {
148
+ $createMany: Omit<
149
+ Mutation<
150
+ To,
151
+ All,
152
+ C[To],
153
+ C,
154
+ "add"
155
+ >,
156
+ FindRelationKey<
157
+ This,
158
+ Name,
159
+ C[To]
160
+ >
161
+ >[];
162
+ }
163
+ | {
164
+ $updateMany: {
165
+ where?: WhereSelection<
166
+ C[To]
167
+ >;
168
+ data: Mutation<
169
+ To,
170
+ All,
171
+ C[To],
172
+ C,
173
+ MutType
174
+ >;
175
+ }[];
176
+ }
177
+ | {
178
+ $deleteMany: RelationValue<
179
+ To,
180
+ C
181
+ >[];
182
+ }
183
+ | {
184
+ $deleteAll: true;
185
+ }
186
+ | {
187
+ $disconnectMany: RelationValue<
188
+ To,
189
+ C
190
+ >[];
191
+ }
192
+ | {
193
+ $disconnectAll: true;
194
+ }
195
+ : never)
196
+ >
197
+ : never
198
+ : never;
199
+ }
200
+ : never
201
+ >
202
+ >;
203
+ export type AddMutation<
204
+ This extends All,
205
+ All extends string,
206
+ Struct extends object,
207
+ C extends CollectionObject<All>
208
+ > = Mutation<This, All, Struct, C, "add">;
209
+ export interface UpdateMutation<
210
+ This extends All,
211
+ All extends string,
212
+ Struct extends object,
213
+ C extends CollectionObject<All>
214
+ > {
215
+ where?: WhereSelection<Struct>;
216
+ data: Mutation<This, All, Struct, C, "update">;
217
+ }
218
+ type WhereSelection<Struct extends object> = Struct extends Model<
219
+ any,
220
+ infer Fields,
221
+ any
222
+ >
223
+ ? WhereObject<Fields>
224
+ : never;
225
+ export {};