@zenstackhq/runtime 3.0.0-alpha.31 → 3.0.0-alpha.32

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.
@@ -674,24 +674,26 @@ type OnQueryHookContext<Schema extends SchemaDef> = {
674
674
  };
675
675
  type EntityMutationHooksDef<Schema extends SchemaDef> = {
676
676
  /**
677
- * This callback determines whether a mutation should be intercepted, and if so,
678
- * what data should be loaded before and after the mutation.
679
- */
680
- mutationInterceptionFilter?: MutationInterceptionFilter<Schema>;
681
- /**
682
- * Called before an entity is mutated.
683
- * @param args.entity Only available if `loadBeforeMutationEntities` is set to true in the
684
- * return value of {@link RuntimePlugin.mutationInterceptionFilter}.
677
+ * Called before entities are mutated.
685
678
  */
686
679
  beforeEntityMutation?: BeforeEntityMutationCallback<Schema>;
687
680
  /**
688
- * Called after an entity is mutated.
689
- * @param args.beforeMutationEntity Only available if `loadBeforeMutationEntities` is set to true in the
690
- * return value of {@link RuntimePlugin.mutationInterceptionFilter}.
691
- * @param args.afterMutationEntity Only available if `loadAfterMutationEntities` is set to true in the
692
- * return value of {@link RuntimePlugin.mutationInterceptionFilter}.
681
+ * Called after entities are mutated.
693
682
  */
694
683
  afterEntityMutation?: AfterEntityMutationCallback<Schema>;
684
+ /**
685
+ * Whether to run after-mutation hooks within the transaction that performs the mutation.
686
+ *
687
+ * If set to `true`, if the mutation already runs inside a transaction, the callbacks are
688
+ * executed immediately after the mutation within the transaction boundary. If the mutation
689
+ * is not running inside a transaction, a new transaction is created to run both the mutation
690
+ * and the callbacks.
691
+ *
692
+ * If set to `false`, the callbacks are executed after the mutation transaction is committed.
693
+ *
694
+ * Defaults to `false`.
695
+ */
696
+ runAfterMutationWithinTransaction?: boolean;
695
697
  };
696
698
  type MutationHooksArgs<Schema extends SchemaDef> = {
697
699
  /**
@@ -706,45 +708,41 @@ type MutationHooksArgs<Schema extends SchemaDef> = {
706
708
  * The mutation data. Only available for create and update actions.
707
709
  */
708
710
  queryNode: OperationNode;
709
- };
710
- type MutationInterceptionFilter<Schema extends SchemaDef> = (args: MutationHooksArgs<Schema>) => MaybePromise<MutationInterceptionFilterResult>;
711
- /**
712
- * The result of the hooks interception filter.
713
- */
714
- type MutationInterceptionFilterResult = {
715
711
  /**
716
- * Whether to intercept the mutation or not.
712
+ * A query ID that uniquely identifies the mutation operation. You can use it to correlate
713
+ * data between the before and after mutation hooks.
717
714
  */
718
- intercept: boolean;
719
- /**
720
- * Whether entities should be loaded before the mutation.
721
- */
722
- loadBeforeMutationEntities?: boolean;
723
- /**
724
- * Whether entities should be loaded after the mutation.
725
- */
726
- loadAfterMutationEntities?: boolean;
715
+ queryId: string;
727
716
  };
728
717
  type BeforeEntityMutationCallback<Schema extends SchemaDef> = (args: PluginBeforeEntityMutationArgs<Schema>) => MaybePromise<void>;
729
718
  type AfterEntityMutationCallback<Schema extends SchemaDef> = (args: PluginAfterEntityMutationArgs<Schema>) => MaybePromise<void>;
