@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.
@@ -1,225 +1,46 @@
1
- import type {
2
- MakeArrayable,
3
- MakeOptional,
4
- RemoveNeverValues,
5
- PartialOnUndefined,
6
- } from "../../types/common.js";
7
- import type {
8
- BaseRelation,
9
- Field,
10
- OptionalRelation,
11
- PrimaryKey,
12
- RelationArray,
13
- } from "../../field/index";
14
- import type { CollectionObject } from "../../builder";
1
+ import type { MakeArrayable, MakeOptional, RemoveNeverValues, PartialOnUndefined } from "../../types/common.js";
2
+ import type { BaseRelation, Field, OptionalRelation, PrimaryKey, RelationArray } from "../../field";
3
+ import type { CollectionObject } from "../../builder.ts";
15
4
  import { WhereObject } from "./find.js";
16
- import { Model, FindRelationKey, RelationValue } from "../../model/index";
17
- export type MutationActions =
18
- | "$connect"
19
- | "$connectMany"
20
- | "$create"
21
- | "$createMany"
22
- | "$update"
23
- | "$updateMany"
24
- | "$delete"
25
- | "$deleteMany"
26
- | "$deleteAll"
27
- | "$disconnect"
28
- | "$disconnectMany"
29
- | "$disconnectAll";
30
- export type Mutation<
31
- This extends All,
32
- All extends string,
33
- Struct extends object,
34
- C extends CollectionObject<All>,
35
- MutType extends string = "add"
36
- > = PartialOnUndefined<
37
- RemoveNeverValues<
38
- Struct extends Model<any, infer Fields, any>
39
- ? {
40
- [K in keyof Fields]: Fields[K] extends Field<
41
- infer Type,
42
- infer HasDefault
43
- >
44
- ? MutType extends "update"
45
- ? Type | undefined | ((value: Type) => Type)
46
- : HasDefault extends true
47
- ? Type | undefined
48
- : Type
49
- : Fields[K] extends PrimaryKey<infer IsAuto, infer Type>
50
- ? MutType extends "update"
51
- ? never
52
- : IsAuto extends true
53
- ? never
54
- : Type
55
- : Fields[K] extends BaseRelation<infer To, infer Name>
56
- ? To extends All
57
- ? MakeOptional<
58
- Fields[K] extends OptionalRelation<any, any>
59
- ? true
60
- : Fields[K] extends RelationArray<any, any>
61
- ? true
62
- : MutType extends "update"
63
- ? true
64
- : false,
65
- | MakeArrayable<
66
- Fields[K] extends RelationArray<any, any>
67
- ? true
68
- : false,
69
- | {
70
- $connect: RelationValue<To, C>;
71
- }
72
- | {
73
- $create: Omit<
74
- Mutation<
75
- To,
76
- All,
77
- C[To],
78
- C,
79
- "add"
80
- >,
81
- FindRelationKey<
82
- This,
83
- Name,
84
- C[To]
85
- >
86
- >;
87
- }
88
- | (MutType extends "update"
89
- ?
90
- | {
91
- $update: Fields[K] extends RelationArray<
92
- any,
93
- any
94
- >
95
- ? {
96
- where?: WhereSelection<
97
- C[To]
98
- >;
99
- data: Mutation<
100
- To,
101
- All,
102
- C[To],
103
- C,
104
- MutType
105
- >;
106
- }
107
- : Mutation<
108
- To,
109
- All,
110
- C[To],
111
- C,
112
- MutType
113
- >;
114
- }
115
- | {
116
- $delete: Fields[K] extends RelationArray<
117
- any,
118
- any
119
- >
120
- ? RelationValue<
121
- To,
122
- C
123
- >
124
- : true;
125
- }
126
- | {
127
- $disconnect: Fields[K] extends RelationArray<
128
- any,
129
- any
130
- >
131
- ? RelationValue<
132
- To,
133
- C
134
- >
135
- : true;
136
- }
137
- : never)
138
- >
139
- | (Fields[K] extends RelationArray<any, any>
140
- ?
141
- | {
142
- $connectMany: RelationValue<
143
- To,
144
- C
145
- >[];
146
- }
147
- | {
148
- $createMany: Omit<
149
- Mutation<
150
- To,
151
- All,
152
- C[To],
153
- C,
154
- "add"
155
- >,
156
- FindRelationKey<
157
- This,
158
- Name,
159
- C[To]
160
- >
161
- >[];
162
- }
163
- | {
164
- $updateMany: {
165
- where?: WhereSelection<
166
- C[To]
167
- >;
168
- data: Mutation<
169
- To,
170
- All,
171
- C[To],
172
- C,
173
- MutType
174
- >;
175
- }[];
176
- }
177
- | {
178
- $deleteMany: RelationValue<
179
- To,
180
- C
181
- >[];
182
- }
183
- | {
184
- $deleteAll: true;
185
- }
186
- | {
187
- $disconnectMany: RelationValue<
188
- To,
189
- C
190
- >[];
191
- }
192
- | {
193
- $disconnectAll: true;
194
- }
195
- : never)
196
- >
197
- : never
198
- : never;
199
- }
200
- : never
201
- >
202
- >;
203
- export type AddMutation<
204
- This extends All,
205
- All extends string,
206
- Struct extends object,
207
- C extends CollectionObject<All>
208
- > = Mutation<This, All, Struct, C, "add">;
209
- export interface UpdateMutation<
210
- This extends All,
211
- All extends string,
212
- Struct extends object,
213
- C extends CollectionObject<All>
214
- > {
5
+ import { Model, FindRelationKey, RelationValue } from "../../model";
6
+ export type MutationActions = "$connect" | "$connectMany" | "$create" | "$createMany" | "$update" | "$updateMany" | "$delete" | "$deleteMany" | "$deleteAll" | "$disconnect" | "$disconnectMany" | "$disconnectAll";
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, {
9
+ $connect: RelationValue<To, C>;
10
+ } | {
11
+ $create: Omit<Mutation<To, All, C[To], C, "add">, FindRelationKey<This, Name, C[To]>>;
12
+ } | (MutType extends "update" ? {
13
+ $update: Fields[K] extends RelationArray<any, any> ? {
14
+ where?: WhereSelection<C[To]>;
15
+ data: Mutation<To, All, C[To], C, MutType>;
16
+ } : Mutation<To, All, C[To], C, MutType>;
17
+ } | {
18
+ $delete: Fields[K] extends RelationArray<any, any> ? RelationValue<To, C> : true;
19
+ } | {
20
+ $disconnect: Fields[K] extends RelationArray<any, any> ? RelationValue<To, C> : true;
21
+ } : never)> | (Fields[K] extends RelationArray<any, any> ? {
22
+ $connectMany: RelationValue<To, C>[];
23
+ } | {
24
+ $createMany: Omit<Mutation<To, All, C[To], C, "add">, FindRelationKey<This, Name, C[To]>>[];
25
+ } | {
26
+ $updateMany: {
27
+ where?: WhereSelection<C[To]>;
28
+ data: Mutation<To, All, C[To], C, MutType>;
29
+ }[];
30
+ } | {
31
+ $deleteMany: RelationValue<To, C>[];
32
+ } | {
33
+ $deleteAll: true;
34
+ } | {
35
+ $disconnectMany: RelationValue<To, C>[];
36
+ } | {
37
+ $disconnectAll: true;
38
+ } : never)> : never : never;
39
+ } : never>>;
40
+ export type AddMutation<This extends All, All extends string, Struct extends object, C extends CollectionObject<All>> = Mutation<This, All, Struct, C, "add">;
41
+ export interface UpdateMutation<This extends All, All extends string, Struct extends object, C extends CollectionObject<All>> {
215
42
  where?: WhereSelection<Struct>;
216
43
  data: Mutation<This, All, Struct, C, "update">;
217
44
  }
