@zenstackhq/runtime 3.0.0-alpha.22 → 3.0.0-alpha.24

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,6 +1,6 @@
1
1
  import Decimal, { Decimal as Decimal$1 } from 'decimal.js';
2
2
  import { SchemaDef, GetModels, ScalarFields, ForeignKeyFields, GetModelFields, FieldHasDefault, GetModelFieldType, ModelFieldIsOptional, GetModelField, NonRelationFields, RelationFields, FieldIsArray, RelationFieldType, FieldIsRelation, GetEnums, GetEnum, BuiltinType, GetModel, FieldDef, GetTypeDefs, GetTypeDefFields, GetTypeDefField, TypeDefFieldIsOptional, IsDelegateModel, GetModelDiscriminator, GetSubModels, FieldIsRelationArray, FieldIsDelegateDiscriminator, FieldType, RelationInfo, FieldIsDelegateRelation, DataSourceProviderType, ProcedureDef } from '@zenstackhq/sdk/schema';
3
- import { Generated, Kysely, ExpressionBuilder, OperandExpression, SqlBool, SelectQueryBuilder, Expression, ExpressionWrapper, RootOperationNode, QueryResult, UnknownRow, OperationNode, Dialect, KyselyConfig } from 'kysely';
3
+ import { Generated, Kysely, ExpressionBuilder, OperandExpression, SqlBool, SelectQueryBuilder, Expression, ExpressionWrapper, OperationNode, RootOperationNode, QueryResult, UnknownRow, Dialect, KyselyConfig } from 'kysely';
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;
@@ -605,79 +605,66 @@ declare abstract class BaseCrudDialect<Schema extends SchemaDef> {
605
605
  type CrudOperation = 'findMany' | 'findUnique' | 'findFirst' | 'create' | 'createMany' | 'createManyAndReturn' | 'update' | 'updateMany' | 'updateManyAndReturn' | 'upsert' | 'delete' | 'deleteMany' | 'count' | 'aggregate' | 'groupBy';
606
606
 
607
607
  /**
608
- * The result of the hooks interception filter.
608
+ * ZenStack runtime plugin.
609
609
  */
610
- type MutationInterceptionFilterResult = {
610
+ interface RuntimePlugin<Schema extends SchemaDef = SchemaDef> {
611
611
  /**
612
- * Whether to intercept the mutation or not.
612
+ * Plugin ID.
613
613
  */
614
- intercept: boolean;
614
+ id: string;
615
615
  /**
616
- * Whether entities should be loaded before the mutation.
616
+ * Plugin display name.
617
617
  */
618
- loadBeforeMutationEntity?: boolean;
618
+ name?: string;
619
619
  /**
620
- * Whether entities should be loaded after the mutation.
620
+ * Plugin description.
621
621
  */
622
- loadAfterMutationEntity?: boolean;
623
- };
624
- type MutationHooksArgs<Schema extends SchemaDef> = {
622
+ description?: string;
625
623
  /**
626
- * The model that is being mutated.
624
+ * Intercepts an ORM query.
627
625
  */
628
- model: GetModels<Schema>;
626
+ onQuery?: OnQueryCallback<Schema>;
629
627
  /**
630
- * The mutation action that is being performed.
628
+ * Intercepts an entity mutation.
631
629
  */
632
- action: 'create' | 'update' | 'delete';
630
+ onEntityMutation?: EntityMutationHooksDef<Schema>;
633
631
  /**
634
- * The mutation data. Only available for create and update actions.
632
+ * Intercepts a Kysely query.
635
633
  */
636
- queryNode: OperationNode;
637
- };
638
- type PluginBeforeEntityMutationArgs<Schema extends SchemaDef> = MutationHooksArgs<Schema> & {
639
- entities?: Record<string, unknown>[];
640
- };
641
- type PluginAfterEntityMutationArgs<Schema extends SchemaDef> = MutationHooksArgs<Schema> & {
642
- beforeMutationEntities?: Record<string, unknown>[];
643
- afterMutationEntities?: Record<string, unknown>[];
644
- };
645
- type OnKyselyQueryArgs<Schema extends SchemaDef> = {
646
- kysely: ToKysely<Schema>;
647
- schema: SchemaDef;
648
- client: ClientContract<Schema>;
649
- query: RootOperationNode;
650
- proceed: ProceedKyselyQueryFunction;
651
- };
652
- type ProceedKyselyQueryFunction = (query: RootOperationNode) => Promise<QueryResult<any>>;
653
- type OnKyselyQueryCallback<Schema extends SchemaDef> = (args: OnKyselyQueryArgs<Schema>) => Promise<QueryResult<UnknownRow>>;
654
- type MutationInterceptionFilter<Schema extends SchemaDef> = (args: MutationHooksArgs<Schema>) => MaybePromise<MutationInterceptionFilterResult>;
655
- type BeforeEntityMutationCallback<Schema extends SchemaDef> = (args: PluginBeforeEntityMutationArgs<Schema>) => MaybePromise<void>;
656
- type AfterEntityMutationCallback<Schema extends SchemaDef> = (args: PluginAfterEntityMutationArgs<Schema>) => MaybePromise<void>;
634
+ onKyselyQuery?: OnKyselyQueryCallback<Schema>;
635
+ }
657
636
  /**
658
- * ZenStack runtime plugin.
637
+ * Defines a ZenStack runtime plugin.
659
638
  */
660
- interface RuntimePlugin<Schema extends SchemaDef = SchemaDef> {
639
+ declare function definePlugin<Schema extends SchemaDef>(plugin: RuntimePlugin<Schema>): RuntimePlugin<Schema>;
640
+
641
+ type OnQueryCallback<Schema extends SchemaDef> = (ctx: OnQueryHookContext<Schema>) => Promise<unknown>;
642
+ type OnQueryHookContext<Schema extends SchemaDef> = {
661
643
  /**
662
- * Plugin ID.
644
+ * The model that is being queried.
663
645
  */
664
- id: string;
646
+ model: GetModels<Schema>;
665
647
  /**
666
- * Plugin display name.
648
+ * The operation that is being performed.
667
649
  */
668
- name?: string;
650
+ operation: CrudOperation;
669
651
  /**
670
- * Plugin description.
652
+ * The query arguments.
671
653
  */
672
- description?: string;
654
+ args: unknown;
673
655
  /**
674
- * Intercepts an ORM query.
656
+ * The function to proceed with the original query.
657
+ * It takes the same arguments as the operation method.
658
+ *
659
+ * @param args The query arguments.
675
660
  */
676
- onQuery?: OnQueryHooks<Schema>;
661
+ proceed: (args: unknown) => Promise<unknown>;
677
662
  /**
678
- * Intercepts a Kysely query.
663
+ * The ZenStack client that is performing the operation.
679
664
  */
680
- onKyselyQuery?: OnKyselyQueryCallback<Schema>;
665
+ client: ClientContract<Schema>;
666
+ };
667
+ type EntityMutationHooksDef<Schema extends SchemaDef> = {
681
668
  /**
682
669
  * This callback determines whether a mutation should be intercepted, and if so,
683
670
  * what data should be loaded before and after the mutation.
@@ -685,64 +672,81 @@ interface RuntimePlugin<Schema extends SchemaDef = SchemaDef> {
685
672
  mutationInterceptionFilter?: MutationInterceptionFilter<Schema>;
686
673
  /**
687
674
  * Called before an entity is mutated.
688
- * @param args.entity Only available if `loadBeforeMutationEntity` is set to true in the
675
+ * @param args.entity Only available if `loadBeforeMutationEntities` is set to true in the
689
676
  * return value of {@link RuntimePlugin.mutationInterceptionFilter}.
690
677
  */
691
678
  beforeEntityMutation?: BeforeEntityMutationCallback<Schema>;
692
679
  /**
693
680
  * Called after an entity is mutated.
694
- * @param args.beforeMutationEntity Only available if `loadBeforeMutationEntity` is set to true in the
681
+ * @param args.beforeMutationEntity Only available if `loadBeforeMutationEntities` is set to true in the
695
682
  * return value of {@link RuntimePlugin.mutationInterceptionFilter}.
696
- * @param args.afterMutationEntity Only available if `loadAfterMutationEntity` is set to true in the
683
+ * @param args.afterMutationEntity Only available if `loadAfterMutationEntities` is set to true in the
697
684
  * return value of {@link RuntimePlugin.mutationInterceptionFilter}.
698
685
  */
699
686
  afterEntityMutation?: AfterEntityMutationCallback<Schema>;
700
- }
701
- type OnQueryHooks<Schema extends SchemaDef = SchemaDef> = {
702
- [Model in GetModels<Schema> as Uncapitalize<Model>]?: OnQueryOperationHooks<Schema, Model>;
703
- } & {
704
- $allModels?: OnQueryOperationHooks<Schema, GetModels<Schema>>;
705
687
  };
706
- type OnQueryOperationHooks<Schema extends SchemaDef, Model extends GetModels<Schema>> = {
707
- [Operation in keyof ModelOperations<Schema, Model>]?: (ctx: OnQueryHookContext<Schema, Model, Operation>) => Promise<Awaited<ReturnType<ModelOperations<Schema, Model>[Operation]>>>;
708
- } & {
709
- $allOperations?: (ctx: {
710
- model: Model;
711
- operation: CrudOperation;
712
- args: unknown;
713
- query: (args: unknown) => Promise<unknown>;
714
- client: ClientContract<Schema>;
715
- }) => MaybePromise<unknown>;
688
+ type MutationHooksArgs<Schema extends SchemaDef> = {
689
+ /**
690
+ * The model that is being mutated.
691
+ */
692
+ model: GetModels<Schema>;
693
+ /**
694
+ * The mutation action that is being performed.
695
+ */
696
+ action: 'create' | 'update' | 'delete';
697
+ /**
698
+ * The mutation data. Only available for create and update actions.
699
+ */
700
+ queryNode: OperationNode;
716
701
  };
717
- type OnQueryHookContext<Schema extends SchemaDef, Model extends GetModels<Schema>, Operation extends keyof ModelOperations<Schema, Model>> = {
702
+ type MutationInterceptionFilter<Schema extends SchemaDef> = (args: MutationHooksArgs<Schema>) => MaybePromise<MutationInterceptionFilterResult>;
703
+ /**
704
+ * The result of the hooks interception filter.
705
+ */
706
+ type MutationInterceptionFilterResult = {
718
707
  /**
719
- * The model that is being queried.
708
+ * Whether to intercept the mutation or not.
720
709
  */
721
- model: Model;
710
+ intercept: boolean;
722
711
  /**
723
- * The operation that is being performed.
712
+ * Whether entities should be loaded before the mutation.
724
713
  */
725
- operation: Operation;
714
+ loadBeforeMutationEntities?: boolean;
726
715
  /**
727
- * The query arguments.
716
+ * Whether entities should be loaded after the mutation.
728
717
  */
729
- args: Parameters<ModelOperations<Schema, Model>[Operation]>[0];
718
+ loadAfterMutationEntities?: boolean;
719
+ };
720
+ type BeforeEntityMutationCallback<Schema extends SchemaDef> = (args: PluginBeforeEntityMutationArgs<Schema>) => MaybePromise<void>;
721
+ type AfterEntityMutationCallback<Schema extends SchemaDef> = (args: PluginAfterEntityMutationArgs<Schema>) => MaybePromise<void>;
722
+ type PluginBeforeEntityMutationArgs<Schema extends SchemaDef> = MutationHooksArgs<Schema> & {
730
723
  /**
731
- * The query function to proceed with the original query.
732
- * It takes the same arguments as the operation method.
733
- *
734
- * @param args The query arguments.
724
+ * Entities that are about to be mutated. Only available if `loadBeforeMutationEntities` is set to
725
+ * true in the return value of {@link RuntimePlugin.mutationInterceptionFilter}.
735
726
  */
736
- query: (args: Parameters<ModelOperations<Schema, Model>[Operation]>[0]) => ReturnType<ModelOperations<Schema, Model>[Operation]>;
727
+ entities?: unknown[];
728
+ };
729
+ type PluginAfterEntityMutationArgs<Schema extends SchemaDef> = MutationHooksArgs<Schema> & {
737
730
  /**
738
- * The ZenStack client that is performing the operation.
731
+ * Entities that are about to be mutated. Only available if `loadBeforeMutationEntities` is set to
732
+ * true in the return value of {@link RuntimePlugin.mutationInterceptionFilter}.
739
733
  */
734
+ beforeMutationEntities?: unknown[];
735
+ /**
736
+ * Entities mutated. Only available if `loadAfterMutationEntities` is set to true in the return
737
+ * value of {@link RuntimePlugin.mutationInterceptionFilter}.
738
+ */
739
+ afterMutationEntities?: unknown[];
740
+ };
741
+ type OnKyselyQueryArgs<Schema extends SchemaDef> = {
742
+ kysely: ToKysely<Schema>;
743
+ schema: SchemaDef;
740
744
  client: ClientContract<Schema>;
745
+ query: RootOperationNode;
746
+ proceed: ProceedKyselyQueryFunction;
741
747
  };
742
- /**
743
- * Defines a ZenStack runtime plugin.
744
- */
745
- declare function definePlugin<Schema extends SchemaDef>(plugin: RuntimePlugin<Schema>): RuntimePlugin<Schema>;
748
+ type ProceedKyselyQueryFunction = (query: RootOperationNode) => Promise<QueryResult<any>>;
749
+ type OnKyselyQueryCallback<Schema extends SchemaDef> = (args: OnKyselyQueryArgs<Schema>) => Promise<QueryResult<UnknownRow>>;
746
750
 
747
751
  type ZModelFunctionContext<Schema extends SchemaDef> = {
748
752
  dialect: BaseCrudDialect<Schema>;
@@ -950,7 +954,7 @@ interface ClientConstructor {
950
954
  * CRUD operations.
951
955
  */
952
956
  type CRUD = 'create' | 'read' | 'update' | 'delete';
953
- type ModelOperations<Schema extends SchemaDef, Model extends GetModels<Schema>> = Omit<{
957
+ type AllModelOperations<Schema extends SchemaDef, Model extends GetModels<Schema>> = {
954
958
  /**
955
959
  * Returns a list of entities.
956
960
  * @param args - query args
@@ -1361,7 +1365,7 @@ type ModelOperations<Schema extends SchemaDef, Model extends GetModels<Schema>>
1361
1365
  * }); // result: `{ id: string; email: string }`
1362
1366
  * ```
1363
1367
  */
1364
- delete<T extends DeleteArgs<Schema, Model>>(args: SelectSubset<T, DeleteArgs<Schema, Model>>): ZenStackPromise<Schema, Simplify<ModelResult<Schema, Model>>>;
1368
+ delete<T extends DeleteArgs<Schema, Model>>(args: SelectSubset<T, DeleteArgs<Schema, Model>>): ZenStackPromise<Schema, Simplify<ModelResult<Schema, Model, T>>>;
1365
1369
  /**
1366
1370
  * Deletes multiple entities.
1367
1371
  * @param args - delete args
@@ -1453,6 +1457,7 @@ type ModelOperations<Schema extends SchemaDef, Model extends GetModels<Schema>>
1453
1457
  * });
1454
1458
  */
1455
1459
  groupBy<T extends GroupByArgs<Schema, Model>>(args: Subset<T, GroupByArgs<Schema, Model>>): ZenStackPromise<Schema, Simplify<GroupByResult<Schema, Model, T>>>;
1456
- }, IsDelegateModel<Schema, Model> extends true ? 'create' | 'createMany' | 'createManyAndReturn' | 'upsert' : never>;
1460
+ };
1461
+ type ModelOperations<Schema extends SchemaDef, Model extends GetModels<Schema>> = Omit<AllModelOperations<Schema, Model>, IsDelegateModel<Schema, Model> extends true ? 'create' | 'createMany' | 'createManyAndReturn' | 'upsert' : never>;
1457
1462
 
1458
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 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 };
@@ -1,6 +1,6 @@
1
1
  import Decimal, { Decimal as Decimal$1 } from 'decimal.js';
2
2
  import { SchemaDef, GetModels, ScalarFields, ForeignKeyFields, GetModelFields, FieldHasDefault, GetModelFieldType, ModelFieldIsOptional, GetModelField, NonRelationFields, RelationFields, FieldIsArray, RelationFieldType, FieldIsRelation, GetEnums, GetEnum, BuiltinType, GetModel, FieldDef, GetTypeDefs, GetTypeDefFields, GetTypeDefField, TypeDefFieldIsOptional, IsDelegateModel, GetModelDiscriminator, GetSubModels, FieldIsRelationArray, FieldIsDelegateDiscriminator, FieldType, RelationInfo, FieldIsDelegateRelation, DataSourceProviderType, ProcedureDef } from '@zenstackhq/sdk/schema';
3
- import { Generated, Kysely, ExpressionBuilder, OperandExpression, SqlBool, SelectQueryBuilder, Expression, ExpressionWrapper, RootOperationNode, QueryResult, UnknownRow, OperationNode, Dialect, KyselyConfig } from 'kysely';
3
+ import { Generated, Kysely, ExpressionBuilder, OperandExpression, SqlBool, SelectQueryBuilder, Expression, ExpressionWrapper, OperationNode, RootOperationNode, QueryResult, UnknownRow, Dialect, KyselyConfig } from 'kysely';
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;
@@ -605,79 +605,66 @@ declare abstract class BaseCrudDialect<Schema extends SchemaDef> {
605
605
  type CrudOperation = 'findMany' | 'findUnique' | 'findFirst' | 'create' | 'createMany' | 'createManyAndReturn' | 'update' | 'updateMany' | 'updateManyAndReturn' | 'upsert' | 'delete' | 'deleteMany' | 'count' | 'aggregate' | 'groupBy';
606
606
 
607
607
  /**
608
- * The result of the hooks interception filter.
608
+ * ZenStack runtime plugin.
609
609
  */
610
- type MutationInterceptionFilterResult = {
610
+ interface RuntimePlugin<Schema extends SchemaDef = SchemaDef> {
611
611
  /**
612
- * Whether to intercept the mutation or not.
612
+ * Plugin ID.
613
613
  */
614
- intercept: boolean;
614
+ id: string;
615
615
  /**
616
- * Whether entities should be loaded before the mutation.
616
+ * Plugin display name.
617
617
  */
618
- loadBeforeMutationEntity?: boolean;
618
+ name?: string;
619
619
  /**
620
- * Whether entities should be loaded after the mutation.
620
+ * Plugin description.
621
621
  */
622
- loadAfterMutationEntity?: boolean;
623
- };
624
- type MutationHooksArgs<Schema extends SchemaDef> = {
622
+ description?: string;
625
623
  /**
626
- * The model that is being mutated.
624
+ * Intercepts an ORM query.
627
625
  */
628
- model: GetModels<Schema>;
626
+ onQuery?: OnQueryCallback<Schema>;
629
627
  /**
630
- * The mutation action that is being performed.
628
+ * Intercepts an entity mutation.
631
629
  */
632
- action: 'create' | 'update' | 'delete';
630
+ onEntityMutation?: EntityMutationHooksDef<Schema>;
633
631
  /**
634
- * The mutation data. Only available for create and update actions.
632
+ * Intercepts a Kysely query.
635
633
  */
636
- queryNode: OperationNode;
637
- };
638
- type PluginBeforeEntityMutationArgs<Schema extends SchemaDef> = MutationHooksArgs<Schema> & {
639
- entities?: Record<string, unknown>[];
640
- };
641
- type PluginAfterEntityMutationArgs<Schema extends SchemaDef> = MutationHooksArgs<Schema> & {
642
- beforeMutationEntities?: Record<string, unknown>[];
643
- afterMutationEntities?: Record<string, unknown>[];
644
- };
645
- type OnKyselyQueryArgs<Schema extends SchemaDef> = {
646
- kysely: ToKysely<Schema>;
647
- schema: SchemaDef;
648
- client: ClientContract<Schema>;
649
- query: RootOperationNode;
650
- proceed: ProceedKyselyQueryFunction;
651
- };
652
- type ProceedKyselyQueryFunction = (query: RootOperationNode) => Promise<QueryResult<any>>;
653
- type OnKyselyQueryCallback<Schema extends SchemaDef> = (args: OnKyselyQueryArgs<Schema>) => Promise<QueryResult<UnknownRow>>;
654
- type MutationInterceptionFilter<Schema extends SchemaDef> = (args: MutationHooksArgs<Schema>) => MaybePromise<MutationInterceptionFilterResult>;
655
- type BeforeEntityMutationCallback<Schema extends SchemaDef> = (args: PluginBeforeEntityMutationArgs<Schema>) => MaybePromise<void>;
656
- type AfterEntityMutationCallback<Schema extends SchemaDef> = (args: PluginAfterEntityMutationArgs<Schema>) => MaybePromise<void>;
634
+ onKyselyQuery?: OnKyselyQueryCallback<Schema>;
635
+ }
657
636
  /**
658
- * ZenStack runtime plugin.
637
+ * Defines a ZenStack runtime plugin.
659
638
  */
660
- interface RuntimePlugin<Schema extends SchemaDef = SchemaDef> {
639
+ declare function definePlugin<Schema extends SchemaDef>(plugin: RuntimePlugin<Schema>): RuntimePlugin<Schema>;
640
+
641
+ type OnQueryCallback<Schema extends SchemaDef> = (ctx: OnQueryHookContext<Schema>) => Promise<unknown>;
642
+ type OnQueryHookContext<Schema extends SchemaDef> = {
661
643
  /**
662
- * Plugin ID.
644
+ * The model that is being queried.
663
645
  */
664
- id: string;
646
+ model: GetModels<Schema>;
665
647
  /**
666
- * Plugin display name.
648
+ * The operation that is being performed.
667
649
  */
668
- name?: string;
650
+ operation: CrudOperation;
669
651
  /**
670
- * Plugin description.
652
+ * The query arguments.
671
653
  */
672
- description?: string;
654
+ args: unknown;
673
655
  /**
674
- * Intercepts an ORM query.
656
+ * The function to proceed with the original query.
657
+ * It takes the same arguments as the operation method.
658
+ *
659
+ * @param args The query arguments.
675
660
  */
676
- onQuery?: OnQueryHooks<Schema>;
661
+ proceed: (args: unknown) => Promise<unknown>;
677
662
  /**
678
- * Intercepts a Kysely query.
663
+ * The ZenStack client that is performing the operation.
679
664
  */
680
- onKyselyQuery?: OnKyselyQueryCallback<Schema>;
665
+ client: ClientContract<Schema>;
666
+ };
667
+ type EntityMutationHooksDef<Schema extends SchemaDef> = {
681
668
  /**
682
669
  * This callback determines whether a mutation should be intercepted, and if so,
683
670
  * what data should be loaded before and after the mutation.
@@ -685,64 +672,81 @@ interface RuntimePlugin<Schema extends SchemaDef = SchemaDef> {
685
672
  mutationInterceptionFilter?: MutationInterceptionFilter<Schema>;
686
673
  /**
687
674
  * Called before an entity is mutated.
688
- * @param args.entity Only available if `loadBeforeMutationEntity` is set to true in the
675
+ * @param args.entity Only available if `loadBeforeMutationEntities` is set to true in the
689
676
  * return value of {@link RuntimePlugin.mutationInterceptionFilter}.
690
677
  */
691
678
  beforeEntityMutation?: BeforeEntityMutationCallback<Schema>;
692
679
  /**
693
680
  * Called after an entity is mutated.
694
- * @param args.beforeMutationEntity Only available if `loadBeforeMutationEntity` is set to true in the
681
+ * @param args.beforeMutationEntity Only available if `loadBeforeMutationEntities` is set to true in the
695
682
  * return value of {@link RuntimePlugin.mutationInterceptionFilter}.
696
- * @param args.afterMutationEntity Only available if `loadAfterMutationEntity` is set to true in the
683
+ * @param args.afterMutationEntity Only available if `loadAfterMutationEntities` is set to true in the
697
684
  * return value of {@link RuntimePlugin.mutationInterceptionFilter}.
698
685
  */
699
686
  afterEntityMutation?: AfterEntityMutationCallback<Schema>;
700
- }
701
- type OnQueryHooks<Schema extends SchemaDef = SchemaDef> = {
702
- [Model in GetModels<Schema> as Uncapitalize<Model>]?: OnQueryOperationHooks<Schema, Model>;
703
- } & {
704
- $allModels?: OnQueryOperationHooks<Schema, GetModels<Schema>>;
705
687
  };
706
- type OnQueryOperationHooks<Schema extends SchemaDef, Model extends GetModels<Schema>> = {
707
- [Operation in keyof ModelOperations<Schema, Model>]?: (ctx: OnQueryHookContext<Schema, Model, Operation>) => Promise<Awaited<ReturnType<ModelOperations<Schema, Model>[Operation]>>>;
708
- } & {
709
- $allOperations?: (ctx: {
710
- model: Model;
711
- operation: CrudOperation;
712
- args: unknown;
713
- query: (args: unknown) => Promise<unknown>;
714
- client: ClientContract<Schema>;
715
- }) => MaybePromise<unknown>;
688
+ type MutationHooksArgs<Schema extends SchemaDef> = {
689
+ /**
690
+ * The model that is being mutated.
691
+ */
692
+ model: GetModels<Schema>;
693
+ /**
694
+ * The mutation action that is being performed.
695
+ */
696
+ action: 'create' | 'update' | 'delete';
697
+ /**
698
+ * The mutation data. Only available for create and update actions.
699
+ */
700
+ queryNode: OperationNode;
716
701
  };
717
- type OnQueryHookContext<Schema extends SchemaDef, Model extends GetModels<Schema>, Operation extends keyof ModelOperations<Schema, Model>> = {
702
+ type MutationInterceptionFilter<Schema extends SchemaDef> = (args: MutationHooksArgs<Schema>) => MaybePromise<MutationInterceptionFilterResult>;
703
+ /**
704
+ * The result of the hooks interception filter.
705
+ */
706
+ type MutationInterceptionFilterResult = {
718
707
  /**
719
- * The model that is being queried.
708
+ * Whether to intercept the mutation or not.
720
709
  */
721
- model: Model;
710
+ intercept: boolean;
722
711
  /**
723
- * The operation that is being performed.
712
+ * Whether entities should be loaded before the mutation.
724
713
  */
725
- operation: Operation;
714
+ loadBeforeMutationEntities?: boolean;
726
715
  /**
727
- * The query arguments.
716
+ * Whether entities should be loaded after the mutation.
728
717
  */
729
- args: Parameters<ModelOperations<Schema, Model>[Operation]>[0];
718
+ loadAfterMutationEntities?: boolean;
719
+ };
720
+ type BeforeEntityMutationCallback<Schema extends SchemaDef> = (args: PluginBeforeEntityMutationArgs<Schema>) => MaybePromise<void>;
721
+ type AfterEntityMutationCallback<Schema extends SchemaDef> = (args: PluginAfterEntityMutationArgs<Schema>) => MaybePromise<void>;
722
+ type PluginBeforeEntityMutationArgs<Schema extends SchemaDef> = MutationHooksArgs<Schema> & {
730
723
  /**
731
- * The query function to proceed with the original query.
732
- * It takes the same arguments as the operation method.
733
- *
734
- * @param args The query arguments.
724
+ * Entities that are about to be mutated. Only available if `loadBeforeMutationEntities` is set to
725
+ * true in the return value of {@link RuntimePlugin.mutationInterceptionFilter}.
735
726
  */
736
- query: (args: Parameters<ModelOperations<Schema, Model>[Operation]>[0]) => ReturnType<ModelOperations<Schema, Model>[Operation]>;
727
+ entities?: unknown[];
728
+ };
729
+ type PluginAfterEntityMutationArgs<Schema extends SchemaDef> = MutationHooksArgs<Schema> & {
737
730
  /**
738
- * The ZenStack client that is performing the operation.
731
+ * Entities that are about to be mutated. Only available if `loadBeforeMutationEntities` is set to
732
+ * true in the return value of {@link RuntimePlugin.mutationInterceptionFilter}.
739
733
  */
734
+ beforeMutationEntities?: unknown[];
735
+ /**
736
+ * Entities mutated. Only available if `loadAfterMutationEntities` is set to true in the return
737
+ * value of {@link RuntimePlugin.mutationInterceptionFilter}.
738
+ */
739
+ afterMutationEntities?: unknown[];
740
+ };
741
+ type OnKyselyQueryArgs<Schema extends SchemaDef> = {
742
+ kysely: ToKysely<Schema>;
743
+ schema: SchemaDef;
740
744
  client: ClientContract<Schema>;
745
+ query: RootOperationNode;
746
+ proceed: ProceedKyselyQueryFunction;
741
747
  };
742
- /**
743
- * Defines a ZenStack runtime plugin.
744
- */
745
- declare function definePlugin<Schema extends SchemaDef>(plugin: RuntimePlugin<Schema>): RuntimePlugin<Schema>;
748
+ type ProceedKyselyQueryFunction = (query: RootOperationNode) => Promise<QueryResult<any>>;
749
+ type OnKyselyQueryCallback<Schema extends SchemaDef> = (args: OnKyselyQueryArgs<Schema>) => Promise<QueryResult<UnknownRow>>;
746
750
 
747
751
  type ZModelFunctionContext<Schema extends SchemaDef> = {
748
752
  dialect: BaseCrudDialect<Schema>;
@@ -950,7 +954,7 @@ interface ClientConstructor {
950
954
  * CRUD operations.
951
955
  */
952
956
  type CRUD = 'create' | 'read' | 'update' | 'delete';
953
- type ModelOperations<Schema extends SchemaDef, Model extends GetModels<Schema>> = Omit<{
957
+ type AllModelOperations<Schema extends SchemaDef, Model extends GetModels<Schema>> = {
954
958
  /**
955
959
  * Returns a list of entities.
956
960
  * @param args - query args
@@ -1361,7 +1365,7 @@ type ModelOperations<Schema extends SchemaDef, Model extends GetModels<Schema>>
1361
1365
  * }); // result: `{ id: string; email: string }`
1362
1366
  * ```
1363
1367
  */
1364
- delete<T extends DeleteArgs<Schema, Model>>(args: SelectSubset<T, DeleteArgs<Schema, Model>>): ZenStackPromise<Schema, Simplify<ModelResult<Schema, Model>>>;
1368
+ delete<T extends DeleteArgs<Schema, Model>>(args: SelectSubset<T, DeleteArgs<Schema, Model>>): ZenStackPromise<Schema, Simplify<ModelResult<Schema, Model, T>>>;
1365
1369
  /**
1366
1370
  * Deletes multiple entities.
1367
1371
  * @param args - delete args
@@ -1453,6 +1457,7 @@ type ModelOperations<Schema extends SchemaDef, Model extends GetModels<Schema>>
1453
1457
  * });
1454
1458
  */
1455
1459
  groupBy<T extends GroupByArgs<Schema, Model>>(args: Subset<T, GroupByArgs<Schema, Model>>): ZenStackPromise<Schema, Simplify<GroupByResult<Schema, Model, T>>>;
1456
- }, IsDelegateModel<Schema, Model> extends true ? 'create' | 'createMany' | 'createManyAndReturn' | 'upsert' : never>;
1460
+ };
1461
+ type ModelOperations<Schema extends SchemaDef, Model extends GetModels<Schema>> = Omit<AllModelOperations<Schema, Model>, IsDelegateModel<Schema, Model> extends true ? 'create' | 'createMany' | 'createManyAndReturn' | 'upsert' : never>;
1457
1462
 
1458
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 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 };