730
719
  type PluginBeforeEntityMutationArgs<Schema extends SchemaDef> = MutationHooksArgs<Schema> & {
731
720
  /**
732
- * Entities that are about to be mutated. Only available if `loadBeforeMutationEntities` is set to
733
- * true in the return value of {@link RuntimePlugin.mutationInterceptionFilter}.
721
+ * Loads the entities that are about to be mutated. The db operation that loads the entities is executed
722
+ * within the same transaction context as the mutation.
723
+ */
724
+ loadBeforeMutationEntities(): Promise<Record<string, unknown>[] | undefined>;
725
+ /**
726
+ * The ZenStack client you can use to perform additional operations. The database operations initiated
727
+ * from this client are executed within the same transaction as the mutation if the mutation is running
728
+ * inside a transaction.
729
+ *
730
+ * Mutations initiated from this client will NOT trigger entity mutation hooks to avoid infinite loops.
734
731
  */
735
- entities?: unknown[];
732
+ client: ClientContract<Schema>;
736
733
  };
737
734
  type PluginAfterEntityMutationArgs<Schema extends SchemaDef> = MutationHooksArgs<Schema> & {
738
735
  /**
739
- * Entities that are about to be mutated. Only available if `loadBeforeMutationEntities` is set to
740
- * true in the return value of {@link RuntimePlugin.mutationInterceptionFilter}.
736
+ * Loads the entities that have been mutated.
741
737
  */
742
- beforeMutationEntities?: unknown[];
738
+ loadAfterMutationEntities(): Promise<Record<string, unknown>[] | undefined>;
743
739
  /**
744
- * Entities mutated. Only available if `loadAfterMutationEntities` is set to true in the return
745
- * value of {@link RuntimePlugin.mutationInterceptionFilter}.
740
+ * The ZenStack client you can use to perform additional operations.
741
+ * See {@link EntityMutationHooksDef.runAfterMutationWithinTransaction} for detailed transaction behavior.
742
+ *
743
+ * Mutations initiated from this client will NOT trigger entity mutation hooks to avoid infinite loops.
746
744
  */
747
- afterMutationEntities?: unknown[];
745
+ client: ClientContract<Schema>;
748
746
  };
