@zenstackhq/runtime 3.0.0-alpha.23 → 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
- loadBeforeMutationEntities?: boolean;
618
+ name?: string;
619
619
  /**
620
- * Whether entities should be loaded after the mutation.
620
+ * Plugin description.
621
621
  */
622
- loadAfterMutationEntities?: 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.
@@ -697,52 +684,69 @@ interface RuntimePlugin<Schema extends SchemaDef = SchemaDef> {
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.
709
+ */
710
+ intercept: boolean;
711
+ /**
712
+ * Whether entities should be loaded before the mutation.
720
713
  */
721
- model: Model;
714
+ loadBeforeMutationEntities?: boolean;
722
715
  /**
723
- * The operation that is being performed.
716
+ * Whether entities should be loaded after the mutation.
724
717
  */
725
- operation: Operation;
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> & {
726
723
  /**
727
- * 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}.
728
726
  */
729
- args: Parameters<ModelOperations<Schema, Model>[Operation]>[0];
727
+ entities?: unknown[];
728
+ };
729
+ type PluginAfterEntityMutationArgs<Schema extends SchemaDef> = MutationHooksArgs<Schema> & {
730
730
  /**
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.
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}.
735
733
  */
736
- query: (args: Parameters<ModelOperations<Schema, Model>[Operation]>[0]) => ReturnType<ModelOperations<Schema, Model>[Operation]>;
734
+ beforeMutationEntities?: unknown[];
737
735
  /**
738
- * The ZenStack client that is performing the operation.
736
+ * Entities mutated. Only available if `loadAfterMutationEntities` is set to true in the return
737
+ * value of {@link RuntimePlugin.mutationInterceptionFilter}.
739
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
- loadBeforeMutationEntities?: boolean;
618
+ name?: string;
619
619
  /**
620
- * Whether entities should be loaded after the mutation.
620
+ * Plugin description.
621
621
  */
622
- loadAfterMutationEntities?: 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.
@@ -697,52 +684,69 @@ interface RuntimePlugin<Schema extends SchemaDef = SchemaDef> {
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.
709
+ */
710
+ intercept: boolean;
711
+ /**
712
+ * Whether entities should be loaded before the mutation.
720
713
  */
721
- model: Model;
714
+ loadBeforeMutationEntities?: boolean;
722
715
  /**
723
- * The operation that is being performed.
716
+ * Whether entities should be loaded after the mutation.
724
717
  */
725
- operation: Operation;
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> & {
726
723
  /**
727
- * 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}.
728
726
  */
729
- args: Parameters<ModelOperations<Schema, Model>[Operation]>[0];
727
+ entities?: unknown[];
728
+ };
729
+ type PluginAfterEntityMutationArgs<Schema extends SchemaDef> = MutationHooksArgs<Schema> & {
730
730
  /**
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.
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}.
735
733
  */
736
- query: (args: Parameters<ModelOperations<Schema, Model>[Operation]>[0]) => ReturnType<ModelOperations<Schema, Model>[Operation]>;
734
+ beforeMutationEntities?: unknown[];
737
735
  /**
738
- * The ZenStack client that is performing the operation.
736
+ * Entities mutated. Only available if `loadAfterMutationEntities` is set to true in the return
737
+ * value of {@link RuntimePlugin.mutationInterceptionFilter}.
739
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 };
package/dist/index.cjs CHANGED
@@ -6015,7 +6015,7 @@ ${compiled.parameters.map((p) => (0, import_node_util2.inspect)(p)).join("\n")}`
6015
6015
  return newExecutor;
6016
6016
  }
6017
6017
  get hasMutationHooks() {
6018
- return this.client.$options.plugins?.some((plugin) => plugin.beforeEntityMutation || plugin.afterEntityMutation);
6018
+ return this.client.$options.plugins?.some((plugin) => !!plugin.onEntityMutation);
6019
6019
  }
6020
6020
  getMutationModel(queryNode) {
6021
6021
  return (0, import_ts_pattern16.match)(queryNode).when(import_kysely13.InsertQueryNode.is, (node) => node.into.table.identifier.name).when(import_kysely13.UpdateQueryNode.is, (node) => node.table.table.identifier.name).when(import_kysely13.DeleteQueryNode.is, (node) => {
@@ -6045,10 +6045,14 @@ ${compiled.parameters.map((p) => (0, import_node_util2.inspect)(p)).join("\n")}`
6045
6045
  where: node.where
6046
6046
  })).exhaustive();
6047
6047
  for (const plugin of plugins) {
6048
- if (!plugin.mutationInterceptionFilter) {
6048
+ const onEntityMutation = plugin.onEntityMutation;
6049
+ if (!onEntityMutation) {
6050
+ continue;
6051
+ }
6052
+ if (!onEntityMutation.mutationInterceptionFilter) {
6049
6053
  result.intercept = true;
6050
6054
  } else {
6051
- const filterResult = await plugin.mutationInterceptionFilter({
6055
+ const filterResult = await onEntityMutation.mutationInterceptionFilter({
6052
6056
  model: mutationModel,
6053
6057
  action,
6054
6058
  queryNode
@@ -6080,8 +6084,9 @@ ${compiled.parameters.map((p) => (0, import_node_util2.inspect)(p)).join("\n")}`
6080
6084
  if (this.options.plugins) {
6081
6085
  const mutationModel = this.getMutationModel(queryNode);
6082
6086
  for (const plugin of this.options.plugins) {
6083
- if (plugin.beforeEntityMutation) {
6084
- await plugin.beforeEntityMutation({
6087
+ const onEntityMutation = plugin.onEntityMutation;
6088
+ if (onEntityMutation?.beforeEntityMutation) {
6089
+ await onEntityMutation.beforeEntityMutation({
6085
6090
  model: mutationModel,
6086
6091
  action: mutationInterceptionInfo.action,
6087
6092
  queryNode,
@@ -6097,8 +6102,9 @@ ${compiled.parameters.map((p) => (0, import_node_util2.inspect)(p)).join("\n")}`
6097
6102
  }
6098
6103
  const hooks = [];
6099
6104
  for (const plugin of this.options.plugins ?? []) {
6100
- if (plugin.afterEntityMutation) {
6101
- hooks.push(plugin.afterEntityMutation.bind(plugin));
6105
+ const onEntityMutation = plugin.onEntityMutation;
6106
+ if (onEntityMutation?.afterEntityMutation) {
6107
+ hooks.push(onEntityMutation.afterEntityMutation.bind(plugin));
6102
6108
  }
6103
6109
  }
6104
6110
  if (hooks.length === 0) {
@@ -6896,7 +6902,7 @@ function createModelCrudHandler(client, model, inputValidator, resultProcessor)
6896
6902
  return createZenStackPromise(async (txClient) => {
6897
6903
  let proceed = /* @__PURE__ */ __name(async (_args) => {
6898
6904
  const _handler = txClient ? handler.withClient(txClient) : handler;
6899
- const r = await _handler.handle(operation, _args ?? args);
6905
+ const r = await _handler.handle(operation, _args);
6900
6906
  if (!r && throwIfNoResult) {
6901
6907
  throw new NotFoundError(model);
6902
6908
  }
@@ -6912,27 +6918,18 @@ function createModelCrudHandler(client, model, inputValidator, resultProcessor)
6912
6918
  ...client.$options.plugins ?? []
6913
6919
  ];
6914
6920
  for (const plugin of plugins) {
6915
- if (plugin.onQuery && typeof plugin.onQuery === "object") {
6916
- for (const [_model, modelHooks] of Object.entries(plugin.onQuery)) {
6917
- if (_model === (0, import_common_helpers13.lowerCaseFirst)(model) || _model === "$allModels") {
6918
- if (modelHooks && typeof modelHooks === "object") {
6919
- for (const [op, opHooks] of Object.entries(modelHooks)) {
6920
- if (op === operation || op === "$allOperations") {
6921
- if (typeof opHooks === "function") {
6922
- const _proceed = proceed;
6923
- proceed = /* @__PURE__ */ __name(() => opHooks({
6924
- client,
6925
- model,
6926
- operation,
6927
- args,
6928
- query: _proceed
6929
- }), "proceed");
6930
- }
6931
- }
6932
- }
6933
- }
6934
- }
6935
- }
6921
+ const onQuery = plugin.onQuery;
6922
+ if (onQuery) {
6923
+ const _proceed = proceed;
6924
+ proceed = /* @__PURE__ */ __name((_args) => onQuery({
6925
+ client,
6926
+ model,
6927
+ operation,
6928
+ // reflect the latest override if provided
6929
+ args: _args,
6930
+ // ensure inner overrides are propagated to the previous proceed
6931
+ proceed: /* @__PURE__ */ __name((nextArgs) => _proceed(nextArgs), "proceed")
6932
+ }), "proceed");
6936
6933
  }
6937
6934
  }
6938
6935
  return proceed(args);