218
- type WhereSelection<Struct extends object> = Struct extends Model<
219
- any,
220
- infer Fields,
221
- any
222
- >
223
- ? WhereObject<Fields>
224
- : never;
45
+ type WhereSelection<Struct extends object> = Struct extends Model<any, infer Fields, any> ? WhereObject<Fields> : never;
225
46
  export {};
@@ -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;
@@ -0,0 +1,70 @@
1
+ import { Literable, ValidKeyType } from "../types/common.js";
2
+ import { FunctionMatch, ReferenceActions, RelationOptions } from "./field-types.js";
3
+ import PrimaryKey from "./primary-key.js";
4
+ import { Relation } from "./relation.js";
5
+ export interface PropertyOptions {
6
+ unique: boolean;
7
+ }
8
+ 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
+ export type ParseResult<T> = {
20
+ success: true;
21
+ data: T;
22
+ error?: undefined;
23
+ } | {
24
+ success: false;
25
+ data?: undefined;
26
+ error: string;
27
+ };
28
+ /**
29
+ * A function to parse and validate an unknown value. It should also handle applying defaults
30
+ */
31
+ export type ParseFn<T> = (value: unknown) => ParseResult<T>;
32
+ export declare abstract class AbstractProperty<Value, HasDefault extends boolean> {
33
+ readonly validate: (value: unknown) => ParseResult<Value>;
34
+ protected type: PropertType;
35
+ protected hasDefault: HasDefault;
36
+ protected options: PropertyOptions;
37
+ constructor(validate: (value: unknown) => ParseResult<Value>, type: PropertType, options?: InputOptions);
38
+ abstract array(...args: unknown[]): AbstractProperty<Value[], false>;
39
+ abstract optional(...args: unknown[]): AbstractProperty<Value | undefined, false>;
40
+ abstract default(defaultValue: Value): AbstractProperty<Exclude<Value, undefined>, true>;
41
+ static array<T>(_prop: AbstractProperty<T, boolean> | ParseFn<T>, _options?: InputOptions): AbstractProperty<T[], false>;
42
+ static literal<const V extends Literable>(_value: V, _options?: InputOptions): AbstractProperty<V, false>;
43
+ static custom<T>(_fn: ParseFn<T>, _options?: InputOptions): AbstractProperty<T, false>;
44
+ static string(_options?: InputOptions): AbstractProperty<string, false>;
45
+ static number(_options?: InputOptions): AbstractProperty<number, false>;
46
+ static boolean(_options?: InputOptions): AbstractProperty<boolean, false>;
47
+ /**
48
+ * Indicates that a field must be unique across all documents
49
+ *
50
+ * **NOTE**: The field type must be a primitive. If this is applied to a non-primitive, it returns `null`
51
+ */
52
+ unique(): Value extends string | number | boolean | symbol ? this : null;
53
+ hasDefaultValue(): HasDefault;
54
+ static relation<To extends string, Name extends string = never>(to: To, options?: RelationOptions<Name, ReferenceActions>): Relation<To, Name>;
55
+ static primaryKey<V extends ValidKeyType = "number">(type?: V): PrimaryKey<false, FunctionMatch<V>>;
56
+ }
57
+ export declare class Property<Value, HasDefault extends boolean> extends AbstractProperty<Value, HasDefault> {
58
+ array(): Property<Value[], false>;
59
+ default(defaultValue: Exclude<Value, undefined>): Property<Exclude<Value, undefined>, true>;
60
+ optional(): Property<Value | undefined, false>;
61
+ static literal<const V extends Literable>(value: V, options?: InputOptions): Property<V, false>;
62
+ static string(options?: InputOptions): Property<string, false>;
63
+ static number(options?: InputOptions): Property<number, false>;
64
+ static boolean(options?: InputOptions): Property<boolean, false>;
65
+ static custom<T>(fn: ParseFn<T>, options?: InputOptions): Property<T, false>;
66
+ static array<T>(item: ParseFn<T> | Property<T, boolean>, options?: InputOptions): Property<T[], false>;
67
+ private static literalToType;
68
+ private static generateArrayValidator;
69
+ }
70
+ export {};