749
747
  type OnKyselyQueryArgs<Schema extends SchemaDef> = {
750
748
  kysely: ToKysely<Schema>;
@@ -674,24 +674,26 @@ type OnQueryHookContext<Schema extends SchemaDef> = {
674
674
  };
675
675
  type EntityMutationHooksDef<Schema extends SchemaDef> = {
676
676
  /**
677
- * This callback determines whether a mutation should be intercepted, and if so,
678
- * what data should be loaded before and after the mutation.
679
- */
680
- mutationInterceptionFilter?: MutationInterceptionFilter<Schema>;
681
- /**
682
- * Called before an entity is mutated.
683
- * @param args.entity Only available if `loadBeforeMutationEntities` is set to true in the
684
- * return value of {@link RuntimePlugin.mutationInterceptionFilter}.
677
+ * Called before entities are mutated.
685
678
  */
686
679
  beforeEntityMutation?: BeforeEntityMutationCallback<Schema>;
687
680
  /**
688
- * Called after an entity is mutated.
689
- * @param args.beforeMutationEntity Only available if `loadBeforeMutationEntities` is set to true in the
690
- * return value of {@link RuntimePlugin.mutationInterceptionFilter}.
691
- * @param args.afterMutationEntity Only available if `loadAfterMutationEntities` is set to true in the
692
- * return value of {@link RuntimePlugin.mutationInterceptionFilter}.
681
+ * Called after entities are mutated.
693
682
  */
694
683
  afterEntityMutation?: AfterEntityMutationCallback<Schema>;
684
+ /**
685
+ * Whether to run after-mutation hooks within the transaction that performs the mutation.
686
+ *
687
+ * If set to `true`, if the mutation already runs inside a transaction, the callbacks are
688
+ * executed immediately after the mutation within the transaction boundary. If the mutation
689
+ * is not running inside a transaction, a new transaction is created to run both the mutation
690
+ * and the callbacks.
691
+ *
692
+ * If set to `false`, the callbacks are executed after the mutation transaction is committed.
693
+ *
694
+ * Defaults to `false`.
695
+ */
696
+ runAfterMutationWithinTransaction?: boolean;
695
697
  };
696
698
  type MutationHooksArgs<Schema extends SchemaDef> = {
697
699
  /**
@@ -706,45 +708,41 @@ type MutationHooksArgs<Schema extends SchemaDef> = {
706
708
  * The mutation data. Only available for create and update actions.
707
709
  */
708
710
  queryNode: OperationNode;
709
- };
710
- type MutationInterceptionFilter<Schema extends SchemaDef> = (args: MutationHooksArgs<Schema>) => MaybePromise<MutationInterceptionFilterResult>;
711
- /**
712
- * The result of the hooks interception filter.
713
- */
714
- type MutationInterceptionFilterResult = {
715
711
  /**
716
- * Whether to intercept the mutation or not.
712
+ * A query ID that uniquely identifies the mutation operation. You can use it to correlate
713
+ * data between the before and after mutation hooks.
717
714
  */
718
- intercept: boolean;
719
- /**
720
- * Whether entities should be loaded before the mutation.
721
- */
722
- loadBeforeMutationEntities?: boolean;
723
- /**
724
- * Whether entities should be loaded after the mutation.
725
- */
726
- loadAfterMutationEntities?: boolean;
715
+ queryId: string;
727
716
  };
728
717
  type BeforeEntityMutationCallback<Schema extends SchemaDef> = (args: PluginBeforeEntityMutationArgs<Schema>) => MaybePromise<void>;
729
718
  type AfterEntityMutationCallback<Schema extends SchemaDef> = (args: PluginAfterEntityMutationArgs<Schema>) => MaybePromise<void>;
730
719
  type PluginBeforeEntityMutationArgs<Schema extends SchemaDef> = MutationHooksArgs<Schema> & {
731
720
  /**
732
- * Entities that are about to be mutated. Only available if `loadBeforeMutationEntities` is set to
733
- * true in the return value of {@link RuntimePlugin.mutationInterceptionFilter}.
721
+ * Loads the entities that are about to be mutated. The db operation that loads the entities is executed
722
+ * within the same transaction context as the mutation.
723
+ */
724
+ loadBeforeMutationEntities(): Promise<Record<string, unknown>[] | undefined>;
725
+ /**
726
+ * The ZenStack client you can use to perform additional operations. The database operations initiated
727
+ * from this client are executed within the same transaction as the mutation if the mutation is running
728
+ * inside a transaction.
729
+ *
730
+ * Mutations initiated from this client will NOT trigger entity mutation hooks to avoid infinite loops.
734
731
  */
735
- entities?: unknown[];
732
+ client: ClientContract<Schema>;
736
733
  };
737
734
  type PluginAfterEntityMutationArgs<Schema extends SchemaDef> = MutationHooksArgs<Schema> & {
738
735
  /**
739
- * Entities that are about to be mutated. Only available if `loadBeforeMutationEntities` is set to
740
- * true in the return value of {@link RuntimePlugin.mutationInterceptionFilter}.
736
+ * Loads the entities that have been mutated.
741
737
  */
742
- beforeMutationEntities?: unknown[];
738
+ loadAfterMutationEntities(): Promise<Record<string, unknown>[] | undefined>;
743
739
  /**
744
- * Entities mutated. Only available if `loadAfterMutationEntities` is set to true in the return
745
- * value of {@link RuntimePlugin.mutationInterceptionFilter}.
740
+ * The ZenStack client you can use to perform additional operations.
741
+ * See {@link EntityMutationHooksDef.runAfterMutationWithinTransaction} for detailed transaction behavior.
742
+ *
743
+ * Mutations initiated from this client will NOT trigger entity mutation hooks to avoid infinite loops.
746
744
  */
747
- afterMutationEntities?: unknown[];
745
+ client: ClientContract<Schema>;
748
746
  };
749
747
  type OnKyselyQueryArgs<Schema extends SchemaDef> = {
750
748
  kysely: ToKysely<Schema>;