@idb-orm/core 1.0.4 → 1.0.6

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
@@ -25,11 +25,11 @@
25
25
  - [ ] findMany
26
26
  - [ ] updateMany
27
27
  - [ ] Convert internal string unions to enums
28
- - [ ] Remove external dependencies
29
- - [ ] deep-equal
28
+ - [x] Remove external dependencies
29
+ - [x] deep-equal
30
30
  - [x] type-fest
31
- - [ ] uuid
32
- - [ ] zod
31
+ - [x] uuid
32
+ - [x] zod
33
33
  - [ ] Make subpackages for adapters for different validation languages
34
34
  - [ ] Zod
35
35
  - [ ] Yup
package/dist/builder.d.ts CHANGED
@@ -1,7 +1,7 @@
1
- import { Model, type CollectionZodSchema } from "./model";
1
+ import { CollectionSchema, Model } from "./model";
2
2
  import { DbClient } from "./client";
3
3
  import { PrimaryKey, type ValidValue } from "./field";
4
- import type { Dict } from "./types/common";
4
+ import type { Dict } from "./util-types";
5
5
  export type CollectionObject<Names extends string> = {
6
6
  [K in Names]: Model<K, any>;
7
7
  };
@@ -16,7 +16,7 @@ export declare class Builder<Name extends string, Names extends string> {
16
16
  export declare class CompiledDb<Name extends string, Names extends string, C extends CollectionObject<Names>> {
17
17
  readonly name: Name;
18
18
  private readonly models;
19
- readonly schemas: CollectionZodSchema<C>;
19
+ readonly schemas: CollectionSchema<C>;
20
20
  private readonly modelKeys;
21
21
  constructor(name: Name, models: C);
22
22
  getModel<N extends Names>(name: N): C[N];
@@ -1,4 +1,4 @@
1
- import type { Dict, Promisable } from "../types/common.js";
1
+ import type { Dict, Promisable } from "../util-types.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";
@@ -1,5 +1,5 @@
1
1
  import type { CollectionObject, CompiledDb } from "../builder";
2
- import type { Arrayable } from "../types/common";
2
+ import type { Arrayable } from "../util-types";
3
3
  import { Transaction, type TransactionOptions } from "../transaction";
4
4
  import { type InterfaceMap } from "./types";
5
5
  export declare class DbClient<Name extends string, ModelNames extends string, Models extends CollectionObject<ModelNames>> {
@@ -1,14 +1,18 @@
1
- import type { Dict, If, Keyof, RemoveNeverValues } from "../../types/common.js";
1
+ import type { Dict, If, Keyof, RemoveNeverValues } from "../../util-types.js";
2
2
  import type { ExtractFields, ModelStructure, RelationlessModelStructure } from "../../model/model-types.js";
3
3
  import type { Model } from "../../model";
4
- import type { BaseRelation, Field, PrimaryKey, RelationOutputStructure, ValidValue } from "../../field";
4
+ import type { BaseRelation, AbstractProperty, PrimaryKey, RelationOutputStructure, ValidValue } from "../../field";
5
5
  import type { CollectionObject } from "../../builder.ts";
6
6
  export type FilterFn<Input> = (item: Input) => boolean;
7
+ /**
8
+ * 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
9
+ */
10
+ export type ProhibitObjects<T> = T extends object ? never : T;
7
11
  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;
12
+ [K in keyof Fields]: Fields[K] extends AbstractProperty<infer Output, any> ? Output | FilterFn<Output> : Fields[K] extends PrimaryKey<any, infer Type> ? ProhibitObjects<Type> | FilterFn<Type> : never;
9
13
  }>>;
10
14
  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;
15
+ [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;
12
16
  };
13
17
  export interface QueryInput<All extends string, Fields extends Dict<ValidValue>, C extends CollectionObject<All>> {
14
18
  where?: WhereObject<Fields>;
@@ -17,7 +21,7 @@ export interface QueryInput<All extends string, Fields extends Dict<ValidValue>,
17
21
  }
18
22
  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
23
  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;
24
+ [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 AbstractProperty<infer Type, any> ? Type : never;
21
25
  } | undefined;
22
26
  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;
23
27
  export {};
@@ -1,5 +1,5 @@
1
1
  import type { CollectionObject } from "../../builder.ts";
2
- import type { Dict, ValidKey } from "../../types/common.js";
2
+ import type { Dict, ValidKey } from "../../util-types.js";
3
3
  import type { ExtractFields, Model, ModelStructure, PrimaryKeyType } from "../../model";
4
4
  import type { AddMutation, UpdateMutation } from "./mutation.ts";
5
5
  import type { FindInput, FindOutput, WhereObject } from "./find.ts";
@@ -1,24 +1,24 @@
1
- import type { MakeArrayable, MakeOptional, RemoveNeverValues, PartialOnUndefined } from "../../types/common.js";
2
- import type { BaseRelation, Field, OptionalRelation, PrimaryKey, RelationArray } from "../../field";
1
+ import type { MakeArrayable, MakeOptional, RemoveNeverValues, PartialOnUndefined } from "../../util-types.js";
2
+ import type { BaseRelation, AbstractProperty, OptionalRelation, PrimaryKey, ArrayRelation } from "../../field";
3
3
  import type { CollectionObject } from "../../builder.ts";
4
4
  import { WhereObject } from "./find.js";
5
5
  import { Model, FindRelationKey, RelationValue } from "../../model";
6
6
  export type MutationActions = "$connect" | "$connectMany" | "$create" | "$createMany" | "$update" | "$updateMany" | "$delete" | "$deleteMany" | "$deleteAll" | "$disconnect" | "$disconnectMany" | "$disconnectAll";
7
7
  export type Mutation<This extends All, All extends string, Struct extends object, C extends CollectionObject<All>, MutType extends string = "add"> = PartialOnUndefined<RemoveNeverValues<Struct extends Model<any, infer Fields, any> ? {
8
- [K in keyof Fields]: Fields[K] extends Field<infer Type, infer HasDefault> ? MutType extends "update" ? Type | undefined | ((value: Type) => Type) : HasDefault extends true ? Type | undefined : Type : Fields[K] extends PrimaryKey<infer IsAuto, infer Type> ? MutType extends "update" ? never : 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 RelationArray<any, any> ? true : MutType extends "update" ? true : false, MakeArrayable<Fields[K] extends RelationArray<any, any> ? true : false, {
8
+ [K in keyof Fields]: Fields[K] extends AbstractProperty<infer Type, infer HasDefault> ? MutType extends "update" ? Type | undefined | ((value: Type) => Type) : HasDefault extends true ? Type | undefined : Type : Fields[K] extends PrimaryKey<infer IsAuto, infer Type> ? MutType extends "update" ? never : 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 : MutType extends "update" ? true : false, MakeArrayable<Fields[K] extends ArrayRelation<any, any> ? true : false, {
9
9
  $connect: RelationValue<To, C>;
10
10
  } | {
11
11
  $create: Omit<Mutation<To, All, C[To], C, "add">, FindRelationKey<This, Name, C[To]>>;
12
12
  } | (MutType extends "update" ? {
13
- $update: Fields[K] extends RelationArray<any, any> ? {
13
+ $update: Fields[K] extends ArrayRelation<any, any> ? {
14
14
  where?: WhereSelection<C[To]>;
15
15
  data: Mutation<To, All, C[To], C, MutType>;
16
16
  } : Mutation<To, All, C[To], C, MutType>;
17
17
  } | {
18
- $delete: Fields[K] extends RelationArray<any, any> ? RelationValue<To, C> : true;
18
+ $delete: Fields[K] extends ArrayRelation<any, any> ? RelationValue<To, C> : true;
19
19
  } | {
20
- $disconnect: Fields[K] extends RelationArray<any, any> ? RelationValue<To, C> : true;
21
- } : never)> | (Fields[K] extends RelationArray<any, any> ? {
20
+ $disconnect: Fields[K] extends ArrayRelation<any, any> ? RelationValue<To, C> : true;
21
+ } : never)> | (Fields[K] extends ArrayRelation<any, any> ? {
22
22
  $connectMany: RelationValue<To, C>[];
23
23
  } | {
24
24
  $createMany: Omit<Mutation<To, All, C[To], C, "add">, FindRelationKey<This, Name, C[To]>>[];
@@ -0,0 +1,3 @@
1
+ import { ParseFn, AbstractProperty, PropertyOptions } from "./field/property.js";
2
+ import type * as util from "./util-types.js";
3
+ export { ParseFn, AbstractProperty, util, PropertyOptions };
@@ -1,7 +1,7 @@
1
- import { ValidKey } from "../types/common.js";
2
- import { Field } from "./field.js";
1
+ import { Dict, ValidKey, ValidKeyType } from "../util-types.js";
3
2
  import PrimaryKey from "./primary-key.js";
4
- import { BaseRelation, OptionalRelation, RelationArray } from "./relation.js";
3
+ import { AbstractProperty, ParseFn, Property } from "./property.js";
4
+ import { BaseRelation, OptionalRelation, ArrayRelation } from "./relation.js";
5
5
  export type ReferenceActions = "Cascade" | "None" | "Restrict";
6
6
  export type OptionalActions = "SetNull" | ReferenceActions;
7
7
  export interface RelationOptions<Name extends string, OnDelete> {
@@ -24,6 +24,13 @@ export interface FieldOptions {
24
24
  unique: boolean;
25
25
  }
26
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>;
27
+ export type RelationOutputStructure<R extends BaseRelation<any, any>, Output> = R extends ArrayRelation<any, any> ? Output[] : R extends OptionalRelation<any, any> ? Output | undefined : Output;
28
+ export type NonRelationOutput<T> = T extends AbstractProperty<infer Out, any> ? Out : T extends PrimaryKey<any, infer Type> ? Type : never;
29
+ export type ValidValue<N extends string = string> = BaseRelation<N, string> | AbstractProperty<any, any> | PrimaryKey<boolean, ValidKey>;
30
+ export type ParseFnWrap<T extends Dict> = {
31
+ [K in keyof T]: ParseFn<T[K]>;
32
+ };
33
+ 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;
34
+ export declare const VALIDATORS: {
35
+ [K in ValidKeyType]: ParseFn<FunctionMatch<K>>;
36
+ };
@@ -18,7 +18,7 @@ export declare class Field<OutputType, HasDefault extends boolean = false> {
18
18
  constructor(schema: z.ZodType<OutputType>, options?: Partial<FieldOptions>);
19
19
  array(): Field<OutputType[], false>;
20
20
  optional(): Field<OutputType | undefined, false>;
21
- default(defaultValue: NonNullable<OutputType>): Field<NonNullable<OutputType>, true>;
21
+ default(defaultValue: Exclude<OutputType, undefined>): Field<Exclude<OutputType, undefined>, true>;
22
22
  refine(refineFn: (val: OutputType) => boolean): void;
23
23
  parse(value: unknown): z.ZodSafeParseResult<OutputType>;
24
24
  hasDefaultValue(): HasDefault;
@@ -1,5 +1,5 @@
1
- export * from "./field.js";
2
1
  import PrimaryKey from "./primary-key.js";
3
2
  export { PrimaryKey };
4
3
  export * from "./relation.js";
5
4
  export * from "./field-types.js";
5
+ export * from "./property.js";
@@ -1,4 +1,4 @@
1
- import { ValidKey, ValidKeyType } from "../types/common.js";
1
+ import { ValidKey, ValidKeyType } from "../util-types.js";
2
2
  import { GenFunction } from "./field-types.js";
3
3
  export default class PrimaryKey<AutoGenerate extends boolean, Type extends ValidKey> {
4
4
  private genFn?;
@@ -11,7 +11,7 @@ export default class PrimaryKey<AutoGenerate extends boolean, Type extends Valid
11
11
  generator(genFn: GenFunction<Type>): PrimaryKey<true, Type>;
12
12
  uuid(): PrimaryKey<true, string>;
13
13
  genKey(): Type;
14
- getSchema(): import("zod").ZodString | import("zod").ZodNumber | import("zod").ZodDate;
14
+ getSchema(): import("./property.js").ParseFn<string> | import("./property.js").ParseFn<number> | import("./property.js").ParseFn<Date>;
15
15
  /**
16
16
  * If the internal objectStore "autoIncrement" utility is being used
17
17
  * @returns
@@ -1,21 +1,12 @@
1
- import { Literable, ValidKeyType } from "../types/common.js";
2
- import { FunctionMatch, ReferenceActions, RelationOptions } from "./field-types.js";
1
+ import { Literable, NoUndefined, ValidKeyType } from "../util-types.js";
2
+ import { Type } from "../utils.js";
3
+ import { FunctionMatch, PropertyUnion, ReferenceActions, RelationOptions } from "./field-types.js";
3
4
  import PrimaryKey from "./primary-key.js";
4
5
  import { Relation } from "./relation.js";
5
6
  export interface PropertyOptions {
6
7
  unique: boolean;
7
8
  }
8
9
  type InputOptions = Partial<PropertyOptions>;
9
- declare enum PropertType {
10
- String = 0,
11
- Number = 1,
12
- BigInt = 2,
13
- Boolean = 3,
14
- Symbol = 4,
15
- Array = 5,
16
- Object = 6,
17
- Unknown = 7
18
- }
19
10
  export type ParseResult<T> = {
20
11
  success: true;
21
12
  data: T;
@@ -31,16 +22,17 @@ export type ParseResult<T> = {
31
22
  export type ParseFn<T> = (value: unknown) => ParseResult<T>;
32
23
  export declare abstract class AbstractProperty<Value, HasDefault extends boolean> {
33
24
  readonly validate: (value: unknown) => ParseResult<Value>;
34
- protected type: PropertType;
25
+ protected type: Type;
35
26
  protected hasDefault: HasDefault;
36
27
  protected options: PropertyOptions;
37
- constructor(validate: (value: unknown) => ParseResult<Value>, type: PropertType, options?: InputOptions);
28
+ constructor(validate: (value: unknown) => ParseResult<Value>, type: Type, options?: InputOptions);
38
29
  abstract array(...args: unknown[]): AbstractProperty<Value[], false>;
39
30
  abstract optional(...args: unknown[]): AbstractProperty<Value | undefined, false>;
40
- abstract default(defaultValue: Value): AbstractProperty<NonNullable<Value>, true>;
31
+ abstract default(defaultValue: Value): AbstractProperty<NoUndefined<Value>, true>;
41
32
  static array<T>(_prop: AbstractProperty<T, boolean> | ParseFn<T>, _options?: InputOptions): AbstractProperty<T[], false>;
42
33
  static literal<const V extends Literable>(_value: V, _options?: InputOptions): AbstractProperty<V, false>;
43
34
  static custom<T>(_fn: ParseFn<T>, _options?: InputOptions): AbstractProperty<T, false>;
35
+ static union<const T extends readonly (ParseFn<any> | AbstractProperty<any, boolean>)[]>(_items: T, _options?: InputOptions): AbstractProperty<PropertyUnion<T>, false>;
44
36
  static string(_options?: InputOptions): AbstractProperty<string, false>;
45
37
  static number(_options?: InputOptions): AbstractProperty<number, false>;
46
38
  static boolean(_options?: InputOptions): AbstractProperty<boolean, false>;
@@ -53,18 +45,24 @@ export declare abstract class AbstractProperty<Value, HasDefault extends boolean
53
45
  hasDefaultValue(): HasDefault;
54
46
  static relation<To extends string, Name extends string = never>(to: To, options?: RelationOptions<Name, ReferenceActions>): Relation<To, Name>;
55
47
  static primaryKey<V extends ValidKeyType = "number">(type?: V): PrimaryKey<false, FunctionMatch<V>>;
48
+ static readonly validators: {
49
+ string: ParseFn<string>;
50
+ number: ParseFn<number>;
51
+ date: ParseFn<Date>;
52
+ };
53
+ protected static literalToType(value: Literable): Type;
56
54
  }
57
55
  export declare class Property<Value, HasDefault extends boolean> extends AbstractProperty<Value, HasDefault> {
58
56
  array(): Property<Value[], false>;
59
- default(defaultValue: NonNullable<Value>): Property<NonNullable<Value>, true>;
57
+ default(defaultValue: NoUndefined<Value>): Property<NoUndefined<Value>, true>;
60
58
  optional(): Property<Value | undefined, false>;
61
59
  static literal<const V extends Literable>(value: V, options?: InputOptions): Property<V, false>;
62
60
  static string(options?: InputOptions): Property<string, false>;
63
61
  static number(options?: InputOptions): Property<number, false>;
64
62
  static boolean(options?: InputOptions): Property<boolean, false>;
63
+ static union<const T extends readonly (ParseFn<any> | AbstractProperty<any, boolean>)[]>(items: T, options?: InputOptions): Property<PropertyUnion<T>, false>;
65
64
  static custom<T>(fn: ParseFn<T>, options?: InputOptions): Property<T, false>;
66
65
  static array<T>(item: ParseFn<T> | Property<T, boolean>, options?: InputOptions): Property<T[], false>;
67
- private static literalToType;
68
66
  private static generateArrayValidator;
69
67
  }
70
68
  export {};
@@ -58,7 +58,7 @@ export declare class Relation<To extends string, Name extends string> extends Ba
58
58
  *
59
59
  * **Note: Calling this function will reset any relation actions to the default**
60
60
  */
61
- array({ onDelete, }?: Omit<RelationOptions<Name, OptionalActions>, "name">): RelationArray<To, Name>;
61
+ array({ onDelete, }?: Omit<RelationOptions<Name, OptionalActions>, "name">): ArrayRelation<To, Name>;
62
62
  /**
63
63
  * Creates an optional relation to the specified model
64
64
  *
@@ -67,7 +67,7 @@ export declare class Relation<To extends string, Name extends string> extends Ba
67
67
  optional({ onDelete, }?: Omit<RelationOptions<Name, OptionalActions>, "name">): OptionalRelation<To, Name>;
68
68
  onDelete(action: ReferenceActions): this;
69
69
  }
70
- export declare class RelationArray<To extends string, Name extends string> extends BaseRelation<To, Name> {
70
+ export declare class ArrayRelation<To extends string, Name extends string> extends BaseRelation<To, Name> {
71
71
  private readonly _brand;
72
72
  constructor(to: To, name?: Name, action?: OptionalActions);
73
73
  }
package/dist/index.d.ts CHANGED
@@ -1,8 +1,9 @@
1
1
  import { Builder } from "./builder.js";
2
2
  import { StoreError, type ErrorType } from "./error.js";
3
+ import "./dev-types.js";
3
4
  export { Builder, StoreError, ErrorType };
4
- import { Field } from "./field";
5
- export { Field };
5
+ import { Property } from "./field";
6
+ export { Property };
6
7
  import { CompiledQuery } from "./client/compiled-query.js";
7
8
  export { CompiledQuery };
8
9
  import type { ModelType } from "./model";