@idb-orm/core 1.0.3 → 1.0.5

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/LICENSE ADDED
@@ -0,0 +1,7 @@
1
+ ISC License
2
+
3
+ Copyright 2025 jtb
4
+
5
+ Permission to use, copy, modify, and/or distribute this software for any purpose with or without fee is hereby granted, provided that the above copyright notice and this permission notice appear in all copies.
6
+
7
+ THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
package/README.md CHANGED
@@ -1,5 +1,9 @@
1
1
  # IDB-ORM
2
2
 
3
+ > THIS PROJECT IS STILL A (ROUGH) WORK IN PROGRESS. I HOPE TO HAVE A GOOD WORKING VERSION SOON.
4
+ >
5
+ > PLEASE CHECK BACK IN A FEW WEEKS/MONTHS
6
+
3
7
  ## Roadmap
4
8
 
5
9
  - [x] "Include" query field
@@ -1,17 +1,7 @@
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
- > {
1
+ import type { CollectionObject } from "../builder.ts";
2
+ import type { DbClient } from "./index.ts";
3
+ import type { FindInput, FindOutput } from "./types/find.ts";
4
+ export declare class CompiledQuery<Stores extends string, Models extends CollectionObject<string>, Db extends DbClient<string, Stores, Models>, Input extends FindInput<Stores, Models[Stores], Models> = FindInput<Stores, Models[Stores], Models>, Output = FindOutput<Stores, Models[Stores], Models, Input>> {
15
5
  private readonly client;
16
6
  private readonly name;
17
7
  private readonly accessedStores;
@@ -0,0 +1,14 @@
1
+ import { CollectionObject } from "../builder.js";
2
+ import { ExtractFields } from "../model/model-types.js";
3
+ import { WhereObject } from "./types/find.js";
4
+ import { MutationState } from "./types";
5
+ import { DbClient } from "./index.js";
6
+ /**
7
+ * Deletes document(s) from a store
8
+ * @param name Name of the store
9
+ * @param client DbClient object
10
+ * @param stopOnFirst Stop after the first deletion
11
+ * @param _state Optional state for multi-stage actions
12
+ * @returns Number of items removed
13
+ */
14
+ export declare function deleteItems<ModelNames extends string, Name extends ModelNames, Models extends CollectionObject<ModelNames>>(name: Name, client: DbClient<string, ModelNames, Models>, where?: WhereObject<ExtractFields<Models[Name]>>, stopOnFirst?: boolean, _state?: MutationState<ModelNames>): Promise<number>;
@@ -1,9 +1,8 @@
1
- import type { Dict } from "../types/common.js";
1
+ import type { Dict, Promisable } from "../types/common.js";
2
2
  import type { DbClient } from "./index.ts";
3
3
  import type { CollectionObject } from "../builder.ts";
4
4
  import type { QueryInput } from "./types/find.ts";
5
5
  import type { Transaction } from "../transaction.js";
6
- import type { Promisable } from "type-fest";
7
6
  export declare function generateWhereClause(where?: Dict): (obj: unknown) => boolean;
8
7
  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
8
  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>;
@@ -1,12 +1,8 @@
1
- import type { Arrayable } from "type-fest";
2
1
  import type { CollectionObject, CompiledDb } from "../builder";
2
+ import type { Arrayable } from "../types/common";
3
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
- > {
4
+ import { type InterfaceMap } from "./types";
5
+ export declare class DbClient<Name extends string, ModelNames extends string, Models extends CollectionObject<ModelNames>> {
10
6
  private readonly db;
11
7
  private readonly models;
12
8
  readonly name: Name;
@@ -14,14 +10,7 @@ export declare class DbClient<
14
10
  readonly stores: InterfaceMap<ModelNames, Models>;
15
11
  constructor(db: IDBDatabase, models: CompiledDb<Name, ModelNames, Models>);
16
12
  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>;
13
+ createTransaction<Mode extends IDBTransactionMode, Names extends ModelNames>(mode: Mode, stores: Arrayable<Names>, options?: TransactionOptions): Transaction<Mode, Names>;
25
14
  deleteDb(): Promise<void>;
26
15
  deleteAllStores(): void;
27
16
  deleteStore(storeNames: Arrayable<ModelNames>): void;
@@ -30,23 +19,6 @@ export declare class DbClient<
30
19
  private createInterface;
31
20
  private add;
32
21
  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
22
  /**
51
23
  * Finds documents from the store that match the filter
52
24
  * @param name Name of the store
@@ -1,107 +1,23 @@
1
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";
2
+ import type { ExtractFields, ModelStructure, RelationlessModelStructure } from "../../model/model-types.js";
3
+ import type { Model } from "../../model";
4
+ import type { BaseRelation, Field, PrimaryKey, RelationOutputStructure, ValidValue } from "../../field";
5
+ import type { CollectionObject } from "../../builder.ts";
16
6
  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;
7
+ export type WhereObject<Fields extends Dict<ValidValue>> = Partial<RemoveNeverValues<{
8
+ [K in keyof Fields]: Fields[K] extends Field<infer Output, any> ? Output | FilterFn<Output> : Fields[K] extends PrimaryKey<any, infer Type> ? Type | FilterFn<Type> : never;
9
+ }>>;
10
+ export type SelectObject<All extends string, Fields extends Dict<ValidValue>, C extends CollectionObject<All>> = {
11
+ [K in keyof Fields]?: Fields[K] extends Field<any, any> ? true : Fields[K] extends PrimaryKey<any, any> ? true : Fields[K] extends BaseRelation<infer To, any> ? true | (To extends Keyof<C> ? C[To] extends Model<any, infer SubFields, any> ? QueryInput<All, SubFields, C> : never : never) : never;
44
12
  };
45
- export interface QueryInput<
46
- All extends string,
47
- Fields extends Dict<ValidValue>,
48
- C extends CollectionObject<All>
49
- > {
13
+ export interface QueryInput<All extends string, Fields extends Dict<ValidValue>, C extends CollectionObject<All>> {
50
14
  where?: WhereObject<Fields>;
51
15
  select?: SelectObject<All, Fields, C>;
52
16
  include?: SelectObject<All, Fields, C>;
53
17
  }
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;
18
+ export type FindInput<All extends string, Struct extends object, C extends CollectionObject<All>> = Struct extends Model<any, infer Fields, any> ? QueryInput<All, Fields, C> : never;
19
+ type _FindOutput<All extends string, Select extends Dict<Dict | true>, Fields extends Dict<ValidValue>, C extends CollectionObject<All>> = {
20
+ [K in Keyof<Select>]: Fields[K] extends BaseRelation<infer To, any> ? To extends Keyof<C> ? C[To] extends Model<any, infer Sub, any> ? If<Select[K] extends true ? true : false, RelationOutputStructure<Fields[K], RelationlessModelStructure<C[To]>>, Select[K] extends Dict<Dict | true> ? RelationOutputStructure<Fields[K], _FindOutput<All, Select[K], Sub, C>> : never> : never : never : Fields[K] extends PrimaryKey<any, infer Type> ? Type : Fields[K] extends Field<infer Type, any> ? Type : never;
21
+ } | undefined;
22
+ export type FindOutput<All extends string, Struct extends Model<any, any, any>, C extends CollectionObject<All>, F extends FindInput<All, Struct, C>> = Struct extends Model<any, infer Fields, any> ? F extends object ? (F["select"] extends Dict<true | Dict> ? _FindOutput<All, F["select"], Fields, C> : ModelStructure<ExtractFields<Struct>, C>) | undefined : never : never;
107
23
  export {};
@@ -1,58 +1,22 @@
1
- import type { CollectionObject } from "../../builder";
1
+ import type { CollectionObject } from "../../builder.ts";
2
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";
3
+ import type { ExtractFields, Model, ModelStructure, PrimaryKeyType } from "../../model";
4
+ import type { AddMutation, UpdateMutation } from "./mutation.ts";
5
+ import type { FindInput, FindOutput, WhereObject } from "./find.ts";
6
+ import type { CompiledQuery } from "../compiled-query.ts";
7
+ import type { DbClient } from "../index.ts";
13
8
  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>>;
9
+ import { BaseRelation } from "../../field";
10
+ export type InsertMutation<N extends string, C extends Dict> = C[N] extends Model<N, infer F, any> ? ModelStructure<F, C> : never;
11
+ export interface StoreInterface<Name extends Names, Names extends string, C extends CollectionObject<Names>, KeyType = PrimaryKeyType<C[Name]>, Add = AddMutation<Name, Names, C[Name], C>, Update = UpdateMutation<Name, Names, C[Name], C>> {
12
+ add(mutation: Add, transaction?: Transaction<"readwrite", Names>): Promise<KeyType>;
13
+ addMany(mutations: Add[], transaction?: Transaction<"readwrite", Names>): Promise<KeyType[]>;
14
+ find<T extends FindInput<Names, C[Name], C>>(query: T, transaction?: Transaction<IDBTransactionMode, Names>): Promise<FindOutput<Names, C[Name], C, T>[]>;
15
+ findFirst<T extends FindInput<Names, C[Name], C>>(query: T, transaction?: Transaction<IDBTransactionMode, Names>): Promise<FindOutput<Names, C[Name], C, T>>;
43
16
  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[]>;
17
+ insert(item: InsertMutation<Name, C>, transaction?: Transaction<"readwrite", Names>): Promise<KeyType>;
18
+ updateFirst(item: Update, transaction?: Transaction<"readwrite", Names>): Promise<KeyType | undefined>;
19
+ updateMany(item: Update, transaction?: Transaction<"readwrite", Names>): Promise<KeyType[]>;
56
20
  delete(key: KeyType): Promise<boolean>;
57
21
  deleteFirst(where?: WhereObject<ExtractFields<C[Name]>>): Promise<boolean>;
58
22
  deleteMany(where: WhereObject<ExtractFields<C[Name]>>): Promise<number>;
@@ -60,39 +24,39 @@ export interface StoreInterface<
60
24
  * Clears a store (does not update any relations)
61
25
  */
62
26
  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>;
27
+ compileQuery<T extends FindInput<Names, C[Name], C>>(query: T): CompiledQuery<Names, C, DbClient<string, Names, C>, T>;
66
28
  get(key: KeyType): Promise<any>;
67
29
  }
68
- export type InterfaceMap<
69
- Names extends string,
70
- C extends CollectionObject<Names>
71
- > = {
30
+ export type InterfaceMap<Names extends string, C extends CollectionObject<Names>> = {
72
31
  [K in Names]: StoreInterface<K, Names, C>;
73
32
  };
74
33
  export interface QueryState<Names extends string> {
75
34
  tx?: Transaction<IDBTransactionMode, Names>;
76
35
  }
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
- }> {}
36
+ export declare const enum MutationBreath {
37
+ Find = 0,
38
+ Singleton = 1
39
+ }
40
+ export interface MutationState<Names extends string> extends Partial<{
41
+ tx: Transaction<"readwrite", Names>;
42
+ relation: {
43
+ id: ValidKey;
44
+ key: string;
45
+ };
46
+ singleton: {
47
+ id: ValidKey;
48
+ };
49
+ finalStep: boolean;
50
+ }> {
51
+ }
85
52
  export type KeyObject<Index = string> = {
86
53
  isFun: boolean;
87
54
  key: Index;
88
- } & (
89
- | {
90
- isRelation: true;
91
- relation: BaseRelation<any, any>;
92
- }
93
- | {
94
- isRelation: false;
95
- relation?: undefined;
96
- }
97
- );
55
+ } & ({
56
+ isRelation: true;
57
+ relation: BaseRelation<any, any>;
58
+ } | {
59
+ isRelation: false;
60
+ relation?: undefined;
61
+ });
98
62
  export type { AddMutation, UpdateMutation };