@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 +38 -0
- package/dist/builder.d.ts +25 -0
- package/dist/client/compiled-query.d.ts +23 -0
- package/dist/client/helpers.d.ts +11 -0
- package/dist/client/index.d.ts +61 -0
- package/dist/client/types/find.d.ts +107 -0
- package/dist/client/types/index.d.ts +98 -0
- package/dist/client/types/mutation.d.ts +225 -0
- package/dist/error.d.ts +194 -0
- package/dist/field/constants.d.ts +9 -0
- package/dist/field/field-types.d.ts +29 -0
- package/dist/field/field.d.ts +40 -0
- package/dist/field/index.d.ts +5 -0
- package/dist/field/primary-key.d.ts +20 -0
- package/dist/field/relation.d.ts +77 -0
- package/dist/index.d.ts +9 -0
- package/dist/index.js +2 -0
- package/dist/index.js.map +1 -0
- package/dist/model/index.d.ts +3 -0
- package/dist/model/model-types.d.ts +125 -0
- package/dist/model/model.d.ts +42 -0
- package/dist/object-store.d.ts +27 -0
- package/dist/transaction.d.ts +50 -0
- package/dist/types/common.d.ts +37 -0
- package/dist/utils.d.ts +17 -0
- package/eslint.config.js +55 -0
- package/package.json +45 -0
- package/playwright.config.ts +79 -0
- package/tests/helpers.ts +72 -0
- package/tests/test.spec.ts +101 -0
package/dist/error.d.ts
ADDED
|
@@ -0,0 +1,194 @@
|
|
|
1
|
+
export type ErrorType = "ID_EXISTS" | "INVALID_ITEM" | "ADD_FAILED" | "UPDATE_FAILED" | "DELETE_FAILED" | "OVERWRITE_RELATION" | "NOT_FOUND" | "GET_FAILED"
|
|
2
|
+
/**
|
|
3
|
+
* The given transaction is invalid for the store it is trying to access
|
|
4
|
+
*/
|
|
5
|
+
| "INVALID_TX"
|
|
6
|
+
/**
|
|
7
|
+
* The database is not found
|
|
8
|
+
*/
|
|
9
|
+
| "NO_DB" | "CUSTOM" | "INVALID_CONFIG" | "ASSERTION_FAILED" | "OPEN_CURSOR" | "UNKNOWN";
|
|
10
|
+
export declare class StoreError extends Error {
|
|
11
|
+
readonly code: ErrorType;
|
|
12
|
+
readonly message: string;
|
|
13
|
+
constructor(code: ErrorType, message: string);
|
|
14
|
+
}
|
|
15
|
+
export declare const InvalidConfigError: {
|
|
16
|
+
new (message?: string): {
|
|
17
|
+
readonly code: ErrorType;
|
|
18
|
+
readonly message: string;
|
|
19
|
+
name: string;
|
|
20
|
+
stack?: string;
|
|
21
|
+
cause?: unknown;
|
|
22
|
+
};
|
|
23
|
+
readonly code: "INVALID_CONFIG";
|
|
24
|
+
of(message: string): void;
|
|
25
|
+
isError(error: unknown): error is Error;
|
|
26
|
+
captureStackTrace(targetObject: object, constructorOpt?: Function): void;
|
|
27
|
+
prepareStackTrace(err: Error, stackTraces: NodeJS.CallSite[]): any;
|
|
28
|
+
stackTraceLimit: number;
|
|
29
|
+
};
|
|
30
|
+
export declare const InvalidTransactionError: {
|
|
31
|
+
new (message?: string): {
|
|
32
|
+
readonly code: ErrorType;
|
|
33
|
+
readonly message: string;
|
|
34
|
+
name: string;
|
|
35
|
+
stack?: string;
|
|
36
|
+
cause?: unknown;
|
|
37
|
+
};
|
|
38
|
+
readonly code: "INVALID_TX";
|
|
39
|
+
of(message: string): void;
|
|
40
|
+
isError(error: unknown): error is Error;
|
|
41
|
+
captureStackTrace(targetObject: object, constructorOpt?: Function): void;
|
|
42
|
+
prepareStackTrace(err: Error, stackTraces: NodeJS.CallSite[]): any;
|
|
43
|
+
stackTraceLimit: number;
|
|
44
|
+
};
|
|
45
|
+
export declare const InvalidItemError: {
|
|
46
|
+
new (message?: string): {
|
|
47
|
+
readonly code: ErrorType;
|
|
48
|
+
readonly message: string;
|
|
49
|
+
name: string;
|
|
50
|
+
stack?: string;
|
|
51
|
+
cause?: unknown;
|
|
52
|
+
};
|
|
53
|
+
readonly code: "INVALID_ITEM";
|
|
54
|
+
of(message: string): void;
|
|
55
|
+
isError(error: unknown): error is Error;
|
|
56
|
+
captureStackTrace(targetObject: object, constructorOpt?: Function): void;
|
|
57
|
+
prepareStackTrace(err: Error, stackTraces: NodeJS.CallSite[]): any;
|
|
58
|
+
stackTraceLimit: number;
|
|
59
|
+
};
|
|
60
|
+
export declare const AssertionError: {
|
|
61
|
+
new (message?: string): {
|
|
62
|
+
readonly code: ErrorType;
|
|
63
|
+
readonly message: string;
|
|
64
|
+
name: string;
|
|
65
|
+
stack?: string;
|
|
66
|
+
cause?: unknown;
|
|
67
|
+
};
|
|
68
|
+
readonly code: "ASSERTION_FAILED";
|
|
69
|
+
of(message: string): void;
|
|
70
|
+
isError(error: unknown): error is Error;
|
|
71
|
+
captureStackTrace(targetObject: object, constructorOpt?: Function): void;
|
|
72
|
+
prepareStackTrace(err: Error, stackTraces: NodeJS.CallSite[]): any;
|
|
73
|
+
stackTraceLimit: number;
|
|
74
|
+
};
|
|
75
|
+
export declare const UnknownError: {
|
|
76
|
+
new (message?: string): {
|
|
77
|
+
readonly code: ErrorType;
|
|
78
|
+
readonly message: string;
|
|
79
|
+
name: string;
|
|
80
|
+
stack?: string;
|
|
81
|
+
cause?: unknown;
|
|
82
|
+
};
|
|
83
|
+
readonly code: "UNKNOWN";
|
|
84
|
+
of(message: string): void;
|
|
85
|
+
isError(error: unknown): error is Error;
|
|
86
|
+
captureStackTrace(targetObject: object, constructorOpt?: Function): void;
|
|
87
|
+
prepareStackTrace(err: Error, stackTraces: NodeJS.CallSite[]): any;
|
|
88
|
+
stackTraceLimit: number;
|
|
89
|
+
};
|
|
90
|
+
export declare const DeleteError: {
|
|
91
|
+
new (message?: string): {
|
|
92
|
+
readonly code: ErrorType;
|
|
93
|
+
readonly message: string;
|
|
94
|
+
name: string;
|
|
95
|
+
stack?: string;
|
|
96
|
+
cause?: unknown;
|
|
97
|
+
};
|
|
98
|
+
readonly code: "DELETE_FAILED";
|
|
99
|
+
of(message: string): void;
|
|
100
|
+
isError(error: unknown): error is Error;
|
|
101
|
+
captureStackTrace(targetObject: object, constructorOpt?: Function): void;
|
|
102
|
+
prepareStackTrace(err: Error, stackTraces: NodeJS.CallSite[]): any;
|
|
103
|
+
stackTraceLimit: number;
|
|
104
|
+
};
|
|
105
|
+
export declare const ObjectStoreNotFoundError: {
|
|
106
|
+
new (message?: string): {
|
|
107
|
+
readonly code: ErrorType;
|
|
108
|
+
readonly message: string;
|
|
109
|
+
name: string;
|
|
110
|
+
stack?: string;
|
|
111
|
+
cause?: unknown;
|
|
112
|
+
};
|
|
113
|
+
readonly code: "NOT_FOUND";
|
|
114
|
+
of(message: string): void;
|
|
115
|
+
isError(error: unknown): error is Error;
|
|
116
|
+
captureStackTrace(targetObject: object, constructorOpt?: Function): void;
|
|
117
|
+
prepareStackTrace(err: Error, stackTraces: NodeJS.CallSite[]): any;
|
|
118
|
+
stackTraceLimit: number;
|
|
119
|
+
};
|
|
120
|
+
export declare const DocumentNotFoundError: {
|
|
121
|
+
new (message?: string): {
|
|
122
|
+
readonly code: ErrorType;
|
|
123
|
+
readonly message: string;
|
|
124
|
+
name: string;
|
|
125
|
+
stack?: string;
|
|
126
|
+
cause?: unknown;
|
|
127
|
+
};
|
|
128
|
+
readonly code: "NOT_FOUND";
|
|
129
|
+
of(message: string): void;
|
|
130
|
+
isError(error: unknown): error is Error;
|
|
131
|
+
captureStackTrace(targetObject: object, constructorOpt?: Function): void;
|
|
132
|
+
prepareStackTrace(err: Error, stackTraces: NodeJS.CallSite[]): any;
|
|
133
|
+
stackTraceLimit: number;
|
|
134
|
+
};
|
|
135
|
+
export declare const UpdateError: {
|
|
136
|
+
new (message?: string): {
|
|
137
|
+
readonly code: ErrorType;
|
|
138
|
+
readonly message: string;
|
|
139
|
+
name: string;
|
|
140
|
+
stack?: string;
|
|
141
|
+
cause?: unknown;
|
|
142
|
+
};
|
|
143
|
+
readonly code: "UPDATE_FAILED";
|
|
144
|
+
of(message: string): void;
|
|
145
|
+
isError(error: unknown): error is Error;
|
|
146
|
+
captureStackTrace(targetObject: object, constructorOpt?: Function): void;
|
|
147
|
+
prepareStackTrace(err: Error, stackTraces: NodeJS.CallSite[]): any;
|
|
148
|
+
stackTraceLimit: number;
|
|
149
|
+
};
|
|
150
|
+
export declare const AddError: {
|
|
151
|
+
new (message?: string): {
|
|
152
|
+
readonly code: ErrorType;
|
|
153
|
+
readonly message: string;
|
|
154
|
+
name: string;
|
|
155
|
+
stack?: string;
|
|
156
|
+
cause?: unknown;
|
|
157
|
+
};
|
|
158
|
+
readonly code: "ADD_FAILED";
|
|
159
|
+
of(message: string): void;
|
|
160
|
+
isError(error: unknown): error is Error;
|
|
161
|
+
captureStackTrace(targetObject: object, constructorOpt?: Function): void;
|
|
162
|
+
prepareStackTrace(err: Error, stackTraces: NodeJS.CallSite[]): any;
|
|
163
|
+
stackTraceLimit: number;
|
|
164
|
+
};
|
|
165
|
+
export declare const OpenCursorError: {
|
|
166
|
+
new (message?: string): {
|
|
167
|
+
readonly code: ErrorType;
|
|
168
|
+
readonly message: string;
|
|
169
|
+
name: string;
|
|
170
|
+
stack?: string;
|
|
171
|
+
cause?: unknown;
|
|
172
|
+
};
|
|
173
|
+
readonly code: "OPEN_CURSOR";
|
|
174
|
+
of(message: string): void;
|
|
175
|
+
isError(error: unknown): error is Error;
|
|
176
|
+
captureStackTrace(targetObject: object, constructorOpt?: Function): void;
|
|
177
|
+
prepareStackTrace(err: Error, stackTraces: NodeJS.CallSite[]): any;
|
|
178
|
+
stackTraceLimit: number;
|
|
179
|
+
};
|
|
180
|
+
export declare const RetrievalError: {
|
|
181
|
+
new (message?: string): {
|
|
182
|
+
readonly code: ErrorType;
|
|
183
|
+
readonly message: string;
|
|
184
|
+
name: string;
|
|
185
|
+
stack?: string;
|
|
186
|
+
cause?: unknown;
|
|
187
|
+
};
|
|
188
|
+
readonly code: "GET_FAILED";
|
|
189
|
+
of(message: string): void;
|
|
190
|
+
isError(error: unknown): error is Error;
|
|
191
|
+
captureStackTrace(targetObject: object, constructorOpt?: Function): void;
|
|
192
|
+
prepareStackTrace(err: Error, stackTraces: NodeJS.CallSite[]): any;
|
|
193
|
+
stackTraceLimit: number;
|
|
194
|
+
};
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import { ValidKey } from "../types/common.js";
|
|
2
|
+
import { Field } from "./field.js";
|
|
3
|
+
import PrimaryKey from "./primary-key.js";
|
|
4
|
+
import { BaseRelation, OptionalRelation, RelationArray } from "./relation.js";
|
|
5
|
+
export type ReferenceActions = "Cascade" | "None" | "Restrict";
|
|
6
|
+
export type OptionalActions = "SetNull" | ReferenceActions;
|
|
7
|
+
export interface RelationOptions<Name extends string, OnDelete> {
|
|
8
|
+
name?: Name;
|
|
9
|
+
onDelete?: OnDelete;
|
|
10
|
+
}
|
|
11
|
+
export interface RelationActions {
|
|
12
|
+
onDelete: OptionalActions;
|
|
13
|
+
}
|
|
14
|
+
export declare const enum FieldTypes {
|
|
15
|
+
Field = 0,
|
|
16
|
+
Relation = 1,
|
|
17
|
+
PrimaryKey = 2,
|
|
18
|
+
Invalid = 3
|
|
19
|
+
}
|
|
20
|
+
export type GenFunction<T extends ValidKey> = () => T;
|
|
21
|
+
export type FunctionMatch<E> = E extends "string" ? string : E extends "number" ? number : E extends "date" ? Date : never;
|
|
22
|
+
export type GetPrimaryKeyType<T> = T extends PrimaryKey<any, infer Type> ? Type : never;
|
|
23
|
+
export interface FieldOptions {
|
|
24
|
+
unique: boolean;
|
|
25
|
+
}
|
|
26
|
+
export type RelationOutput<T> = T extends PrimaryKey<any, infer Type> ? Type : never;
|
|
27
|
+
export type RelationOutputStructure<R extends BaseRelation<any, any>, Output> = R extends RelationArray<any, any> ? Output[] : R extends OptionalRelation<any, any> ? Output | undefined : Output;
|
|
28
|
+
export type NonRelationOutput<T> = T extends Field<infer Out, any> ? Out : T extends PrimaryKey<any, infer Type> ? Type : never;
|
|
29
|
+
export type ValidValue<N extends string = string> = BaseRelation<N, string> | Field<any, any> | PrimaryKey<boolean, ValidKey>;
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
import z from "zod";
|
|
2
|
+
import type { ValidKeyType } from "../types/common.js";
|
|
3
|
+
import type { FieldOptions, FunctionMatch, ReferenceActions, RelationOptions } from "./field-types.js";
|
|
4
|
+
import PrimaryKey from "./primary-key.js";
|
|
5
|
+
import { Relation } from "./relation.js";
|
|
6
|
+
export declare class Field<OutputType, HasDefault extends boolean = false> {
|
|
7
|
+
schema: z.ZodType<OutputType>;
|
|
8
|
+
private hasDefault;
|
|
9
|
+
static readonly schemas: {
|
|
10
|
+
string: z.ZodString;
|
|
11
|
+
boolean: z.ZodBoolean;
|
|
12
|
+
number: z.ZodNumber;
|
|
13
|
+
array: z.ZodArray<z.ZodAny>;
|
|
14
|
+
object: z.ZodObject<{}, z.core.$loose>;
|
|
15
|
+
date: z.ZodDate;
|
|
16
|
+
};
|
|
17
|
+
private options;
|
|
18
|
+
constructor(schema: z.ZodType<OutputType>, options?: Partial<FieldOptions>);
|
|
19
|
+
array(): Field<OutputType[], false>;
|
|
20
|
+
optional(): Field<OutputType | undefined, false>;
|
|
21
|
+
default(defaultValue: NonNullable<OutputType>): Field<NonNullable<OutputType>, true>;
|
|
22
|
+
refine(refineFn: (val: OutputType) => boolean): void;
|
|
23
|
+
parse(value: unknown): z.ZodSafeParseResult<OutputType>;
|
|
24
|
+
hasDefaultValue(): HasDefault;
|
|
25
|
+
/**
|
|
26
|
+
* Indicates that a field must be unique across all documents
|
|
27
|
+
*
|
|
28
|
+
* **NOTE**: The field type must be a primitive. If this is applied to a non-primitive, it returns `null`
|
|
29
|
+
*/
|
|
30
|
+
unique(): OutputType extends string | number | boolean | symbol ? this : null;
|
|
31
|
+
static array<T>(item: z.ZodType<T> | Field<T>, options?: FieldOptions): Field<T[], false>;
|
|
32
|
+
static boolean(schema?: z.ZodType<boolean>, options?: FieldOptions): Field<boolean, false>;
|
|
33
|
+
static custom<T>(schema: z.ZodType<T>, options?: FieldOptions): Field<T, false>;
|
|
34
|
+
static object<T extends Record<string, z.ZodType>>(item: T, options?: FieldOptions): Field<z.core.$InferObjectOutput<{ -readonly [P in keyof T]: T[P]; }, {}>, false>;
|
|
35
|
+
static primaryKey<V extends ValidKeyType = "number">(type?: V): PrimaryKey<false, FunctionMatch<V>>;
|
|
36
|
+
static literal<V extends string | number | boolean>(value: V): Field<V, false>;
|
|
37
|
+
static number(schema?: z.ZodType<number>, options?: FieldOptions): Field<number, false>;
|
|
38
|
+
static string(schema?: z.ZodType<string>, options?: FieldOptions): Field<string, false>;
|
|
39
|
+
static relation<To extends string, Name extends string = never>(to: To, options?: RelationOptions<Name, ReferenceActions>): Relation<To, Name>;
|
|
40
|
+
}
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import { ValidKey, ValidKeyType } from "../types/common.js";
|
|
2
|
+
import { GenFunction } from "./field-types.js";
|
|
3
|
+
export default class PrimaryKey<AutoGenerate extends boolean, Type extends ValidKey> {
|
|
4
|
+
private genFn?;
|
|
5
|
+
private autoGenerate;
|
|
6
|
+
readonly type: ValidKeyType;
|
|
7
|
+
constructor();
|
|
8
|
+
constructor(type: ValidKeyType);
|
|
9
|
+
constructor(type: ValidKeyType, generator: GenFunction<Type>);
|
|
10
|
+
autoIncrement(): PrimaryKey<true, number>;
|
|
11
|
+
generator(genFn: GenFunction<Type>): PrimaryKey<true, Type>;
|
|
12
|
+
uuid(): PrimaryKey<true, string>;
|
|
13
|
+
genKey(): Type;
|
|
14
|
+
getSchema(): import("zod").ZodString | import("zod").ZodNumber | import("zod").ZodDate;
|
|
15
|
+
/**
|
|
16
|
+
* If the internal objectStore "autoIncrement" utility is being used
|
|
17
|
+
* @returns
|
|
18
|
+
*/
|
|
19
|
+
isAutoIncremented(): boolean;
|
|
20
|
+
}
|
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
import type { OptionalActions, ReferenceActions, RelationActions, RelationOptions } from "./field-types.js";
|
|
2
|
+
export declare class BaseRelation<To extends string, Name extends string = never> {
|
|
3
|
+
/**
|
|
4
|
+
* The name of the model this relation is pointing to
|
|
5
|
+
*/
|
|
6
|
+
readonly to: To;
|
|
7
|
+
/**
|
|
8
|
+
* An optional label to give to the relation. This helps distinguish it from others
|
|
9
|
+
*/
|
|
10
|
+
readonly name: Name;
|
|
11
|
+
/**
|
|
12
|
+
* If the relation is optional or not
|
|
13
|
+
*/
|
|
14
|
+
readonly isOptional: boolean;
|
|
15
|
+
/**
|
|
16
|
+
* If the relation is an array or not
|
|
17
|
+
*/
|
|
18
|
+
readonly isArray: boolean;
|
|
19
|
+
/**
|
|
20
|
+
* Actions to be performed under certain conditions
|
|
21
|
+
*/
|
|
22
|
+
protected actions: RelationActions;
|
|
23
|
+
/**
|
|
24
|
+
* The corresponding relation key on the model this relation points to
|
|
25
|
+
*/
|
|
26
|
+
private relatedKey;
|
|
27
|
+
constructor(
|
|
28
|
+
/**
|
|
29
|
+
* The name of the model this relation is pointing to
|
|
30
|
+
*/
|
|
31
|
+
to: To,
|
|
32
|
+
/**
|
|
33
|
+
* An optional label to give to the relation. This helps distinguish it from others
|
|
34
|
+
*/
|
|
35
|
+
name?: Name,
|
|
36
|
+
/**
|
|
37
|
+
* If the relation is optional or not
|
|
38
|
+
*/
|
|
39
|
+
isOptional?: boolean,
|
|
40
|
+
/**
|
|
41
|
+
* If the relation is an array or not
|
|
42
|
+
*/
|
|
43
|
+
isArray?: boolean, onDelete?: OptionalActions);
|
|
44
|
+
getActions(): {
|
|
45
|
+
onDelete: OptionalActions;
|
|
46
|
+
};
|
|
47
|
+
setRelatedKey(key: string): void;
|
|
48
|
+
/**
|
|
49
|
+
* Gets the key on the corresponding model this relation points to
|
|
50
|
+
*/
|
|
51
|
+
getRelatedKey(): string;
|
|
52
|
+
}
|
|
53
|
+
export declare class Relation<To extends string, Name extends string> extends BaseRelation<To, Name> {
|
|
54
|
+
private readonly _brand;
|
|
55
|
+
constructor(to: To, options?: RelationOptions<Name, ReferenceActions>);
|
|
56
|
+
/**
|
|
57
|
+
* Creates an array relation to the specified model
|
|
58
|
+
*
|
|
59
|
+
* **Note: Calling this function will reset any relation actions to the default**
|
|
60
|
+
*/
|
|
61
|
+
array({ onDelete, }?: Omit<RelationOptions<Name, OptionalActions>, "name">): RelationArray<To, Name>;
|
|
62
|
+
/**
|
|
63
|
+
* Creates an optional relation to the specified model
|
|
64
|
+
*
|
|
65
|
+
* **Note: Calling this function will reset any relation actions to the default**
|
|
66
|
+
*/
|
|
67
|
+
optional({ onDelete, }?: Omit<RelationOptions<Name, OptionalActions>, "name">): OptionalRelation<To, Name>;
|
|
68
|
+
onDelete(action: ReferenceActions): this;
|
|
69
|
+
}
|
|
70
|
+
export declare class RelationArray<To extends string, Name extends string> extends BaseRelation<To, Name> {
|
|
71
|
+
private readonly _brand;
|
|
72
|
+
constructor(to: To, name?: Name, action?: OptionalActions);
|
|
73
|
+
}
|
|
74
|
+
export declare class OptionalRelation<To extends string, Name extends string> extends BaseRelation<To, Name> {
|
|
75
|
+
private readonly _brand;
|
|
76
|
+
constructor(to: To, name?: Name, action?: OptionalActions);
|
|
77
|
+
}
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import { Builder } from "./builder.js";
|
|
2
|
+
import { StoreError, type ErrorType } from "./error.js";
|
|
3
|
+
export { Builder, StoreError, ErrorType };
|
|
4
|
+
import { Field } from "./field";
|
|
5
|
+
export { Field };
|
|
6
|
+
import { CompiledQuery } from "./client/compiled-query.js";
|
|
7
|
+
export { CompiledQuery };
|
|
8
|
+
import type { ModelType } from "./model";
|
|
9
|
+
export { ModelType };
|