@zenstackhq/runtime 3.0.0-beta.1 → 3.0.0-beta.10
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-CusA0mQO.d.cts → auth-CzM6GLw6.d.cts} +17 -960
- package/dist/{contract-CusA0mQO.d.ts → auth-CzM6GLw6.d.ts} +17 -960
- package/dist/index.cjs +5204 -5406
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +1288 -11
- package/dist/index.d.ts +1288 -11
- package/dist/index.js +5194 -5406
- package/dist/index.js.map +1 -1
- package/dist/schema.cjs +3 -0
- package/dist/schema.cjs.map +1 -1
- package/dist/schema.d.cts +3 -0
- package/dist/schema.d.ts +3 -0
- package/dist/schema.js +3 -0
- package/dist/schema.js.map +1 -1
- package/package.json +13 -16
- package/dist/plugins/policy/index.cjs +0 -2650
- package/dist/plugins/policy/index.cjs.map +0 -1
- package/dist/plugins/policy/index.d.cts +0 -22
- package/dist/plugins/policy/index.d.ts +0 -22
- package/dist/plugins/policy/index.js +0 -2624
- package/dist/plugins/policy/index.js.map +0 -1
- package/dist/plugins/policy/plugin.zmodel +0 -33
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
import
|
|
2
|
-
import {
|
|
3
|
-
import
|
|
1
|
+
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 } from '@zenstackhq/sdk/schema';
|
|
2
|
+
import { Generated, Kysely, ExpressionBuilder, OperandExpression, SqlBool } from 'kysely';
|
|
3
|
+
import Decimal from 'decimal.js';
|
|
4
4
|
|
|
5
5
|
type Optional<T extends object, K extends keyof T = keyof T> = Omit<T, K> & Partial<Pick<T, K>>;
|
|
6
6
|
type NullableIf<T, Condition extends boolean> = Condition extends true ? T | null : T;
|
|
@@ -9,7 +9,7 @@ type _Depth = [never, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9];
|
|
|
9
9
|
type Simplify<T, D extends number = 6> = D extends 0 ? T : T extends object ? T extends _Preserve ? T : {
|
|
10
10
|
[K in keyof T]: Simplify<T[K], _Depth[D]>;
|
|
11
11
|
} & {} : T;
|
|
12
|
-
type WrapType<T, Optional = false, Array = false> = Optional extends true ? T | null :
|
|
12
|
+
type WrapType<T, Optional = false, Array = false> = Array extends true ? Optional extends true ? T[] | null : T[] : Optional extends true ? T | null : T;
|
|
13
13
|
type TypeMap = {
|
|
14
14
|
String: string;
|
|
15
15
|
Boolean: boolean;
|
|
@@ -56,8 +56,8 @@ type ToKyselyTable<Schema extends SchemaDef, Model extends GetModels<Schema>> =
|
|
|
56
56
|
};
|
|
57
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;
|
|
58
58
|
type WrapNull<T, Null> = Null extends true ? T | null : T;
|
|
59
|
-
type MapType
|
|
60
|
-
type toKyselyFieldType<Schema extends SchemaDef, Model extends GetModels<Schema>, Field extends GetModelFields<Schema, Model>> = FieldHasDefault<Schema, Model, Field> extends true ? Generated<MapType
|
|
59
|
+
type MapType<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<Schema, Model, Field>> : MapType<Schema, Model, Field>;
|
|
61
61
|
|
|
62
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
63
|
[Key in NonRelationFields<Schema, Model> as Key extends keyof Omit ? Omit[Key] extends true ? never : Key : Key]: MapModelFieldType<Schema, Model, Key>;
|
|
@@ -203,15 +203,12 @@ type OmitInput<Schema extends SchemaDef, Model extends GetModels<Schema>> = {
|
|
|
203
203
|
};
|
|
204
204
|
type SelectIncludeOmit<Schema extends SchemaDef, Model extends GetModels<Schema>, AllowCount extends boolean> = {
|
|
205
205
|
select?: SelectInput<Schema, Model, AllowCount, boolean>;
|
|
206
|
-
include?: IncludeInput<Schema, Model>;
|
|
206
|
+
include?: IncludeInput<Schema, Model, AllowCount>;
|
|
207
207
|
omit?: OmitInput<Schema, Model>;
|
|
208
208
|
};
|
|
209
209
|
type SelectInput<Schema extends SchemaDef, Model extends GetModels<Schema>, AllowCount extends boolean = true, AllowRelation extends boolean = true> = {
|
|
210
210
|
[Key in NonRelationFields<Schema, Model>]?: boolean;
|
|
211
|
-
} & (AllowRelation extends true ? IncludeInput<Schema, Model> : {})
|
|
212
|
-
(AllowCount extends true ? HasToManyRelations<Schema, Model> extends true ? {
|
|
213
|
-
_count?: SelectCount<Schema, Model>;
|
|
214
|
-
} : {} : {});
|
|
211
|
+
} & (AllowRelation extends true ? IncludeInput<Schema, Model, AllowCount> : {});
|
|
215
212
|
type SelectCount<Schema extends SchemaDef, Model extends GetModels<Schema>> = boolean | {
|
|
216
213
|
select: {
|
|
217
214
|
[Key in RelationFields<Schema, Model> as FieldIsArray<Schema, Model, Key> extends true ? Key : never]?: boolean | {
|
|
@@ -219,9 +216,11 @@ type SelectCount<Schema extends SchemaDef, Model extends GetModels<Schema>> = bo
|
|
|
219
216
|
};
|
|
220
217
|
};
|
|
221
218
|
};
|
|
222
|
-
type IncludeInput<Schema extends SchemaDef, Model extends GetModels<Schema
|
|
219
|
+
type IncludeInput<Schema extends SchemaDef, Model extends GetModels<Schema>, AllowCount extends boolean = true> = {
|
|
223
220
|
[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>;
|
|
224
|
-
}
|
|
221
|
+
} & (AllowCount extends true ? HasToManyRelations<Schema, Model> extends true ? {
|
|
222
|
+
_count?: SelectCount<Schema, Model>;
|
|
223
|
+
} : {} : {});
|
|
225
224
|
type Subset<T, U> = {
|
|
226
225
|
[key in keyof T]: key extends keyof U ? T[key] : never;
|
|
227
226
|
};
|
|
@@ -273,7 +272,7 @@ type FindUniqueArgs<Schema extends SchemaDef, Model extends GetModels<Schema>> =
|
|
|
273
272
|
} & SelectIncludeOmit<Schema, Model, true>;
|
|
274
273
|
type CreateArgs<Schema extends SchemaDef, Model extends GetModels<Schema>> = {
|
|
275
274
|
data: CreateInput<Schema, Model>;
|
|
276
|
-
select?: SelectInput<Schema, Model
|
|
275
|
+
select?: SelectInput<Schema, Model>;
|
|
277
276
|
include?: IncludeInput<Schema, Model>;
|
|
278
277
|
omit?: OmitInput<Schema, Model>;
|
|
279
278
|
};
|
|
@@ -320,7 +319,7 @@ type NestedCreateManyInput<Schema extends SchemaDef, Model extends GetModels<Sch
|
|
|
320
319
|
type UpdateArgs<Schema extends SchemaDef, Model extends GetModels<Schema>> = {
|
|
321
320
|
data: UpdateInput<Schema, Model>;
|
|
322
321
|
where: WhereUniqueInput<Schema, Model>;
|
|
323
|
-
select?: SelectInput<Schema, Model
|
|
322
|
+
select?: SelectInput<Schema, Model>;
|
|
324
323
|
include?: IncludeInput<Schema, Model>;
|
|
325
324
|
omit?: OmitInput<Schema, Model>;
|
|
326
325
|
};
|
|
@@ -338,7 +337,7 @@ type UpsertArgs<Schema extends SchemaDef, Model extends GetModels<Schema>> = {
|
|
|
338
337
|
create: CreateInput<Schema, Model>;
|
|
339
338
|
update: UpdateInput<Schema, Model>;
|
|
340
339
|
where: WhereUniqueInput<Schema, Model>;
|
|
341
|
-
select?: SelectInput<Schema, Model
|
|
340
|
+
select?: SelectInput<Schema, Model>;
|
|
342
341
|
include?: IncludeInput<Schema, Model>;
|
|
343
342
|
omit?: OmitInput<Schema, Model>;
|
|
344
343
|
};
|
|
@@ -385,7 +384,7 @@ type ToOneRelationUpdateInput<Schema extends SchemaDef, Model extends GetModels<
|
|
|
385
384
|
} : {}), FieldIsDelegateRelation<Schema, Model, Field> extends true ? 'create' | 'connectOrCreate' | 'upsert' : never>;
|
|
386
385
|
type DeleteArgs<Schema extends SchemaDef, Model extends GetModels<Schema>> = {
|
|
387
386
|
where: WhereUniqueInput<Schema, Model>;
|
|
388
|
-
select?: SelectInput<Schema, Model
|
|
387
|
+
select?: SelectInput<Schema, Model>;
|
|
389
388
|
include?: IncludeInput<Schema, Model>;
|
|
390
389
|
omit?: OmitInput<Schema, Model>;
|
|
391
390
|
};
|
|
@@ -522,946 +521,4 @@ type ProviderSupportsDistinct<Schema extends SchemaDef> = Schema['provider']['ty
|
|
|
522
521
|
|
|
523
522
|
type AuthType<Schema extends SchemaDef> = string extends GetModels<Schema> ? Record<string, unknown> : Schema['authType'] extends GetModels<Schema> ? Partial<ModelResult<Schema, Schema['authType']>> : never;
|
|
524
523
|
|
|
525
|
-
|
|
526
|
-
* Client API methods that are not supported in transactions.
|
|
527
|
-
*/
|
|
528
|
-
declare const TRANSACTION_UNSUPPORTED_METHODS: readonly ["$transaction", "$disconnect", "$use"];
|
|
529
|
-
/**
|
|
530
|
-
* Logical combinators used in filters.
|
|
531
|
-
*/
|
|
532
|
-
declare const LOGICAL_COMBINATORS: readonly ["AND", "OR", "NOT"];
|
|
533
|
-
|
|
534
|
-
declare abstract class BaseCrudDialect<Schema extends SchemaDef> {
|
|
535
|
-
protected readonly schema: Schema;
|
|
536
|
-
protected readonly options: ClientOptions<Schema>;
|
|
537
|
-
constructor(schema: Schema, options: ClientOptions<Schema>);
|
|
538
|
-
transformPrimitive(value: unknown, _type: BuiltinType, _forArrayField: boolean): unknown;
|
|
539
|
-
buildSelectModel(eb: ExpressionBuilder<any, any>, model: string, modelAlias: string): SelectQueryBuilder<any, any, {}>;
|
|
540
|
-
buildFilterSortTake(model: GetModels<Schema>, args: FindArgs<Schema, GetModels<Schema>, true>, query: SelectQueryBuilder<any, any, {}>, modelAlias: string): SelectQueryBuilder<any, any, {}>;
|
|
541
|
-
buildFilter(eb: ExpressionBuilder<any, any>, model: string, modelAlias: string, where: boolean | object | undefined): Expression<SqlBool>;
|
|
542
|
-
private buildCursorFilter;
|
|
543
|
-
private isLogicalCombinator;
|
|
544
|
-
protected buildCompositeFilter(eb: ExpressionBuilder<any, any>, model: string, modelAlias: string, key: (typeof LOGICAL_COMBINATORS)[number], payload: any): Expression<SqlBool>;
|
|
545
|
-
private buildRelationFilter;
|
|
546
|
-
private buildToOneRelationFilter;
|
|
547
|
-
private buildToManyRelationFilter;
|
|
548
|
-
private buildArrayFilter;
|
|
549
|
-
buildPrimitiveFilter(eb: ExpressionBuilder<any, any>, fieldRef: Expression<any>, fieldDef: FieldDef, payload: any): Expression<SqlBool>;
|
|
550
|
-
private buildLiteralFilter;
|
|
551
|
-
private buildStandardFilter;
|
|
552
|
-
private buildStringFilter;
|
|
553
|
-
private prepStringCasing;
|
|
554
|
-
private buildNumberFilter;
|
|
555
|
-
private buildBooleanFilter;
|
|
556
|
-
private buildDateTimeFilter;
|
|
557
|
-
private buildBytesFilter;
|
|
558
|
-
private buildEnumFilter;
|
|
559
|
-
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>;
|
|
560
|
-
buildSelectAllFields(model: string, query: SelectQueryBuilder<any, any, any>, omit: Record<string, boolean | undefined> | undefined, modelAlias: string): SelectQueryBuilder<any, any, any>;
|
|
561
|
-
protected buildModelSelect(eb: ExpressionBuilder<any, any>, model: GetModels<Schema>, subQueryAlias: string, payload: true | FindArgs<Schema, GetModels<Schema>, true>, selectAllFields: boolean): SelectQueryBuilder<any, any, {}>;
|
|
562
|
-
buildSelectField(query: SelectQueryBuilder<any, any, any>, model: string, modelAlias: string, field: string): SelectQueryBuilder<any, any, any>;
|
|
563
|
-
buildDelegateJoin(thisModel: string, thisModelAlias: string, otherModelAlias: string, query: SelectQueryBuilder<any, any, any>): SelectQueryBuilder<any, any, any>;
|
|
564
|
-
buildCountJson(model: string, eb: ExpressionBuilder<any, any>, parentAlias: string, payload: any): ExpressionWrapper<any, any, unknown>;
|
|
565
|
-
private negateSort;
|
|
566
|
-
true(eb: ExpressionBuilder<any, any>): Expression<SqlBool>;
|
|
567
|
-
false(eb: ExpressionBuilder<any, any>): Expression<SqlBool>;
|
|
568
|
-
isTrue(expression: Expression<SqlBool>): boolean;
|
|
569
|
-
isFalse(expression: Expression<SqlBool>): boolean;
|
|
570
|
-
protected and(eb: ExpressionBuilder<any, any>, ...args: Expression<SqlBool>[]): Expression<SqlBool>;
|
|
571
|
-
protected or(eb: ExpressionBuilder<any, any>, ...args: Expression<SqlBool>[]): Expression<SqlBool>;
|
|
572
|
-
protected not(eb: ExpressionBuilder<any, any>, ...args: Expression<SqlBool>[]): ExpressionWrapper<any, any, SqlBool>;
|
|
573
|
-
fieldRef(model: string, field: string, eb: ExpressionBuilder<any, any>, modelAlias?: string, inlineComputedField?: boolean): ExpressionWrapper<any, any, unknown>;
|
|
574
|
-
protected canJoinWithoutNestedSelect(modelDef: ModelDef, payload: boolean | FindArgs<Schema, GetModels<Schema>, true>): boolean;
|
|
575
|
-
abstract get provider(): DataSourceProviderType;
|
|
576
|
-
/**
|
|
577
|
-
* Builds selection for a relation field.
|
|
578
|
-
*/
|
|
579
|
-
abstract buildRelationSelection(query: SelectQueryBuilder<any, any, any>, model: string, relationField: string, parentAlias: string, payload: true | FindArgs<Schema, GetModels<Schema>, true>): SelectQueryBuilder<any, any, any>;
|
|
580
|
-
/**
|
|
581
|
-
* Builds skip and take clauses.
|
|
582
|
-
*/
|
|
583
|
-
abstract buildSkipTake(query: SelectQueryBuilder<any, any, any>, skip: number | undefined, take: number | undefined): SelectQueryBuilder<any, any, any>;
|
|
584
|
-
/**
|
|
585
|
-
* Builds an Kysely expression that returns a JSON object for the given key-value pairs.
|
|
586
|
-
*/
|
|
587
|
-
abstract buildJsonObject(eb: ExpressionBuilder<any, any>, value: Record<string, Expression<unknown>>): ExpressionWrapper<any, any, unknown>;
|
|
588
|
-
/**
|
|
589
|
-
* Builds an Kysely expression that returns the length of an array.
|
|
590
|
-
*/
|
|
591
|
-
abstract buildArrayLength(eb: ExpressionBuilder<any, any>, array: Expression<unknown>): ExpressionWrapper<any, any, number>;
|
|
592
|
-
/**
|
|
593
|
-
* Builds an array literal SQL string for the given values.
|
|
594
|
-
*/
|
|
595
|
-
abstract buildArrayLiteralSQL(values: unknown[]): string;
|
|
596
|
-
/**
|
|
597
|
-
* Whether the dialect supports updating with a limit on the number of updated rows.
|
|
598
|
-
*/
|
|
599
|
-
abstract get supportsUpdateWithLimit(): boolean;
|
|
600
|
-
/**
|
|
601
|
-
* Whether the dialect supports deleting with a limit on the number of deleted rows.
|
|
602
|
-
*/
|
|
603
|
-
abstract get supportsDeleteWithLimit(): boolean;
|
|
604
|
-
/**
|
|
605
|
-
* Whether the dialect supports DISTINCT ON.
|
|
606
|
-
*/
|
|
607
|
-
abstract get supportsDistinctOn(): boolean;
|
|
608
|
-
/**
|
|
609
|
-
* Whether the dialect support inserting with `DEFAULT` as field value.
|
|
610
|
-
*/
|
|
611
|
-
abstract get supportInsertWithDefault(): boolean;
|
|
612
|
-
}
|
|
613
|
-
|
|
614
|
-
type CoreCrudOperation = 'findMany' | 'findUnique' | 'findFirst' | 'create' | 'createMany' | 'createManyAndReturn' | 'update' | 'updateMany' | 'updateManyAndReturn' | 'upsert' | 'delete' | 'deleteMany' | 'count' | 'aggregate' | 'groupBy';
|
|
615
|
-
type AllCrudOperation = CoreCrudOperation | 'findUniqueOrThrow' | 'findFirstOrThrow';
|
|
616
|
-
|
|
617
|
-
/**
|
|
618
|
-
* ZenStack runtime plugin.
|
|
619
|
-
*/
|
|
620
|
-
interface RuntimePlugin<Schema extends SchemaDef = SchemaDef> {
|
|
621
|
-
/**
|
|
622
|
-
* Plugin ID.
|
|
623
|
-
*/
|
|
624
|
-
id: string;
|
|
625
|
-
/**
|
|
626
|
-
* Plugin display name.
|
|
627
|
-
*/
|
|
628
|
-
name?: string;
|
|
629
|
-
/**
|
|
630
|
-
* Plugin description.
|
|
631
|
-
*/
|
|
632
|
-
description?: string;
|
|
633
|
-
/**
|
|
634
|
-
* Intercepts an ORM query.
|
|
635
|
-
*/
|
|
636
|
-
onQuery?: OnQueryCallback<Schema>;
|
|
637
|
-
/**
|
|
638
|
-
* Intercepts an entity mutation.
|
|
639
|
-
*/
|
|
640
|
-
onEntityMutation?: EntityMutationHooksDef<Schema>;
|
|
641
|
-
/**
|
|
642
|
-
* Intercepts a Kysely query.
|
|
643
|
-
*/
|
|
644
|
-
onKyselyQuery?: OnKyselyQueryCallback<Schema>;
|
|
645
|
-
}
|
|
646
|
-
/**
|
|
647
|
-
* Defines a ZenStack runtime plugin.
|
|
648
|
-
*/
|
|
649
|
-
declare function definePlugin<Schema extends SchemaDef>(plugin: RuntimePlugin<Schema>): RuntimePlugin<Schema>;
|
|
650
|
-
|
|
651
|
-
type OnQueryCallback<Schema extends SchemaDef> = (ctx: OnQueryHookContext<Schema>) => Promise<unknown>;
|
|
652
|
-
type OnQueryHookContext<Schema extends SchemaDef> = {
|
|
653
|
-
/**
|
|
654
|
-
* The model that is being queried.
|
|
655
|
-
*/
|
|
656
|
-
model: GetModels<Schema>;
|
|
657
|
-
/**
|
|
658
|
-
* The operation that is being performed.
|
|
659
|
-
*/
|
|
660
|
-
operation: AllCrudOperation;
|
|
661
|
-
/**
|
|
662
|
-
* The query arguments.
|
|
663
|
-
*/
|
|
664
|
-
args: unknown;
|
|
665
|
-
/**
|
|
666
|
-
* The function to proceed with the original query.
|
|
667
|
-
* It takes the same arguments as the operation method.
|
|
668
|
-
*
|
|
669
|
-
* @param args The query arguments.
|
|
670
|
-
*/
|
|
671
|
-
proceed: (args: unknown) => Promise<unknown>;
|
|
672
|
-
/**
|
|
673
|
-
* The ZenStack client that is performing the operation.
|
|
674
|
-
*/
|
|
675
|
-
client: ClientContract<Schema>;
|
|
676
|
-
};
|
|
677
|
-
type EntityMutationHooksDef<Schema extends SchemaDef> = {
|
|
678
|
-
/**
|
|
679
|
-
* Called before entities are mutated.
|
|
680
|
-
*/
|
|
681
|
-
beforeEntityMutation?: BeforeEntityMutationCallback<Schema>;
|
|
682
|
-
/**
|
|
683
|
-
* Called after entities are mutated.
|
|
684
|
-
*/
|
|
685
|
-
afterEntityMutation?: AfterEntityMutationCallback<Schema>;
|
|
686
|
-
/**
|
|
687
|
-
* Whether to run after-mutation hooks within the transaction that performs the mutation.
|
|
688
|
-
*
|
|
689
|
-
* If set to `true`, if the mutation already runs inside a transaction, the callbacks are
|
|
690
|
-
* executed immediately after the mutation within the transaction boundary. If the mutation
|
|
691
|
-
* is not running inside a transaction, a new transaction is created to run both the mutation
|
|
692
|
-
* and the callbacks.
|
|
693
|
-
*
|
|
694
|
-
* If set to `false`, the callbacks are executed after the mutation transaction is committed.
|
|
695
|
-
*
|
|
696
|
-
* Defaults to `false`.
|
|
697
|
-
*/
|
|
698
|
-
runAfterMutationWithinTransaction?: boolean;
|
|
699
|
-
};
|
|
700
|
-
type MutationHooksArgs<Schema extends SchemaDef> = {
|
|
701
|
-
/**
|
|
702
|
-
* The model that is being mutated.
|
|
703
|
-
*/
|
|
704
|
-
model: GetModels<Schema>;
|
|
705
|
-
/**
|
|
706
|
-
* The mutation action that is being performed.
|
|
707
|
-
*/
|
|
708
|
-
action: 'create' | 'update' | 'delete';
|
|
709
|
-
/**
|
|
710
|
-
* The mutation data. Only available for create and update actions.
|
|
711
|
-
*/
|
|
712
|
-
queryNode: OperationNode;
|
|
713
|
-
/**
|
|
714
|
-
* A query ID that uniquely identifies the mutation operation. You can use it to correlate
|
|
715
|
-
* data between the before and after mutation hooks.
|
|
716
|
-
*/
|
|
717
|
-
queryId: string;
|
|
718
|
-
};
|
|
719
|
-
type BeforeEntityMutationCallback<Schema extends SchemaDef> = (args: PluginBeforeEntityMutationArgs<Schema>) => MaybePromise<void>;
|
|
720
|
-
type AfterEntityMutationCallback<Schema extends SchemaDef> = (args: PluginAfterEntityMutationArgs<Schema>) => MaybePromise<void>;
|
|
721
|
-
type PluginBeforeEntityMutationArgs<Schema extends SchemaDef> = MutationHooksArgs<Schema> & {
|
|
722
|
-
/**
|
|
723
|
-
* Loads the entities that are about to be mutated. The db operation that loads the entities is executed
|
|
724
|
-
* within the same transaction context as the mutation.
|
|
725
|
-
*/
|
|
726
|
-
loadBeforeMutationEntities(): Promise<Record<string, unknown>[] | undefined>;
|
|
727
|
-
/**
|
|
728
|
-
* The ZenStack client you can use to perform additional operations. The database operations initiated
|
|
729
|
-
* from this client are executed within the same transaction as the mutation if the mutation is running
|
|
730
|
-
* inside a transaction.
|
|
731
|
-
*
|
|
732
|
-
* Mutations initiated from this client will NOT trigger entity mutation hooks to avoid infinite loops.
|
|
733
|
-
*/
|
|
734
|
-
client: ClientContract<Schema>;
|
|
735
|
-
};
|
|
736
|
-
type PluginAfterEntityMutationArgs<Schema extends SchemaDef> = MutationHooksArgs<Schema> & {
|
|
737
|
-
/**
|
|
738
|
-
* Loads the entities that have been mutated.
|
|
739
|
-
*/
|
|
740
|
-
loadAfterMutationEntities(): Promise<Record<string, unknown>[] | undefined>;
|
|
741
|
-
/**
|
|
742
|
-
* The ZenStack client you can use to perform additional operations.
|
|
743
|
-
* See {@link EntityMutationHooksDef.runAfterMutationWithinTransaction} for detailed transaction behavior.
|
|
744
|
-
*
|
|
745
|
-
* Mutations initiated from this client will NOT trigger entity mutation hooks to avoid infinite loops.
|
|
746
|
-
*/
|
|
747
|
-
client: ClientContract<Schema>;
|
|
748
|
-
};
|
|
749
|
-
type OnKyselyQueryArgs<Schema extends SchemaDef> = {
|
|
750
|
-
kysely: ToKysely<Schema>;
|
|
751
|
-
schema: SchemaDef;
|
|
752
|
-
client: ClientContract<Schema>;
|
|
753
|
-
query: RootOperationNode;
|
|
754
|
-
proceed: ProceedKyselyQueryFunction;
|
|
755
|
-
};
|
|
756
|
-
type ProceedKyselyQueryFunction = (query: RootOperationNode) => Promise<QueryResult<any>>;
|
|
757
|
-
type OnKyselyQueryCallback<Schema extends SchemaDef> = (args: OnKyselyQueryArgs<Schema>) => Promise<QueryResult<UnknownRow>>;
|
|
758
|
-
|
|
759
|
-
type ZModelFunctionContext<Schema extends SchemaDef> = {
|
|
760
|
-
dialect: BaseCrudDialect<Schema>;
|
|
761
|
-
model: GetModels<Schema>;
|
|
762
|
-
operation: CRUD;
|
|
763
|
-
};
|
|
764
|
-
type ZModelFunction<Schema extends SchemaDef> = (eb: ExpressionBuilder<ToKyselySchema<Schema>, keyof ToKyselySchema<Schema>>, args: Expression<any>[], context: ZModelFunctionContext<Schema>) => Expression<unknown>;
|
|
765
|
-
/**
|
|
766
|
-
* ZenStack client options.
|
|
767
|
-
*/
|
|
768
|
-
type ClientOptions<Schema extends SchemaDef> = {
|
|
769
|
-
/**
|
|
770
|
-
* Kysely dialect.
|
|
771
|
-
*/
|
|
772
|
-
dialect: Dialect;
|
|
773
|
-
/**
|
|
774
|
-
* Custom function definitions.
|
|
775
|
-
*/
|
|
776
|
-
functions?: Record<string, ZModelFunction<Schema>>;
|
|
777
|
-
/**
|
|
778
|
-
* Plugins.
|
|
779
|
-
*/
|
|
780
|
-
plugins?: RuntimePlugin<Schema>[];
|
|
781
|
-
/**
|
|
782
|
-
* Logging configuration.
|
|
783
|
-
*/
|
|
784
|
-
log?: KyselyConfig['log'];
|
|
785
|
-
} & (HasComputedFields<Schema> extends true ? {
|
|
786
|
-
/**
|
|
787
|
-
* Computed field definitions.
|
|
788
|
-
*/
|
|
789
|
-
computedFields: ComputedFieldsOptions<Schema>;
|
|
790
|
-
} : {}) & (HasProcedures<Schema> extends true ? {
|
|
791
|
-
/**
|
|
792
|
-
* Custom procedure definitions.
|
|
793
|
-
*/
|
|
794
|
-
procedures: ProceduresOptions<Schema>;
|
|
795
|
-
} : {});
|
|
796
|
-
type ComputedFieldsOptions<Schema extends SchemaDef> = {
|
|
797
|
-
[Model in GetModels<Schema> as 'computedFields' extends keyof GetModel<Schema, Model> ? Model : never]: {
|
|
798
|
-
[Field in keyof Schema['models'][Model]['computedFields']]: PrependParameter<ExpressionBuilder<ToKyselySchema<Schema>, Model>, Schema['models'][Model]['computedFields'][Field]>;
|
|
799
|
-
};
|
|
800
|
-
};
|
|
801
|
-
type HasComputedFields<Schema extends SchemaDef> = string extends GetModels<Schema> ? false : keyof ComputedFieldsOptions<Schema> extends never ? false : true;
|
|
802
|
-
type ProceduresOptions<Schema extends SchemaDef> = Schema extends {
|
|
803
|
-
procedures: Record<string, ProcedureDef>;
|
|
804
|
-
} ? {
|
|
805
|
-
[Key in keyof Schema['procedures']]: PrependParameter<ClientContract<Schema>, ProcedureFunc<Schema, Schema['procedures'][Key]>>;
|
|
806
|
-
} : {};
|
|
807
|
-
type HasProcedures<Schema extends SchemaDef> = Schema extends {
|
|
808
|
-
procedures: Record<string, ProcedureDef>;
|
|
809
|
-
} ? true : false;
|
|
810
|
-
|
|
811
|
-
/**
|
|
812
|
-
* A promise that only executes when it's awaited or .then() is called.
|
|
813
|
-
*/
|
|
814
|
-
type ZenStackPromise<Schema extends SchemaDef, T> = Promise<T> & {
|
|
815
|
-
/**
|
|
816
|
-
* @private
|
|
817
|
-
* Callable to get a plain promise.
|
|
818
|
-
*/
|
|
819
|
-
cb: (txClient?: ClientContract<Schema>) => Promise<T>;
|
|
820
|
-
};
|
|
821
|
-
|
|
822
|
-
type TransactionUnsupportedMethods = (typeof TRANSACTION_UNSUPPORTED_METHODS)[number];
|
|
823
|
-
/**
|
|
824
|
-
* Transaction isolation levels.
|
|
825
|
-
*/
|
|
826
|
-
declare enum TransactionIsolationLevel {
|
|
827
|
-
ReadUncommitted = "read uncommitted",
|
|
828
|
-
ReadCommitted = "read committed",
|
|
829
|
-
RepeatableRead = "repeatable read",
|
|
830
|
-
Serializable = "serializable",
|
|
831
|
-
Snapshot = "snapshot"
|
|
832
|
-
}
|
|
833
|
-
/**
|
|
834
|
-
* ZenStack client interface.
|
|
835
|
-
*/
|
|
836
|
-
type ClientContract<Schema extends SchemaDef> = {
|
|
837
|
-
readonly $schema: Schema;
|
|
838
|
-
/**
|
|
839
|
-
* The client options.
|
|
840
|
-
*/
|
|
841
|
-
readonly $options: ClientOptions<Schema>;
|
|
842
|
-
/**
|
|
843
|
-
* Executes a prepared raw query and returns the number of affected rows.
|
|
844
|
-
* @example
|
|
845
|
-
* ```
|
|
846
|
-
* const result = await db.$executeRaw`UPDATE User SET cool = ${true} WHERE email = ${'user@email.com'};`
|
|
847
|
-
* ```
|
|
848
|
-
*/
|
|
849
|
-
$executeRaw(query: TemplateStringsArray, ...values: any[]): ZenStackPromise<Schema, number>;
|
|
850
|
-
/**
|
|
851
|
-
* Executes a raw query and returns the number of affected rows.
|
|
852
|
-
* This method is susceptible to SQL injections.
|
|
853
|
-
* @example
|
|
854
|
-
* ```
|
|
855
|
-
* const result = await db.$executeRawUnsafe('UPDATE User SET cool = $1 WHERE email = $2 ;', true, 'user@email.com')
|
|
856
|
-
* ```
|
|
857
|
-
*/
|
|
858
|
-
$executeRawUnsafe(query: string, ...values: any[]): ZenStackPromise<Schema, number>;
|
|
859
|
-
/**
|
|
860
|
-
* Performs a prepared raw query and returns the `SELECT` data.
|
|
861
|
-
* @example
|
|
862
|
-
* ```
|
|
863
|
-
* const result = await db.$queryRaw`SELECT * FROM User WHERE id = ${1} OR email = ${'user@email.com'};`
|
|
864
|
-
* ```
|
|
865
|
-
*/
|
|
866
|
-
$queryRaw<T = unknown>(query: TemplateStringsArray, ...values: any[]): ZenStackPromise<Schema, T>;
|
|
867
|
-
/**
|
|
868
|
-
* Performs a raw query and returns the `SELECT` data.
|
|
869
|
-
* This method is susceptible to SQL injections.
|
|
870
|
-
* @example
|
|
871
|
-
* ```
|
|
872
|
-
* const result = await db.$queryRawUnsafe('SELECT * FROM User WHERE id = $1 OR email = $2;', 1, 'user@email.com')
|
|
873
|
-
* ```
|
|
874
|
-
*/
|
|
875
|
-
$queryRawUnsafe<T = unknown>(query: string, ...values: any[]): ZenStackPromise<Schema, T>;
|
|
876
|
-
/**
|
|
877
|
-
* The current user identity.
|
|
878
|
-
*/
|
|
879
|
-
get $auth(): AuthType<Schema> | undefined;
|
|
880
|
-
/**
|
|
881
|
-
* Sets the current user identity.
|
|
882
|
-
*/
|
|
883
|
-
$setAuth(auth: AuthType<Schema> | undefined): ClientContract<Schema>;
|
|
884
|
-
/**
|
|
885
|
-
* The Kysely query builder instance.
|
|
886
|
-
*/
|
|
887
|
-
readonly $qb: ToKysely<Schema>;
|
|
888
|
-
/**
|
|
889
|
-
* The raw Kysely query builder without any ZenStack enhancements.
|
|
890
|
-
*/
|
|
891
|
-
readonly $qbRaw: ToKysely<any>;
|
|
892
|
-
/**
|
|
893
|
-
* Starts an interactive transaction.
|
|
894
|
-
*/
|
|
895
|
-
$transaction<T>(callback: (tx: Omit<ClientContract<Schema>, TransactionUnsupportedMethods>) => Promise<T>, options?: {
|
|
896
|
-
isolationLevel?: TransactionIsolationLevel;
|
|
897
|
-
}): Promise<T>;
|
|
898
|
-
/**
|
|
899
|
-
* Starts a sequential transaction.
|
|
900
|
-
*/
|
|
901
|
-
$transaction<P extends ZenStackPromise<Schema, any>[]>(arg: [...P], options?: {
|
|
902
|
-
isolationLevel?: TransactionIsolationLevel;
|
|
903
|
-
}): Promise<UnwrapTuplePromises<P>>;
|
|
904
|
-
/**
|
|
905
|
-
* Returns a new client with the specified plugin installed.
|
|
906
|
-
*/
|
|
907
|
-
$use(plugin: RuntimePlugin<Schema>): ClientContract<Schema>;
|
|
908
|
-
/**
|
|
909
|
-
* Returns a new client with the specified plugin removed.
|
|
910
|
-
*/
|
|
911
|
-
$unuse(pluginId: string): ClientContract<Schema>;
|
|
912
|
-
/**
|
|
913
|
-
* Returns a new client with all plugins removed.
|
|
914
|
-
*/
|
|
915
|
-
$unuseAll(): ClientContract<Schema>;
|
|
916
|
-
/**
|
|
917
|
-
* Disconnects the underlying Kysely instance from the database.
|
|
918
|
-
*/
|
|
919
|
-
$disconnect(): Promise<void>;
|
|
920
|
-
/**
|
|
921
|
-
* Pushes the schema to the database. For testing purposes only.
|
|
922
|
-
* @private
|
|
923
|
-
*/
|
|
924
|
-
$pushSchema(): Promise<void>;
|
|
925
|
-
} & {
|
|
926
|
-
[Key in GetModels<Schema> as Uncapitalize<Key>]: ModelOperations<Schema, Key>;
|
|
927
|
-
} & Procedures<Schema>;
|
|
928
|
-
type _TypeMap = {
|
|
929
|
-
String: string;
|
|
930
|
-
Int: number;
|
|
931
|
-
Float: number;
|
|
932
|
-
BigInt: bigint;
|
|
933
|
-
Decimal: Decimal$1;
|
|
934
|
-
Boolean: boolean;
|
|
935
|
-
DateTime: Date;
|
|
936
|
-
};
|
|
937
|
-
type MapType<Schema extends SchemaDef, T extends string> = T extends keyof _TypeMap ? _TypeMap[T] : T extends GetModels<Schema> ? ModelResult<Schema, T> : unknown;
|
|
938
|
-
type Procedures<Schema extends SchemaDef> = Schema['procedures'] extends Record<string, ProcedureDef> ? {
|
|
939
|
-
$procedures: {
|
|
940
|
-
[Key in keyof Schema['procedures']]: ProcedureFunc<Schema, Schema['procedures'][Key]>;
|
|
941
|
-
};
|
|
942
|
-
} : {};
|
|
943
|
-
type ProcedureFunc<Schema extends SchemaDef, Proc extends ProcedureDef> = (...args: MapProcedureParams<Schema, Proc['params']>) => Promise<MapType<Schema, Proc['returnType']>>;
|
|
944
|
-
type MapProcedureParams<Schema extends SchemaDef, Params> = {
|
|
945
|
-
[P in keyof Params]: Params[P] extends {
|
|
946
|
-
type: infer U;
|
|
947
|
-
} ? OrUndefinedIf<MapType<Schema, U & string>, Params[P] extends {
|
|
948
|
-
optional: true;
|
|
949
|
-
} ? true : false> : never;
|
|
950
|
-
};
|
|
951
|
-
/**
|
|
952
|
-
* Creates a new ZenStack client instance.
|
|
953
|
-
*/
|
|
954
|
-
interface ClientConstructor {
|
|
955
|
-
new <Schema extends SchemaDef>(schema: Schema, options: ClientOptions<Schema>): ClientContract<Schema>;
|
|
956
|
-
}
|
|
957
|
-
/**
|
|
958
|
-
* CRUD operations.
|
|
959
|
-
*/
|
|
960
|
-
type CRUD = 'create' | 'read' | 'update' | 'delete';
|
|
961
|
-
type AllModelOperations<Schema extends SchemaDef, Model extends GetModels<Schema>> = {
|
|
962
|
-
/**
|
|
963
|
-
* Returns a list of entities.
|
|
964
|
-
* @param args - query args
|
|
965
|
-
* @returns a list of entities
|
|
966
|
-
*
|
|
967
|
-
* @example
|
|
968
|
-
* ```ts
|
|
969
|
-
* // find all users and return all scalar fields
|
|
970
|
-
* await db.user.findMany();
|
|
971
|
-
*
|
|
972
|
-
* // find all users with name 'Alex'
|
|
973
|
-
* await db.user.findMany({
|
|
974
|
-
* where: {
|
|
975
|
-
* name: 'Alex'
|
|
976
|
-
* }
|
|
977
|
-
* });
|
|
978
|
-
*
|
|
979
|
-
* // select fields
|
|
980
|
-
* await db.user.findMany({
|
|
981
|
-
* select: {
|
|
982
|
-
* name: true,
|
|
983
|
-
* email: true,
|
|
984
|
-
* }
|
|
985
|
-
* }); // result: `Array<{ name: string, email: string }>`
|
|
986
|
-
*
|
|
987
|
-
* // omit fields
|
|
988
|
-
* await db.user.findMany({
|
|
989
|
-
* omit: {
|
|
990
|
-
* name: true,
|
|
991
|
-
* }
|
|
992
|
-
* }); // result: `Array<{ id: number; email: string; ... }>`
|
|
993
|
-
*
|
|
994
|
-
* // include relations (and all scalar fields)
|
|
995
|
-
* await db.user.findMany({
|
|
996
|
-
* include: {
|
|
997
|
-
* posts: true,
|
|
998
|
-
* }
|
|
999
|
-
* }); // result: `Array<{ ...; posts: Post[] }>`
|
|
1000
|
-
*
|
|
1001
|
-
* // include relations with filter
|
|
1002
|
-
* await db.user.findMany({
|
|
1003
|
-
* include: {
|
|
1004
|
-
* posts: {
|
|
1005
|
-
* where: {
|
|
1006
|
-
* published: true
|
|
1007
|
-
* }
|
|
1008
|
-
* }
|
|
1009
|
-
* }
|
|
1010
|
-
* });
|
|
1011
|
-
*
|
|
1012
|
-
* // pagination and sorting
|
|
1013
|
-
* await db.user.findMany({
|
|
1014
|
-
* skip: 10,
|
|
1015
|
-
* take: 10,
|
|
1016
|
-
* orderBy: [{ name: 'asc' }, { email: 'desc' }],
|
|
1017
|
-
* });
|
|
1018
|
-
*
|
|
1019
|
-
* // pagination with cursor (https://www.prisma.io/docs/orm/prisma-client/queries/pagination#cursor-based-pagination)
|
|
1020
|
-
* await db.user.findMany({
|
|
1021
|
-
* cursor: { id: 10 },
|
|
1022
|
-
* skip: 1,
|
|
1023
|
-
* take: 10,
|
|
1024
|
-
* orderBy: { id: 'asc' },
|
|
1025
|
-
* });
|
|
1026
|
-
*
|
|
1027
|
-
* // distinct
|
|
1028
|
-
* await db.user.findMany({
|
|
1029
|
-
* distinct: ['name']
|
|
1030
|
-
* });
|
|
1031
|
-
*
|
|
1032
|
-
* // count all relations
|
|
1033
|
-
* await db.user.findMany({
|
|
1034
|
-
* _count: true,
|
|
1035
|
-
* }); // result: `{ _count: { posts: number; ... } }`
|
|
1036
|
-
*
|
|
1037
|
-
* // count selected relations
|
|
1038
|
-
* await db.user.findMany({
|
|
1039
|
-
* _count: { select: { posts: true } },
|
|
1040
|
-
* }); // result: `{ _count: { posts: number } }`
|
|
1041
|
-
* ```
|
|
1042
|
-
*/
|
|
1043
|
-
findMany<T extends FindArgs<Schema, Model, true>>(args?: SelectSubset<T, FindArgs<Schema, Model, true>>): ZenStackPromise<Schema, Simplify<ModelResult<Schema, Model, T>>[]>;
|
|
1044
|
-
/**
|
|
1045
|
-
* Returns a uniquely identified entity.
|
|
1046
|
-
* @param args - query args
|
|
1047
|
-
* @returns a single entity or null if not found
|
|
1048
|
-
* @see {@link findMany}
|
|
1049
|
-
*/
|
|
1050
|
-
findUnique<T extends FindUniqueArgs<Schema, Model>>(args: SelectSubset<T, FindUniqueArgs<Schema, Model>>): ZenStackPromise<Schema, Simplify<ModelResult<Schema, Model, T>> | null>;
|
|
1051
|
-
/**
|
|
1052
|
-
* Returns a uniquely identified entity or throws `NotFoundError` if not found.
|
|
1053
|
-
* @param args - query args
|
|
1054
|
-
* @returns a single entity
|
|
1055
|
-
* @see {@link findMany}
|
|
1056
|
-
*/
|
|
1057
|
-
findUniqueOrThrow<T extends FindUniqueArgs<Schema, Model>>(args: SelectSubset<T, FindUniqueArgs<Schema, Model>>): ZenStackPromise<Schema, Simplify<ModelResult<Schema, Model, T>>>;
|
|
1058
|
-
/**
|
|
1059
|
-
* Returns the first entity.
|
|
1060
|
-
* @param args - query args
|
|
1061
|
-
* @returns a single entity or null if not found
|
|
1062
|
-
* @see {@link findMany}
|
|
1063
|
-
*/
|
|
1064
|
-
findFirst<T extends FindArgs<Schema, Model, true>>(args?: SelectSubset<T, FindArgs<Schema, Model, true>>): ZenStackPromise<Schema, Simplify<ModelResult<Schema, Model, T>> | null>;
|
|
1065
|
-
/**
|
|
1066
|
-
* Returns the first entity or throws `NotFoundError` if not found.
|
|
1067
|
-
* @param args - query args
|
|
1068
|
-
* @returns a single entity
|
|
1069
|
-
* @see {@link findMany}
|
|
1070
|
-
*/
|
|
1071
|
-
findFirstOrThrow<T extends FindArgs<Schema, Model, true>>(args?: SelectSubset<T, FindArgs<Schema, Model, true>>): ZenStackPromise<Schema, Simplify<ModelResult<Schema, Model, T>>>;
|
|
1072
|
-
/**
|
|
1073
|
-
* Creates a new entity.
|
|
1074
|
-
* @param args - create args
|
|
1075
|
-
* @returns the created entity
|
|
1076
|
-
*
|
|
1077
|
-
* @example
|
|
1078
|
-
* ```ts
|
|
1079
|
-
* // simple create
|
|
1080
|
-
* await db.user.create({
|
|
1081
|
-
* data: { name: 'Alex', email: 'alex@zenstack.dev' }
|
|
1082
|
-
* });
|
|
1083
|
-
*
|
|
1084
|
-
* // nested create with relation
|
|
1085
|
-
* await db.user.create({
|
|
1086
|
-
* data: {
|
|
1087
|
-
* email: 'alex@zenstack.dev',
|
|
1088
|
-
* posts: { create: { title: 'Hello World' } }
|
|
1089
|
-
* }
|
|
1090
|
-
* });
|
|
1091
|
-
*
|
|
1092
|
-
* // you can use `select`, `omit`, and `include` to control
|
|
1093
|
-
* // the fields returned by the query, as with `findMany`
|
|
1094
|
-
* await db.user.create({
|
|
1095
|
-
* data: {
|
|
1096
|
-
* email: 'alex@zenstack.dev',
|
|
1097
|
-
* posts: { create: { title: 'Hello World' } }
|
|
1098
|
-
* },
|
|
1099
|
-
* include: { posts: true }
|
|
1100
|
-
* }); // result: `{ id: number; posts: Post[] }`
|
|
1101
|
-
*
|
|
1102
|
-
* // connect relations
|
|
1103
|
-
* await db.user.create({
|
|
1104
|
-
* data: {
|
|
1105
|
-
* email: 'alex@zenstack.dev',
|
|
1106
|
-
* posts: { connect: { id: 1 } }
|
|
1107
|
-
* }
|
|
1108
|
-
* });
|
|
1109
|
-
*
|
|
1110
|
-
* // connect relations, and create if not found
|
|
1111
|
-
* await db.user.create({
|
|
1112
|
-
* data: {
|
|
1113
|
-
* email: 'alex@zenstack.dev',
|
|
1114
|
-
* posts: {
|
|
1115
|
-
* connectOrCreate: {
|
|
1116
|
-
* where: { id: 1 },
|
|
1117
|
-
* create: { title: 'Hello World' }
|
|
1118
|
-
* }
|
|
1119
|
-
* }
|
|
1120
|
-
* }
|
|
1121
|
-
* });
|
|
1122
|
-
* ```
|
|
1123
|
-
*/
|
|
1124
|
-
create<T extends CreateArgs<Schema, Model>>(args: SelectSubset<T, CreateArgs<Schema, Model>>): ZenStackPromise<Schema, Simplify<ModelResult<Schema, Model, T>>>;
|
|
1125
|
-
/**
|
|
1126
|
-
* Creates multiple entities. Only scalar fields are allowed.
|
|
1127
|
-
* @param args - create args
|
|
1128
|
-
* @returns count of created entities: `{ count: number }`
|
|
1129
|
-
*
|
|
1130
|
-
* @example
|
|
1131
|
-
* ```ts
|
|
1132
|
-
* // create multiple entities
|
|
1133
|
-
* await db.user.createMany({
|
|
1134
|
-
* data: [
|
|
1135
|
-
* { name: 'Alex', email: 'alex@zenstack.dev' },
|
|
1136
|
-
* { name: 'John', email: 'john@zenstack.dev' }
|
|
1137
|
-
* ]
|
|
1138
|
-
* });
|
|
1139
|
-
*
|
|
1140
|
-
* // skip items that cause unique constraint violation
|
|
1141
|
-
* await db.user.createMany({
|
|
1142
|
-
* data: [
|
|
1143
|
-
* { name: 'Alex', email: 'alex@zenstack.dev' },
|
|
1144
|
-
* { name: 'John', email: 'john@zenstack.dev' }
|
|
1145
|
-
* ],
|
|
1146
|
-
* skipDuplicates: true
|
|
1147
|
-
* });
|
|
1148
|
-
* ```
|
|
1149
|
-
*/
|
|
1150
|
-
createMany<T extends CreateManyArgs<Schema, Model>>(args?: SelectSubset<T, CreateManyArgs<Schema, Model>>): ZenStackPromise<Schema, BatchResult>;
|
|
1151
|
-
/**
|
|
1152
|
-
* Creates multiple entities and returns them.
|
|
1153
|
-
* @param args - create args. See {@link createMany} for input. Use
|
|
1154
|
-
* `select` and `omit` to control the fields returned.
|
|
1155
|
-
* @returns the created entities
|
|
1156
|
-
*
|
|
1157
|
-
* @example
|
|
1158
|
-
* ```ts
|
|
1159
|
-
* // create multiple entities and return selected fields
|
|
1160
|
-
* await db.user.createManyAndReturn({
|
|
1161
|
-
* data: [
|
|
1162
|
-
* { name: 'Alex', email: 'alex@zenstack.dev' },
|
|
1163
|
-
* { name: 'John', email: 'john@zenstack.dev' }
|
|
1164
|
-
* ],
|
|
1165
|
-
* select: { id: true, email: true }
|
|
1166
|
-
* });
|
|
1167
|
-
* ```
|
|
1168
|
-
*/
|
|
1169
|
-
createManyAndReturn<T extends CreateManyAndReturnArgs<Schema, Model>>(args?: SelectSubset<T, CreateManyAndReturnArgs<Schema, Model>>): ZenStackPromise<Schema, Simplify<ModelResult<Schema, Model, T>>[]>;
|
|
1170
|
-
/**
|
|
1171
|
-
* Updates a uniquely identified entity.
|
|
1172
|
-
* @param args - update args. See {@link findMany} for how to control
|
|
1173
|
-
* fields and relations returned.
|
|
1174
|
-
* @returns the updated entity. Throws `NotFoundError` if the entity is not found.
|
|
1175
|
-
*
|
|
1176
|
-
* @example
|
|
1177
|
-
* ```ts
|
|
1178
|
-
* // update fields
|
|
1179
|
-
* await db.user.update({
|
|
1180
|
-
* where: { id: 1 },
|
|
1181
|
-
* data: { name: 'Alex' }
|
|
1182
|
-
* });
|
|
1183
|
-
*
|
|
1184
|
-
* // connect a relation
|
|
1185
|
-
* await db.user.update({
|
|
1186
|
-
* where: { id: 1 },
|
|
1187
|
-
* data: { posts: { connect: { id: 1 } } }
|
|
1188
|
-
* });
|
|
1189
|
-
*
|
|
1190
|
-
* // connect relation, and create if not found
|
|
1191
|
-
* await db.user.update({
|
|
1192
|
-
* where: { id: 1 },
|
|
1193
|
-
* data: {
|
|
1194
|
-
* posts: {
|
|
1195
|
-
* connectOrCreate: {
|
|
1196
|
-
* where: { id: 1 },
|
|
1197
|
-
* create: { title: 'Hello World' }
|
|
1198
|
-
* }
|
|
1199
|
-
* }
|
|
1200
|
-
* }
|
|
1201
|
-
* });
|
|
1202
|
-
*
|
|
1203
|
-
* // create many related entities (only available for one-to-many relations)
|
|
1204
|
-
* await db.user.update({
|
|
1205
|
-
* where: { id: 1 },
|
|
1206
|
-
* data: {
|
|
1207
|
-
* posts: {
|
|
1208
|
-
* createMany: {
|
|
1209
|
-
* data: [{ title: 'Hello World' }, { title: 'Hello World 2' }],
|
|
1210
|
-
* }
|
|
1211
|
-
* }
|
|
1212
|
-
* }
|
|
1213
|
-
* });
|
|
1214
|
-
*
|
|
1215
|
-
* // disconnect a one-to-many relation
|
|
1216
|
-
* await db.user.update({
|
|
1217
|
-
* where: { id: 1 },
|
|
1218
|
-
* data: { posts: { disconnect: { id: 1 } } }
|
|
1219
|
-
* });
|
|
1220
|
-
*
|
|
1221
|
-
* // disconnect a one-to-one relation
|
|
1222
|
-
* await db.user.update({
|
|
1223
|
-
* where: { id: 1 },
|
|
1224
|
-
* data: { profile: { disconnect: true } }
|
|
1225
|
-
* });
|
|
1226
|
-
*
|
|
1227
|
-
* // replace a relation (only available for one-to-many relations)
|
|
1228
|
-
* await db.user.update({
|
|
1229
|
-
* where: { id: 1 },
|
|
1230
|
-
* data: {
|
|
1231
|
-
* posts: {
|
|
1232
|
-
* set: [{ id: 1 }, { id: 2 }]
|
|
1233
|
-
* }
|
|
1234
|
-
* }
|
|
1235
|
-
* });
|
|
1236
|
-
*
|
|
1237
|
-
* // update a relation
|
|
1238
|
-
* await db.user.update({
|
|
1239
|
-
* where: { id: 1 },
|
|
1240
|
-
* data: {
|
|
1241
|
-
* posts: {
|
|
1242
|
-
* update: { where: { id: 1 }, data: { title: 'Hello World' } }
|
|
1243
|
-
* }
|
|
1244
|
-
* }
|
|
1245
|
-
* });
|
|
1246
|
-
*
|
|
1247
|
-
* // upsert a relation
|
|
1248
|
-
* await db.user.update({
|
|
1249
|
-
* where: { id: 1 },
|
|
1250
|
-
* data: {
|
|
1251
|
-
* posts: {
|
|
1252
|
-
* upsert: {
|
|
1253
|
-
* where: { id: 1 },
|
|
1254
|
-
* create: { title: 'Hello World' },
|
|
1255
|
-
* update: { title: 'Hello World' }
|
|
1256
|
-
* }
|
|
1257
|
-
* }
|
|
1258
|
-
* }
|
|
1259
|
-
* });
|
|
1260
|
-
*
|
|
1261
|
-
* // update many related entities (only available for one-to-many relations)
|
|
1262
|
-
* await db.user.update({
|
|
1263
|
-
* where: { id: 1 },
|
|
1264
|
-
* data: {
|
|
1265
|
-
* posts: {
|
|
1266
|
-
* updateMany: {
|
|
1267
|
-
* where: { published: true },
|
|
1268
|
-
* data: { title: 'Hello World' }
|
|
1269
|
-
* }
|
|
1270
|
-
* }
|
|
1271
|
-
* }
|
|
1272
|
-
* });
|
|
1273
|
-
*
|
|
1274
|
-
* // delete a one-to-many relation
|
|
1275
|
-
* await db.user.update({
|
|
1276
|
-
* where: { id: 1 },
|
|
1277
|
-
* data: { posts: { delete: { id: 1 } } }
|
|
1278
|
-
* });
|
|
1279
|
-
*
|
|
1280
|
-
* // delete a one-to-one relation
|
|
1281
|
-
* await db.user.update({
|
|
1282
|
-
* where: { id: 1 },
|
|
1283
|
-
* data: { profile: { delete: true } }
|
|
1284
|
-
* });
|
|
1285
|
-
* ```
|
|
1286
|
-
*/
|
|
1287
|
-
update<T extends UpdateArgs<Schema, Model>>(args: SelectSubset<T, UpdateArgs<Schema, Model>>): ZenStackPromise<Schema, Simplify<ModelResult<Schema, Model, T>>>;
|
|
1288
|
-
/**
|
|
1289
|
-
* Updates multiple entities.
|
|
1290
|
-
* @param args - update args. Only scalar fields are allowed for data.
|
|
1291
|
-
* @returns count of updated entities: `{ count: number }`
|
|
1292
|
-
*
|
|
1293
|
-
* @example
|
|
1294
|
-
* ```ts
|
|
1295
|
-
* // update many entities
|
|
1296
|
-
* await db.user.updateMany({
|
|
1297
|
-
* where: { email: { endsWith: '@zenstack.dev' } },
|
|
1298
|
-
* data: { role: 'ADMIN' }
|
|
1299
|
-
* });
|
|
1300
|
-
*
|
|
1301
|
-
* // limit the number of updated entities
|
|
1302
|
-
* await db.user.updateMany({
|
|
1303
|
-
* where: { email: { endsWith: '@zenstack.dev' } },
|
|
1304
|
-
* data: { role: 'ADMIN' },
|
|
1305
|
-
* limit: 10
|
|
1306
|
-
* });
|
|
1307
|
-
*/
|
|
1308
|
-
updateMany<T extends UpdateManyArgs<Schema, Model>>(args: Subset<T, UpdateManyArgs<Schema, Model>>): ZenStackPromise<Schema, BatchResult>;
|
|
1309
|
-
/**
|
|
1310
|
-
* Updates multiple entities and returns them.
|
|
1311
|
-
* @param args - update args. Only scalar fields are allowed for data.
|
|
1312
|
-
* @returns the updated entities
|
|
1313
|
-
*
|
|
1314
|
-
* @example
|
|
1315
|
-
* ```ts
|
|
1316
|
-
* // update many entities and return selected fields
|
|
1317
|
-
* await db.user.updateManyAndReturn({
|
|
1318
|
-
* where: { email: { endsWith: '@zenstack.dev' } },
|
|
1319
|
-
* data: { role: 'ADMIN' },
|
|
1320
|
-
* select: { id: true, email: true }
|
|
1321
|
-
* }); // result: `Array<{ id: string; email: string }>`
|
|
1322
|
-
*
|
|
1323
|
-
* // limit the number of updated entities
|
|
1324
|
-
* await db.user.updateManyAndReturn({
|
|
1325
|
-
* where: { email: { endsWith: '@zenstack.dev' } },
|
|
1326
|
-
* data: { role: 'ADMIN' },
|
|
1327
|
-
* limit: 10
|
|
1328
|
-
* });
|
|
1329
|
-
* ```
|
|
1330
|
-
*/
|
|
1331
|
-
updateManyAndReturn<T extends UpdateManyAndReturnArgs<Schema, Model>>(args: Subset<T, UpdateManyAndReturnArgs<Schema, Model>>): ZenStackPromise<Schema, Simplify<ModelResult<Schema, Model, T>>[]>;
|
|
1332
|
-
/**
|
|
1333
|
-
* Creates or updates an entity.
|
|
1334
|
-
* @param args - upsert args
|
|
1335
|
-
* @returns the upserted entity
|
|
1336
|
-
*
|
|
1337
|
-
* @example
|
|
1338
|
-
* ```ts
|
|
1339
|
-
* // upsert an entity
|
|
1340
|
-
* await db.user.upsert({
|
|
1341
|
-
* // `where` clause is used to find the entity
|
|
1342
|
-
* where: { id: 1 },
|
|
1343
|
-
* // `create` clause is used if the entity is not found
|
|
1344
|
-
* create: { email: 'alex@zenstack.dev', name: 'Alex' },
|
|
1345
|
-
* // `update` clause is used if the entity is found
|
|
1346
|
-
* update: { name: 'Alex-new' },
|
|
1347
|
-
* // `select` and `omit` can be used to control the returned fields
|
|
1348
|
-
* ...
|
|
1349
|
-
* });
|
|
1350
|
-
* ```
|
|
1351
|
-
*/
|
|
1352
|
-
upsert<T extends UpsertArgs<Schema, Model>>(args: SelectSubset<T, UpsertArgs<Schema, Model>>): ZenStackPromise<Schema, Simplify<ModelResult<Schema, Model, T>>>;
|
|
1353
|
-
/**
|
|
1354
|
-
* Deletes a uniquely identifiable entity.
|
|
1355
|
-
* @param args - delete args
|
|
1356
|
-
* @returns the deleted entity. Throws `NotFoundError` if the entity is not found.
|
|
1357
|
-
*
|
|
1358
|
-
* @example
|
|
1359
|
-
* ```ts
|
|
1360
|
-
* // delete an entity
|
|
1361
|
-
* await db.user.delete({
|
|
1362
|
-
* where: { id: 1 }
|
|
1363
|
-
* });
|
|
1364
|
-
*
|
|
1365
|
-
* // delete an entity and return selected fields
|
|
1366
|
-
* await db.user.delete({
|
|
1367
|
-
* where: { id: 1 },
|
|
1368
|
-
* select: { id: true, email: true }
|
|
1369
|
-
* }); // result: `{ id: string; email: string }`
|
|
1370
|
-
* ```
|
|
1371
|
-
*/
|
|
1372
|
-
delete<T extends DeleteArgs<Schema, Model>>(args: SelectSubset<T, DeleteArgs<Schema, Model>>): ZenStackPromise<Schema, Simplify<ModelResult<Schema, Model, T>>>;
|
|
1373
|
-
/**
|
|
1374
|
-
* Deletes multiple entities.
|
|
1375
|
-
* @param args - delete args
|
|
1376
|
-
* @returns count of deleted entities: `{ count: number }`
|
|
1377
|
-
*
|
|
1378
|
-
* @example
|
|
1379
|
-
* ```ts
|
|
1380
|
-
* // delete many entities
|
|
1381
|
-
* await db.user.deleteMany({
|
|
1382
|
-
* where: { email: { endsWith: '@zenstack.dev' } }
|
|
1383
|
-
* });
|
|
1384
|
-
*
|
|
1385
|
-
* // limit the number of deleted entities
|
|
1386
|
-
* await db.user.deleteMany({
|
|
1387
|
-
* where: { email: { endsWith: '@zenstack.dev' } },
|
|
1388
|
-
* limit: 10
|
|
1389
|
-
* });
|
|
1390
|
-
* ```
|
|
1391
|
-
*/
|
|
1392
|
-
deleteMany<T extends DeleteManyArgs<Schema, Model>>(args?: Subset<T, DeleteManyArgs<Schema, Model>>): ZenStackPromise<Schema, BatchResult>;
|
|
1393
|
-
/**
|
|
1394
|
-
* Counts rows or field values.
|
|
1395
|
-
* @param args - count args
|
|
1396
|
-
* @returns `number`, or an object containing count of selected relations
|
|
1397
|
-
*
|
|
1398
|
-
* @example
|
|
1399
|
-
* ```ts
|
|
1400
|
-
* // count all
|
|
1401
|
-
* await db.user.count();
|
|
1402
|
-
*
|
|
1403
|
-
* // count with a filter
|
|
1404
|
-
* await db.user.count({ where: { email: { endsWith: '@zenstack.dev' } } });
|
|
1405
|
-
*
|
|
1406
|
-
* // count rows and field values
|
|
1407
|
-
* await db.user.count({
|
|
1408
|
-
* select: { _all: true, email: true }
|
|
1409
|
-
* }); // result: `{ _all: number, email: number }`
|
|
1410
|
-
*/
|
|
1411
|
-
count<T extends CountArgs<Schema, Model>>(args?: Subset<T, CountArgs<Schema, Model>>): ZenStackPromise<Schema, Simplify<CountResult<Schema, Model, T>>>;
|
|
1412
|
-
/**
|
|
1413
|
-
* Aggregates rows.
|
|
1414
|
-
* @param args - aggregation args
|
|
1415
|
-
* @returns an object containing aggregated values
|
|
1416
|
-
*
|
|
1417
|
-
* @example
|
|
1418
|
-
* ```ts
|
|
1419
|
-
* // aggregate rows
|
|
1420
|
-
* await db.profile.aggregate({
|
|
1421
|
-
* where: { email: { endsWith: '@zenstack.dev' } },
|
|
1422
|
-
* _count: true,
|
|
1423
|
-
* _avg: { age: true },
|
|
1424
|
-
* _sum: { age: true },
|
|
1425
|
-
* _min: { age: true },
|
|
1426
|
-
* _max: { age: true }
|
|
1427
|
-
* }); // result: `{ _count: number, _avg: { age: number }, ... }`
|
|
1428
|
-
*/
|
|
1429
|
-
aggregate<T extends AggregateArgs<Schema, Model>>(args: Subset<T, AggregateArgs<Schema, Model>>): ZenStackPromise<Schema, Simplify<AggregateResult<Schema, Model, T>>>;
|
|
1430
|
-
/**
|
|
1431
|
-
* Groups rows by columns.
|
|
1432
|
-
* @param args - groupBy args
|
|
1433
|
-
* @returns an object containing grouped values
|
|
1434
|
-
*
|
|
1435
|
-
* @example
|
|
1436
|
-
* ```ts
|
|
1437
|
-
* // group by a field
|
|
1438
|
-
* await db.profile.groupBy({
|
|
1439
|
-
* by: 'country',
|
|
1440
|
-
* _count: true
|
|
1441
|
-
* }); // result: `Array<{ country: string, _count: number }>`
|
|
1442
|
-
*
|
|
1443
|
-
* // group by multiple fields
|
|
1444
|
-
* await db.profile.groupBy({
|
|
1445
|
-
* by: ['country', 'city'],
|
|
1446
|
-
* _count: true
|
|
1447
|
-
* }); // result: `Array<{ country: string, city: string, _count: number }>`
|
|
1448
|
-
*
|
|
1449
|
-
* // group by with sorting, the `orderBy` fields must be either an aggregation
|
|
1450
|
-
* // or a field used in the `by` list
|
|
1451
|
-
* await db.profile.groupBy({
|
|
1452
|
-
* by: 'country',
|
|
1453
|
-
* orderBy: { country: 'desc' }
|
|
1454
|
-
* });
|
|
1455
|
-
*
|
|
1456
|
-
* // group by with having (post-aggregation filter), the fields used in `having` must
|
|
1457
|
-
* // be either an aggregation, or a field used in the `by` list
|
|
1458
|
-
* await db.profile.groupBy({
|
|
1459
|
-
* by: 'country',
|
|
1460
|
-
* having: { country: 'US', age: { _avg: { gte: 18 } } }
|
|
1461
|
-
* });
|
|
1462
|
-
*/
|
|
1463
|
-
groupBy<T extends GroupByArgs<Schema, Model>>(args: Subset<T, GroupByArgs<Schema, Model>>): ZenStackPromise<Schema, Simplify<GroupByResult<Schema, Model, T>>>;
|
|
1464
|
-
};
|
|
1465
|
-
type ModelOperations<Schema extends SchemaDef, Model extends GetModels<Schema>> = Omit<AllModelOperations<Schema, Model>, IsDelegateModel<Schema, Model> extends true ? 'create' | 'createMany' | 'createManyAndReturn' | 'upsert' : never>;
|
|
1466
|
-
|
|
1467
|
-
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 };
|
|
524
|
+
export type { AuthType as A, BatchResult as B, CreateArgs as C, DeleteArgs as D, BooleanFilter as E, FindArgs as F, GroupByArgs as G, SortOrder as H, NullsOrder as I, JsonArray as J, WhereUniqueInput as K, OmitInput as L, MaybePromise as M, NumberFilter as N, OrArray as O, PrependParameter as P, SelectIncludeOmit as Q, SelectInput as R, SelectSubset as S, ToKyselySchema as T, UnwrapTuplePromises as U, IncludeInput as V, WhereInput as W, FindManyArgs as X, FindFirstArgs as Y, OrderBy as a, ToKysely as b, Simplify as c, ModelResult as d, FindUniqueArgs as e, CreateManyArgs as f, CreateManyAndReturnArgs as g, UpdateArgs as h, UpdateManyArgs as i, Subset as j, UpdateManyAndReturnArgs as k, UpsertArgs as l, DeleteManyArgs as m, CountArgs as n, CountResult as o, AggregateArgs as p, AggregateResult as q, GroupByResult as r, OrUndefinedIf as s, JsonObject as t, JsonValue as u, SimplifiedModelResult as v, TypeDefResult as w, StringFilter as x, DateTimeFilter as y, BytesFilter as z };
|