@idb-orm/core 1.0.8 → 1.0.9
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/dist/builder.d.ts +23 -0
- package/dist/client/compiled-query.d.ts +13 -0
- package/dist/client/delete.d.ts +13 -0
- package/dist/client/helpers.d.ts +18 -0
- package/dist/client/index.d.ts +59 -0
- package/dist/client/types/find.d.ts +29 -0
- package/dist/client/types/index.d.ts +63 -0
- package/dist/client/types/mutation.d.ts +49 -0
- package/dist/core.d.ts +11 -0
- package/dist/dump/class.d.ts +22 -0
- package/dist/dump/json.d.ts +9 -0
- package/dist/error.d.ts +223 -0
- package/dist/field/field-types.d.ts +37 -0
- package/dist/field/index.d.ts +6 -0
- package/dist/field/primary-key.d.ts +23 -0
- package/dist/field/property.d.ts +75 -0
- package/dist/field/relation.d.ts +96 -0
- package/dist/field/type-wrapper.d.ts +108 -0
- package/dist/field/validators.d.ts +13 -0
- package/dist/index.cjs.js +1 -0
- package/dist/index.d.ts +9 -0
- package/dist/index.es.js +2 -0
- package/dist/model/index.d.ts +3 -0
- package/dist/model/model-types.d.ts +41 -0
- package/dist/model/model.d.ts +40 -0
- package/dist/object-store.d.ts +28 -0
- package/dist/transaction.d.ts +56 -0
- package/dist/util-types.d.ts +46 -0
- package/dist/utils.d.ts +18 -0
- package/package.json +1 -1
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import { CollectionObject, CollectionSchema, FindPrimaryKey, Model } from './model';
|
|
2
|
+
import { DbClient } from './client';
|
|
3
|
+
import { ValidValue } from './field';
|
|
4
|
+
import { Dict } from './util-types';
|
|
5
|
+
export declare class Builder<Name extends string, Names extends string> {
|
|
6
|
+
readonly name: Name;
|
|
7
|
+
readonly names: Names[];
|
|
8
|
+
private models;
|
|
9
|
+
constructor(name: Name, names: Names[]);
|
|
10
|
+
defineModel<N extends Names, T extends Dict<ValidValue<Names>>>(model: Model<N, T, FindPrimaryKey<T>>): Model<N, T, FindPrimaryKey<T>>;
|
|
11
|
+
defineModel<N extends Names, T extends Dict<ValidValue<Names>>>(name: N, values: T): Model<N, T, FindPrimaryKey<T>>;
|
|
12
|
+
compile<M extends CollectionObject<Names>>(models: M): CompiledDb<Name, Names, M>;
|
|
13
|
+
}
|
|
14
|
+
export declare class CompiledDb<Name extends string, Names extends string, C extends CollectionObject<Names>> {
|
|
15
|
+
readonly name: Name;
|
|
16
|
+
private readonly models;
|
|
17
|
+
readonly schemas: CollectionSchema<C>;
|
|
18
|
+
private readonly modelKeys;
|
|
19
|
+
constructor(name: Name, models: C);
|
|
20
|
+
getModel<N extends Names>(name: N): C[N];
|
|
21
|
+
createClient(version?: number): Promise<DbClient<Name, Names, C>>;
|
|
22
|
+
keys(): Names[];
|
|
23
|
+
}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import { CollectionObject } from '../model';
|
|
2
|
+
import { DbClient } from './index.ts';
|
|
3
|
+
import { 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>> {
|
|
5
|
+
private readonly client;
|
|
6
|
+
private readonly name;
|
|
7
|
+
private readonly accessedStores;
|
|
8
|
+
private readonly selectClause;
|
|
9
|
+
constructor(client: Db, name: Stores, input: Input);
|
|
10
|
+
find(): Promise<Output[]>;
|
|
11
|
+
findFirst(): Promise<Output>;
|
|
12
|
+
private _find;
|
|
13
|
+
}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import { CollectionObject, ExtractFields } from '../model';
|
|
2
|
+
import { WhereObject } from './types/find.js';
|
|
3
|
+
import { MutationState } from './types';
|
|
4
|
+
import { DbClient } from './index.js';
|
|
5
|
+
/**
|
|
6
|
+
* Deletes document(s) from a store
|
|
7
|
+
* @param name Name of the store
|
|
8
|
+
* @param client DbClient object
|
|
9
|
+
* @param stopOnFirst Stop after the first deletion
|
|
10
|
+
* @param _state Optional state for multi-stage actions
|
|
11
|
+
* @returns Number of items removed
|
|
12
|
+
*/
|
|
13
|
+
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>;
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import { Dict, Promisable } from '../util-types.js';
|
|
2
|
+
import { DbClient } from './index.ts';
|
|
3
|
+
import { QueryInput } from './types/find.ts';
|
|
4
|
+
import { Transaction } from '../transaction.js';
|
|
5
|
+
import { CollectionObject } from '../model';
|
|
6
|
+
type WhereClauseElement = [key: string, isFun: true, fn: (value: unknown) => boolean] | [key: string, isFun: false, value: unknown];
|
|
7
|
+
export declare function generateWhereClause(where?: Dict): WhereClauseElement[];
|
|
8
|
+
/**
|
|
9
|
+
* Parses a WhereClause array and returns if the item passes the checks or not
|
|
10
|
+
* @param whereArray Result of `generateWhereClause()`
|
|
11
|
+
* @param obj Object to check
|
|
12
|
+
* @returns If the object satisfies the where clause or not
|
|
13
|
+
*/
|
|
14
|
+
export declare function parseWhere(whereArray: WhereClauseElement[], obj: unknown): boolean;
|
|
15
|
+
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>;
|
|
16
|
+
export declare function getAccessedStores<ModelNames extends string, Models extends CollectionObject<ModelNames>>(name: ModelNames, query: Dict, isMutation: boolean, client: DbClient<string, ModelNames, Models>): Set<ModelNames>;
|
|
17
|
+
export declare function getSearchableQuery(q: QueryInput<any, any, any>): import('./types/find.ts').SelectObject<any, any, any>;
|
|
18
|
+
export {};
|
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
import { CompiledDb } from '../builder';
|
|
2
|
+
import { Arrayable } from '../util-types';
|
|
3
|
+
import { CollectionObject } from '../model';
|
|
4
|
+
import { Transaction, TransactionOptions } from '../transaction';
|
|
5
|
+
import { InterfaceMap } from './types';
|
|
6
|
+
import { CsvDump, JsonDump } from '../dump/class.js';
|
|
7
|
+
export declare class DbClient<Name extends string, ModelNames extends string, Models extends CollectionObject<ModelNames>> {
|
|
8
|
+
private readonly db;
|
|
9
|
+
private readonly models;
|
|
10
|
+
readonly name: Name;
|
|
11
|
+
readonly version: number;
|
|
12
|
+
readonly stores: InterfaceMap<ModelNames, Models>;
|
|
13
|
+
constructor(db: IDBDatabase, models: CompiledDb<Name, ModelNames, Models>);
|
|
14
|
+
getDb(): IDBDatabase;
|
|
15
|
+
getStore<Name extends ModelNames>(name: Name): (typeof this.stores)[Name];
|
|
16
|
+
getStoreNames(): ModelNames[];
|
|
17
|
+
createTransaction<Mode extends IDBTransactionMode, Names extends ModelNames>(mode: Mode, stores: Arrayable<Names>, options?: TransactionOptions): Transaction<Mode, Names>;
|
|
18
|
+
deleteDb(): Promise<void>;
|
|
19
|
+
dump<Format extends "json" | "csv", Return = Format extends "json" ? JsonDump : CsvDump>(format: Format, stores?: ModelNames[]): Promise<Return>;
|
|
20
|
+
deleteAllStores(): void;
|
|
21
|
+
deleteStore(storeNames: Arrayable<ModelNames>): void;
|
|
22
|
+
getModel<N extends ModelNames>(name: N): Models[N];
|
|
23
|
+
private getAccessedStores;
|
|
24
|
+
private createInterface;
|
|
25
|
+
private add;
|
|
26
|
+
private clear;
|
|
27
|
+
/**
|
|
28
|
+
* Finds documents from the store that match the filter
|
|
29
|
+
* @param name Name of the store
|
|
30
|
+
* @param item Object containing the filter and the selection query
|
|
31
|
+
* @param stopOnFirst Flag to stop after one successful find
|
|
32
|
+
* @param _state Optional state for mutli-stage actions
|
|
33
|
+
* @returns Transformed selection item
|
|
34
|
+
*/
|
|
35
|
+
private find;
|
|
36
|
+
private update;
|
|
37
|
+
/**
|
|
38
|
+
* Connects a document to another
|
|
39
|
+
*
|
|
40
|
+
* **This must be called within a wrapped environment**
|
|
41
|
+
* @param relation Relation object
|
|
42
|
+
* @param thisId Id of the source document
|
|
43
|
+
* @param documentId If of the target document
|
|
44
|
+
* @param tx Transaction this function is wrapped in
|
|
45
|
+
* @returns Id of the target document
|
|
46
|
+
*/
|
|
47
|
+
private connectDocument;
|
|
48
|
+
/**
|
|
49
|
+
* Disconnects a document from another
|
|
50
|
+
*
|
|
51
|
+
* **This must be called within a wrapped environment**
|
|
52
|
+
* @param relation Relation object
|
|
53
|
+
* @param thisId Id of the source document
|
|
54
|
+
* @param documentId If of the target document
|
|
55
|
+
* @param tx Transaction this function is wrapped in
|
|
56
|
+
* @returns Id of the target document
|
|
57
|
+
*/
|
|
58
|
+
private disconnectDocument;
|
|
59
|
+
}
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import { Dict, If, Keyof, MakeRequired, NoUndefined, RemoveNeverValues, Simplify } from '../../util-types.js';
|
|
2
|
+
import { ExtractFields, ModelStructure, RelationlessModelStructure, RelationValue } from '../../model/model-types';
|
|
3
|
+
import { Model, CollectionObject } from '../../model';
|
|
4
|
+
import { BaseRelation, AbstractProperty, PrimaryKey, RelationOutputStructure, ValidValue, Relation } from '../../field';
|
|
5
|
+
export type FilterFn<Input> = (item: Input) => boolean;
|
|
6
|
+
/**
|
|
7
|
+
* If an property is an object type (dictionary, array, map, set, etc...) return never. If it's a union it strips out any object types
|
|
8
|
+
*/
|
|
9
|
+
export type ProhibitObjects<T> = T extends Date ? Date : T extends object ? never : T;
|
|
10
|
+
export type WhereObject<Fields extends Dict<ValidValue>> = Partial<RemoveNeverValues<{
|
|
11
|
+
[K in keyof Fields]: Fields[K] extends AbstractProperty<infer Output, any> ? ProhibitObjects<Output> | FilterFn<Output> : Fields[K] extends PrimaryKey<any, infer Type> ? ProhibitObjects<Type> | FilterFn<Type> : never;
|
|
12
|
+
}>>;
|
|
13
|
+
export type SelectObject<All extends string, Fields extends Dict<ValidValue>, C extends CollectionObject<All>> = {
|
|
14
|
+
[K in keyof Fields]?: Fields[K] extends AbstractProperty<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;
|
|
15
|
+
};
|
|
16
|
+
export interface QueryInput<All extends string, Fields extends Dict<ValidValue>, C extends CollectionObject<All>> {
|
|
17
|
+
where?: WhereObject<Fields>;
|
|
18
|
+
select?: SelectObject<All, Fields, C>;
|
|
19
|
+
include?: SelectObject<All, Fields, C>;
|
|
20
|
+
}
|
|
21
|
+
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;
|
|
22
|
+
type SelectOutput<All extends string, Select extends Dict<Dict | true>, Fields extends Dict<ValidValue>, C extends CollectionObject<All>> = {
|
|
23
|
+
[K in Keyof<Select>]: Fields[K] extends BaseRelation<infer To, any> ? To extends Keyof<C> ? C[To] extends Model<any, any, any> ? If<Select[K] extends true ? true : false, RelationOutputStructure<Fields[K], RelationlessModelStructure<C[To]>>, Select[K] extends FindInput<All, C[To], C> ? RelationOutputStructure<Fields[K], FindOutput<All, C[To], C, Select[K]>> : never> : never : never : Fields[K] extends PrimaryKey<any, infer Type> ? Type : Fields[K] extends AbstractProperty<infer Type, any> ? Type : never;
|
|
24
|
+
};
|
|
25
|
+
type IncludeOutput<All extends string, Include extends Dict<Dict | true>, Fields extends Dict<ValidValue>, C extends CollectionObject<All>> = Simplify<{
|
|
26
|
+
[K in Keyof<Fields>]: Fields[K] extends BaseRelation<infer To, any> ? To extends Keyof<C> ? C[To] extends Model<any, any, any> ? K extends Keyof<Include> ? Include[K] extends true ? RelationOutputStructure<Fields[K], RelationlessModelStructure<C[To]>> : Include[K] extends FindInput<All, C[To], C> ? MakeRequired<Fields[K] extends Relation<any, any> ? true : false, NoUndefined<RelationOutputStructure<Fields[K], NoUndefined<FindOutput<All, C[To], C, Include[K]>>>>> : unknown : RelationOutputStructure<Fields[K], RelationValue<To, C>> : never : never : Fields[K] extends PrimaryKey<any, infer Type> ? Type : Fields[K] extends AbstractProperty<infer Type, any> ? Type : never;
|
|
27
|
+
}>;
|
|
28
|
+
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> ? Simplify<F["select"] extends Dict<true | Dict> ? SelectOutput<All, F["select"], Fields, C> : F["include"] extends Dict<true | Dict> ? IncludeOutput<All, F["include"], Fields, C> : ModelStructure<ExtractFields<Struct>, C>> | undefined : never;
|
|
29
|
+
export {};
|
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
import { Dict, Simplify } from '../../util-types.js';
|
|
2
|
+
import { ExtractFields, Model, ModelStructure, PrimaryKeyType, CollectionObject } from '../../model';
|
|
3
|
+
import { AddMutation, MutationAction, UpdateMutation } from './mutation.ts';
|
|
4
|
+
import { FindInput, FindOutput, WhereObject } from './find.ts';
|
|
5
|
+
import { CompiledQuery } from '../compiled-query.ts';
|
|
6
|
+
import { DbClient } from '../index.ts';
|
|
7
|
+
import { Transaction } from '../../transaction.js';
|
|
8
|
+
import { BaseRelation, ValidKey } from '../../field';
|
|
9
|
+
import { CsvDump, JsonDump } from '../../dump/class.js';
|
|
10
|
+
export type GetStructure<N extends string, C extends Dict> = C[N] extends Model<N, infer F, any> ? Simplify<ModelStructure<F, C>> : never;
|
|
11
|
+
export type ExportFormat = "json";
|
|
12
|
+
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 extends UpdateMutation<Name, Names, C[Name], C> = UpdateMutation<Name, Names, C[Name], C>> {
|
|
13
|
+
add(mutation: Add, transaction?: Transaction<"readwrite", Names>): Promise<KeyType>;
|
|
14
|
+
addMany(mutations: Add[], transaction?: Transaction<"readwrite", Names>): Promise<KeyType[]>;
|
|
15
|
+
find<T extends FindInput<Names, C[Name], C>>(query: T, transaction?: Transaction<IDBTransactionMode, Names>): Promise<NonNullable<FindOutput<Names, C[Name], C, T>>[]>;
|
|
16
|
+
findFirst<T extends FindInput<Names, C[Name], C>>(query: T, transaction?: Transaction<IDBTransactionMode, Names>): Promise<FindOutput<Names, C[Name], C, T>>;
|
|
17
|
+
update(key: KeyType, data: Update["data"]): Promise<GetStructure<Name, C>>;
|
|
18
|
+
updateFirst(item: Update, transaction?: Transaction<"readwrite", Names>): Promise<GetStructure<Name, C> | undefined>;
|
|
19
|
+
updateMany(item: Update, transaction?: Transaction<"readwrite", Names>): Promise<GetStructure<Name, C>[]>;
|
|
20
|
+
delete(key: KeyType): Promise<boolean>;
|
|
21
|
+
deleteFirst(where?: WhereObject<ExtractFields<C[Name]>>): Promise<boolean>;
|
|
22
|
+
deleteMany(where: WhereObject<ExtractFields<C[Name]>>): Promise<number>;
|
|
23
|
+
compileQuery<T extends FindInput<Names, C[Name], C>>(query: T): CompiledQuery<Names, C, DbClient<string, Names, C>, T>;
|
|
24
|
+
get(key: KeyType): Promise<GetStructure<Name, C> | undefined>;
|
|
25
|
+
dump<Format extends ExportFormat>(format: Format, where?: WhereObject<C[Name] extends Model<any, infer Fields, any> ? Fields : never>): Promise<Format extends "json" ? JsonDump : CsvDump>;
|
|
26
|
+
}
|
|
27
|
+
export type InterfaceMap<Names extends string, C extends CollectionObject<Names>> = {
|
|
28
|
+
[K in Names]: StoreInterface<K, Names, C>;
|
|
29
|
+
};
|
|
30
|
+
export interface QueryState<Names extends string> {
|
|
31
|
+
tx?: Transaction<IDBTransactionMode, Names>;
|
|
32
|
+
}
|
|
33
|
+
export interface MutationState<Names extends string> extends Partial<{
|
|
34
|
+
tx: Transaction<"readwrite", Names>;
|
|
35
|
+
relation: {
|
|
36
|
+
id: ValidKey;
|
|
37
|
+
key: string;
|
|
38
|
+
};
|
|
39
|
+
singleton: {
|
|
40
|
+
id: ValidKey;
|
|
41
|
+
};
|
|
42
|
+
/**
|
|
43
|
+
* Flag indicating whether or not this is the final step of the query/mutation
|
|
44
|
+
* @default true
|
|
45
|
+
*/
|
|
46
|
+
finalStep: boolean;
|
|
47
|
+
}> {
|
|
48
|
+
}
|
|
49
|
+
export type ActionItem = [action: MutationAction, value: unknown];
|
|
50
|
+
export type KeyObject<Index = string> = ({
|
|
51
|
+
key: Index;
|
|
52
|
+
} & ({
|
|
53
|
+
isRelation: true;
|
|
54
|
+
actions: ActionItem[];
|
|
55
|
+
relation: BaseRelation<any, any>;
|
|
56
|
+
updateFn?: undefined;
|
|
57
|
+
} | {
|
|
58
|
+
isRelation: false;
|
|
59
|
+
actions?: undefined;
|
|
60
|
+
relation?: undefined;
|
|
61
|
+
updateFn: <T>(value: T) => T;
|
|
62
|
+
}));
|
|
63
|
+
export type { AddMutation, UpdateMutation };
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
import { MakeArrayable, MakeOptional, RemoveNeverValues, PartialOnUndefined, Extends, If, Or, Keyof } from '../../util-types.js';
|
|
2
|
+
import { BaseRelation, AbstractProperty, OptionalRelation, PrimaryKey, ArrayRelation } from '../../field';
|
|
3
|
+
import { WhereObject } from './find.js';
|
|
4
|
+
import { Model, FindRelationKey, RelationValue, CollectionObject } from '../../model';
|
|
5
|
+
export type MutationAction = "$connect" | "$connectMany" | "$create" | "$createMany" | "$update" | "$updateMany" | "$delete" | "$deleteMany" | "$deleteAll" | "$disconnect" | "$disconnectMany" | "$disconnectAll";
|
|
6
|
+
type WhereSelection<Struct extends object> = Struct extends Model<any, infer Fields, any> ? WhereObject<Fields> : never;
|
|
7
|
+
type _UpdateRelationMutation<This extends All, All extends string, C extends CollectionObject<All>, To extends All, Name extends string, Relation extends BaseRelation<To, Name>, ThisKey extends string, OmitKeys extends string, IsOptional extends boolean = Extends<Relation, OptionalRelation<any, any>>, IsArray extends boolean = Extends<Relation, ArrayRelation<any, any>>, IsNullable extends boolean = Or<IsOptional, IsArray>> = MakeOptional<IsNullable, MakeArrayable<IsArray, {
|
|
8
|
+
$connect: RelationValue<To, C>;
|
|
9
|
+
} | {
|
|
10
|
+
$create: Omit<AddMutation<To, All, C[To], C>, OmitKeys | FindRelationKey<This, Name, C[To]>>;
|
|
11
|
+
} | {
|
|
12
|
+
$update: If<IsArray, UpdateMutation<To, All, C[To], C, OmitKeys | ThisKey>, Omit<UpdateMutation<To, All, C[To], C>["data"], OmitKeys>>;
|
|
13
|
+
} | If<IsNullable, {
|
|
14
|
+
$delete: If<IsArray, RelationValue<To, C>, true>;
|
|
15
|
+
} | {
|
|
16
|
+
$disconnect: If<IsArray, RelationValue<To, C>, true>;
|
|
17
|
+
}, never>> | If<IsArray, {
|
|
18
|
+
$connectMany: RelationValue<To, C>[];
|
|
19
|
+
} | {
|
|
20
|
+
$createMany: Omit<AddMutation<To, All, C[To], C>, OmitKeys | FindRelationKey<This, Name, C[To]>>[];
|
|
21
|
+
} | {
|
|
22
|
+
$updateMany: UpdateMutation<To, All, C[To], C, OmitKeys | ThisKey>[];
|
|
23
|
+
} | {
|
|
24
|
+
$deleteMany: RelationValue<To, C>[];
|
|
25
|
+
} | {
|
|
26
|
+
$deleteAll: true;
|
|
27
|
+
} | {
|
|
28
|
+
$disconnectMany: RelationValue<To, C>[];
|
|
29
|
+
} | {
|
|
30
|
+
$disconnectAll: true;
|
|
31
|
+
}, never>>;
|
|
32
|
+
export type UpdateMutation<This extends All, All extends string, Struct extends object, C extends CollectionObject<All>, OmitKeys extends string = never> = {
|
|
33
|
+
where?: WhereSelection<Struct>;
|
|
34
|
+
data: PartialOnUndefined<RemoveNeverValues<Struct extends Model<any, infer Fields, any> ? {
|
|
35
|
+
[K in Exclude<Keyof<Fields>, OmitKeys>]: Fields[K] extends AbstractProperty<infer Type, any> ? Type | undefined | ((value: Type) => Type) : Fields[K] extends PrimaryKey<any, any> ? never : Fields[K] extends BaseRelation<infer To, infer Name> ? To extends All ? _UpdateRelationMutation<This, All, C, To, Name, Fields[K], K, OmitKeys> | undefined : never : never;
|
|
36
|
+
} : never>>;
|
|
37
|
+
};
|
|
38
|
+
export type AddMutation<This extends All, All extends string, Struct extends object, C extends CollectionObject<All>> = PartialOnUndefined<RemoveNeverValues<Struct extends Model<any, infer Fields, any> ? {
|
|
39
|
+
[K in keyof Fields]: Fields[K] extends AbstractProperty<infer Type, infer HasDefault> ? HasDefault extends true ? Type | undefined : Type : Fields[K] extends PrimaryKey<infer IsAuto, infer Type> ? IsAuto extends true ? never : Type : Fields[K] extends BaseRelation<infer To, infer Name> ? To extends All ? MakeOptional<Fields[K] extends OptionalRelation<any, any> ? true : Fields[K] extends ArrayRelation<any, any> ? true : false, MakeArrayable<Fields[K] extends ArrayRelation<any, any> ? true : false, {
|
|
40
|
+
$connect: RelationValue<To, C>;
|
|
41
|
+
} | {
|
|
42
|
+
$create: Omit<AddMutation<To, All, C[To], C>, FindRelationKey<This, Name, C[To]>>;
|
|
43
|
+
}> | (Fields[K] extends ArrayRelation<any, any> ? {
|
|
44
|
+
$connectMany: RelationValue<To, C>[];
|
|
45
|
+
} | {
|
|
46
|
+
$createMany: Omit<AddMutation<To, All, C[To], C>, FindRelationKey<This, Name, C[To]>>[];
|
|
47
|
+
} : never)> : never : never;
|
|
48
|
+
} : never>>;
|
|
49
|
+
export {};
|
package/dist/core.d.ts
ADDED
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
export { AbstractProperty } from './field/property';
|
|
2
|
+
export type { ParseFn, PropertyOptions, PropertyInputOptions, } from './field/property';
|
|
3
|
+
export type { PropertyUnion } from './field/field-types';
|
|
4
|
+
export * from './util-types';
|
|
5
|
+
export { Model } from './model';
|
|
6
|
+
export type { FindPrimaryKey } from './model';
|
|
7
|
+
export { VALIDATORS } from './field/validators.js';
|
|
8
|
+
export * from './field';
|
|
9
|
+
export * from './client';
|
|
10
|
+
export { Type } from './field/type-wrapper';
|
|
11
|
+
export type { TypeTag } from './field/type-wrapper';
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import { Arrayable, Dict } from '../util-types';
|
|
2
|
+
declare abstract class Dump<T> {
|
|
3
|
+
protected readonly name: string;
|
|
4
|
+
protected readonly content: T;
|
|
5
|
+
protected readonly extension: string;
|
|
6
|
+
constructor(name: string, content: T, extension: string);
|
|
7
|
+
abstract getValue(): T;
|
|
8
|
+
abstract toFile(filename?: string, options?: FilePropertyBag): File;
|
|
9
|
+
download(filename?: string, options?: FilePropertyBag): void;
|
|
10
|
+
}
|
|
11
|
+
type JsonValue = Arrayable<Dict>;
|
|
12
|
+
export declare class JsonDump extends Dump<JsonValue> {
|
|
13
|
+
constructor(name: string, content: JsonValue);
|
|
14
|
+
getValue(): JsonValue;
|
|
15
|
+
toFile(filename?: string, options?: FilePropertyBag): File;
|
|
16
|
+
}
|
|
17
|
+
export declare class CsvDump extends Dump<string> {
|
|
18
|
+
constructor(name: string, content: string);
|
|
19
|
+
getValue(): string;
|
|
20
|
+
toFile(filename?: string, options?: FilePropertyBag): File;
|
|
21
|
+
}
|
|
22
|
+
export {};
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import { WhereObject } from '../client/types/find.js';
|
|
2
|
+
import { Dict } from '../util-types';
|
|
3
|
+
import { DbClient } from '../client';
|
|
4
|
+
import { ValidValue } from '../field';
|
|
5
|
+
import { CollectionObject } from '../model';
|
|
6
|
+
import { Transaction } from '../transaction.js';
|
|
7
|
+
import { JsonDump } from './class.js';
|
|
8
|
+
export declare function dumpStoreToJson<Current extends Names, Names extends string, Models extends CollectionObject<Names>>(db: DbClient<string, Names, CollectionObject<Names>>, store: Current, where?: WhereObject<Models[Current] extends Dict<ValidValue> ? Models[Current] : never>, tx?: Transaction<"readonly", Names>): Promise<JsonDump>;
|
|
9
|
+
export declare function dumpDatabaseToJSON<Names extends string>(db: DbClient<string, Names, any>, stores?: Names[]): Promise<JsonDump>;
|
package/dist/error.d.ts
ADDED
|
@@ -0,0 +1,223 @@
|
|
|
1
|
+
export type ErrorType = "ID_EXISTS" | "INVALID_ITEM" | "ADD_FAILED" | "UPDATE_FAILED" | "DELETE_FAILED" | "OVERWRITE_RELATION" | "NOT_FOUND" | "GET_FAILED" | "EXPORT"
|
|
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
|
+
constructor(code: ErrorType, message: string);
|
|
13
|
+
}
|
|
14
|
+
export declare const InvalidConfigError: {
|
|
15
|
+
new (message?: string): {
|
|
16
|
+
readonly code: ErrorType;
|
|
17
|
+
name: string;
|
|
18
|
+
message: string;
|
|
19
|
+
stack?: string;
|
|
20
|
+
cause?: unknown;
|
|
21
|
+
};
|
|
22
|
+
readonly code: "INVALID_CONFIG";
|
|
23
|
+
of(message: string): void;
|
|
24
|
+
isError(error: unknown): error is Error;
|
|
25
|
+
captureStackTrace(targetObject: object, constructorOpt?: Function): void;
|
|
26
|
+
prepareStackTrace(err: Error, stackTraces: NodeJS.CallSite[]): any;
|
|
27
|
+
stackTraceLimit: number;
|
|
28
|
+
};
|
|
29
|
+
export declare const InvalidTransactionError: {
|
|
30
|
+
new (message?: string): {
|
|
31
|
+
readonly code: ErrorType;
|
|
32
|
+
name: string;
|
|
33
|
+
message: string;
|
|
34
|
+
stack?: string;
|
|
35
|
+
cause?: unknown;
|
|
36
|
+
};
|
|
37
|
+
readonly code: "INVALID_TX";
|
|
38
|
+
of(message: string): void;
|
|
39
|
+
isError(error: unknown): error is Error;
|
|
40
|
+
captureStackTrace(targetObject: object, constructorOpt?: Function): void;
|
|
41
|
+
prepareStackTrace(err: Error, stackTraces: NodeJS.CallSite[]): any;
|
|
42
|
+
stackTraceLimit: number;
|
|
43
|
+
};
|
|
44
|
+
export declare const InvalidItemError: {
|
|
45
|
+
new (message?: string): {
|
|
46
|
+
readonly code: ErrorType;
|
|
47
|
+
name: string;
|
|
48
|
+
message: string;
|
|
49
|
+
stack?: string;
|
|
50
|
+
cause?: unknown;
|
|
51
|
+
};
|
|
52
|
+
readonly code: "INVALID_ITEM";
|
|
53
|
+
of(message: string): void;
|
|
54
|
+
isError(error: unknown): error is Error;
|
|
55
|
+
captureStackTrace(targetObject: object, constructorOpt?: Function): void;
|
|
56
|
+
prepareStackTrace(err: Error, stackTraces: NodeJS.CallSite[]): any;
|
|
57
|
+
stackTraceLimit: number;
|
|
58
|
+
};
|
|
59
|
+
export declare const AssertionError: {
|
|
60
|
+
new (message?: string): {
|
|
61
|
+
readonly code: ErrorType;
|
|
62
|
+
name: string;
|
|
63
|
+
message: string;
|
|
64
|
+
stack?: string;
|
|
65
|
+
cause?: unknown;
|
|
66
|
+
};
|
|
67
|
+
readonly code: "ASSERTION_FAILED";
|
|
68
|
+
of(message: string): void;
|
|
69
|
+
isError(error: unknown): error is Error;
|
|
70
|
+
captureStackTrace(targetObject: object, constructorOpt?: Function): void;
|
|
71
|
+
prepareStackTrace(err: Error, stackTraces: NodeJS.CallSite[]): any;
|
|
72
|
+
stackTraceLimit: number;
|
|
73
|
+
};
|
|
74
|
+
export declare const UnknownError: {
|
|
75
|
+
new (message?: string): {
|
|
76
|
+
readonly code: ErrorType;
|
|
77
|
+
name: string;
|
|
78
|
+
message: string;
|
|
79
|
+
stack?: string;
|
|
80
|
+
cause?: unknown;
|
|
81
|
+
};
|
|
82
|
+
readonly code: "UNKNOWN";
|
|
83
|
+
of(message: string): void;
|
|
84
|
+
isError(error: unknown): error is Error;
|
|
85
|
+
captureStackTrace(targetObject: object, constructorOpt?: Function): void;
|
|
86
|
+
prepareStackTrace(err: Error, stackTraces: NodeJS.CallSite[]): any;
|
|
87
|
+
stackTraceLimit: number;
|
|
88
|
+
};
|
|
89
|
+
export declare const DeleteError: {
|
|
90
|
+
new (message?: string): {
|
|
91
|
+
readonly code: ErrorType;
|
|
92
|
+
name: string;
|
|
93
|
+
message: string;
|
|
94
|
+
stack?: string;
|
|
95
|
+
cause?: unknown;
|
|
96
|
+
};
|
|
97
|
+
readonly code: "DELETE_FAILED";
|
|
98
|
+
of(message: string): void;
|
|
99
|
+
isError(error: unknown): error is Error;
|
|
100
|
+
captureStackTrace(targetObject: object, constructorOpt?: Function): void;
|
|
101
|
+
prepareStackTrace(err: Error, stackTraces: NodeJS.CallSite[]): any;
|
|
102
|
+
stackTraceLimit: number;
|
|
103
|
+
};
|
|
104
|
+
export declare const ObjectStoreNotFoundError: {
|
|
105
|
+
new (message?: string): {
|
|
106
|
+
readonly code: ErrorType;
|
|
107
|
+
name: string;
|
|
108
|
+
message: string;
|
|
109
|
+
stack?: string;
|
|
110
|
+
cause?: unknown;
|
|
111
|
+
};
|
|
112
|
+
readonly code: "NOT_FOUND";
|
|
113
|
+
of(message: string): void;
|
|
114
|
+
isError(error: unknown): error is Error;
|
|
115
|
+
captureStackTrace(targetObject: object, constructorOpt?: Function): void;
|
|
116
|
+
prepareStackTrace(err: Error, stackTraces: NodeJS.CallSite[]): any;
|
|
117
|
+
stackTraceLimit: number;
|
|
118
|
+
};
|
|
119
|
+
export declare const DocumentNotFoundError: {
|
|
120
|
+
new (message?: string): {
|
|
121
|
+
readonly code: ErrorType;
|
|
122
|
+
name: string;
|
|
123
|
+
message: string;
|
|
124
|
+
stack?: string;
|
|
125
|
+
cause?: unknown;
|
|
126
|
+
};
|
|
127
|
+
readonly code: "NOT_FOUND";
|
|
128
|
+
of(message: string): void;
|
|
129
|
+
isError(error: unknown): error is Error;
|
|
130
|
+
captureStackTrace(targetObject: object, constructorOpt?: Function): void;
|
|
131
|
+
prepareStackTrace(err: Error, stackTraces: NodeJS.CallSite[]): any;
|
|
132
|
+
stackTraceLimit: number;
|
|
133
|
+
};
|
|
134
|
+
export declare const UpdateError: {
|
|
135
|
+
new (message?: string): {
|
|
136
|
+
readonly code: ErrorType;
|
|
137
|
+
name: string;
|
|
138
|
+
message: string;
|
|
139
|
+
stack?: string;
|
|
140
|
+
cause?: unknown;
|
|
141
|
+
};
|
|
142
|
+
readonly code: "UPDATE_FAILED";
|
|
143
|
+
of(message: string): void;
|
|
144
|
+
isError(error: unknown): error is Error;
|
|
145
|
+
captureStackTrace(targetObject: object, constructorOpt?: Function): void;
|
|
146
|
+
prepareStackTrace(err: Error, stackTraces: NodeJS.CallSite[]): any;
|
|
147
|
+
stackTraceLimit: number;
|
|
148
|
+
};
|
|
149
|
+
export declare const AddError: {
|
|
150
|
+
new (message?: string): {
|
|
151
|
+
readonly code: ErrorType;
|
|
152
|
+
name: string;
|
|
153
|
+
message: string;
|
|
154
|
+
stack?: string;
|
|
155
|
+
cause?: unknown;
|
|
156
|
+
};
|
|
157
|
+
readonly code: "ADD_FAILED";
|
|
158
|
+
of(message: string): void;
|
|
159
|
+
isError(error: unknown): error is Error;
|
|
160
|
+
captureStackTrace(targetObject: object, constructorOpt?: Function): void;
|
|
161
|
+
prepareStackTrace(err: Error, stackTraces: NodeJS.CallSite[]): any;
|
|
162
|
+
stackTraceLimit: number;
|
|
163
|
+
};
|
|
164
|
+
export declare const OpenCursorError: {
|
|
165
|
+
new (message?: string): {
|
|
166
|
+
readonly code: ErrorType;
|
|
167
|
+
name: string;
|
|
168
|
+
message: string;
|
|
169
|
+
stack?: string;
|
|
170
|
+
cause?: unknown;
|
|
171
|
+
};
|
|
172
|
+
readonly code: "OPEN_CURSOR";
|
|
173
|
+
of(message: string): void;
|
|
174
|
+
isError(error: unknown): error is Error;
|
|
175
|
+
captureStackTrace(targetObject: object, constructorOpt?: Function): void;
|
|
176
|
+
prepareStackTrace(err: Error, stackTraces: NodeJS.CallSite[]): any;
|
|
177
|
+
stackTraceLimit: number;
|
|
178
|
+
};
|
|
179
|
+
export declare const RetrievalError: {
|
|
180
|
+
new (message?: string): {
|
|
181
|
+
readonly code: ErrorType;
|
|
182
|
+
name: string;
|
|
183
|
+
message: string;
|
|
184
|
+
stack?: string;
|
|
185
|
+
cause?: unknown;
|
|
186
|
+
};
|
|
187
|
+
readonly code: "GET_FAILED";
|
|
188
|
+
of(message: string): void;
|
|
189
|
+
isError(error: unknown): error is Error;
|
|
190
|
+
captureStackTrace(targetObject: object, constructorOpt?: Function): void;
|
|
191
|
+
prepareStackTrace(err: Error, stackTraces: NodeJS.CallSite[]): any;
|
|
192
|
+
stackTraceLimit: number;
|
|
193
|
+
};
|
|
194
|
+
export declare const OverwriteRelationError: {
|
|
195
|
+
new (message?: string): {
|
|
196
|
+
readonly code: ErrorType;
|
|
197
|
+
name: string;
|
|
198
|
+
message: string;
|
|
199
|
+
stack?: string;
|
|
200
|
+
cause?: unknown;
|
|
201
|
+
};
|
|
202
|
+
readonly code: "OVERWRITE_RELATION";
|
|
203
|
+
of(message: string): void;
|
|
204
|
+
isError(error: unknown): error is Error;
|
|
205
|
+
captureStackTrace(targetObject: object, constructorOpt?: Function): void;
|
|
206
|
+
prepareStackTrace(err: Error, stackTraces: NodeJS.CallSite[]): any;
|
|
207
|
+
stackTraceLimit: number;
|
|
208
|
+
};
|
|
209
|
+
export declare const ExportError: {
|
|
210
|
+
new (message?: string): {
|
|
211
|
+
readonly code: ErrorType;
|
|
212
|
+
name: string;
|
|
213
|
+
message: string;
|
|
214
|
+
stack?: string;
|
|
215
|
+
cause?: unknown;
|
|
216
|
+
};
|
|
217
|
+
readonly code: "EXPORT";
|
|
218
|
+
of(message: string): void;
|
|
219
|
+
isError(error: unknown): error is Error;
|
|
220
|
+
captureStackTrace(targetObject: object, constructorOpt?: Function): void;
|
|
221
|
+
prepareStackTrace(err: Error, stackTraces: NodeJS.CallSite[]): any;
|
|
222
|
+
stackTraceLimit: number;
|
|
223
|
+
};
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
import { Dict } from '../util-types.js';
|
|
2
|
+
import { default as PrimaryKey } from './primary-key.js';
|
|
3
|
+
import { AbstractProperty, ParseFn, Property } from './property.js';
|
|
4
|
+
import { BaseRelation, OptionalRelation, ArrayRelation } from './relation.js';
|
|
5
|
+
import { DateTag, NumberTag, StringTag } from './type-wrapper.js';
|
|
6
|
+
export type ValidKey = string | number | Date;
|
|
7
|
+
export type StringValidKeyType = "string" | "date" | "number";
|
|
8
|
+
export type ValidKeyType = StringTag | DateTag | NumberTag;
|
|
9
|
+
export type ReferenceActions = "Cascade" | "None" | "Restrict";
|
|
10
|
+
export type OptionalActions = "SetNull" | ReferenceActions;
|
|
11
|
+
export interface RelationOptions<Name extends string, OnDelete> {
|
|
12
|
+
name?: Name;
|
|
13
|
+
onDelete?: OnDelete;
|
|
14
|
+
}
|
|
15
|
+
export interface RelationActions {
|
|
16
|
+
onDelete: OptionalActions;
|
|
17
|
+
}
|
|
18
|
+
export declare const enum FieldTypes {
|
|
19
|
+
Property = 0,
|
|
20
|
+
Relation = 1,
|
|
21
|
+
PrimaryKey = 2,
|
|
22
|
+
Invalid = 3
|
|
23
|
+
}
|
|
24
|
+
export type GenFunction<T extends ValidKey> = () => T;
|
|
25
|
+
export type FunctionMatch<E> = E extends "string" ? string : E extends "number" ? number : E extends "date" ? Date : never;
|
|
26
|
+
export type GetPrimaryKeyType<T> = T extends PrimaryKey<any, infer Type> ? Type : never;
|
|
27
|
+
export interface FieldOptions {
|
|
28
|
+
unique: boolean;
|
|
29
|
+
}
|
|
30
|
+
export type RelationOutput<T> = T extends PrimaryKey<any, infer Type> ? Type : never;
|
|
31
|
+
export type RelationOutputStructure<R extends BaseRelation<any, any>, Output> = R extends ArrayRelation<any, any> ? Output[] : R extends OptionalRelation<any, any> ? Output | null : Output;
|
|
32
|
+
export type NonRelationOutput<T> = T extends AbstractProperty<infer Out, any> ? Out : T extends PrimaryKey<any, infer Type> ? Type : never;
|
|
33
|
+
export type ValidValue<N extends string = string> = BaseRelation<N, string> | AbstractProperty<any, any> | PrimaryKey<boolean, ValidKey>;
|
|
34
|
+
export type ParseFnWrap<T extends Dict> = {
|
|
35
|
+
[K in keyof T]: ParseFn<T[K]>;
|
|
36
|
+
};
|
|
37
|
+
export type PropertyUnion<T extends readonly (Property<any, boolean> | ParseFn<any>)[]> = T[number] extends Property<infer Type, boolean> ? Type : T extends ParseFn<infer Type> ? Type : never;
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import { GenFunction, ValidKey, ValidKeyType } from './field-types.js';
|
|
2
|
+
export default class PrimaryKey<AutoGenerate extends boolean, KeyType extends ValidKey> {
|
|
3
|
+
readonly symbol: symbol;
|
|
4
|
+
private genFn?;
|
|
5
|
+
private autoGenerate;
|
|
6
|
+
readonly type: ValidKeyType;
|
|
7
|
+
constructor();
|
|
8
|
+
constructor(type: ValidKeyType);
|
|
9
|
+
constructor(type: ValidKeyType, generator: GenFunction<KeyType>);
|
|
10
|
+
getType(): ValidKeyType;
|
|
11
|
+
generator(genFn: GenFunction<KeyType>): PrimaryKey<true, KeyType>;
|
|
12
|
+
autoIncrement(): PrimaryKey<true, number>;
|
|
13
|
+
uuid(): PrimaryKey<true, string>;
|
|
14
|
+
date(): PrimaryKey<true, Date>;
|
|
15
|
+
genKey(): KeyType;
|
|
16
|
+
getSchema(): import('./property.js').ParseFn<string> | import('./property.js').ParseFn<number> | import('./property.js').ParseFn<Date>;
|
|
17
|
+
/**
|
|
18
|
+
* If the internal objectStore "autoIncrement" utility is being used
|
|
19
|
+
* @returns
|
|
20
|
+
*/
|
|
21
|
+
isAutoIncremented(): boolean;
|
|
22
|
+
static is(value: object): value is PrimaryKey<any, any>;
|
|
23
|
+
}
|