@zenstackhq/runtime 3.0.0-alpha.3 → 3.0.0-alpha.30
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/{contract-0F-AwA7Z.d.cts → contract-DUtKB39c.d.cts} +999 -808
- package/dist/{contract-0F-AwA7Z.d.ts → contract-DUtKB39c.d.ts} +999 -808
- package/dist/helpers.cjs +31 -0
- package/dist/helpers.cjs.map +1 -0
- package/dist/helpers.d.cts +1 -0
- package/dist/helpers.d.ts +1 -0
- package/dist/helpers.js +6 -0
- package/dist/helpers.js.map +1 -0
- package/dist/index.cjs +1940 -1011
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +30 -8
- package/dist/index.d.ts +30 -8
- package/dist/index.js +1910 -987
- package/dist/index.js.map +1 -1
- package/dist/plugins/{policy.cjs → policy/index.cjs} +578 -310
- package/dist/plugins/policy/index.cjs.map +1 -0
- package/dist/plugins/{policy.d.ts → policy/index.d.cts} +2 -4
- package/dist/plugins/{policy.d.cts → policy/index.d.ts} +2 -4
- package/dist/plugins/{policy.js → policy/index.js} +553 -275
- package/dist/plugins/policy/index.js.map +1 -0
- package/dist/plugins/policy/plugin.zmodel +33 -0
- package/dist/schema.cjs.map +1 -1
- package/dist/schema.js.map +1 -1
- package/package.json +30 -28
- package/dist/client.cjs +0 -6084
- package/dist/client.cjs.map +0 -1
- package/dist/client.d.cts +0 -19
- package/dist/client.d.ts +0 -19
- package/dist/client.js +0 -6050
- package/dist/client.js.map +0 -1
- package/dist/plugins/policy.cjs.map +0 -1
- package/dist/plugins/policy.js.map +0 -1
|
@@ -1,12 +1,27 @@
|
|
|
1
|
-
import { SchemaDef, GetModels, ScalarFields, ForeignKeyFields, GetFields, FieldHasDefault, GetFieldType, FieldIsOptional, RelationFields, RelationFieldType, FieldIsArray, GetEnums, GetEnum, BuiltinType, NonRelationFields, GetModel, FieldDef, GetField, FieldIsRelationArray, RelationInfo, FieldType, FieldIsRelation, DataSourceProviderType, ProcedureDef, DataSourceProvider } from '@zenstackhq/sdk/schema';
|
|
2
1
|
import Decimal, { Decimal as Decimal$1 } from 'decimal.js';
|
|
3
|
-
import {
|
|
4
|
-
import {
|
|
5
|
-
import { Model } from '@zenstackhq/language/ast';
|
|
2
|
+
import { SchemaDef, GetModels, ScalarFields, ForeignKeyFields, GetModelFields, FieldHasDefault, GetModelFieldType, ModelFieldIsOptional, GetModelField, NonRelationFields, RelationFields, FieldIsArray, RelationFieldType, FieldIsRelation, GetModel, FieldDef, GetEnums, GetEnum, GetTypeDefs, GetTypeDefFields, GetTypeDefField, TypeDefFieldIsOptional, BuiltinType, IsDelegateModel, GetModelDiscriminator, GetSubModels, FieldIsDelegateDiscriminator, FieldType, RelationInfo, FieldIsDelegateRelation, DataSourceProviderType, ProcedureDef } from '@zenstackhq/sdk/schema';
|
|
3
|
+
import { Generated, Kysely, ExpressionBuilder, OperandExpression, SqlBool, SelectQueryBuilder, Expression, ExpressionWrapper, OperationNode, RootOperationNode, QueryResult, UnknownRow, Dialect, KyselyConfig } from 'kysely';
|
|
6
4
|
|
|
5
|
+
type Optional<T extends object, K extends keyof T = keyof T> = Omit<T, K> & Partial<Pick<T, K>>;
|
|
7
6
|
type NullableIf<T, Condition extends boolean> = Condition extends true ? T | null : T;
|
|
7
|
+
type _Preserve = Date | Function | Decimal | Uint8Array | JsonObject | JsonValue;
|
|
8
|
+
type _Depth = [never, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9];
|
|
9
|
+
type Simplify<T, D extends number = 6> = D extends 0 ? T : T extends object ? T extends _Preserve ? T : {
|
|
10
|
+
[K in keyof T]: Simplify<T[K], _Depth[D]>;
|
|
11
|
+
} & {} : T;
|
|
8
12
|
type WrapType<T, Optional = false, Array = false> = Optional extends true ? T | null : Array extends true ? T[] : T;
|
|
9
|
-
type
|
|
13
|
+
type TypeMap = {
|
|
14
|
+
String: string;
|
|
15
|
+
Boolean: boolean;
|
|
16
|
+
Int: number;
|
|
17
|
+
Float: number;
|
|
18
|
+
BigInt: bigint;
|
|
19
|
+
Decimal: Decimal;
|
|
20
|
+
DateTime: Date;
|
|
21
|
+
Bytes: Uint8Array;
|
|
22
|
+
Json: JsonValue;
|
|
23
|
+
};
|
|
24
|
+
type MapBaseType$1<T extends string> = T extends keyof TypeMap ? TypeMap[T] : unknown;
|
|
10
25
|
type JsonValue = string | number | boolean | null | JsonObject | JsonArray;
|
|
11
26
|
type JsonObject = {
|
|
12
27
|
[key: string]: JsonValue;
|
|
@@ -24,28 +39,34 @@ type AtLeast<O extends object, K extends string> = NoExpand<O extends unknown ?
|
|
|
24
39
|
type Without<T, U> = {
|
|
25
40
|
[P in Exclude<keyof T, keyof U>]?: never;
|
|
26
41
|
};
|
|
27
|
-
type XOR<T, U> = T extends object ? U extends object ? (Without<T, U> & U) | (Without<U, T> & T) : U : T;
|
|
42
|
+
type XOR<T, U> = T extends object ? (U extends object ? (Without<T, U> & U) | (Without<U, T> & T) : U) : T;
|
|
28
43
|
type MaybePromise<T> = T | Promise<T>;
|
|
29
44
|
type PrependParameter<Param, Func> = Func extends (...args: any[]) => infer R ? (p: Param, ...args: Parameters<Func>) => R : never;
|
|
30
45
|
type OrUndefinedIf<T, Condition extends boolean> = Condition extends true ? T | undefined : T;
|
|
46
|
+
type UnwrapTuplePromises<T extends readonly unknown[]> = {
|
|
47
|
+
[K in keyof T]: Awaited<T[K]>;
|
|
48
|
+
};
|
|
31
49
|
|
|
32
50
|
type ToKyselySchema<Schema extends SchemaDef> = {
|
|
33
51
|
[Model in GetModels<Schema>]: ToKyselyTable<Schema, Model>;
|
|
34
52
|
};
|
|
35
53
|
type ToKysely<Schema extends SchemaDef> = Kysely<ToKyselySchema<Schema>>;
|
|
36
54
|
type ToKyselyTable<Schema extends SchemaDef, Model extends GetModels<Schema>> = {
|
|
37
|
-
[Field in ScalarFields<Schema, Model, false> | ForeignKeyFields<Schema, Model>]: toKyselyFieldType<Schema, Model, Field>;
|
|
55
|
+
[Field in ScalarFields<Schema, Model, false> | ForeignKeyFields<Schema, Model> as GetModelField<Schema, Model, Field>['originModel'] extends string ? never : Field]: toKyselyFieldType<Schema, Model, Field>;
|
|
38
56
|
};
|
|
39
57
|
type MapBaseType<T> = T extends 'String' ? string : T extends 'Boolean' ? boolean : T extends 'Int' | 'Float' ? number : T extends 'BigInt' ? bigint : T extends 'Decimal' ? Decimal : T extends 'DateTime' ? string : unknown;
|
|
40
58
|
type WrapNull<T, Null> = Null extends true ? T | null : T;
|
|
41
|
-
type MapType$1<Schema extends SchemaDef, Model extends GetModels<Schema>, Field extends
|
|
42
|
-
type toKyselyFieldType<Schema extends SchemaDef, Model extends GetModels<Schema>, Field extends
|
|
59
|
+
type MapType$1<Schema extends SchemaDef, Model extends GetModels<Schema>, Field extends GetModelFields<Schema, Model>> = WrapNull<MapBaseType<GetModelFieldType<Schema, Model, Field>>, ModelFieldIsOptional<Schema, Model, Field>>;
|
|
60
|
+
type toKyselyFieldType<Schema extends SchemaDef, Model extends GetModels<Schema>, Field extends GetModelFields<Schema, Model>> = FieldHasDefault<Schema, Model, Field> extends true ? Generated<MapType$1<Schema, Model, Field>> : MapType$1<Schema, Model, Field>;
|
|
43
61
|
|
|
44
|
-
type DefaultModelResult<Schema extends SchemaDef, Model extends GetModels<Schema>, Omit = undefined, Optional = false, Array = false> = WrapType<{
|
|
45
|
-
[Key in NonRelationFields<Schema, Model> as Key extends keyof Omit ? Omit[Key] extends true ? never : Key : Key]:
|
|
62
|
+
type DefaultModelResult<Schema extends SchemaDef, Model extends GetModels<Schema>, Omit = undefined, Optional = false, Array = false> = WrapType<IsDelegateModel<Schema, Model> extends true ? DelegateUnionResult<Schema, Model, GetSubModels<Schema, Model>, Omit> : {
|
|
63
|
+
[Key in NonRelationFields<Schema, Model> as Key extends keyof Omit ? Omit[Key] extends true ? never : Key : Key]: MapModelFieldType<Schema, Model, Key>;
|
|
46
64
|
}, Optional, Array>;
|
|
65
|
+
type DelegateUnionResult<Schema extends SchemaDef, Model extends GetModels<Schema>, SubModel extends GetModels<Schema>, Omit = undefined> = SubModel extends string ? DefaultModelResult<Schema, SubModel, Omit> & {
|
|
66
|
+
[K in GetModelDiscriminator<Schema, Model>]: SubModel;
|
|
67
|
+
} : never;
|
|
47
68
|
type ModelSelectResult<Schema extends SchemaDef, Model extends GetModels<Schema>, Select, Omit> = {
|
|
48
|
-
[Key in keyof Select as Select[Key] extends false | undefined ? never : Key extends keyof Omit ? Omit[Key] extends true ? never : Key : Key extends '_count' ? Select[Key] extends SelectCount<Schema, Model> ? Key : never : Key]: Key extends '_count' ? SelectCountResult<Schema, Model, Select[Key]> : Key extends NonRelationFields<Schema, Model> ?
|
|
69
|
+
[Key in keyof Select as Select[Key] extends false | undefined ? never : Key extends keyof Omit ? Omit[Key] extends true ? never : Key : Key extends '_count' ? Select[Key] extends SelectCount<Schema, Model> ? Key : never : Key]: Key extends '_count' ? SelectCountResult<Schema, Model, Select[Key]> : Key extends NonRelationFields<Schema, Model> ? MapModelFieldType<Schema, Model, Key> : Key extends RelationFields<Schema, Model> ? Select[Key] extends FindArgs<Schema, RelationFieldType<Schema, Model, Key>, FieldIsArray<Schema, Model, Key>> ? 'select' extends keyof Select[Key] ? ModelResult<Schema, RelationFieldType<Schema, Model, Key>, Pick<Select[Key], 'select'>, ModelFieldIsOptional<Schema, Model, Key>, FieldIsArray<Schema, Model, Key>> : ModelResult<Schema, RelationFieldType<Schema, Model, Key>, Pick<Select[Key], 'include' | 'omit'>, ModelFieldIsOptional<Schema, Model, Key>, FieldIsArray<Schema, Model, Key>> : DefaultModelResult<Schema, RelationFieldType<Schema, Model, Key>, Omit, ModelFieldIsOptional<Schema, Model, Key>, FieldIsArray<Schema, Model, Key>> : never;
|
|
49
70
|
};
|
|
50
71
|
type SelectCountResult<Schema extends SchemaDef, Model extends GetModels<Schema>, C> = C extends true ? {
|
|
51
72
|
[Key in RelationFields<Schema, Model> as FieldIsArray<Schema, Model, Key> extends true ? Key : never]: number;
|
|
@@ -61,15 +82,21 @@ type ModelResult<Schema extends SchemaDef, Model extends GetModels<Schema>, Args
|
|
|
61
82
|
include: infer I;
|
|
62
83
|
omit?: infer O;
|
|
63
84
|
} ? DefaultModelResult<Schema, Model, O> & {
|
|
64
|
-
[Key in keyof I & RelationFields<Schema, Model> as I[Key] extends false | undefined ? never : Key]: I[Key] extends FindArgs<Schema, RelationFieldType<Schema, Model, Key>, FieldIsArray<Schema, Model, Key>> ? ModelResult<Schema, RelationFieldType<Schema, Model, Key>, I[Key],
|
|
85
|
+
[Key in keyof I & RelationFields<Schema, Model> as I[Key] extends false | undefined ? never : Key]: I[Key] extends FindArgs<Schema, RelationFieldType<Schema, Model, Key>, FieldIsArray<Schema, Model, Key>> ? ModelResult<Schema, RelationFieldType<Schema, Model, Key>, I[Key], ModelFieldIsOptional<Schema, Model, Key>, FieldIsArray<Schema, Model, Key>> : DefaultModelResult<Schema, RelationFieldType<Schema, Model, Key>, undefined, ModelFieldIsOptional<Schema, Model, Key>, FieldIsArray<Schema, Model, Key>>;
|
|
65
86
|
} : Args extends {
|
|
66
87
|
omit: infer O;
|
|
67
88
|
} ? DefaultModelResult<Schema, Model, O> : DefaultModelResult<Schema, Model>, Optional, Array>;
|
|
89
|
+
type SimplifiedModelResult<Schema extends SchemaDef, Model extends GetModels<Schema>, Args extends SelectIncludeOmit<Schema, Model, boolean> = {}, Optional = false, Array = false> = Simplify<ModelResult<Schema, Model, Args, Optional, Array>>;
|
|
90
|
+
type TypeDefResult<Schema extends SchemaDef, TypeDef extends GetTypeDefs<Schema>> = Optional<{
|
|
91
|
+
[Key in GetTypeDefFields<Schema, TypeDef>]: MapTypeDefFieldType<Schema, TypeDef, Key>;
|
|
92
|
+
}, keyof {
|
|
93
|
+
[Key in GetTypeDefFields<Schema, TypeDef> as TypeDefFieldIsOptional<Schema, TypeDef, Key> extends true ? Key : never]: true;
|
|
94
|
+
}>;
|
|
68
95
|
type BatchResult = {
|
|
69
96
|
count: number;
|
|
70
97
|
};
|
|
71
|
-
type WhereInput<Schema extends SchemaDef, Model extends GetModels<Schema>, ScalarOnly extends boolean = false> = {
|
|
72
|
-
[Key in
|
|
98
|
+
type WhereInput<Schema extends SchemaDef, Model extends GetModels<Schema>, ScalarOnly extends boolean = false, WithAggregations extends boolean = false> = {
|
|
99
|
+
[Key in GetModelFields<Schema, Model> as ScalarOnly extends true ? Key extends RelationFields<Schema, Model> ? never : Key : Key]?: Key extends RelationFields<Schema, Model> ? RelationFilter<Schema, Model, Key> : FieldIsArray<Schema, Model, Key> extends true ? ArrayFilter<Schema, GetModelFieldType<Schema, Model, Key>> : GetModelFieldType<Schema, Model, Key> extends GetEnums<Schema> ? EnumFilter<Schema, GetModelFieldType<Schema, Model, Key>, ModelFieldIsOptional<Schema, Model, Key>> : PrimitiveFilter<Schema, GetModelFieldType<Schema, Model, Key>, ModelFieldIsOptional<Schema, Model, Key>, WithAggregations>;
|
|
73
100
|
} & {
|
|
74
101
|
$expr?: (eb: ExpressionBuilder<ToKyselySchema<Schema>, Model>) => OperandExpression<SqlBool>;
|
|
75
102
|
} & {
|
|
@@ -83,15 +110,16 @@ type EnumFilter<Schema extends SchemaDef, T extends GetEnums<Schema>, Nullable e
|
|
|
83
110
|
notIn?: (keyof GetEnum<Schema, T>)[];
|
|
84
111
|
not?: EnumFilter<Schema, T, Nullable>;
|
|
85
112
|
};
|
|
86
|
-
type ArrayFilter<T extends string> = {
|
|
87
|
-
equals?:
|
|
88
|
-
has?:
|
|
89
|
-
hasEvery?:
|
|
90
|
-
hasSome?:
|
|
113
|
+
type ArrayFilter<Schema extends SchemaDef, T extends string> = {
|
|
114
|
+
equals?: MapScalarType<Schema, T>[] | null;
|
|
115
|
+
has?: MapScalarType<Schema, T> | null;
|
|
116
|
+
hasEvery?: MapScalarType<Schema, T>[];
|
|
117
|
+
hasSome?: MapScalarType<Schema, T>[];
|
|
91
118
|
isEmpty?: boolean;
|
|
92
119
|
};
|
|
93
|
-
type
|
|
94
|
-
type
|
|
120
|
+
type MapScalarType<Schema extends SchemaDef, T extends string> = T extends GetEnums<Schema> ? keyof GetEnum<Schema, T> : MapBaseType$1<T>;
|
|
121
|
+
type PrimitiveFilter<Schema extends SchemaDef, T extends string, Nullable extends boolean, WithAggregations extends boolean> = T extends 'String' ? StringFilter<Schema, Nullable, WithAggregations> : T extends 'Int' | 'Float' | 'Decimal' | 'BigInt' ? NumberFilter<Schema, T, Nullable, WithAggregations> : T extends 'Boolean' ? BooleanFilter<Schema, Nullable, WithAggregations> : T extends 'DateTime' ? DateTimeFilter<Schema, Nullable, WithAggregations> : T extends 'Bytes' ? BytesFilter<Schema, Nullable, WithAggregations> : T extends 'Json' ? 'Not implemented yet' : never;
|
|
122
|
+
type CommonPrimitiveFilter<Schema extends SchemaDef, DataType, T extends BuiltinType, Nullable extends boolean, WithAggregations extends boolean> = {
|
|
95
123
|
equals?: NullableIf<DataType, Nullable>;
|
|
96
124
|
in?: DataType[];
|
|
97
125
|
notIn?: DataType[];
|
|
@@ -99,30 +127,53 @@ type CommonPrimitiveFilter<DataType, T extends BuiltinType, Nullable extends boo
|
|
|
99
127
|
lte?: DataType;
|
|
100
128
|
gt?: DataType;
|
|
101
129
|
gte?: DataType;
|
|
102
|
-
not?: PrimitiveFilter<T, Nullable>;
|
|
130
|
+
not?: PrimitiveFilter<Schema, T, Nullable, WithAggregations>;
|
|
103
131
|
};
|
|
104
|
-
type StringFilter<Nullable extends boolean> = NullableIf<string, Nullable> | (CommonPrimitiveFilter<string, 'String', Nullable> & {
|
|
132
|
+
type StringFilter<Schema extends SchemaDef, Nullable extends boolean, WithAggregations extends boolean> = NullableIf<string, Nullable> | (CommonPrimitiveFilter<Schema, string, 'String', Nullable, WithAggregations> & {
|
|
105
133
|
contains?: string;
|
|
106
134
|
startsWith?: string;
|
|
107
135
|
endsWith?: string;
|
|
136
|
+
} & (WithAggregations extends true ? {
|
|
137
|
+
_count?: NumberFilter<Schema, 'Int', false, false>;
|
|
138
|
+
_min?: StringFilter<Schema, false, false>;
|
|
139
|
+
_max?: StringFilter<Schema, false, false>;
|
|
140
|
+
} : {}) & (ProviderSupportsCaseSensitivity<Schema> extends true ? {
|
|
108
141
|
mode?: 'default' | 'insensitive';
|
|
109
|
-
});
|
|
110
|
-
type NumberFilter<T extends 'Int' | 'Float' | 'Decimal' | 'BigInt', Nullable extends boolean> = NullableIf<number | bigint, Nullable> | CommonPrimitiveFilter<number, T, Nullable
|
|
111
|
-
|
|
112
|
-
|
|
142
|
+
} : {}));
|
|
143
|
+
type NumberFilter<Schema extends SchemaDef, T extends 'Int' | 'Float' | 'Decimal' | 'BigInt', Nullable extends boolean, WithAggregations extends boolean> = NullableIf<number | bigint, Nullable> | (CommonPrimitiveFilter<Schema, number, T, Nullable, WithAggregations> & (WithAggregations extends true ? {
|
|
144
|
+
_count?: NumberFilter<Schema, 'Int', false, false>;
|
|
145
|
+
_avg?: NumberFilter<Schema, T, false, false>;
|
|
146
|
+
_sum?: NumberFilter<Schema, T, false, false>;
|
|
147
|
+
_min?: NumberFilter<Schema, T, false, false>;
|
|
148
|
+
_max?: NumberFilter<Schema, T, false, false>;
|
|
149
|
+
} : {}));
|
|
150
|
+
type DateTimeFilter<Schema extends SchemaDef, Nullable extends boolean, WithAggregations extends boolean> = NullableIf<Date | string, Nullable> | (CommonPrimitiveFilter<Schema, Date | string, 'DateTime', Nullable, WithAggregations> & (WithAggregations extends true ? {
|
|
151
|
+
_count?: NumberFilter<Schema, 'Int', false, false>;
|
|
152
|
+
_min?: DateTimeFilter<Schema, false, false>;
|
|
153
|
+
_max?: DateTimeFilter<Schema, false, false>;
|
|
154
|
+
} : {}));
|
|
155
|
+
type BytesFilter<Schema extends SchemaDef, Nullable extends boolean, WithAggregations extends boolean> = NullableIf<Uint8Array | Buffer, Nullable> | ({
|
|
113
156
|
equals?: NullableIf<Uint8Array, Nullable>;
|
|
114
157
|
in?: Uint8Array[];
|
|
115
158
|
notIn?: Uint8Array[];
|
|
116
|
-
not?: BytesFilter<Nullable>;
|
|
117
|
-
}
|
|
118
|
-
|
|
159
|
+
not?: BytesFilter<Schema, Nullable, WithAggregations>;
|
|
160
|
+
} & (WithAggregations extends true ? {
|
|
161
|
+
_count?: NumberFilter<Schema, 'Int', false, false>;
|
|
162
|
+
_min?: BytesFilter<Schema, false, false>;
|
|
163
|
+
_max?: BytesFilter<Schema, false, false>;
|
|
164
|
+
} : {}));
|
|
165
|
+
type BooleanFilter<Schema extends SchemaDef, Nullable extends boolean, WithAggregations extends boolean> = NullableIf<boolean, Nullable> | ({
|
|
119
166
|
equals?: NullableIf<boolean, Nullable>;
|
|
120
|
-
not?: BooleanFilter<Nullable>;
|
|
121
|
-
}
|
|
167
|
+
not?: BooleanFilter<Schema, Nullable, WithAggregations>;
|
|
168
|
+
} & (WithAggregations extends true ? {
|
|
169
|
+
_count?: NumberFilter<Schema, 'Int', false, false>;
|
|
170
|
+
_min?: BooleanFilter<Schema, false, false>;
|
|
171
|
+
_max?: BooleanFilter<Schema, false, false>;
|
|
172
|
+
} : {}));
|
|
122
173
|
type SortOrder = 'asc' | 'desc';
|
|
123
174
|
type NullsOrder = 'first' | 'last';
|
|
124
175
|
type OrderBy<Schema extends SchemaDef, Model extends GetModels<Schema>, WithRelation extends boolean, WithAggregation extends boolean> = {
|
|
125
|
-
[Key in NonRelationFields<Schema, Model>]?:
|
|
176
|
+
[Key in NonRelationFields<Schema, Model>]?: ModelFieldIsOptional<Schema, Model, Key> extends true ? SortOrder | {
|
|
126
177
|
sort: SortOrder;
|
|
127
178
|
nulls?: NullsOrder;
|
|
128
179
|
} : SortOrder;
|
|
@@ -131,47 +182,41 @@ type OrderBy<Schema extends SchemaDef, Model extends GetModels<Schema>, WithRela
|
|
|
131
182
|
_count?: SortOrder;
|
|
132
183
|
} : OrderBy<Schema, RelationFieldType<Schema, Model, Key>, WithRelation, WithAggregation>;
|
|
133
184
|
} : {}) & (WithAggregation extends true ? {
|
|
134
|
-
_count?: OrderBy<Schema, Model,
|
|
185
|
+
_count?: OrderBy<Schema, Model, false, false>;
|
|
186
|
+
_min?: MinMaxInput<Schema, Model, SortOrder>;
|
|
187
|
+
_max?: MinMaxInput<Schema, Model, SortOrder>;
|
|
135
188
|
} & (NumericFields<Schema, Model> extends never ? {} : {
|
|
136
|
-
_avg?: SumAvgInput<Schema, Model>;
|
|
137
|
-
_sum?: SumAvgInput<Schema, Model>;
|
|
138
|
-
_min?: MinMaxInput<Schema, Model>;
|
|
139
|
-
_max?: MinMaxInput<Schema, Model>;
|
|
189
|
+
_avg?: SumAvgInput<Schema, Model, SortOrder>;
|
|
190
|
+
_sum?: SumAvgInput<Schema, Model, SortOrder>;
|
|
140
191
|
}) : {});
|
|
141
192
|
type WhereUniqueInput<Schema extends SchemaDef, Model extends GetModels<Schema>> = AtLeast<{
|
|
142
193
|
[Key in keyof GetModel<Schema, Model>['uniqueFields']]?: GetModel<Schema, Model>['uniqueFields'][Key] extends Pick<FieldDef, 'type'> ? MapFieldDefType<Schema, GetModel<Schema, Model>['uniqueFields'][Key]> : {
|
|
143
194
|
[Key1 in keyof GetModel<Schema, Model>['uniqueFields'][Key]]: GetModel<Schema, Model>['uniqueFields'][Key][Key1] extends Pick<FieldDef, 'type'> ? MapFieldDefType<Schema, GetModel<Schema, Model>['uniqueFields'][Key][Key1]> : never;
|
|
144
195
|
};
|
|
145
196
|
} & WhereInput<Schema, Model>, Extract<keyof GetModel<Schema, Model>['uniqueFields'], string>>;
|
|
146
|
-
type
|
|
147
|
-
[Key in NonRelationFields<Schema, Model>]?:
|
|
197
|
+
type OmitInput<Schema extends SchemaDef, Model extends GetModels<Schema>> = {
|
|
198
|
+
[Key in NonRelationFields<Schema, Model>]?: boolean;
|
|
148
199
|
};
|
|
149
200
|
type SelectIncludeOmit<Schema extends SchemaDef, Model extends GetModels<Schema>, AllowCount extends boolean> = {
|
|
150
|
-
select?:
|
|
151
|
-
include?:
|
|
152
|
-
omit?:
|
|
201
|
+
select?: SelectInput<Schema, Model, AllowCount, boolean>;
|
|
202
|
+
include?: IncludeInput<Schema, Model>;
|
|
203
|
+
omit?: OmitInput<Schema, Model>;
|
|
153
204
|
};
|
|
154
|
-
type
|
|
155
|
-
|
|
156
|
-
}
|
|
157
|
-
|
|
158
|
-
cursor?: WhereUniqueInput<Schema, Model>;
|
|
159
|
-
};
|
|
160
|
-
type Select<Schema extends SchemaDef, Model extends GetModels<Schema>, AllowCount extends Boolean, AllowRelation extends boolean = true> = {
|
|
161
|
-
[Key in NonRelationFields<Schema, Model>]?: true;
|
|
162
|
-
} & (AllowRelation extends true ? Include<Schema, Model> : {}) & // relation fields
|
|
163
|
-
(AllowCount extends true ? {
|
|
205
|
+
type SelectInput<Schema extends SchemaDef, Model extends GetModels<Schema>, AllowCount extends boolean = true, AllowRelation extends boolean = true> = {
|
|
206
|
+
[Key in NonRelationFields<Schema, Model>]?: boolean;
|
|
207
|
+
} & (AllowRelation extends true ? IncludeInput<Schema, Model> : {}) & // relation fields
|
|
208
|
+
(AllowCount extends true ? HasToManyRelations<Schema, Model> extends true ? {
|
|
164
209
|
_count?: SelectCount<Schema, Model>;
|
|
165
|
-
} : {});
|
|
166
|
-
type SelectCount<Schema extends SchemaDef, Model extends GetModels<Schema>> =
|
|
210
|
+
} : {} : {});
|
|
211
|
+
type SelectCount<Schema extends SchemaDef, Model extends GetModels<Schema>> = boolean | {
|
|
167
212
|
select: {
|
|
168
|
-
[Key in RelationFields<Schema, Model> as FieldIsArray<Schema, Model, Key> extends true ? Key : never]
|
|
213
|
+
[Key in RelationFields<Schema, Model> as FieldIsArray<Schema, Model, Key> extends true ? Key : never]?: boolean | {
|
|
169
214
|
where: WhereInput<Schema, RelationFieldType<Schema, Model, Key>, false>;
|
|
170
215
|
};
|
|
171
216
|
};
|
|
172
217
|
};
|
|
173
|
-
type
|
|
174
|
-
[Key in RelationFields<Schema, Model>]?: boolean | FindArgs<Schema, RelationFieldType<Schema, Model, Key>, FieldIsArray<Schema, Model, Key>, FieldIsArray<Schema, Model, Key> extends true ? true :
|
|
218
|
+
type IncludeInput<Schema extends SchemaDef, Model extends GetModels<Schema>> = {
|
|
219
|
+
[Key in RelationFields<Schema, Model>]?: boolean | FindArgs<Schema, RelationFieldType<Schema, Model, Key>, FieldIsArray<Schema, Model, Key>, FieldIsArray<Schema, Model, Key> extends true ? true : ModelFieldIsOptional<Schema, Model, Key> extends true ? true : false>;
|
|
175
220
|
};
|
|
176
221
|
type Subset<T, U> = {
|
|
177
222
|
[key in keyof T]: key extends keyof U ? T[key] : never;
|
|
@@ -191,83 +236,94 @@ type ToManyRelationFilter<Schema extends SchemaDef, Model extends GetModels<Sche
|
|
|
191
236
|
none?: WhereInput<Schema, RelationFieldType<Schema, Model, Field>>;
|
|
192
237
|
};
|
|
193
238
|
type ToOneRelationFilter<Schema extends SchemaDef, Model extends GetModels<Schema>, Field extends RelationFields<Schema, Model>> = NullableIf<WhereInput<Schema, RelationFieldType<Schema, Model, Field>> & {
|
|
194
|
-
is?: NullableIf<WhereInput<Schema, RelationFieldType<Schema, Model, Field>>,
|
|
195
|
-
isNot?: NullableIf<WhereInput<Schema, RelationFieldType<Schema, Model, Field>>,
|
|
196
|
-
},
|
|
239
|
+
is?: NullableIf<WhereInput<Schema, RelationFieldType<Schema, Model, Field>>, ModelFieldIsOptional<Schema, Model, Field>>;
|
|
240
|
+
isNot?: NullableIf<WhereInput<Schema, RelationFieldType<Schema, Model, Field>>, ModelFieldIsOptional<Schema, Model, Field>>;
|
|
241
|
+
}, ModelFieldIsOptional<Schema, Model, Field>>;
|
|
197
242
|
type RelationFilter<Schema extends SchemaDef, Model extends GetModels<Schema>, Field extends RelationFields<Schema, Model>> = FieldIsArray<Schema, Model, Field> extends true ? ToManyRelationFilter<Schema, Model, Field> : ToOneRelationFilter<Schema, Model, Field>;
|
|
198
|
-
type
|
|
199
|
-
type
|
|
243
|
+
type MapModelFieldType<Schema extends SchemaDef, Model extends GetModels<Schema>, Field extends GetModelFields<Schema, Model>> = MapFieldDefType<Schema, GetModelField<Schema, Model, Field>>;
|
|
244
|
+
type MapTypeDefFieldType<Schema extends SchemaDef, TypeDef extends GetTypeDefs<Schema>, Field extends GetTypeDefFields<Schema, TypeDef>> = MapFieldDefType<Schema, GetTypeDefField<Schema, TypeDef, Field>>;
|
|
245
|
+
type MapFieldDefType<Schema extends SchemaDef, T extends Pick<FieldDef, 'type' | 'optional' | 'array'>> = WrapType<T['type'] extends GetEnums<Schema> ? keyof GetEnum<Schema, T['type']> : T['type'] extends GetTypeDefs<Schema> ? TypeDefResult<Schema, T['type']> & Record<string, unknown> : MapBaseType$1<T['type']>, T['optional'], T['array']>;
|
|
200
246
|
type OptionalFieldsForCreate<Schema extends SchemaDef, Model extends GetModels<Schema>> = keyof {
|
|
201
|
-
[Key in
|
|
247
|
+
[Key in GetModelFields<Schema, Model> as ModelFieldIsOptional<Schema, Model, Key> extends true ? Key : FieldHasDefault<Schema, Model, Key> extends true ? Key : FieldIsArray<Schema, Model, Key> extends true ? Key : GetModelField<Schema, Model, Key>['updatedAt'] extends true ? Key : never]: GetModelField<Schema, Model, Key>;
|
|
248
|
+
};
|
|
249
|
+
type GetRelation<Schema extends SchemaDef, Model extends GetModels<Schema>, Field extends GetModelFields<Schema, Model>> = GetModelField<Schema, Model, Field>['relation'];
|
|
250
|
+
type OppositeRelation<Schema extends SchemaDef, Model extends GetModels<Schema>, Field extends GetModelFields<Schema, Model>, FT = FieldType<Schema, Model, Field>> = FT extends GetModels<Schema> ? GetRelation<Schema, Model, Field> extends RelationInfo ? GetRelation<Schema, Model, Field>['opposite'] extends GetModelFields<Schema, FT> ? Schema['models'][FT]['fields'][GetRelation<Schema, Model, Field>['opposite']]['relation'] : never : never : never;
|
|
251
|
+
type OppositeRelationFields<Schema extends SchemaDef, Model extends GetModels<Schema>, Field extends GetModelFields<Schema, Model>, Opposite = OppositeRelation<Schema, Model, Field>> = Opposite extends RelationInfo ? (Opposite['fields'] extends string[] ? Opposite['fields'] : []) : [];
|
|
252
|
+
type OppositeRelationAndFK<Schema extends SchemaDef, Model extends GetModels<Schema>, Field extends GetModelFields<Schema, Model>, FT = FieldType<Schema, Model, Field>, Relation = GetModelField<Schema, Model, Field>['relation'], Opposite = Relation extends RelationInfo ? Relation['opposite'] : never> = FT extends GetModels<Schema> ? Opposite extends GetModelFields<Schema, FT> ? Opposite | OppositeRelationFields<Schema, Model, Field>[number] : never : never;
|
|
253
|
+
type FilterArgs<Schema extends SchemaDef, Model extends GetModels<Schema>> = {
|
|
254
|
+
where?: WhereInput<Schema, Model>;
|
|
202
255
|
};
|
|
203
|
-
type
|
|
204
|
-
type OppositeRelation<Schema extends SchemaDef, Model extends GetModels<Schema>, Field extends GetFields<Schema, Model>, FT = FieldType<Schema, Model, Field>> = FT extends GetModels<Schema> ? GetRelation<Schema, Model, Field> extends RelationInfo ? GetRelation<Schema, Model, Field>['opposite'] extends GetFields<Schema, FT> ? Schema['models'][FT]['fields'][GetRelation<Schema, Model, Field>['opposite']]['relation'] : never : never : never;
|
|
205
|
-
type OppositeRelationFields<Schema extends SchemaDef, Model extends GetModels<Schema>, Field extends GetFields<Schema, Model>, Opposite = OppositeRelation<Schema, Model, Field>> = Opposite extends RelationInfo ? Opposite['fields'] extends string[] ? Opposite['fields'] : [] : [];
|
|
206
|
-
type OppositeRelationAndFK<Schema extends SchemaDef, Model extends GetModels<Schema>, Field extends GetFields<Schema, Model>, FT = FieldType<Schema, Model, Field>, Relation = GetField<Schema, Model, Field>['relation'], Opposite = Relation extends RelationInfo ? Relation['opposite'] : never> = FT extends GetModels<Schema> ? Opposite extends GetFields<Schema, FT> ? Opposite | OppositeRelationFields<Schema, Model, Field>[number] : never : never;
|
|
207
|
-
type FindArgs<Schema extends SchemaDef, Model extends GetModels<Schema>, Collection extends boolean, AllowFilter extends boolean = true> = (Collection extends true ? {
|
|
256
|
+
type SortAndTakeArgs<Schema extends SchemaDef, Model extends GetModels<Schema>> = {
|
|
208
257
|
skip?: number;
|
|
209
258
|
take?: number;
|
|
210
259
|
orderBy?: OrArray<OrderBy<Schema, Model, true, false>>;
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
260
|
+
cursor?: WhereUniqueInput<Schema, Model>;
|
|
261
|
+
};
|
|
262
|
+
type FindArgs<Schema extends SchemaDef, Model extends GetModels<Schema>, Collection extends boolean, AllowFilter extends boolean = true> = ProviderSupportsDistinct<Schema> extends true ? (Collection extends true ? SortAndTakeArgs<Schema, Model> & {
|
|
263
|
+
distinct?: OrArray<NonRelationFields<Schema, Model>>;
|
|
264
|
+
} : {}) & (AllowFilter extends true ? FilterArgs<Schema, Model> : {}) & SelectIncludeOmit<Schema, Model, Collection> : (Collection extends true ? SortAndTakeArgs<Schema, Model> : {}) & (AllowFilter extends true ? FilterArgs<Schema, Model> : {}) & SelectIncludeOmit<Schema, Model, Collection>;
|
|
265
|
+
type FindManyArgs<Schema extends SchemaDef, Model extends GetModels<Schema>> = FindArgs<Schema, Model, true>;
|
|
266
|
+
type FindFirstArgs<Schema extends SchemaDef, Model extends GetModels<Schema>> = FindArgs<Schema, Model, false>;
|
|
214
267
|
type FindUniqueArgs<Schema extends SchemaDef, Model extends GetModels<Schema>> = {
|
|
215
268
|
where?: WhereUniqueInput<Schema, Model>;
|
|
216
269
|
} & SelectIncludeOmit<Schema, Model, true>;
|
|
217
270
|
type CreateArgs<Schema extends SchemaDef, Model extends GetModels<Schema>> = {
|
|
218
271
|
data: CreateInput<Schema, Model>;
|
|
219
|
-
select?:
|
|
220
|
-
include?:
|
|
221
|
-
omit?:
|
|
272
|
+
select?: SelectInput<Schema, Model, true>;
|
|
273
|
+
include?: IncludeInput<Schema, Model>;
|
|
274
|
+
omit?: OmitInput<Schema, Model>;
|
|
222
275
|
};
|
|
223
|
-
type CreateManyArgs<Schema extends SchemaDef, Model extends GetModels<Schema>> =
|
|
224
|
-
type CreateManyAndReturnArgs<Schema extends SchemaDef, Model extends GetModels<Schema>> =
|
|
225
|
-
select?:
|
|
226
|
-
omit?:
|
|
276
|
+
type CreateManyArgs<Schema extends SchemaDef, Model extends GetModels<Schema>> = CreateManyInput<Schema, Model>;
|
|
277
|
+
type CreateManyAndReturnArgs<Schema extends SchemaDef, Model extends GetModels<Schema>> = CreateManyInput<Schema, Model> & {
|
|
278
|
+
select?: SelectInput<Schema, Model, false, false>;
|
|
279
|
+
omit?: OmitInput<Schema, Model>;
|
|
227
280
|
};
|
|
228
281
|
type OptionalWrap<Schema extends SchemaDef, Model extends GetModels<Schema>, T extends object> = Optional<T, keyof T & OptionalFieldsForCreate<Schema, Model>>;
|
|
229
282
|
type CreateScalarPayload<Schema extends SchemaDef, Model extends GetModels<Schema>> = OptionalWrap<Schema, Model, {
|
|
230
|
-
[Key in ScalarFields<Schema, Model, false>]: ScalarCreatePayload<Schema, Model, Key>;
|
|
283
|
+
[Key in ScalarFields<Schema, Model, false> as FieldIsDelegateDiscriminator<Schema, Model, Key> extends true ? never : Key]: ScalarCreatePayload<Schema, Model, Key>;
|
|
231
284
|
}>;
|
|
232
|
-
type ScalarCreatePayload<Schema extends SchemaDef, Model extends GetModels<Schema>, Field extends ScalarFields<Schema, Model, false>> =
|
|
233
|
-
set?:
|
|
285
|
+
type ScalarCreatePayload<Schema extends SchemaDef, Model extends GetModels<Schema>, Field extends ScalarFields<Schema, Model, false>> = MapModelFieldType<Schema, Model, Field> | (FieldIsArray<Schema, Model, Field> extends true ? {
|
|
286
|
+
set?: MapModelFieldType<Schema, Model, Field>;
|
|
234
287
|
} : never);
|
|
235
288
|
type CreateFKPayload<Schema extends SchemaDef, Model extends GetModels<Schema>> = OptionalWrap<Schema, Model, {
|
|
236
|
-
[Key in ForeignKeyFields<Schema, Model>]:
|
|
289
|
+
[Key in ForeignKeyFields<Schema, Model>]: MapModelFieldType<Schema, Model, Key>;
|
|
237
290
|
}>;
|
|
238
291
|
type CreateRelationFieldPayload<Schema extends SchemaDef, Model extends GetModels<Schema>, Field extends RelationFields<Schema, Model>> = Omit<{
|
|
292
|
+
connectOrCreate?: ConnectOrCreateInput<Schema, Model, Field>;
|
|
239
293
|
create?: NestedCreateInput<Schema, Model, Field>;
|
|
240
294
|
createMany?: NestedCreateManyInput<Schema, Model, Field>;
|
|
241
295
|
connect?: ConnectInput<Schema, Model, Field>;
|
|
242
|
-
|
|
243
|
-
}, FieldIsArray<Schema, Model, Field> extends true ? never : 'createMany'>;
|
|
296
|
+
}, (FieldIsArray<Schema, Model, Field> extends true ? never : 'createMany') | (FieldIsDelegateRelation<Schema, Model, Field> extends true ? 'create' | 'createMany' | 'connectOrCreate' : never)>;
|
|
244
297
|
type CreateRelationPayload<Schema extends SchemaDef, Model extends GetModels<Schema>> = OptionalWrap<Schema, Model, {
|
|
245
298
|
[Key in RelationFields<Schema, Model>]: CreateRelationFieldPayload<Schema, Model, Key>;
|
|
246
299
|
}>;
|
|
247
|
-
type CreateWithFKInput<Schema extends SchemaDef, Model extends GetModels<Schema>> = CreateScalarPayload<Schema, Model> & CreateFKPayload<Schema, Model>;
|
|
300
|
+
type CreateWithFKInput<Schema extends SchemaDef, Model extends GetModels<Schema>> = CreateScalarPayload<Schema, Model> & CreateFKPayload<Schema, Model> & CreateWithNonOwnedRelationPayload<Schema, Model>;
|
|
248
301
|
type CreateWithRelationInput<Schema extends SchemaDef, Model extends GetModels<Schema>> = CreateScalarPayload<Schema, Model> & CreateRelationPayload<Schema, Model>;
|
|
302
|
+
type CreateWithNonOwnedRelationPayload<Schema extends SchemaDef, Model extends GetModels<Schema>> = OptionalWrap<Schema, Model, {
|
|
303
|
+
[Key in NonOwnedRelationFields<Schema, Model>]: CreateRelationFieldPayload<Schema, Model, Key>;
|
|
304
|
+
}>;
|
|
249
305
|
type ConnectOrCreatePayload<Schema extends SchemaDef, Model extends GetModels<Schema>, Without extends string = never> = {
|
|
250
306
|
where: WhereUniqueInput<Schema, Model>;
|
|
251
307
|
create: CreateInput<Schema, Model, Without>;
|
|
252
308
|
};
|
|
253
|
-
type
|
|
309
|
+
type CreateManyInput<Schema extends SchemaDef, Model extends GetModels<Schema>, Without extends string = never> = {
|
|
254
310
|
data: OrArray<Omit<CreateScalarPayload<Schema, Model>, Without> & Omit<CreateFKPayload<Schema, Model>, Without>>;
|
|
255
311
|
skipDuplicates?: boolean;
|
|
256
312
|
};
|
|
257
313
|
type CreateInput<Schema extends SchemaDef, Model extends GetModels<Schema>, Without extends string = never> = XOR<Omit<CreateWithFKInput<Schema, Model>, Without>, Omit<CreateWithRelationInput<Schema, Model>, Without>>;
|
|
258
314
|
type NestedCreateInput<Schema extends SchemaDef, Model extends GetModels<Schema>, Field extends RelationFields<Schema, Model>> = OrArray<CreateInput<Schema, RelationFieldType<Schema, Model, Field>, OppositeRelationAndFK<Schema, Model, Field>>, FieldIsArray<Schema, Model, Field>>;
|
|
259
|
-
type NestedCreateManyInput<Schema extends SchemaDef, Model extends GetModels<Schema>, Field extends RelationFields<Schema, Model>> =
|
|
315
|
+
type NestedCreateManyInput<Schema extends SchemaDef, Model extends GetModels<Schema>, Field extends RelationFields<Schema, Model>> = CreateManyInput<Schema, RelationFieldType<Schema, Model, Field>, OppositeRelationAndFK<Schema, Model, Field>>;
|
|
260
316
|
type UpdateArgs<Schema extends SchemaDef, Model extends GetModels<Schema>> = {
|
|
261
317
|
data: UpdateInput<Schema, Model>;
|
|
262
318
|
where: WhereUniqueInput<Schema, Model>;
|
|
263
|
-
select?:
|
|
264
|
-
include?:
|
|
265
|
-
omit?:
|
|
319
|
+
select?: SelectInput<Schema, Model, true>;
|
|
320
|
+
include?: IncludeInput<Schema, Model>;
|
|
321
|
+
omit?: OmitInput<Schema, Model>;
|
|
266
322
|
};
|
|
267
323
|
type UpdateManyArgs<Schema extends SchemaDef, Model extends GetModels<Schema>> = UpdateManyPayload<Schema, Model>;
|
|
268
324
|
type UpdateManyAndReturnArgs<Schema extends SchemaDef, Model extends GetModels<Schema>> = UpdateManyPayload<Schema, Model> & {
|
|
269
|
-
select?:
|
|
270
|
-
omit?:
|
|
325
|
+
select?: SelectInput<Schema, Model, false, false>;
|
|
326
|
+
omit?: OmitInput<Schema, Model>;
|
|
271
327
|
};
|
|
272
328
|
type UpdateManyPayload<Schema extends SchemaDef, Model extends GetModels<Schema>, Without extends string = never> = {
|
|
273
329
|
data: OrArray<UpdateScalarInput<Schema, Model, Without>>;
|
|
@@ -278,29 +334,29 @@ type UpsertArgs<Schema extends SchemaDef, Model extends GetModels<Schema>> = {
|
|
|
278
334
|
create: CreateInput<Schema, Model>;
|
|
279
335
|
update: UpdateInput<Schema, Model>;
|
|
280
336
|
where: WhereUniqueInput<Schema, Model>;
|
|
281
|
-
select?:
|
|
282
|
-
include?:
|
|
283
|
-
omit?:
|
|
337
|
+
select?: SelectInput<Schema, Model, true>;
|
|
338
|
+
include?: IncludeInput<Schema, Model>;
|
|
339
|
+
omit?: OmitInput<Schema, Model>;
|
|
284
340
|
};
|
|
285
341
|
type UpdateScalarInput<Schema extends SchemaDef, Model extends GetModels<Schema>, Without extends string = never> = Omit<{
|
|
286
|
-
[Key in NonRelationFields<Schema, Model>]?: ScalarUpdatePayload<Schema, Model, Key>;
|
|
342
|
+
[Key in NonRelationFields<Schema, Model> as FieldIsDelegateDiscriminator<Schema, Model, Key> extends true ? never : Key]?: ScalarUpdatePayload<Schema, Model, Key>;
|
|
287
343
|
}, Without>;
|
|
288
|
-
type ScalarUpdatePayload<Schema extends SchemaDef, Model extends GetModels<Schema>, Field extends NonRelationFields<Schema, Model>> =
|
|
289
|
-
set?: NullableIf<number,
|
|
344
|
+
type ScalarUpdatePayload<Schema extends SchemaDef, Model extends GetModels<Schema>, Field extends NonRelationFields<Schema, Model>> = MapModelFieldType<Schema, Model, Field> | (Field extends NumericFields<Schema, Model> ? {
|
|
345
|
+
set?: NullableIf<number, ModelFieldIsOptional<Schema, Model, Field>>;
|
|
290
346
|
increment?: number;
|
|
291
347
|
decrement?: number;
|
|
292
348
|
multiply?: number;
|
|
293
349
|
divide?: number;
|
|
294
350
|
} : never) | (FieldIsArray<Schema, Model, Field> extends true ? {
|
|
295
|
-
set?:
|
|
296
|
-
push?: OrArray<
|
|
351
|
+
set?: MapModelFieldType<Schema, Model, Field>[];
|
|
352
|
+
push?: OrArray<MapModelFieldType<Schema, Model, Field>, true>;
|
|
297
353
|
} : never);
|
|
298
354
|
type UpdateRelationInput<Schema extends SchemaDef, Model extends GetModels<Schema>, Without extends string = never> = Omit<{
|
|
299
355
|
[Key in RelationFields<Schema, Model>]?: UpdateRelationFieldPayload<Schema, Model, Key>;
|
|
300
356
|
}, Without>;
|
|
301
357
|
type UpdateInput<Schema extends SchemaDef, Model extends GetModels<Schema>, Without extends string = never> = UpdateScalarInput<Schema, Model, Without> & UpdateRelationInput<Schema, Model, Without>;
|
|
302
358
|
type UpdateRelationFieldPayload<Schema extends SchemaDef, Model extends GetModels<Schema>, Field extends RelationFields<Schema, Model>> = FieldIsArray<Schema, Model, Field> extends true ? ToManyRelationUpdateInput<Schema, Model, Field> : ToOneRelationUpdateInput<Schema, Model, Field>;
|
|
303
|
-
type ToManyRelationUpdateInput<Schema extends SchemaDef, Model extends GetModels<Schema>, Field extends RelationFields<Schema, Model>> = {
|
|
359
|
+
type ToManyRelationUpdateInput<Schema extends SchemaDef, Model extends GetModels<Schema>, Field extends RelationFields<Schema, Model>> = Omit<{
|
|
304
360
|
create?: NestedCreateInput<Schema, Model, Field>;
|
|
305
361
|
createMany?: NestedCreateManyInput<Schema, Model, Field>;
|
|
306
362
|
connect?: ConnectInput<Schema, Model, Field>;
|
|
@@ -312,22 +368,22 @@ type ToManyRelationUpdateInput<Schema extends SchemaDef, Model extends GetModels
|
|
|
312
368
|
delete?: NestedDeleteInput<Schema, Model, Field>;
|
|
313
369
|
deleteMany?: NestedDeleteManyInput<Schema, Model, Field>;
|
|
314
370
|
set?: SetRelationInput<Schema, Model, Field>;
|
|
315
|
-
}
|
|
316
|
-
type ToOneRelationUpdateInput<Schema extends SchemaDef, Model extends GetModels<Schema>, Field extends RelationFields<Schema, Model>> = {
|
|
371
|
+
}, FieldIsDelegateRelation<Schema, Model, Field> extends true ? 'create' | 'createMany' | 'connectOrCreate' | 'upsert' : never>;
|
|
372
|
+
type ToOneRelationUpdateInput<Schema extends SchemaDef, Model extends GetModels<Schema>, Field extends RelationFields<Schema, Model>> = Omit<{
|
|
317
373
|
create?: NestedCreateInput<Schema, Model, Field>;
|
|
318
374
|
connect?: ConnectInput<Schema, Model, Field>;
|
|
319
375
|
connectOrCreate?: ConnectOrCreateInput<Schema, Model, Field>;
|
|
320
376
|
update?: NestedUpdateInput<Schema, Model, Field>;
|
|
321
377
|
upsert?: NestedUpsertInput<Schema, Model, Field>;
|
|
322
|
-
} & (
|
|
378
|
+
} & (ModelFieldIsOptional<Schema, Model, Field> extends true ? {
|
|
323
379
|
disconnect?: DisconnectInput<Schema, Model, Field>;
|
|
324
380
|
delete?: NestedDeleteInput<Schema, Model, Field>;
|
|
325
|
-
} : {})
|
|
381
|
+
} : {}), FieldIsDelegateRelation<Schema, Model, Field> extends true ? 'create' | 'connectOrCreate' | 'upsert' : never>;
|
|
326
382
|
type DeleteArgs<Schema extends SchemaDef, Model extends GetModels<Schema>> = {
|
|
327
383
|
where: WhereUniqueInput<Schema, Model>;
|
|
328
|
-
select?:
|
|
329
|
-
include?:
|
|
330
|
-
omit?:
|
|
384
|
+
select?: SelectInput<Schema, Model, true>;
|
|
385
|
+
include?: IncludeInput<Schema, Model>;
|
|
386
|
+
omit?: OmitInput<Schema, Model>;
|
|
331
387
|
};
|
|
332
388
|
type DeleteManyArgs<Schema extends SchemaDef, Model extends GetModels<Schema>> = {
|
|
333
389
|
where?: WhereInput<Schema, Model>;
|
|
@@ -353,20 +409,20 @@ type AggregateArgs<Schema extends SchemaDef, Model extends GetModels<Schema>> =
|
|
|
353
409
|
orderBy?: OrArray<OrderBy<Schema, Model, true, false>>;
|
|
354
410
|
} & {
|
|
355
411
|
_count?: true | CountAggregateInput<Schema, Model>;
|
|
412
|
+
_min?: MinMaxInput<Schema, Model, true>;
|
|
413
|
+
_max?: MinMaxInput<Schema, Model, true>;
|
|
356
414
|
} & (NumericFields<Schema, Model> extends never ? {} : {
|
|
357
|
-
_avg?: SumAvgInput<Schema, Model>;
|
|
358
|
-
_sum?: SumAvgInput<Schema, Model>;
|
|
359
|
-
_min?: MinMaxInput<Schema, Model>;
|
|
360
|
-
_max?: MinMaxInput<Schema, Model>;
|
|
415
|
+
_avg?: SumAvgInput<Schema, Model, true>;
|
|
416
|
+
_sum?: SumAvgInput<Schema, Model, true>;
|
|
361
417
|
});
|
|
362
418
|
type NumericFields<Schema extends SchemaDef, Model extends GetModels<Schema>> = keyof {
|
|
363
|
-
[Key in
|
|
419
|
+
[Key in GetModelFields<Schema, Model> as GetModelFieldType<Schema, Model, Key> extends 'Int' | 'Float' | 'BigInt' | 'Decimal' ? FieldIsArray<Schema, Model, Key> extends true ? never : Key : never]: GetModelField<Schema, Model, Key>;
|
|
364
420
|
};
|
|
365
|
-
type SumAvgInput<Schema extends SchemaDef, Model extends GetModels<Schema
|
|
366
|
-
[Key in NumericFields<Schema, Model>]?:
|
|
421
|
+
type SumAvgInput<Schema extends SchemaDef, Model extends GetModels<Schema>, ValueType> = {
|
|
422
|
+
[Key in NumericFields<Schema, Model>]?: ValueType;
|
|
367
423
|
};
|
|
368
|
-
type MinMaxInput<Schema extends SchemaDef, Model extends GetModels<Schema
|
|
369
|
-
[Key in
|
|
424
|
+
type MinMaxInput<Schema extends SchemaDef, Model extends GetModels<Schema>, ValueType> = {
|
|
425
|
+
[Key in GetModelFields<Schema, Model> as FieldIsArray<Schema, Model, Key> extends true ? never : FieldIsRelation<Schema, Model, Key> extends true ? never : Key]?: ValueType;
|
|
370
426
|
};
|
|
371
427
|
type AggregateResult<Schema extends SchemaDef, Model extends GetModels<Schema>, Args extends AggregateArgs<Schema, Model>> = (Args extends {
|
|
372
428
|
_count: infer Count;
|
|
@@ -392,22 +448,23 @@ type AggregateResult<Schema extends SchemaDef, Model extends GetModels<Schema>,
|
|
|
392
448
|
type AggCommonOutput<Input> = Input extends true ? number : Input extends {} ? {
|
|
393
449
|
[Key in keyof Input]: number;
|
|
394
450
|
} : never;
|
|
451
|
+
type GroupByHaving<Schema extends SchemaDef, Model extends GetModels<Schema>> = Omit<WhereInput<Schema, Model, true, true>, '$expr'>;
|
|
395
452
|
type GroupByArgs<Schema extends SchemaDef, Model extends GetModels<Schema>> = {
|
|
396
453
|
where?: WhereInput<Schema, Model>;
|
|
397
454
|
orderBy?: OrArray<OrderBy<Schema, Model, false, true>>;
|
|
398
455
|
by: NonRelationFields<Schema, Model> | NonEmptyArray<NonRelationFields<Schema, Model>>;
|
|
399
|
-
having?:
|
|
456
|
+
having?: GroupByHaving<Schema, Model>;
|
|
400
457
|
take?: number;
|
|
401
458
|
skip?: number;
|
|
402
459
|
_count?: true | CountAggregateInput<Schema, Model>;
|
|
460
|
+
_min?: MinMaxInput<Schema, Model, true>;
|
|
461
|
+
_max?: MinMaxInput<Schema, Model, true>;
|
|
403
462
|
} & (NumericFields<Schema, Model> extends never ? {} : {
|
|
404
|
-
_avg?: SumAvgInput<Schema, Model>;
|
|
405
|
-
_sum?: SumAvgInput<Schema, Model>;
|
|
406
|
-
_min?: MinMaxInput<Schema, Model>;
|
|
407
|
-
_max?: MinMaxInput<Schema, Model>;
|
|
463
|
+
_avg?: SumAvgInput<Schema, Model, true>;
|
|
464
|
+
_sum?: SumAvgInput<Schema, Model, true>;
|
|
408
465
|
});
|
|
409
466
|
type GroupByResult<Schema extends SchemaDef, Model extends GetModels<Schema>, Args extends GroupByArgs<Schema, Model>> = Array<{
|
|
410
|
-
[Key in NonRelationFields<Schema, Model> as Key extends ValueOfPotentialTuple<Args['by']> ? Key : never]:
|
|
467
|
+
[Key in NonRelationFields<Schema, Model> as Key extends ValueOfPotentialTuple<Args['by']> ? Key : never]: MapModelFieldType<Schema, Model, Key>;
|
|
411
468
|
} & (Args extends {
|
|
412
469
|
_count: infer Count;
|
|
413
470
|
} ? {
|
|
@@ -441,598 +498,212 @@ type NestedUpdateInput<Schema extends SchemaDef, Model extends GetModels<Schema>
|
|
|
441
498
|
data: UpdateInput<Schema, RelationFieldType<Schema, Model, Field>, OppositeRelationAndFK<Schema, Model, Field>>;
|
|
442
499
|
}, UpdateInput<Schema, RelationFieldType<Schema, Model, Field>, OppositeRelationAndFK<Schema, Model, Field>>>;
|
|
443
500
|
type NestedUpsertInput<Schema extends SchemaDef, Model extends GetModels<Schema>, Field extends RelationFields<Schema, Model>> = OrArray<{
|
|
444
|
-
where: WhereUniqueInput<Schema, Model
|
|
501
|
+
where: WhereUniqueInput<Schema, RelationFieldType<Schema, Model, Field>>;
|
|
445
502
|
create: CreateInput<Schema, RelationFieldType<Schema, Model, Field>, OppositeRelationAndFK<Schema, Model, Field>>;
|
|
446
503
|
update: UpdateInput<Schema, RelationFieldType<Schema, Model, Field>, OppositeRelationAndFK<Schema, Model, Field>>;
|
|
447
504
|
}, FieldIsArray<Schema, Model, Field>>;
|
|
448
505
|
type NestedUpdateManyInput<Schema extends SchemaDef, Model extends GetModels<Schema>, Field extends RelationFields<Schema, Model>> = OrArray<UpdateManyPayload<Schema, RelationFieldType<Schema, Model, Field>, OppositeRelationAndFK<Schema, Model, Field>>>;
|
|
449
506
|
type NestedDeleteInput<Schema extends SchemaDef, Model extends GetModels<Schema>, Field extends RelationFields<Schema, Model>> = FieldIsArray<Schema, Model, Field> extends true ? OrArray<WhereUniqueInput<Schema, RelationFieldType<Schema, Model, Field>>, true> : boolean | WhereInput<Schema, RelationFieldType<Schema, Model, Field>>;
|
|
450
507
|
type NestedDeleteManyInput<Schema extends SchemaDef, Model extends GetModels<Schema>, Field extends RelationFields<Schema, Model>> = OrArray<WhereInput<Schema, RelationFieldType<Schema, Model, Field>, true>>;
|
|
451
|
-
|
|
508
|
+
type NonOwnedRelationFields<Schema extends SchemaDef, Model extends GetModels<Schema>> = keyof {
|
|
509
|
+
[Key in RelationFields<Schema, Model> as GetModelField<Schema, Model, Key>['relation'] extends {
|
|
510
|
+
references: unknown[];
|
|
511
|
+
} ? never : Key]: true;
|
|
512
|
+
};
|
|
513
|
+
type HasToManyRelations<Schema extends SchemaDef, Model extends GetModels<Schema>> = keyof {
|
|
514
|
+
[Key in RelationFields<Schema, Model> as FieldIsArray<Schema, Model, Key> extends true ? Key : never]: true;
|
|
515
|
+
} extends never ? false : true;
|
|
516
|
+
type ProviderSupportsCaseSensitivity<Schema extends SchemaDef> = Schema['provider']['type'] extends 'postgresql' ? true : false;
|
|
517
|
+
type ProviderSupportsDistinct<Schema extends SchemaDef> = Schema['provider']['type'] extends 'postgresql' ? true : false;
|
|
518
|
+
|
|
519
|
+
type AuthType<Schema extends SchemaDef> = string extends GetModels<Schema> ? Record<string, unknown> : Schema['authType'] extends GetModels<Schema> ? Partial<ModelResult<Schema, Schema['authType']>> : never;
|
|
520
|
+
|
|
521
|
+
/**
|
|
522
|
+
* Client API methods that are not supported in transactions.
|
|
523
|
+
*/
|
|
524
|
+
declare const TRANSACTION_UNSUPPORTED_METHODS: readonly ["$transaction", "$disconnect", "$use"];
|
|
525
|
+
/**
|
|
526
|
+
* Logical combinators used in filters.
|
|
527
|
+
*/
|
|
528
|
+
declare const LOGICAL_COMBINATORS: readonly ["AND", "OR", "NOT"];
|
|
529
|
+
|
|
530
|
+
declare abstract class BaseCrudDialect<Schema extends SchemaDef> {
|
|
531
|
+
protected readonly schema: Schema;
|
|
532
|
+
protected readonly options: ClientOptions<Schema>;
|
|
533
|
+
constructor(schema: Schema, options: ClientOptions<Schema>);
|
|
534
|
+
transformPrimitive(value: unknown, _type: BuiltinType, _forArrayField: boolean): unknown;
|
|
535
|
+
buildSelectModel(eb: ExpressionBuilder<any, any>, model: string, modelAlias: string): SelectQueryBuilder<any, any, {}>;
|
|
536
|
+
buildFilterSortTake(model: GetModels<Schema>, args: FindArgs<Schema, GetModels<Schema>, true>, query: SelectQueryBuilder<any, any, {}>, modelAlias: string): SelectQueryBuilder<any, any, {}>;
|
|
537
|
+
buildFilter(eb: ExpressionBuilder<any, any>, model: string, modelAlias: string, where: boolean | object | undefined): Expression<SqlBool>;
|
|
538
|
+
private buildCursorFilter;
|
|
539
|
+
private isLogicalCombinator;
|
|
540
|
+
protected buildCompositeFilter(eb: ExpressionBuilder<any, any>, model: string, modelAlias: string, key: (typeof LOGICAL_COMBINATORS)[number], payload: any): Expression<SqlBool>;
|
|
541
|
+
private buildRelationFilter;
|
|
542
|
+
private buildToOneRelationFilter;
|
|
543
|
+
private buildToManyRelationFilter;
|
|
544
|
+
private buildArrayFilter;
|
|
545
|
+
buildPrimitiveFilter(eb: ExpressionBuilder<any, any>, fieldRef: Expression<any>, fieldDef: FieldDef, payload: any): Expression<SqlBool>;
|
|
546
|
+
private buildLiteralFilter;
|
|
547
|
+
private buildStandardFilter;
|
|
548
|
+
private buildStringFilter;
|
|
549
|
+
private prepStringCasing;
|
|
550
|
+
private buildNumberFilter;
|
|
551
|
+
private buildBooleanFilter;
|
|
552
|
+
private buildDateTimeFilter;
|
|
553
|
+
private buildBytesFilter;
|
|
554
|
+
private buildEnumFilter;
|
|
555
|
+
buildOrderBy(query: SelectQueryBuilder<any, any, any>, model: string, modelAlias: string, orderBy: OrArray<OrderBy<Schema, GetModels<Schema>, boolean, boolean>> | undefined, useDefaultIfEmpty: boolean, negated: boolean): SelectQueryBuilder<any, any, any>;
|
|
556
|
+
buildSelectAllFields(model: string, query: SelectQueryBuilder<any, any, any>, omit: Record<string, boolean | undefined> | undefined, modelAlias: string): SelectQueryBuilder<any, any, any>;
|
|
557
|
+
buildSelectField(query: SelectQueryBuilder<any, any, any>, model: string, modelAlias: string, field: string): SelectQueryBuilder<any, any, any>;
|
|
558
|
+
buildDelegateJoin(thisModel: string, thisModelAlias: string, otherModelAlias: string, query: SelectQueryBuilder<any, any, any>): SelectQueryBuilder<any, any, any>;
|
|
559
|
+
buildCountJson(model: string, eb: ExpressionBuilder<any, any>, parentAlias: string, payload: any): ExpressionWrapper<any, any, unknown>;
|
|
560
|
+
private negateSort;
|
|
561
|
+
true(eb: ExpressionBuilder<any, any>): Expression<SqlBool>;
|
|
562
|
+
false(eb: ExpressionBuilder<any, any>): Expression<SqlBool>;
|
|
563
|
+
isTrue(expression: Expression<SqlBool>): boolean;
|
|
564
|
+
isFalse(expression: Expression<SqlBool>): boolean;
|
|
565
|
+
protected and(eb: ExpressionBuilder<any, any>, ...args: Expression<SqlBool>[]): Expression<SqlBool>;
|
|
566
|
+
protected or(eb: ExpressionBuilder<any, any>, ...args: Expression<SqlBool>[]): Expression<SqlBool>;
|
|
567
|
+
protected not(eb: ExpressionBuilder<any, any>, ...args: Expression<SqlBool>[]): ExpressionWrapper<any, any, SqlBool>;
|
|
568
|
+
fieldRef(model: string, field: string, eb: ExpressionBuilder<any, any>, modelAlias?: string, inlineComputedField?: boolean): ExpressionWrapper<any, any, unknown>;
|
|
569
|
+
abstract get provider(): DataSourceProviderType;
|
|
452
570
|
/**
|
|
453
|
-
*
|
|
454
|
-
* @param args - query args
|
|
455
|
-
* @returns a list of entities
|
|
456
|
-
*
|
|
457
|
-
* @example
|
|
458
|
-
* ```ts
|
|
459
|
-
* // find all users and return all scalar fields
|
|
460
|
-
* await client.user.findMany();
|
|
461
|
-
*
|
|
462
|
-
* // find all users with name 'Alex'
|
|
463
|
-
* await client.user.findMany({
|
|
464
|
-
* where: {
|
|
465
|
-
* name: 'Alex'
|
|
466
|
-
* }
|
|
467
|
-
* });
|
|
468
|
-
*
|
|
469
|
-
* // select fields
|
|
470
|
-
* await client.user.findMany({
|
|
471
|
-
* select: {
|
|
472
|
-
* name: true,
|
|
473
|
-
* email: true,
|
|
474
|
-
* }
|
|
475
|
-
* }); // result: `Array<{ name: string, email: string }>`
|
|
476
|
-
*
|
|
477
|
-
* // omit fields
|
|
478
|
-
* await client.user.findMany({
|
|
479
|
-
* omit: {
|
|
480
|
-
* name: true,
|
|
481
|
-
* }
|
|
482
|
-
* }); // result: `Array<{ id: number; email: string; ... }>`
|
|
483
|
-
*
|
|
484
|
-
* // include relations (and all scalar fields)
|
|
485
|
-
* await client.user.findMany({
|
|
486
|
-
* include: {
|
|
487
|
-
* posts: true,
|
|
488
|
-
* }
|
|
489
|
-
* }); // result: `Array<{ ...; posts: Post[] }>`
|
|
490
|
-
*
|
|
491
|
-
* // include relations with filter
|
|
492
|
-
* await client.user.findMany({
|
|
493
|
-
* include: {
|
|
494
|
-
* posts: {
|
|
495
|
-
* where: {
|
|
496
|
-
* published: true
|
|
497
|
-
* }
|
|
498
|
-
* }
|
|
499
|
-
* }
|
|
500
|
-
* });
|
|
501
|
-
*
|
|
502
|
-
* // pagination and sorting
|
|
503
|
-
* await client.user.findMany({
|
|
504
|
-
* skip: 10,
|
|
505
|
-
* take: 10,
|
|
506
|
-
* orderBy: [{ name: 'asc' }, { email: 'desc' }],
|
|
507
|
-
* });
|
|
508
|
-
*
|
|
509
|
-
* // pagination with cursor (https://www.prisma.io/docs/orm/prisma-client/queries/pagination#cursor-based-pagination)
|
|
510
|
-
* await client.user.findMany({
|
|
511
|
-
* cursor: { id: 10 },
|
|
512
|
-
* skip: 1,
|
|
513
|
-
* take: 10,
|
|
514
|
-
* orderBy: { id: 'asc' },
|
|
515
|
-
* });
|
|
516
|
-
*
|
|
517
|
-
* // distinct
|
|
518
|
-
* await client.user.findMany({
|
|
519
|
-
* distinct: ['name']
|
|
520
|
-
* });
|
|
521
|
-
*
|
|
522
|
-
* // count all relations
|
|
523
|
-
* await client.user.findMany({
|
|
524
|
-
* _count: true,
|
|
525
|
-
* }); // result: `{ _count: { posts: number; ... } }`
|
|
526
|
-
*
|
|
527
|
-
* // count selected relations
|
|
528
|
-
* await client.user.findMany({
|
|
529
|
-
* _count: { select: { posts: true } },
|
|
530
|
-
* }); // result: `{ _count: { posts: number } }`
|
|
531
|
-
* ```
|
|
571
|
+
* Builds selection for a relation field.
|
|
532
572
|
*/
|
|
533
|
-
|
|
573
|
+
abstract buildRelationSelection(query: SelectQueryBuilder<any, any, any>, model: string, relationField: string, parentAlias: string, payload: true | FindArgs<Schema, GetModels<Schema>, true>): SelectQueryBuilder<any, any, any>;
|
|
534
574
|
/**
|
|
535
|
-
*
|
|
536
|
-
* @param args - query args
|
|
537
|
-
* @returns a single entity or null if not found
|
|
538
|
-
* @see {@link findMany}
|
|
575
|
+
* Builds skip and take clauses.
|
|
539
576
|
*/
|
|
540
|
-
|
|
577
|
+
abstract buildSkipTake(query: SelectQueryBuilder<any, any, any>, skip: number | undefined, take: number | undefined): SelectQueryBuilder<any, any, any>;
|
|
541
578
|
/**
|
|
542
|
-
*
|
|
543
|
-
* @param args - query args
|
|
544
|
-
* @returns a single entity
|
|
545
|
-
* @see {@link findMany}
|
|
579
|
+
* Builds an Kysely expression that returns a JSON object for the given key-value pairs.
|
|
546
580
|
*/
|
|
547
|
-
|
|
581
|
+
abstract buildJsonObject(eb: ExpressionBuilder<any, any>, value: Record<string, Expression<unknown>>): ExpressionWrapper<any, any, unknown>;
|
|
548
582
|
/**
|
|
549
|
-
*
|
|
550
|
-
* @param args - query args
|
|
551
|
-
* @returns a single entity or null if not found
|
|
552
|
-
* @see {@link findMany}
|
|
583
|
+
* Builds an Kysely expression that returns the length of an array.
|
|
553
584
|
*/
|
|
554
|
-
|
|
585
|
+
abstract buildArrayLength(eb: ExpressionBuilder<any, any>, array: Expression<unknown>): ExpressionWrapper<any, any, number>;
|
|
555
586
|
/**
|
|
556
|
-
*
|
|
557
|
-
* @param args - query args
|
|
558
|
-
* @returns a single entity
|
|
559
|
-
* @see {@link findMany}
|
|
587
|
+
* Builds an array literal SQL string for the given values.
|
|
560
588
|
*/
|
|
561
|
-
|
|
589
|
+
abstract buildArrayLiteralSQL(values: unknown[]): string;
|
|
562
590
|
/**
|
|
563
|
-
*
|
|
564
|
-
* @param args - create args
|
|
565
|
-
* @returns the created entity
|
|
566
|
-
*
|
|
567
|
-
* @example
|
|
568
|
-
* ```ts
|
|
569
|
-
* // simple create
|
|
570
|
-
* await client.user.create({
|
|
571
|
-
* data: { name: 'Alex', email: 'alex@zenstack.dev' }
|
|
572
|
-
* });
|
|
573
|
-
*
|
|
574
|
-
* // nested create with relation
|
|
575
|
-
* await client.user.create({
|
|
576
|
-
* data: {
|
|
577
|
-
* email: 'alex@zenstack.dev',
|
|
578
|
-
* posts: { create: { title: 'Hello World' } }
|
|
579
|
-
* }
|
|
580
|
-
* });
|
|
581
|
-
*
|
|
582
|
-
* // you can use `select`, `omit`, and `include` to control
|
|
583
|
-
* // the fields returned by the query, as with `findMany`
|
|
584
|
-
* await client.user.create({
|
|
585
|
-
* data: {
|
|
586
|
-
* email: 'alex@zenstack.dev',
|
|
587
|
-
* posts: { create: { title: 'Hello World' } }
|
|
588
|
-
* },
|
|
589
|
-
* include: { posts: true }
|
|
590
|
-
* }); // result: `{ id: number; posts: Post[] }`
|
|
591
|
-
*
|
|
592
|
-
* // connect relations
|
|
593
|
-
* await client.user.create({
|
|
594
|
-
* data: {
|
|
595
|
-
* email: 'alex@zenstack.dev',
|
|
596
|
-
* posts: { connect: { id: 1 } }
|
|
597
|
-
* }
|
|
598
|
-
* });
|
|
599
|
-
*
|
|
600
|
-
* // connect relations, and create if not found
|
|
601
|
-
* await client.user.create({
|
|
602
|
-
* data: {
|
|
603
|
-
* email: 'alex@zenstack.dev',
|
|
604
|
-
* posts: {
|
|
605
|
-
* connectOrCreate: {
|
|
606
|
-
* where: { id: 1 },
|
|
607
|
-
* create: { title: 'Hello World' }
|
|
608
|
-
* }
|
|
609
|
-
* }
|
|
610
|
-
* }
|
|
611
|
-
* });
|
|
612
|
-
* ```
|
|
591
|
+
* Whether the dialect supports updating with a limit on the number of updated rows.
|
|
613
592
|
*/
|
|
614
|
-
|
|
593
|
+
abstract get supportsUpdateWithLimit(): boolean;
|
|
615
594
|
/**
|
|
616
|
-
*
|
|
617
|
-
* @param args - create args
|
|
618
|
-
* @returns count of created entities: `{ count: number }`
|
|
619
|
-
*
|
|
620
|
-
* @example
|
|
621
|
-
* ```ts
|
|
622
|
-
* // create multiple entities
|
|
623
|
-
* await client.user.createMany({
|
|
624
|
-
* data: [
|
|
625
|
-
* { name: 'Alex', email: 'alex@zenstack.dev' },
|
|
626
|
-
* { name: 'John', email: 'john@zenstack.dev' }
|
|
627
|
-
* ]
|
|
628
|
-
* });
|
|
629
|
-
*
|
|
630
|
-
* // skip items that cause unique constraint violation
|
|
631
|
-
* await client.user.createMany({
|
|
632
|
-
* data: [
|
|
633
|
-
* { name: 'Alex', email: 'alex@zenstack.dev' },
|
|
634
|
-
* { name: 'John', email: 'john@zenstack.dev' }
|
|
635
|
-
* ],
|
|
636
|
-
* skipDuplicates: true
|
|
637
|
-
* });
|
|
638
|
-
* ```
|
|
595
|
+
* Whether the dialect supports deleting with a limit on the number of deleted rows.
|
|
639
596
|
*/
|
|
640
|
-
|
|
597
|
+
abstract get supportsDeleteWithLimit(): boolean;
|
|
641
598
|
/**
|
|
642
|
-
*
|
|
643
|
-
* @param args - create args. See {@link createMany} for input. Use
|
|
644
|
-
* `select` and `omit` to control the fields returned.
|
|
645
|
-
* @returns the created entities
|
|
646
|
-
*
|
|
647
|
-
* @example
|
|
648
|
-
* ```ts
|
|
649
|
-
* // create multiple entities and return selected fields
|
|
650
|
-
* await client.user.createManyAndReturn({
|
|
651
|
-
* data: [
|
|
652
|
-
* { name: 'Alex', email: 'alex@zenstack.dev' },
|
|
653
|
-
* { name: 'John', email: 'john@zenstack.dev' }
|
|
654
|
-
* ],
|
|
655
|
-
* select: { id: true, email: true }
|
|
656
|
-
* });
|
|
657
|
-
* ```
|
|
599
|
+
* Whether the dialect supports DISTINCT ON.
|
|
658
600
|
*/
|
|
659
|
-
|
|
601
|
+
abstract get supportsDistinctOn(): boolean;
|
|
660
602
|
/**
|
|
661
|
-
*
|
|
662
|
-
* @param args - update args. See {@link findMany} for how to control
|
|
663
|
-
* fields and relations returned.
|
|
664
|
-
* @returns the updated entity. Throws `NotFoundError` if the entity is not found.
|
|
665
|
-
*
|
|
666
|
-
* @example
|
|
667
|
-
* ```ts
|
|
668
|
-
* // update fields
|
|
669
|
-
* await client.user.update({
|
|
670
|
-
* where: { id: 1 },
|
|
671
|
-
* data: { name: 'Alex' }
|
|
672
|
-
* });
|
|
673
|
-
*
|
|
674
|
-
* // connect a relation
|
|
675
|
-
* await client.user.update({
|
|
676
|
-
* where: { id: 1 },
|
|
677
|
-
* data: { posts: { connect: { id: 1 } } }
|
|
678
|
-
* });
|
|
679
|
-
*
|
|
680
|
-
* // connect relation, and create if not found
|
|
681
|
-
* await client.user.update({
|
|
682
|
-
* where: { id: 1 },
|
|
683
|
-
* data: {
|
|
684
|
-
* posts: {
|
|
685
|
-
* connectOrCreate: {
|
|
686
|
-
* where: { id: 1 },
|
|
687
|
-
* create: { title: 'Hello World' }
|
|
688
|
-
* }
|
|
689
|
-
* }
|
|
690
|
-
* }
|
|
691
|
-
* });
|
|
692
|
-
*
|
|
693
|
-
* // create many related entities (only available for one-to-many relations)
|
|
694
|
-
* await client.user.update({
|
|
695
|
-
* where: { id: 1 },
|
|
696
|
-
* data: {
|
|
697
|
-
* posts: {
|
|
698
|
-
* createMany: {
|
|
699
|
-
* data: [{ title: 'Hello World' }, { title: 'Hello World 2' }],
|
|
700
|
-
* }
|
|
701
|
-
* }
|
|
702
|
-
* }
|
|
703
|
-
* });
|
|
704
|
-
*
|
|
705
|
-
* // disconnect a one-to-many relation
|
|
706
|
-
* await client.user.update({
|
|
707
|
-
* where: { id: 1 },
|
|
708
|
-
* data: { posts: { disconnect: { id: 1 } } }
|
|
709
|
-
* });
|
|
710
|
-
*
|
|
711
|
-
* // disconnect a one-to-one relation
|
|
712
|
-
* await client.user.update({
|
|
713
|
-
* where: { id: 1 },
|
|
714
|
-
* data: { profile: { disconnect: true } }
|
|
715
|
-
* });
|
|
716
|
-
*
|
|
717
|
-
* // replace a relation (only available for one-to-many relations)
|
|
718
|
-
* await client.user.update({
|
|
719
|
-
* where: { id: 1 },
|
|
720
|
-
* data: {
|
|
721
|
-
* posts: {
|
|
722
|
-
* set: [{ id: 1 }, { id: 2 }]
|
|
723
|
-
* }
|
|
724
|
-
* }
|
|
725
|
-
* });
|
|
726
|
-
*
|
|
727
|
-
* // update a relation
|
|
728
|
-
* await client.user.update({
|
|
729
|
-
* where: { id: 1 },
|
|
730
|
-
* data: {
|
|
731
|
-
* posts: {
|
|
732
|
-
* update: { where: { id: 1 }, data: { title: 'Hello World' } }
|
|
733
|
-
* }
|
|
734
|
-
* }
|
|
735
|
-
* });
|
|
736
|
-
*
|
|
737
|
-
* // upsert a relation
|
|
738
|
-
* await client.user.update({
|
|
739
|
-
* where: { id: 1 },
|
|
740
|
-
* data: {
|
|
741
|
-
* posts: {
|
|
742
|
-
* upsert: {
|
|
743
|
-
* where: { id: 1 },
|
|
744
|
-
* create: { title: 'Hello World' },
|
|
745
|
-
* update: { title: 'Hello World' }
|
|
746
|
-
* }
|
|
747
|
-
* }
|
|
748
|
-
* }
|
|
749
|
-
* });
|
|
750
|
-
*
|
|
751
|
-
* // update many related entities (only available for one-to-many relations)
|
|
752
|
-
* await client.user.update({
|
|
753
|
-
* where: { id: 1 },
|
|
754
|
-
* data: {
|
|
755
|
-
* posts: {
|
|
756
|
-
* updateMany: {
|
|
757
|
-
* where: { published: true },
|
|
758
|
-
* data: { title: 'Hello World' }
|
|
759
|
-
* }
|
|
760
|
-
* }
|
|
761
|
-
* }
|
|
762
|
-
* });
|
|
763
|
-
*
|
|
764
|
-
* // delete a one-to-many relation
|
|
765
|
-
* await client.user.update({
|
|
766
|
-
* where: { id: 1 },
|
|
767
|
-
* data: { posts: { delete: { id: 1 } } }
|
|
768
|
-
* });
|
|
769
|
-
*
|
|
770
|
-
* // delete a one-to-one relation
|
|
771
|
-
* await client.user.update({
|
|
772
|
-
* where: { id: 1 },
|
|
773
|
-
* data: { profile: { delete: true } }
|
|
774
|
-
* });
|
|
775
|
-
* ```
|
|
776
|
-
*/
|
|
777
|
-
update<T extends UpdateArgs<Schema, Model>>(args: SelectSubset<T, UpdateArgs<Schema, Model>>): Promise<ModelResult<Schema, Model, T>>;
|
|
778
|
-
/**
|
|
779
|
-
* Updates multiple entities.
|
|
780
|
-
* @param args - update args. Only scalar fields are allowed for data.
|
|
781
|
-
* @returns count of updated entities: `{ count: number }`
|
|
782
|
-
*
|
|
783
|
-
* @example
|
|
784
|
-
* ```ts
|
|
785
|
-
* // update many entities
|
|
786
|
-
* await client.user.updateMany({
|
|
787
|
-
* where: { email: { endsWith: '@zenstack.dev' } },
|
|
788
|
-
* data: { role: 'ADMIN' }
|
|
789
|
-
* });
|
|
790
|
-
*
|
|
791
|
-
* // limit the number of updated entities
|
|
792
|
-
* await client.user.updateMany({
|
|
793
|
-
* where: { email: { endsWith: '@zenstack.dev' } },
|
|
794
|
-
* data: { role: 'ADMIN' },
|
|
795
|
-
* limit: 10
|
|
796
|
-
* });
|
|
797
|
-
*/
|
|
798
|
-
updateMany<T extends UpdateManyArgs<Schema, Model>>(args: Subset<T, UpdateManyArgs<Schema, Model>>): Promise<BatchResult>;
|
|
799
|
-
/**
|
|
800
|
-
* Updates multiple entities and returns them.
|
|
801
|
-
* @param args - update args. Only scalar fields are allowed for data.
|
|
802
|
-
* @returns the updated entities
|
|
803
|
-
*
|
|
804
|
-
* @example
|
|
805
|
-
* ```ts
|
|
806
|
-
* // update many entities and return selected fields
|
|
807
|
-
* await client.user.updateManyAndReturn({
|
|
808
|
-
* where: { email: { endsWith: '@zenstack.dev' } },
|
|
809
|
-
* data: { role: 'ADMIN' },
|
|
810
|
-
* select: { id: true, email: true }
|
|
811
|
-
* }); // result: `Array<{ id: string; email: string }>`
|
|
812
|
-
*
|
|
813
|
-
* // limit the number of updated entities
|
|
814
|
-
* await client.user.updateManyAndReturn({
|
|
815
|
-
* where: { email: { endsWith: '@zenstack.dev' } },
|
|
816
|
-
* data: { role: 'ADMIN' },
|
|
817
|
-
* limit: 10
|
|
818
|
-
* });
|
|
819
|
-
* ```
|
|
603
|
+
* Whether the dialect support inserting with `DEFAULT` as field value.
|
|
820
604
|
*/
|
|
821
|
-
|
|
605
|
+
abstract get supportInsertWithDefault(): boolean;
|
|
606
|
+
}
|
|
607
|
+
|
|
608
|
+
type CoreCrudOperation = 'findMany' | 'findUnique' | 'findFirst' | 'create' | 'createMany' | 'createManyAndReturn' | 'update' | 'updateMany' | 'updateManyAndReturn' | 'upsert' | 'delete' | 'deleteMany' | 'count' | 'aggregate' | 'groupBy';
|
|
609
|
+
type AllCrudOperation = CoreCrudOperation | 'findUniqueOrThrow' | 'findFirstOrThrow';
|
|
610
|
+
|
|
611
|
+
/**
|
|
612
|
+
* ZenStack runtime plugin.
|
|
613
|
+
*/
|
|
614
|
+
interface RuntimePlugin<Schema extends SchemaDef = SchemaDef> {
|
|
822
615
|
/**
|
|
823
|
-
*
|
|
824
|
-
* @param args - upsert args
|
|
825
|
-
* @returns the upserted entity
|
|
826
|
-
*
|
|
827
|
-
* @example
|
|
828
|
-
* ```ts
|
|
829
|
-
* // upsert an entity
|
|
830
|
-
* await client.user.upsert({
|
|
831
|
-
* // `where` clause is used to find the entity
|
|
832
|
-
* where: { id: 1 },
|
|
833
|
-
* // `create` clause is used if the entity is not found
|
|
834
|
-
* create: { email: 'alex@zenstack.dev', name: 'Alex' },
|
|
835
|
-
* // `update` clause is used if the entity is found
|
|
836
|
-
* update: { name: 'Alex-new' },
|
|
837
|
-
* // `select` and `omit` can be used to control the returned fields
|
|
838
|
-
* ...
|
|
839
|
-
* });
|
|
840
|
-
* ```
|
|
616
|
+
* Plugin ID.
|
|
841
617
|
*/
|
|
842
|
-
|
|
618
|
+
id: string;
|
|
843
619
|
/**
|
|
844
|
-
*
|
|
845
|
-
* @param args - delete args
|
|
846
|
-
* @returns the deleted entity. Throws `NotFoundError` if the entity is not found.
|
|
847
|
-
*
|
|
848
|
-
* @example
|
|
849
|
-
* ```ts
|
|
850
|
-
* // delete an entity
|
|
851
|
-
* await client.user.delete({
|
|
852
|
-
* where: { id: 1 }
|
|
853
|
-
* });
|
|
854
|
-
*
|
|
855
|
-
* // delete an entity and return selected fields
|
|
856
|
-
* await client.user.delete({
|
|
857
|
-
* where: { id: 1 },
|
|
858
|
-
* select: { id: true, email: true }
|
|
859
|
-
* }); // result: `{ id: string; email: string }`
|
|
860
|
-
* ```
|
|
620
|
+
* Plugin display name.
|
|
861
621
|
*/
|
|
862
|
-
|
|
622
|
+
name?: string;
|
|
863
623
|
/**
|
|
864
|
-
*
|
|
865
|
-
* @param args - delete args
|
|
866
|
-
* @returns count of deleted entities: `{ count: number }`
|
|
867
|
-
*
|
|
868
|
-
* @example
|
|
869
|
-
* ```ts
|
|
870
|
-
* // delete many entities
|
|
871
|
-
* await client.user.deleteMany({
|
|
872
|
-
* where: { email: { endsWith: '@zenstack.dev' } }
|
|
873
|
-
* });
|
|
874
|
-
*
|
|
875
|
-
* // limit the number of deleted entities
|
|
876
|
-
* await client.user.deleteMany({
|
|
877
|
-
* where: { email: { endsWith: '@zenstack.dev' } },
|
|
878
|
-
* limit: 10
|
|
879
|
-
* });
|
|
880
|
-
* ```
|
|
624
|
+
* Plugin description.
|
|
881
625
|
*/
|
|
882
|
-
|
|
626
|
+
description?: string;
|
|
883
627
|
/**
|
|
884
|
-
*
|
|
885
|
-
* @param args - count args
|
|
886
|
-
* @returns `number`, or an object containing count of selected relations
|
|
887
|
-
*
|
|
888
|
-
* @example
|
|
889
|
-
* ```ts
|
|
890
|
-
* // count all
|
|
891
|
-
* await client.user.count();
|
|
892
|
-
*
|
|
893
|
-
* // count with a filter
|
|
894
|
-
* await client.user.count({ where: { email: { endsWith: '@zenstack.dev' } } });
|
|
895
|
-
*
|
|
896
|
-
* // count rows and field values
|
|
897
|
-
* await client.user.count({
|
|
898
|
-
* select: { _all: true, email: true }
|
|
899
|
-
* }); // result: `{ _all: number, email: number }`
|
|
628
|
+
* Intercepts an ORM query.
|
|
900
629
|
*/
|
|
901
|
-
|
|
630
|
+
onQuery?: OnQueryCallback<Schema>;
|
|
902
631
|
/**
|
|
903
|
-
*
|
|
904
|
-
* @param args - aggregation args
|
|
905
|
-
* @returns an object containing aggregated values
|
|
906
|
-
*
|
|
907
|
-
* @example
|
|
908
|
-
* ```ts
|
|
909
|
-
* // aggregate rows
|
|
910
|
-
* await client.profile.aggregate({
|
|
911
|
-
* where: { email: { endsWith: '@zenstack.dev' } },
|
|
912
|
-
* _count: true,
|
|
913
|
-
* _avg: { age: true },
|
|
914
|
-
* _sum: { age: true },
|
|
915
|
-
* _min: { age: true },
|
|
916
|
-
* _max: { age: true }
|
|
917
|
-
* }); // result: `{ _count: number, _avg: { age: number }, ... }`
|
|
632
|
+
* Intercepts an entity mutation.
|
|
918
633
|
*/
|
|
919
|
-
|
|
634
|
+
onEntityMutation?: EntityMutationHooksDef<Schema>;
|
|
920
635
|
/**
|
|
921
|
-
*
|
|
922
|
-
* @param args - groupBy args
|
|
923
|
-
* @returns an object containing grouped values
|
|
924
|
-
*
|
|
925
|
-
* @example
|
|
926
|
-
* ```ts
|
|
927
|
-
* // group by a field
|
|
928
|
-
* await client.profile.groupBy({
|
|
929
|
-
* by: 'country',
|
|
930
|
-
* _count: true
|
|
931
|
-
* }); // result: `Array<{ country: string, _count: number }>`
|
|
932
|
-
*
|
|
933
|
-
* // group by multiple fields
|
|
934
|
-
* await client.profile.groupBy({
|
|
935
|
-
* by: ['country', 'city'],
|
|
936
|
-
* _count: true
|
|
937
|
-
* }); // result: `Array<{ country: string, city: string, _count: number }>`
|
|
938
|
-
*
|
|
939
|
-
* // group by with sorting, the `orderBy` fields must be in the `by` list
|
|
940
|
-
* await client.profile.groupBy({
|
|
941
|
-
* by: 'country',
|
|
942
|
-
* orderBy: { country: 'desc' }
|
|
943
|
-
* });
|
|
944
|
-
*
|
|
945
|
-
* // group by with having (post-aggregation filter), the `having` fields must
|
|
946
|
-
* // be in the `by` list
|
|
947
|
-
* await client.profile.groupBy({
|
|
948
|
-
* by: 'country',
|
|
949
|
-
* having: { country: 'US' }
|
|
950
|
-
* });
|
|
636
|
+
* Intercepts a Kysely query.
|
|
951
637
|
*/
|
|
952
|
-
|
|
638
|
+
onKyselyQuery?: OnKyselyQueryCallback<Schema>;
|
|
953
639
|
}
|
|
640
|
+
/**
|
|
641
|
+
* Defines a ZenStack runtime plugin.
|
|
642
|
+
*/
|
|
643
|
+
declare function definePlugin<Schema extends SchemaDef>(plugin: RuntimePlugin<Schema>): RuntimePlugin<Schema>;
|
|
954
644
|
|
|
955
|
-
type
|
|
956
|
-
|
|
957
|
-
declare abstract class BaseCrudDialect<Schema extends SchemaDef> {
|
|
958
|
-
protected readonly schema: Schema;
|
|
959
|
-
protected readonly options: ClientOptions<Schema>;
|
|
960
|
-
constructor(schema: Schema, options: ClientOptions<Schema>);
|
|
961
|
-
abstract get provider(): DataSourceProviderType;
|
|
962
|
-
transformPrimitive(value: unknown, _type: BuiltinType): unknown;
|
|
963
|
-
abstract buildRelationSelection(query: SelectQueryBuilder<any, any, any>, model: string, relationField: string, parentAlias: string, payload: true | FindArgs<Schema, GetModels<Schema>, true>): SelectQueryBuilder<any, any, any>;
|
|
964
|
-
abstract buildSkipTake(query: SelectQueryBuilder<any, any, any>, skip: number | undefined, take: number | undefined): SelectQueryBuilder<any, any, any>;
|
|
965
|
-
buildFilter(eb: ExpressionBuilder<any, any>, model: string, modelAlias: string, where: boolean | object | undefined): Expression<SqlBool>;
|
|
966
|
-
protected buildCompositeFilter(eb: ExpressionBuilder<any, any>, model: string, modelAlias: string, key: 'AND' | 'OR' | 'NOT', payload: any): Expression<SqlBool>;
|
|
967
|
-
private buildRelationFilter;
|
|
968
|
-
private buildToOneRelationFilter;
|
|
969
|
-
private buildToManyRelationFilter;
|
|
970
|
-
private buildArrayFilter;
|
|
971
|
-
buildPrimitiveFilter(eb: ExpressionBuilder<any, any>, model: string, modelAlias: string, field: string, fieldDef: FieldDef, payload: any): Expression<SqlBool>;
|
|
972
|
-
private buildLiteralFilter;
|
|
973
|
-
private buildStandardFilter;
|
|
974
|
-
private buildStringFilter;
|
|
975
|
-
private prepStringCasing;
|
|
976
|
-
private buildNumberFilter;
|
|
977
|
-
private buildBooleanFilter;
|
|
978
|
-
private buildDateTimeFilter;
|
|
979
|
-
private buildBytesFilter;
|
|
980
|
-
private buildEnumFilter;
|
|
981
|
-
buildOrderBy(query: SelectQueryBuilder<any, any, any>, model: string, modelAlias: string, orderBy: OrArray<OrderBy<Schema, GetModels<Schema>, boolean, boolean>> | undefined, useDefaultIfEmpty: boolean, negated: boolean): SelectQueryBuilder<any, any, any>;
|
|
982
|
-
private negateSort;
|
|
983
|
-
true(eb: ExpressionBuilder<any, any>): Expression<SqlBool>;
|
|
984
|
-
false(eb: ExpressionBuilder<any, any>): Expression<SqlBool>;
|
|
985
|
-
isTrue(expression: Expression<SqlBool>): boolean;
|
|
986
|
-
isFalse(expression: Expression<SqlBool>): boolean;
|
|
987
|
-
protected and(eb: ExpressionBuilder<any, any>, ...args: Expression<SqlBool>[]): Expression<SqlBool>;
|
|
988
|
-
protected or(eb: ExpressionBuilder<any, any>, ...args: Expression<SqlBool>[]): Expression<SqlBool>;
|
|
989
|
-
protected not(eb: ExpressionBuilder<any, any>, ...args: Expression<SqlBool>[]): ExpressionWrapper<any, any, SqlBool>;
|
|
645
|
+
type OnQueryCallback<Schema extends SchemaDef> = (ctx: OnQueryHookContext<Schema>) => Promise<unknown>;
|
|
646
|
+
type OnQueryHookContext<Schema extends SchemaDef> = {
|
|
990
647
|
/**
|
|
991
|
-
*
|
|
648
|
+
* The model that is being queried.
|
|
992
649
|
*/
|
|
993
|
-
|
|
650
|
+
model: GetModels<Schema>;
|
|
994
651
|
/**
|
|
995
|
-
*
|
|
652
|
+
* The operation that is being performed.
|
|
996
653
|
*/
|
|
997
|
-
|
|
654
|
+
operation: AllCrudOperation;
|
|
998
655
|
/**
|
|
999
|
-
*
|
|
656
|
+
* The query arguments.
|
|
1000
657
|
*/
|
|
1001
|
-
|
|
658
|
+
args: unknown;
|
|
1002
659
|
/**
|
|
1003
|
-
*
|
|
660
|
+
* The function to proceed with the original query.
|
|
661
|
+
* It takes the same arguments as the operation method.
|
|
662
|
+
*
|
|
663
|
+
* @param args The query arguments.
|
|
1004
664
|
*/
|
|
1005
|
-
|
|
665
|
+
proceed: (args: unknown) => Promise<unknown>;
|
|
1006
666
|
/**
|
|
1007
|
-
*
|
|
667
|
+
* The ZenStack client that is performing the operation.
|
|
1008
668
|
*/
|
|
1009
|
-
|
|
669
|
+
client: ClientContract<Schema>;
|
|
670
|
+
};
|
|
671
|
+
type EntityMutationHooksDef<Schema extends SchemaDef> = {
|
|
1010
672
|
/**
|
|
1011
|
-
*
|
|
673
|
+
* This callback determines whether a mutation should be intercepted, and if so,
|
|
674
|
+
* what data should be loaded before and after the mutation.
|
|
1012
675
|
*/
|
|
1013
|
-
|
|
1014
|
-
}
|
|
1015
|
-
|
|
1016
|
-
type CrudOperation = 'findMany' | 'findUnique' | 'findFirst' | 'create' | 'createMany' | 'createManyAndReturn' | 'update' | 'updateMany' | 'updateManyAndReturn' | 'upsert' | 'delete' | 'deleteMany' | 'count' | 'aggregate' | 'groupBy';
|
|
1017
|
-
|
|
1018
|
-
type QueryContext<Schema extends SchemaDef> = {
|
|
676
|
+
mutationInterceptionFilter?: MutationInterceptionFilter<Schema>;
|
|
1019
677
|
/**
|
|
1020
|
-
*
|
|
678
|
+
* Called before an entity is mutated.
|
|
679
|
+
* @param args.entity Only available if `loadBeforeMutationEntities` is set to true in the
|
|
680
|
+
* return value of {@link RuntimePlugin.mutationInterceptionFilter}.
|
|
1021
681
|
*/
|
|
1022
|
-
|
|
682
|
+
beforeEntityMutation?: BeforeEntityMutationCallback<Schema>;
|
|
1023
683
|
/**
|
|
1024
|
-
*
|
|
684
|
+
* Called after an entity is mutated.
|
|
685
|
+
* @param args.beforeMutationEntity Only available if `loadBeforeMutationEntities` is set to true in the
|
|
686
|
+
* return value of {@link RuntimePlugin.mutationInterceptionFilter}.
|
|
687
|
+
* @param args.afterMutationEntity Only available if `loadAfterMutationEntities` is set to true in the
|
|
688
|
+
* return value of {@link RuntimePlugin.mutationInterceptionFilter}.
|
|
689
|
+
*/
|
|
690
|
+
afterEntityMutation?: AfterEntityMutationCallback<Schema>;
|
|
691
|
+
};
|
|
692
|
+
type MutationHooksArgs<Schema extends SchemaDef> = {
|
|
693
|
+
/**
|
|
694
|
+
* The model that is being mutated.
|
|
1025
695
|
*/
|
|
1026
696
|
model: GetModels<Schema>;
|
|
1027
697
|
/**
|
|
1028
|
-
* The
|
|
698
|
+
* The mutation action that is being performed.
|
|
1029
699
|
*/
|
|
1030
|
-
|
|
700
|
+
action: 'create' | 'update' | 'delete';
|
|
1031
701
|
/**
|
|
1032
|
-
* The
|
|
702
|
+
* The mutation data. Only available for create and update actions.
|
|
1033
703
|
*/
|
|
1034
|
-
|
|
704
|
+
queryNode: OperationNode;
|
|
1035
705
|
};
|
|
706
|
+
type MutationInterceptionFilter<Schema extends SchemaDef> = (args: MutationHooksArgs<Schema>) => MaybePromise<MutationInterceptionFilterResult>;
|
|
1036
707
|
/**
|
|
1037
708
|
* The result of the hooks interception filter.
|
|
1038
709
|
*/
|
|
@@ -1041,232 +712,752 @@ type MutationInterceptionFilterResult = {
|
|
|
1041
712
|
* Whether to intercept the mutation or not.
|
|
1042
713
|
*/
|
|
1043
714
|
intercept: boolean;
|
|
1044
|
-
/**
|
|
1045
|
-
* Whether to use a transaction for the mutation.
|
|
1046
|
-
*/
|
|
1047
|
-
useTransactionForMutation?: boolean;
|
|
1048
715
|
/**
|
|
1049
716
|
* Whether entities should be loaded before the mutation.
|
|
1050
717
|
*/
|
|
1051
|
-
|
|
718
|
+
loadBeforeMutationEntities?: boolean;
|
|
1052
719
|
/**
|
|
1053
720
|
* Whether entities should be loaded after the mutation.
|
|
1054
721
|
*/
|
|
1055
|
-
|
|
722
|
+
loadAfterMutationEntities?: boolean;
|
|
1056
723
|
};
|
|
1057
|
-
type
|
|
724
|
+
type BeforeEntityMutationCallback<Schema extends SchemaDef> = (args: PluginBeforeEntityMutationArgs<Schema>) => MaybePromise<void>;
|
|
725
|
+
type AfterEntityMutationCallback<Schema extends SchemaDef> = (args: PluginAfterEntityMutationArgs<Schema>) => MaybePromise<void>;
|
|
726
|
+
type PluginBeforeEntityMutationArgs<Schema extends SchemaDef> = MutationHooksArgs<Schema> & {
|
|
1058
727
|
/**
|
|
1059
|
-
*
|
|
728
|
+
* Entities that are about to be mutated. Only available if `loadBeforeMutationEntities` is set to
|
|
729
|
+
* true in the return value of {@link RuntimePlugin.mutationInterceptionFilter}.
|
|
1060
730
|
*/
|
|
1061
|
-
|
|
731
|
+
entities?: unknown[];
|
|
732
|
+
};
|
|
733
|
+
type PluginAfterEntityMutationArgs<Schema extends SchemaDef> = MutationHooksArgs<Schema> & {
|
|
1062
734
|
/**
|
|
1063
|
-
*
|
|
735
|
+
* Entities that are about to be mutated. Only available if `loadBeforeMutationEntities` is set to
|
|
736
|
+
* true in the return value of {@link RuntimePlugin.mutationInterceptionFilter}.
|
|
1064
737
|
*/
|
|
1065
|
-
|
|
738
|
+
beforeMutationEntities?: unknown[];
|
|
1066
739
|
/**
|
|
1067
|
-
*
|
|
740
|
+
* Entities mutated. Only available if `loadAfterMutationEntities` is set to true in the return
|
|
741
|
+
* value of {@link RuntimePlugin.mutationInterceptionFilter}.
|
|
1068
742
|
*/
|
|
1069
|
-
|
|
1070
|
-
};
|
|
1071
|
-
type OnQueryArgs<Schema extends SchemaDef> = QueryContext<Schema> & {
|
|
1072
|
-
proceed: ProceedQueryFunction<Schema>;
|
|
1073
|
-
};
|
|
1074
|
-
type PluginBeforeEntityMutationArgs<Schema extends SchemaDef> = MutationHooksArgs<Schema> & {
|
|
1075
|
-
entities?: Record<string, unknown>[];
|
|
743
|
+
afterMutationEntities?: unknown[];
|
|
1076
744
|
};
|
|
1077
|
-
type PluginAfterEntityMutationArgs<Schema extends SchemaDef> = MutationHooksArgs<Schema> & {
|
|
1078
|
-
beforeMutationEntities?: Record<string, unknown>[];
|
|
1079
|
-
afterMutationEntities?: Record<string, unknown>[];
|
|
1080
|
-
};
|
|
1081
|
-
type ProceedQueryFunction<Schema extends SchemaDef> = (queryArgs: unknown, tx?: ClientContract<Schema>) => Promise<unknown>;
|
|
1082
|
-
type OnKyselyQueryTransactionCallback = (proceed: ProceedKyselyQueryFunction) => Promise<QueryResult<any>>;
|
|
1083
|
-
type OnKyselyQueryTransaction = (callback: OnKyselyQueryTransactionCallback) => Promise<QueryResult<any>>;
|
|
1084
745
|
type OnKyselyQueryArgs<Schema extends SchemaDef> = {
|
|
1085
746
|
kysely: ToKysely<Schema>;
|
|
1086
747
|
schema: SchemaDef;
|
|
1087
748
|
client: ClientContract<Schema>;
|
|
1088
749
|
query: RootOperationNode;
|
|
1089
750
|
proceed: ProceedKyselyQueryFunction;
|
|
1090
|
-
transaction: OnKyselyQueryTransaction;
|
|
1091
751
|
};
|
|
1092
752
|
type ProceedKyselyQueryFunction = (query: RootOperationNode) => Promise<QueryResult<any>>;
|
|
753
|
+
type OnKyselyQueryCallback<Schema extends SchemaDef> = (args: OnKyselyQueryArgs<Schema>) => Promise<QueryResult<UnknownRow>>;
|
|
754
|
+
|
|
755
|
+
type ZModelFunctionContext<Schema extends SchemaDef> = {
|
|
756
|
+
dialect: BaseCrudDialect<Schema>;
|
|
757
|
+
model: GetModels<Schema>;
|
|
758
|
+
operation: CRUD;
|
|
759
|
+
};
|
|
760
|
+
type ZModelFunction<Schema extends SchemaDef> = (eb: ExpressionBuilder<ToKyselySchema<Schema>, keyof ToKyselySchema<Schema>>, args: Expression<any>[], context: ZModelFunctionContext<Schema>) => Expression<unknown>;
|
|
1093
761
|
/**
|
|
1094
|
-
* ZenStack
|
|
762
|
+
* ZenStack client options.
|
|
1095
763
|
*/
|
|
1096
|
-
|
|
764
|
+
type ClientOptions<Schema extends SchemaDef> = {
|
|
1097
765
|
/**
|
|
1098
|
-
*
|
|
766
|
+
* Kysely dialect.
|
|
1099
767
|
*/
|
|
1100
|
-
|
|
768
|
+
dialect: Dialect;
|
|
1101
769
|
/**
|
|
1102
|
-
*
|
|
770
|
+
* Custom function definitions.
|
|
1103
771
|
*/
|
|
1104
|
-
|
|
772
|
+
functions?: Record<string, ZModelFunction<Schema>>;
|
|
1105
773
|
/**
|
|
1106
|
-
*
|
|
774
|
+
* Plugins.
|
|
1107
775
|
*/
|
|
1108
|
-
|
|
776
|
+
plugins?: RuntimePlugin<Schema>[];
|
|
1109
777
|
/**
|
|
1110
|
-
*
|
|
778
|
+
* Logging configuration.
|
|
1111
779
|
*/
|
|
1112
|
-
|
|
780
|
+
log?: KyselyConfig['log'];
|
|
781
|
+
} & (HasComputedFields<Schema> extends true ? {
|
|
1113
782
|
/**
|
|
1114
|
-
*
|
|
783
|
+
* Computed field definitions.
|
|
1115
784
|
*/
|
|
1116
|
-
|
|
785
|
+
computedFields: ComputedFieldsOptions<Schema>;
|
|
786
|
+
} : {}) & (HasProcedures<Schema> extends true ? {
|
|
1117
787
|
/**
|
|
1118
|
-
*
|
|
1119
|
-
* what data should be loaded before and after the mutation.
|
|
788
|
+
* Custom procedure definitions.
|
|
1120
789
|
*/
|
|
1121
|
-
|
|
790
|
+
procedures: ProceduresOptions<Schema>;
|
|
791
|
+
} : {});
|
|
792
|
+
type ComputedFieldsOptions<Schema extends SchemaDef> = {
|
|
793
|
+
[Model in GetModels<Schema> as 'computedFields' extends keyof GetModel<Schema, Model> ? Model : never]: {
|
|
794
|
+
[Field in keyof Schema['models'][Model]['computedFields']]: PrependParameter<ExpressionBuilder<ToKyselySchema<Schema>, Model>, Schema['models'][Model]['computedFields'][Field]>;
|
|
795
|
+
};
|
|
796
|
+
};
|
|
797
|
+
type HasComputedFields<Schema extends SchemaDef> = string extends GetModels<Schema> ? false : keyof ComputedFieldsOptions<Schema> extends never ? false : true;
|
|
798
|
+
type ProceduresOptions<Schema extends SchemaDef> = Schema extends {
|
|
799
|
+
procedures: Record<string, ProcedureDef>;
|
|
800
|
+
} ? {
|
|
801
|
+
[Key in keyof Schema['procedures']]: PrependParameter<ClientContract<Schema>, ProcedureFunc<Schema, Schema['procedures'][Key]>>;
|
|
802
|
+
} : {};
|
|
803
|
+
type HasProcedures<Schema extends SchemaDef> = Schema extends {
|
|
804
|
+
procedures: Record<string, ProcedureDef>;
|
|
805
|
+
} ? true : false;
|
|
806
|
+
|
|
807
|
+
/**
|
|
808
|
+
* A promise that only executes when it's awaited or .then() is called.
|
|
809
|
+
*/
|
|
810
|
+
type ZenStackPromise<Schema extends SchemaDef, T> = Promise<T> & {
|
|
1122
811
|
/**
|
|
1123
|
-
*
|
|
1124
|
-
*
|
|
1125
|
-
* return value of {@link RuntimePlugin.mutationInterceptionFilter}.
|
|
812
|
+
* @private
|
|
813
|
+
* Callable to get a plain promise.
|
|
1126
814
|
*/
|
|
1127
|
-
|
|
815
|
+
cb: (txClient?: ClientContract<Schema>) => Promise<T>;
|
|
816
|
+
};
|
|
817
|
+
|
|
818
|
+
type TransactionUnsupportedMethods = (typeof TRANSACTION_UNSUPPORTED_METHODS)[number];
|
|
819
|
+
/**
|
|
820
|
+
* Transaction isolation levels.
|
|
821
|
+
*/
|
|
822
|
+
declare enum TransactionIsolationLevel {
|
|
823
|
+
ReadUncommitted = "read uncommitted",
|
|
824
|
+
ReadCommitted = "read committed",
|
|
825
|
+
RepeatableRead = "repeatable read",
|
|
826
|
+
Serializable = "serializable",
|
|
827
|
+
Snapshot = "snapshot"
|
|
828
|
+
}
|
|
829
|
+
/**
|
|
830
|
+
* ZenStack client interface.
|
|
831
|
+
*/
|
|
832
|
+
type ClientContract<Schema extends SchemaDef> = {
|
|
833
|
+
readonly $schema: Schema;
|
|
1128
834
|
/**
|
|
1129
|
-
*
|
|
1130
|
-
|
|
1131
|
-
|
|
1132
|
-
|
|
1133
|
-
*
|
|
835
|
+
* The client options.
|
|
836
|
+
*/
|
|
837
|
+
readonly $options: ClientOptions<Schema>;
|
|
838
|
+
/**
|
|
839
|
+
* Executes a prepared raw query and returns the number of affected rows.
|
|
840
|
+
* @example
|
|
841
|
+
* ```
|
|
842
|
+
* const result = await db.$executeRaw`UPDATE User SET cool = ${true} WHERE email = ${'user@email.com'};`
|
|
843
|
+
* ```
|
|
844
|
+
*/
|
|
845
|
+
$executeRaw(query: TemplateStringsArray, ...values: any[]): ZenStackPromise<Schema, number>;
|
|
846
|
+
/**
|
|
847
|
+
* Executes a raw query and returns the number of affected rows.
|
|
848
|
+
* This method is susceptible to SQL injections.
|
|
849
|
+
* @example
|
|
850
|
+
* ```
|
|
851
|
+
* const result = await db.$executeRawUnsafe('UPDATE User SET cool = $1 WHERE email = $2 ;', true, 'user@email.com')
|
|
852
|
+
* ```
|
|
853
|
+
*/
|
|
854
|
+
$executeRawUnsafe(query: string, ...values: any[]): ZenStackPromise<Schema, number>;
|
|
855
|
+
/**
|
|
856
|
+
* Performs a prepared raw query and returns the `SELECT` data.
|
|
857
|
+
* @example
|
|
858
|
+
* ```
|
|
859
|
+
* const result = await db.$queryRaw`SELECT * FROM User WHERE id = ${1} OR email = ${'user@email.com'};`
|
|
860
|
+
* ```
|
|
861
|
+
*/
|
|
862
|
+
$queryRaw<T = unknown>(query: TemplateStringsArray, ...values: any[]): ZenStackPromise<Schema, T>;
|
|
863
|
+
/**
|
|
864
|
+
* Performs a raw query and returns the `SELECT` data.
|
|
865
|
+
* This method is susceptible to SQL injections.
|
|
866
|
+
* @example
|
|
867
|
+
* ```
|
|
868
|
+
* const result = await db.$queryRawUnsafe('SELECT * FROM User WHERE id = $1 OR email = $2;', 1, 'user@email.com')
|
|
869
|
+
* ```
|
|
870
|
+
*/
|
|
871
|
+
$queryRawUnsafe<T = unknown>(query: string, ...values: any[]): ZenStackPromise<Schema, T>;
|
|
872
|
+
/**
|
|
873
|
+
* The current user identity.
|
|
874
|
+
*/
|
|
875
|
+
get $auth(): AuthType<Schema> | undefined;
|
|
876
|
+
/**
|
|
877
|
+
* Sets the current user identity.
|
|
878
|
+
*/
|
|
879
|
+
$setAuth(auth: AuthType<Schema> | undefined): ClientContract<Schema>;
|
|
880
|
+
/**
|
|
881
|
+
* The Kysely query builder instance.
|
|
882
|
+
*/
|
|
883
|
+
readonly $qb: ToKysely<Schema>;
|
|
884
|
+
/**
|
|
885
|
+
* The raw Kysely query builder without any ZenStack enhancements.
|
|
886
|
+
*/
|
|
887
|
+
readonly $qbRaw: ToKysely<any>;
|
|
888
|
+
/**
|
|
889
|
+
* Starts an interactive transaction.
|
|
890
|
+
*/
|
|
891
|
+
$transaction<T>(callback: (tx: Omit<ClientContract<Schema>, TransactionUnsupportedMethods>) => Promise<T>, options?: {
|
|
892
|
+
isolationLevel?: TransactionIsolationLevel;
|
|
893
|
+
}): Promise<T>;
|
|
894
|
+
/**
|
|
895
|
+
* Starts a sequential transaction.
|
|
896
|
+
*/
|
|
897
|
+
$transaction<P extends ZenStackPromise<Schema, any>[]>(arg: [...P], options?: {
|
|
898
|
+
isolationLevel?: TransactionIsolationLevel;
|
|
899
|
+
}): Promise<UnwrapTuplePromises<P>>;
|
|
900
|
+
/**
|
|
901
|
+
* Returns a new client with the specified plugin installed.
|
|
902
|
+
*/
|
|
903
|
+
$use(plugin: RuntimePlugin<Schema>): ClientContract<Schema>;
|
|
904
|
+
/**
|
|
905
|
+
* Returns a new client with the specified plugin removed.
|
|
906
|
+
*/
|
|
907
|
+
$unuse(pluginId: string): ClientContract<Schema>;
|
|
908
|
+
/**
|
|
909
|
+
* Returns a new client with all plugins removed.
|
|
910
|
+
*/
|
|
911
|
+
$unuseAll(): ClientContract<Schema>;
|
|
912
|
+
/**
|
|
913
|
+
* Disconnects the underlying Kysely instance from the database.
|
|
914
|
+
*/
|
|
915
|
+
$disconnect(): Promise<void>;
|
|
916
|
+
/**
|
|
917
|
+
* Pushes the schema to the database. For testing purposes only.
|
|
918
|
+
* @private
|
|
1134
919
|
*/
|
|
1135
|
-
|
|
1136
|
-
}
|
|
1137
|
-
|
|
1138
|
-
|
|
1139
|
-
|
|
1140
|
-
|
|
920
|
+
$pushSchema(): Promise<void>;
|
|
921
|
+
} & {
|
|
922
|
+
[Key in GetModels<Schema> as Uncapitalize<Key>]: ModelOperations<Schema, Key>;
|
|
923
|
+
} & Procedures<Schema>;
|
|
924
|
+
type _TypeMap = {
|
|
925
|
+
String: string;
|
|
926
|
+
Int: number;
|
|
927
|
+
Float: number;
|
|
928
|
+
BigInt: bigint;
|
|
929
|
+
Decimal: Decimal$1;
|
|
930
|
+
Boolean: boolean;
|
|
931
|
+
DateTime: Date;
|
|
1141
932
|
};
|
|
1142
|
-
type
|
|
1143
|
-
|
|
1144
|
-
|
|
1145
|
-
|
|
1146
|
-
|
|
1147
|
-
|
|
1148
|
-
|
|
933
|
+
type MapType<Schema extends SchemaDef, T extends string> = T extends keyof _TypeMap ? _TypeMap[T] : T extends GetModels<Schema> ? ModelResult<Schema, T> : unknown;
|
|
934
|
+
type Procedures<Schema extends SchemaDef> = Schema['procedures'] extends Record<string, ProcedureDef> ? {
|
|
935
|
+
$procedures: {
|
|
936
|
+
[Key in keyof Schema['procedures']]: ProcedureFunc<Schema, Schema['procedures'][Key]>;
|
|
937
|
+
};
|
|
938
|
+
} : {};
|
|
939
|
+
type ProcedureFunc<Schema extends SchemaDef, Proc extends ProcedureDef> = (...args: MapProcedureParams<Schema, Proc['params']>) => Promise<MapType<Schema, Proc['returnType']>>;
|
|
940
|
+
type MapProcedureParams<Schema extends SchemaDef, Params> = {
|
|
941
|
+
[P in keyof Params]: Params[P] extends {
|
|
942
|
+
type: infer U;
|
|
943
|
+
} ? OrUndefinedIf<MapType<Schema, U & string>, Params[P] extends {
|
|
944
|
+
optional: true;
|
|
945
|
+
} ? true : false> : never;
|
|
1149
946
|
};
|
|
1150
|
-
type ZModelFunction<Schema extends SchemaDef> = (eb: ExpressionBuilder<ToKyselySchema<Schema>, keyof ToKyselySchema<Schema>>, args: Expression<any>[], context: ZModelFunctionContext<Schema>) => Expression<unknown>;
|
|
1151
947
|
/**
|
|
1152
|
-
* ZenStack client
|
|
948
|
+
* Creates a new ZenStack client instance.
|
|
1153
949
|
*/
|
|
1154
|
-
|
|
950
|
+
interface ClientConstructor {
|
|
951
|
+
new <Schema extends SchemaDef>(schema: Schema, options: ClientOptions<Schema>): ClientContract<Schema>;
|
|
952
|
+
}
|
|
953
|
+
/**
|
|
954
|
+
* CRUD operations.
|
|
955
|
+
*/
|
|
956
|
+
type CRUD = 'create' | 'read' | 'update' | 'delete';
|
|
957
|
+
type AllModelOperations<Schema extends SchemaDef, Model extends GetModels<Schema>> = {
|
|
1155
958
|
/**
|
|
1156
|
-
*
|
|
959
|
+
* Returns a list of entities.
|
|
960
|
+
* @param args - query args
|
|
961
|
+
* @returns a list of entities
|
|
962
|
+
*
|
|
963
|
+
* @example
|
|
964
|
+
* ```ts
|
|
965
|
+
* // find all users and return all scalar fields
|
|
966
|
+
* await db.user.findMany();
|
|
967
|
+
*
|
|
968
|
+
* // find all users with name 'Alex'
|
|
969
|
+
* await db.user.findMany({
|
|
970
|
+
* where: {
|
|
971
|
+
* name: 'Alex'
|
|
972
|
+
* }
|
|
973
|
+
* });
|
|
974
|
+
*
|
|
975
|
+
* // select fields
|
|
976
|
+
* await db.user.findMany({
|
|
977
|
+
* select: {
|
|
978
|
+
* name: true,
|
|
979
|
+
* email: true,
|
|
980
|
+
* }
|
|
981
|
+
* }); // result: `Array<{ name: string, email: string }>`
|
|
982
|
+
*
|
|
983
|
+
* // omit fields
|
|
984
|
+
* await db.user.findMany({
|
|
985
|
+
* omit: {
|
|
986
|
+
* name: true,
|
|
987
|
+
* }
|
|
988
|
+
* }); // result: `Array<{ id: number; email: string; ... }>`
|
|
989
|
+
*
|
|
990
|
+
* // include relations (and all scalar fields)
|
|
991
|
+
* await db.user.findMany({
|
|
992
|
+
* include: {
|
|
993
|
+
* posts: true,
|
|
994
|
+
* }
|
|
995
|
+
* }); // result: `Array<{ ...; posts: Post[] }>`
|
|
996
|
+
*
|
|
997
|
+
* // include relations with filter
|
|
998
|
+
* await db.user.findMany({
|
|
999
|
+
* include: {
|
|
1000
|
+
* posts: {
|
|
1001
|
+
* where: {
|
|
1002
|
+
* published: true
|
|
1003
|
+
* }
|
|
1004
|
+
* }
|
|
1005
|
+
* }
|
|
1006
|
+
* });
|
|
1007
|
+
*
|
|
1008
|
+
* // pagination and sorting
|
|
1009
|
+
* await db.user.findMany({
|
|
1010
|
+
* skip: 10,
|
|
1011
|
+
* take: 10,
|
|
1012
|
+
* orderBy: [{ name: 'asc' }, { email: 'desc' }],
|
|
1013
|
+
* });
|
|
1014
|
+
*
|
|
1015
|
+
* // pagination with cursor (https://www.prisma.io/docs/orm/prisma-client/queries/pagination#cursor-based-pagination)
|
|
1016
|
+
* await db.user.findMany({
|
|
1017
|
+
* cursor: { id: 10 },
|
|
1018
|
+
* skip: 1,
|
|
1019
|
+
* take: 10,
|
|
1020
|
+
* orderBy: { id: 'asc' },
|
|
1021
|
+
* });
|
|
1022
|
+
*
|
|
1023
|
+
* // distinct
|
|
1024
|
+
* await db.user.findMany({
|
|
1025
|
+
* distinct: ['name']
|
|
1026
|
+
* });
|
|
1027
|
+
*
|
|
1028
|
+
* // count all relations
|
|
1029
|
+
* await db.user.findMany({
|
|
1030
|
+
* _count: true,
|
|
1031
|
+
* }); // result: `{ _count: { posts: number; ... } }`
|
|
1032
|
+
*
|
|
1033
|
+
* // count selected relations
|
|
1034
|
+
* await db.user.findMany({
|
|
1035
|
+
* _count: { select: { posts: true } },
|
|
1036
|
+
* }); // result: `{ _count: { posts: number } }`
|
|
1037
|
+
* ```
|
|
1157
1038
|
*/
|
|
1158
|
-
|
|
1039
|
+
findMany<T extends FindArgs<Schema, Model, true>>(args?: SelectSubset<T, FindArgs<Schema, Model, true>>): ZenStackPromise<Schema, Simplify<ModelResult<Schema, Model, T>>[]>;
|
|
1159
1040
|
/**
|
|
1160
|
-
*
|
|
1041
|
+
* Returns a uniquely identified entity.
|
|
1042
|
+
* @param args - query args
|
|
1043
|
+
* @returns a single entity or null if not found
|
|
1044
|
+
* @see {@link findMany}
|
|
1161
1045
|
*/
|
|
1162
|
-
|
|
1046
|
+
findUnique<T extends FindUniqueArgs<Schema, Model>>(args: SelectSubset<T, FindUniqueArgs<Schema, Model>>): ZenStackPromise<Schema, Simplify<ModelResult<Schema, Model, T>> | null>;
|
|
1163
1047
|
/**
|
|
1164
|
-
*
|
|
1048
|
+
* Returns a uniquely identified entity or throws `NotFoundError` if not found.
|
|
1049
|
+
* @param args - query args
|
|
1050
|
+
* @returns a single entity
|
|
1051
|
+
* @see {@link findMany}
|
|
1165
1052
|
*/
|
|
1166
|
-
|
|
1053
|
+
findUniqueOrThrow<T extends FindUniqueArgs<Schema, Model>>(args: SelectSubset<T, FindUniqueArgs<Schema, Model>>): ZenStackPromise<Schema, Simplify<ModelResult<Schema, Model, T>>>;
|
|
1167
1054
|
/**
|
|
1168
|
-
*
|
|
1055
|
+
* Returns the first entity.
|
|
1056
|
+
* @param args - query args
|
|
1057
|
+
* @returns a single entity or null if not found
|
|
1058
|
+
* @see {@link findMany}
|
|
1169
1059
|
*/
|
|
1170
|
-
|
|
1171
|
-
} & (HasComputedFields<Schema> extends true ? {
|
|
1060
|
+
findFirst<T extends FindArgs<Schema, Model, true>>(args?: SelectSubset<T, FindArgs<Schema, Model, true>>): ZenStackPromise<Schema, Simplify<ModelResult<Schema, Model, T>> | null>;
|
|
1172
1061
|
/**
|
|
1173
|
-
*
|
|
1062
|
+
* Returns the first entity or throws `NotFoundError` if not found.
|
|
1063
|
+
* @param args - query args
|
|
1064
|
+
* @returns a single entity
|
|
1065
|
+
* @see {@link findMany}
|
|
1174
1066
|
*/
|
|
1175
|
-
|
|
1176
|
-
} : {}) & (HasProcedures<Schema> extends true ? {
|
|
1067
|
+
findFirstOrThrow<T extends FindArgs<Schema, Model, true>>(args?: SelectSubset<T, FindArgs<Schema, Model, true>>): ZenStackPromise<Schema, Simplify<ModelResult<Schema, Model, T>>>;
|
|
1177
1068
|
/**
|
|
1178
|
-
*
|
|
1069
|
+
* Creates a new entity.
|
|
1070
|
+
* @param args - create args
|
|
1071
|
+
* @returns the created entity
|
|
1072
|
+
*
|
|
1073
|
+
* @example
|
|
1074
|
+
* ```ts
|
|
1075
|
+
* // simple create
|
|
1076
|
+
* await db.user.create({
|
|
1077
|
+
* data: { name: 'Alex', email: 'alex@zenstack.dev' }
|
|
1078
|
+
* });
|
|
1079
|
+
*
|
|
1080
|
+
* // nested create with relation
|
|
1081
|
+
* await db.user.create({
|
|
1082
|
+
* data: {
|
|
1083
|
+
* email: 'alex@zenstack.dev',
|
|
1084
|
+
* posts: { create: { title: 'Hello World' } }
|
|
1085
|
+
* }
|
|
1086
|
+
* });
|
|
1087
|
+
*
|
|
1088
|
+
* // you can use `select`, `omit`, and `include` to control
|
|
1089
|
+
* // the fields returned by the query, as with `findMany`
|
|
1090
|
+
* await db.user.create({
|
|
1091
|
+
* data: {
|
|
1092
|
+
* email: 'alex@zenstack.dev',
|
|
1093
|
+
* posts: { create: { title: 'Hello World' } }
|
|
1094
|
+
* },
|
|
1095
|
+
* include: { posts: true }
|
|
1096
|
+
* }); // result: `{ id: number; posts: Post[] }`
|
|
1097
|
+
*
|
|
1098
|
+
* // connect relations
|
|
1099
|
+
* await db.user.create({
|
|
1100
|
+
* data: {
|
|
1101
|
+
* email: 'alex@zenstack.dev',
|
|
1102
|
+
* posts: { connect: { id: 1 } }
|
|
1103
|
+
* }
|
|
1104
|
+
* });
|
|
1105
|
+
*
|
|
1106
|
+
* // connect relations, and create if not found
|
|
1107
|
+
* await db.user.create({
|
|
1108
|
+
* data: {
|
|
1109
|
+
* email: 'alex@zenstack.dev',
|
|
1110
|
+
* posts: {
|
|
1111
|
+
* connectOrCreate: {
|
|
1112
|
+
* where: { id: 1 },
|
|
1113
|
+
* create: { title: 'Hello World' }
|
|
1114
|
+
* }
|
|
1115
|
+
* }
|
|
1116
|
+
* }
|
|
1117
|
+
* });
|
|
1118
|
+
* ```
|
|
1179
1119
|
*/
|
|
1180
|
-
|
|
1181
|
-
} : {});
|
|
1182
|
-
type ComputedFieldsOptions<Schema extends SchemaDef> = {
|
|
1183
|
-
[Model in GetModels<Schema> as 'computedFields' extends keyof GetModel<Schema, Model> ? Model : never]: {
|
|
1184
|
-
[Field in keyof Schema['models'][Model]['computedFields']]: PrependParameter<ExpressionBuilder<ToKyselySchema<Schema>, Model>, Schema['models'][Model]['computedFields'][Field]>;
|
|
1185
|
-
};
|
|
1186
|
-
};
|
|
1187
|
-
type HasComputedFields<Schema extends SchemaDef> = string extends GetModels<Schema> ? false : keyof ComputedFieldsOptions<Schema> extends never ? false : true;
|
|
1188
|
-
type ProceduresOptions<Schema extends SchemaDef> = Schema extends {
|
|
1189
|
-
procedures: Record<string, ProcedureDef>;
|
|
1190
|
-
} ? {
|
|
1191
|
-
[Key in keyof Schema['procedures']]: PrependParameter<ClientContract<Schema>, ProcedureFunc<Schema, Schema['procedures'][Key]>>;
|
|
1192
|
-
} : {};
|
|
1193
|
-
type HasProcedures<Schema extends SchemaDef> = Schema extends {
|
|
1194
|
-
procedures: Record<string, ProcedureDef>;
|
|
1195
|
-
} ? true : false;
|
|
1196
|
-
|
|
1197
|
-
/**
|
|
1198
|
-
* ZenStack client interface.
|
|
1199
|
-
*/
|
|
1200
|
-
type ClientContract<Schema extends SchemaDef> = {
|
|
1201
|
-
readonly $schema: Schema;
|
|
1120
|
+
create<T extends CreateArgs<Schema, Model>>(args: SelectSubset<T, CreateArgs<Schema, Model>>): ZenStackPromise<Schema, Simplify<ModelResult<Schema, Model, T>>>;
|
|
1202
1121
|
/**
|
|
1203
|
-
*
|
|
1122
|
+
* Creates multiple entities. Only scalar fields are allowed.
|
|
1123
|
+
* @param args - create args
|
|
1124
|
+
* @returns count of created entities: `{ count: number }`
|
|
1125
|
+
*
|
|
1126
|
+
* @example
|
|
1127
|
+
* ```ts
|
|
1128
|
+
* // create multiple entities
|
|
1129
|
+
* await db.user.createMany({
|
|
1130
|
+
* data: [
|
|
1131
|
+
* { name: 'Alex', email: 'alex@zenstack.dev' },
|
|
1132
|
+
* { name: 'John', email: 'john@zenstack.dev' }
|
|
1133
|
+
* ]
|
|
1134
|
+
* });
|
|
1135
|
+
*
|
|
1136
|
+
* // skip items that cause unique constraint violation
|
|
1137
|
+
* await db.user.createMany({
|
|
1138
|
+
* data: [
|
|
1139
|
+
* { name: 'Alex', email: 'alex@zenstack.dev' },
|
|
1140
|
+
* { name: 'John', email: 'john@zenstack.dev' }
|
|
1141
|
+
* ],
|
|
1142
|
+
* skipDuplicates: true
|
|
1143
|
+
* });
|
|
1144
|
+
* ```
|
|
1204
1145
|
*/
|
|
1205
|
-
|
|
1146
|
+
createMany<T extends CreateManyArgs<Schema, Model>>(args?: SelectSubset<T, CreateManyArgs<Schema, Model>>): ZenStackPromise<Schema, BatchResult>;
|
|
1206
1147
|
/**
|
|
1207
|
-
*
|
|
1148
|
+
* Creates multiple entities and returns them.
|
|
1149
|
+
* @param args - create args. See {@link createMany} for input. Use
|
|
1150
|
+
* `select` and `omit` to control the fields returned.
|
|
1151
|
+
* @returns the created entities
|
|
1152
|
+
*
|
|
1153
|
+
* @example
|
|
1154
|
+
* ```ts
|
|
1155
|
+
* // create multiple entities and return selected fields
|
|
1156
|
+
* await db.user.createManyAndReturn({
|
|
1157
|
+
* data: [
|
|
1158
|
+
* { name: 'Alex', email: 'alex@zenstack.dev' },
|
|
1159
|
+
* { name: 'John', email: 'john@zenstack.dev' }
|
|
1160
|
+
* ],
|
|
1161
|
+
* select: { id: true, email: true }
|
|
1162
|
+
* });
|
|
1163
|
+
* ```
|
|
1164
|
+
*/
|
|
1165
|
+
createManyAndReturn<T extends CreateManyAndReturnArgs<Schema, Model>>(args?: SelectSubset<T, CreateManyAndReturnArgs<Schema, Model>>): ZenStackPromise<Schema, Simplify<ModelResult<Schema, Model, T>>[]>;
|
|
1166
|
+
/**
|
|
1167
|
+
* Updates a uniquely identified entity.
|
|
1168
|
+
* @param args - update args. See {@link findMany} for how to control
|
|
1169
|
+
* fields and relations returned.
|
|
1170
|
+
* @returns the updated entity. Throws `NotFoundError` if the entity is not found.
|
|
1171
|
+
*
|
|
1172
|
+
* @example
|
|
1173
|
+
* ```ts
|
|
1174
|
+
* // update fields
|
|
1175
|
+
* await db.user.update({
|
|
1176
|
+
* where: { id: 1 },
|
|
1177
|
+
* data: { name: 'Alex' }
|
|
1178
|
+
* });
|
|
1179
|
+
*
|
|
1180
|
+
* // connect a relation
|
|
1181
|
+
* await db.user.update({
|
|
1182
|
+
* where: { id: 1 },
|
|
1183
|
+
* data: { posts: { connect: { id: 1 } } }
|
|
1184
|
+
* });
|
|
1185
|
+
*
|
|
1186
|
+
* // connect relation, and create if not found
|
|
1187
|
+
* await db.user.update({
|
|
1188
|
+
* where: { id: 1 },
|
|
1189
|
+
* data: {
|
|
1190
|
+
* posts: {
|
|
1191
|
+
* connectOrCreate: {
|
|
1192
|
+
* where: { id: 1 },
|
|
1193
|
+
* create: { title: 'Hello World' }
|
|
1194
|
+
* }
|
|
1195
|
+
* }
|
|
1196
|
+
* }
|
|
1197
|
+
* });
|
|
1198
|
+
*
|
|
1199
|
+
* // create many related entities (only available for one-to-many relations)
|
|
1200
|
+
* await db.user.update({
|
|
1201
|
+
* where: { id: 1 },
|
|
1202
|
+
* data: {
|
|
1203
|
+
* posts: {
|
|
1204
|
+
* createMany: {
|
|
1205
|
+
* data: [{ title: 'Hello World' }, { title: 'Hello World 2' }],
|
|
1206
|
+
* }
|
|
1207
|
+
* }
|
|
1208
|
+
* }
|
|
1209
|
+
* });
|
|
1210
|
+
*
|
|
1211
|
+
* // disconnect a one-to-many relation
|
|
1212
|
+
* await db.user.update({
|
|
1213
|
+
* where: { id: 1 },
|
|
1214
|
+
* data: { posts: { disconnect: { id: 1 } } }
|
|
1215
|
+
* });
|
|
1216
|
+
*
|
|
1217
|
+
* // disconnect a one-to-one relation
|
|
1218
|
+
* await db.user.update({
|
|
1219
|
+
* where: { id: 1 },
|
|
1220
|
+
* data: { profile: { disconnect: true } }
|
|
1221
|
+
* });
|
|
1222
|
+
*
|
|
1223
|
+
* // replace a relation (only available for one-to-many relations)
|
|
1224
|
+
* await db.user.update({
|
|
1225
|
+
* where: { id: 1 },
|
|
1226
|
+
* data: {
|
|
1227
|
+
* posts: {
|
|
1228
|
+
* set: [{ id: 1 }, { id: 2 }]
|
|
1229
|
+
* }
|
|
1230
|
+
* }
|
|
1231
|
+
* });
|
|
1232
|
+
*
|
|
1233
|
+
* // update a relation
|
|
1234
|
+
* await db.user.update({
|
|
1235
|
+
* where: { id: 1 },
|
|
1236
|
+
* data: {
|
|
1237
|
+
* posts: {
|
|
1238
|
+
* update: { where: { id: 1 }, data: { title: 'Hello World' } }
|
|
1239
|
+
* }
|
|
1240
|
+
* }
|
|
1241
|
+
* });
|
|
1242
|
+
*
|
|
1243
|
+
* // upsert a relation
|
|
1244
|
+
* await db.user.update({
|
|
1245
|
+
* where: { id: 1 },
|
|
1246
|
+
* data: {
|
|
1247
|
+
* posts: {
|
|
1248
|
+
* upsert: {
|
|
1249
|
+
* where: { id: 1 },
|
|
1250
|
+
* create: { title: 'Hello World' },
|
|
1251
|
+
* update: { title: 'Hello World' }
|
|
1252
|
+
* }
|
|
1253
|
+
* }
|
|
1254
|
+
* }
|
|
1255
|
+
* });
|
|
1256
|
+
*
|
|
1257
|
+
* // update many related entities (only available for one-to-many relations)
|
|
1258
|
+
* await db.user.update({
|
|
1259
|
+
* where: { id: 1 },
|
|
1260
|
+
* data: {
|
|
1261
|
+
* posts: {
|
|
1262
|
+
* updateMany: {
|
|
1263
|
+
* where: { published: true },
|
|
1264
|
+
* data: { title: 'Hello World' }
|
|
1265
|
+
* }
|
|
1266
|
+
* }
|
|
1267
|
+
* }
|
|
1268
|
+
* });
|
|
1269
|
+
*
|
|
1270
|
+
* // delete a one-to-many relation
|
|
1271
|
+
* await db.user.update({
|
|
1272
|
+
* where: { id: 1 },
|
|
1273
|
+
* data: { posts: { delete: { id: 1 } } }
|
|
1274
|
+
* });
|
|
1275
|
+
*
|
|
1276
|
+
* // delete a one-to-one relation
|
|
1277
|
+
* await db.user.update({
|
|
1278
|
+
* where: { id: 1 },
|
|
1279
|
+
* data: { profile: { delete: true } }
|
|
1280
|
+
* });
|
|
1281
|
+
* ```
|
|
1208
1282
|
*/
|
|
1209
|
-
|
|
1283
|
+
update<T extends UpdateArgs<Schema, Model>>(args: SelectSubset<T, UpdateArgs<Schema, Model>>): ZenStackPromise<Schema, Simplify<ModelResult<Schema, Model, T>>>;
|
|
1210
1284
|
/**
|
|
1211
|
-
*
|
|
1285
|
+
* Updates multiple entities.
|
|
1286
|
+
* @param args - update args. Only scalar fields are allowed for data.
|
|
1287
|
+
* @returns count of updated entities: `{ count: number }`
|
|
1288
|
+
*
|
|
1289
|
+
* @example
|
|
1290
|
+
* ```ts
|
|
1291
|
+
* // update many entities
|
|
1292
|
+
* await db.user.updateMany({
|
|
1293
|
+
* where: { email: { endsWith: '@zenstack.dev' } },
|
|
1294
|
+
* data: { role: 'ADMIN' }
|
|
1295
|
+
* });
|
|
1296
|
+
*
|
|
1297
|
+
* // limit the number of updated entities
|
|
1298
|
+
* await db.user.updateMany({
|
|
1299
|
+
* where: { email: { endsWith: '@zenstack.dev' } },
|
|
1300
|
+
* data: { role: 'ADMIN' },
|
|
1301
|
+
* limit: 10
|
|
1302
|
+
* });
|
|
1212
1303
|
*/
|
|
1213
|
-
|
|
1304
|
+
updateMany<T extends UpdateManyArgs<Schema, Model>>(args: Subset<T, UpdateManyArgs<Schema, Model>>): ZenStackPromise<Schema, BatchResult>;
|
|
1214
1305
|
/**
|
|
1215
|
-
*
|
|
1306
|
+
* Updates multiple entities and returns them.
|
|
1307
|
+
* @param args - update args. Only scalar fields are allowed for data.
|
|
1308
|
+
* @returns the updated entities
|
|
1309
|
+
*
|
|
1310
|
+
* @example
|
|
1311
|
+
* ```ts
|
|
1312
|
+
* // update many entities and return selected fields
|
|
1313
|
+
* await db.user.updateManyAndReturn({
|
|
1314
|
+
* where: { email: { endsWith: '@zenstack.dev' } },
|
|
1315
|
+
* data: { role: 'ADMIN' },
|
|
1316
|
+
* select: { id: true, email: true }
|
|
1317
|
+
* }); // result: `Array<{ id: string; email: string }>`
|
|
1318
|
+
*
|
|
1319
|
+
* // limit the number of updated entities
|
|
1320
|
+
* await db.user.updateManyAndReturn({
|
|
1321
|
+
* where: { email: { endsWith: '@zenstack.dev' } },
|
|
1322
|
+
* data: { role: 'ADMIN' },
|
|
1323
|
+
* limit: 10
|
|
1324
|
+
* });
|
|
1325
|
+
* ```
|
|
1216
1326
|
*/
|
|
1217
|
-
|
|
1327
|
+
updateManyAndReturn<T extends UpdateManyAndReturnArgs<Schema, Model>>(args: Subset<T, UpdateManyAndReturnArgs<Schema, Model>>): ZenStackPromise<Schema, Simplify<ModelResult<Schema, Model, T>>[]>;
|
|
1218
1328
|
/**
|
|
1219
|
-
*
|
|
1329
|
+
* Creates or updates an entity.
|
|
1330
|
+
* @param args - upsert args
|
|
1331
|
+
* @returns the upserted entity
|
|
1332
|
+
*
|
|
1333
|
+
* @example
|
|
1334
|
+
* ```ts
|
|
1335
|
+
* // upsert an entity
|
|
1336
|
+
* await db.user.upsert({
|
|
1337
|
+
* // `where` clause is used to find the entity
|
|
1338
|
+
* where: { id: 1 },
|
|
1339
|
+
* // `create` clause is used if the entity is not found
|
|
1340
|
+
* create: { email: 'alex@zenstack.dev', name: 'Alex' },
|
|
1341
|
+
* // `update` clause is used if the entity is found
|
|
1342
|
+
* update: { name: 'Alex-new' },
|
|
1343
|
+
* // `select` and `omit` can be used to control the returned fields
|
|
1344
|
+
* ...
|
|
1345
|
+
* });
|
|
1346
|
+
* ```
|
|
1220
1347
|
*/
|
|
1221
|
-
|
|
1348
|
+
upsert<T extends UpsertArgs<Schema, Model>>(args: SelectSubset<T, UpsertArgs<Schema, Model>>): ZenStackPromise<Schema, Simplify<ModelResult<Schema, Model, T>>>;
|
|
1222
1349
|
/**
|
|
1223
|
-
*
|
|
1350
|
+
* Deletes a uniquely identifiable entity.
|
|
1351
|
+
* @param args - delete args
|
|
1352
|
+
* @returns the deleted entity. Throws `NotFoundError` if the entity is not found.
|
|
1353
|
+
*
|
|
1354
|
+
* @example
|
|
1355
|
+
* ```ts
|
|
1356
|
+
* // delete an entity
|
|
1357
|
+
* await db.user.delete({
|
|
1358
|
+
* where: { id: 1 }
|
|
1359
|
+
* });
|
|
1360
|
+
*
|
|
1361
|
+
* // delete an entity and return selected fields
|
|
1362
|
+
* await db.user.delete({
|
|
1363
|
+
* where: { id: 1 },
|
|
1364
|
+
* select: { id: true, email: true }
|
|
1365
|
+
* }); // result: `{ id: string; email: string }`
|
|
1366
|
+
* ```
|
|
1224
1367
|
*/
|
|
1225
|
-
|
|
1368
|
+
delete<T extends DeleteArgs<Schema, Model>>(args: SelectSubset<T, DeleteArgs<Schema, Model>>): ZenStackPromise<Schema, Simplify<ModelResult<Schema, Model, T>>>;
|
|
1226
1369
|
/**
|
|
1227
|
-
*
|
|
1370
|
+
* Deletes multiple entities.
|
|
1371
|
+
* @param args - delete args
|
|
1372
|
+
* @returns count of deleted entities: `{ count: number }`
|
|
1373
|
+
*
|
|
1374
|
+
* @example
|
|
1375
|
+
* ```ts
|
|
1376
|
+
* // delete many entities
|
|
1377
|
+
* await db.user.deleteMany({
|
|
1378
|
+
* where: { email: { endsWith: '@zenstack.dev' } }
|
|
1379
|
+
* });
|
|
1380
|
+
*
|
|
1381
|
+
* // limit the number of deleted entities
|
|
1382
|
+
* await db.user.deleteMany({
|
|
1383
|
+
* where: { email: { endsWith: '@zenstack.dev' } },
|
|
1384
|
+
* limit: 10
|
|
1385
|
+
* });
|
|
1386
|
+
* ```
|
|
1228
1387
|
*/
|
|
1229
|
-
|
|
1388
|
+
deleteMany<T extends DeleteManyArgs<Schema, Model>>(args?: Subset<T, DeleteManyArgs<Schema, Model>>): ZenStackPromise<Schema, BatchResult>;
|
|
1230
1389
|
/**
|
|
1231
|
-
*
|
|
1390
|
+
* Counts rows or field values.
|
|
1391
|
+
* @param args - count args
|
|
1392
|
+
* @returns `number`, or an object containing count of selected relations
|
|
1393
|
+
*
|
|
1394
|
+
* @example
|
|
1395
|
+
* ```ts
|
|
1396
|
+
* // count all
|
|
1397
|
+
* await db.user.count();
|
|
1398
|
+
*
|
|
1399
|
+
* // count with a filter
|
|
1400
|
+
* await db.user.count({ where: { email: { endsWith: '@zenstack.dev' } } });
|
|
1401
|
+
*
|
|
1402
|
+
* // count rows and field values
|
|
1403
|
+
* await db.user.count({
|
|
1404
|
+
* select: { _all: true, email: true }
|
|
1405
|
+
* }); // result: `{ _all: number, email: number }`
|
|
1232
1406
|
*/
|
|
1233
|
-
|
|
1407
|
+
count<T extends CountArgs<Schema, Model>>(args?: Subset<T, CountArgs<Schema, Model>>): ZenStackPromise<Schema, Simplify<CountResult<Schema, Model, T>>>;
|
|
1234
1408
|
/**
|
|
1235
|
-
*
|
|
1409
|
+
* Aggregates rows.
|
|
1410
|
+
* @param args - aggregation args
|
|
1411
|
+
* @returns an object containing aggregated values
|
|
1412
|
+
*
|
|
1413
|
+
* @example
|
|
1414
|
+
* ```ts
|
|
1415
|
+
* // aggregate rows
|
|
1416
|
+
* await db.profile.aggregate({
|
|
1417
|
+
* where: { email: { endsWith: '@zenstack.dev' } },
|
|
1418
|
+
* _count: true,
|
|
1419
|
+
* _avg: { age: true },
|
|
1420
|
+
* _sum: { age: true },
|
|
1421
|
+
* _min: { age: true },
|
|
1422
|
+
* _max: { age: true }
|
|
1423
|
+
* }); // result: `{ _count: number, _avg: { age: number }, ... }`
|
|
1236
1424
|
*/
|
|
1237
|
-
|
|
1425
|
+
aggregate<T extends AggregateArgs<Schema, Model>>(args: Subset<T, AggregateArgs<Schema, Model>>): ZenStackPromise<Schema, Simplify<AggregateResult<Schema, Model, T>>>;
|
|
1238
1426
|
/**
|
|
1239
|
-
*
|
|
1240
|
-
* @
|
|
1427
|
+
* Groups rows by columns.
|
|
1428
|
+
* @param args - groupBy args
|
|
1429
|
+
* @returns an object containing grouped values
|
|
1430
|
+
*
|
|
1431
|
+
* @example
|
|
1432
|
+
* ```ts
|
|
1433
|
+
* // group by a field
|
|
1434
|
+
* await db.profile.groupBy({
|
|
1435
|
+
* by: 'country',
|
|
1436
|
+
* _count: true
|
|
1437
|
+
* }); // result: `Array<{ country: string, _count: number }>`
|
|
1438
|
+
*
|
|
1439
|
+
* // group by multiple fields
|
|
1440
|
+
* await db.profile.groupBy({
|
|
1441
|
+
* by: ['country', 'city'],
|
|
1442
|
+
* _count: true
|
|
1443
|
+
* }); // result: `Array<{ country: string, city: string, _count: number }>`
|
|
1444
|
+
*
|
|
1445
|
+
* // group by with sorting, the `orderBy` fields must be either an aggregation
|
|
1446
|
+
* // or a field used in the `by` list
|
|
1447
|
+
* await db.profile.groupBy({
|
|
1448
|
+
* by: 'country',
|
|
1449
|
+
* orderBy: { country: 'desc' }
|
|
1450
|
+
* });
|
|
1451
|
+
*
|
|
1452
|
+
* // group by with having (post-aggregation filter), the fields used in `having` must
|
|
1453
|
+
* // be either an aggregation, or a field used in the `by` list
|
|
1454
|
+
* await db.profile.groupBy({
|
|
1455
|
+
* by: 'country',
|
|
1456
|
+
* having: { country: 'US', age: { _avg: { gte: 18 } } }
|
|
1457
|
+
* });
|
|
1241
1458
|
*/
|
|
1242
|
-
|
|
1243
|
-
} & {
|
|
1244
|
-
[Key in GetModels<Schema> as Uncapitalize<Key>]: ModelOperations<Schema, Key>;
|
|
1245
|
-
} & Procedures<Schema>;
|
|
1246
|
-
type MapType<Schema extends SchemaDef, T extends string> = T extends 'String' ? string : T extends 'Int' ? number : T extends 'Float' ? number : T extends 'BigInt' ? bigint : T extends 'Decimal' ? Decimal$1 : T extends 'Boolean' ? boolean : T extends 'DateTime' ? Date : T extends GetModels<Schema> ? ModelResult<Schema, T> : unknown;
|
|
1247
|
-
type Procedures<Schema extends SchemaDef> = Schema['procedures'] extends Record<string, ProcedureDef> ? {
|
|
1248
|
-
$procedures: {
|
|
1249
|
-
[Key in keyof Schema['procedures']]: ProcedureFunc<Schema, Schema['procedures'][Key]>;
|
|
1250
|
-
};
|
|
1251
|
-
} : {};
|
|
1252
|
-
type ProcedureFunc<Schema extends SchemaDef, Proc extends ProcedureDef> = (...args: MapProcedureParams<Schema, Proc['params']>) => Promise<MapType<Schema, Proc['returnType']>>;
|
|
1253
|
-
type MapProcedureParams<Schema extends SchemaDef, Params> = {
|
|
1254
|
-
[P in keyof Params]: Params[P] extends {
|
|
1255
|
-
type: infer U;
|
|
1256
|
-
} ? OrUndefinedIf<MapType<Schema, U & string>, Params[P] extends {
|
|
1257
|
-
optional: true;
|
|
1258
|
-
} ? true : false> : never;
|
|
1459
|
+
groupBy<T extends GroupByArgs<Schema, Model>>(args: Subset<T, GroupByArgs<Schema, Model>>): ZenStackPromise<Schema, Simplify<GroupByResult<Schema, Model, T>>>;
|
|
1259
1460
|
};
|
|
1260
|
-
|
|
1261
|
-
* Creates a new ZenStack client instance.
|
|
1262
|
-
*/
|
|
1263
|
-
interface ClientConstructor {
|
|
1264
|
-
new <Schema extends SchemaDef>(schema: HasComputedFields<Schema> extends false ? Schema : never): ClientContract<Schema>;
|
|
1265
|
-
new <Schema extends SchemaDef>(schema: Schema, options: ClientOptions<Schema>): ClientContract<Schema>;
|
|
1266
|
-
}
|
|
1267
|
-
/**
|
|
1268
|
-
* CRUD operations.
|
|
1269
|
-
*/
|
|
1270
|
-
type CRUD = 'create' | 'read' | 'update' | 'delete';
|
|
1461
|
+
type ModelOperations<Schema extends SchemaDef, Model extends GetModels<Schema>> = Omit<AllModelOperations<Schema, Model>, IsDelegateModel<Schema, Model> extends true ? 'create' | 'createMany' | 'createManyAndReturn' | 'upsert' : never>;
|
|
1271
1462
|
|
|
1272
|
-
export type
|
|
1463
|
+
export { type DeleteArgs as A, type BatchResult as B, type ClientConstructor as C, type DateTimeFilter as D, type DeleteManyArgs as E, type FindArgs as F, type CountArgs as G, type CountResult as H, type IncludeInput as I, type JsonArray as J, type AggregateArgs as K, type AggregateResult as L, type ModelResult as M, type NumberFilter as N, type OrderBy as O, type GroupByArgs as P, type GroupByResult as Q, type RuntimePlugin as R, type SimplifiedModelResult as S, type ToKysely as T, type UpdateArgs as U, type OnKyselyQueryArgs as V, type WhereInput as W, type ZenStackPromise as Z, type JsonObject as a, type JsonValue as b, type ClientContract as c, type ClientOptions as d, definePlugin as e, type TypeDefResult as f, type StringFilter as g, type BytesFilter as h, type BooleanFilter as i, type SortOrder as j, type NullsOrder as k, type WhereUniqueInput as l, type OmitInput as m, type SelectIncludeOmit as n, type SelectInput as o, type Subset as p, type SelectSubset as q, type FindManyArgs as r, type FindFirstArgs as s, type FindUniqueArgs as t, type CreateArgs as u, type CreateManyArgs as v, type CreateManyAndReturnArgs as w, type UpdateManyArgs as x, type UpdateManyAndReturnArgs as y, type UpsertArgs as z };
|