@zenstackhq/runtime 3.0.0-alpha.30 → 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.
@@ -96,7 +96,7 @@ type BatchResult = {
96
96
  count: number;
97
97
  };
98
98
  type WhereInput<Schema extends SchemaDef, Model extends GetModels<Schema>, ScalarOnly extends boolean = false, WithAggregations extends boolean = false> = {
99
- [Key in GetModelFields<Schema, Model> as ScalarOnly extends true ? Key extends RelationFields<Schema, Model> ? never : Key : Key]?: Key extends RelationFields<Schema, Model> ? RelationFilter<Schema, Model, Key> : FieldIsArray<Schema, Model, Key> extends true ? ArrayFilter<Schema, GetModelFieldType<Schema, Model, Key>> : GetModelFieldType<Schema, Model, Key> extends GetEnums<Schema> ? EnumFilter<Schema, GetModelFieldType<Schema, Model, Key>, ModelFieldIsOptional<Schema, Model, Key>> : PrimitiveFilter<Schema, GetModelFieldType<Schema, Model, Key>, ModelFieldIsOptional<Schema, Model, Key>, WithAggregations>;
99
+ [Key in GetModelFields<Schema, Model> as ScalarOnly extends true ? Key extends RelationFields<Schema, Model> ? never : Key : Key]?: Key extends RelationFields<Schema, Model> ? RelationFilter<Schema, Model, Key> : FieldIsArray<Schema, Model, Key> extends true ? ArrayFilter<Schema, GetModelFieldType<Schema, Model, Key>> : GetModelFieldType<Schema, Model, Key> extends GetEnums<Schema> ? EnumFilter<Schema, GetModelFieldType<Schema, Model, Key>, ModelFieldIsOptional<Schema, Model, Key>, WithAggregations> : PrimitiveFilter<Schema, GetModelFieldType<Schema, Model, Key>, ModelFieldIsOptional<Schema, Model, Key>, WithAggregations>;
100
100
  } & {
101
101
  $expr?: (eb: ExpressionBuilder<ToKyselySchema<Schema>, Model>) => OperandExpression<SqlBool>;
102
102
  } & {
@@ -104,12 +104,16 @@ type WhereInput<Schema extends SchemaDef, Model extends GetModels<Schema>, Scala
104
104
  OR?: WhereInput<Schema, Model, ScalarOnly>[];
105
105
  NOT?: OrArray<WhereInput<Schema, Model, ScalarOnly>>;
106
106
  };
107
- type EnumFilter<Schema extends SchemaDef, T extends GetEnums<Schema>, Nullable extends boolean> = NullableIf<keyof GetEnum<Schema, T>, Nullable> | {
107
+ type EnumFilter<Schema extends SchemaDef, T extends GetEnums<Schema>, Nullable extends boolean, WithAggregations extends boolean> = NullableIf<keyof GetEnum<Schema, T>, Nullable> | ({
108
108
  equals?: NullableIf<keyof GetEnum<Schema, T>, Nullable>;
109
109
  in?: (keyof GetEnum<Schema, T>)[];
110
110
  notIn?: (keyof GetEnum<Schema, T>)[];
111
- not?: EnumFilter<Schema, T, Nullable>;
112
- };
111
+ not?: EnumFilter<Schema, T, Nullable, WithAggregations>;
112
+ } & (WithAggregations extends true ? {
113
+ _count?: NumberFilter<Schema, 'Int', false, false>;
114
+ _min?: EnumFilter<Schema, T, false, false>;
115
+ _max?: EnumFilter<Schema, T, false, false>;
116
+ } : {}));
113
117
  type ArrayFilter<Schema extends SchemaDef, T extends string> = {
114
118
  equals?: MapScalarType<Schema, T>[] | null;
115
119
  has?: MapScalarType<Schema, T> | null;
@@ -670,24 +674,26 @@ type OnQueryHookContext<Schema extends SchemaDef> = {
670
674
  };
671
675
  type EntityMutationHooksDef<Schema extends SchemaDef> = {
672
676
  /**
673
- * This callback determines whether a mutation should be intercepted, and if so,
674
- * what data should be loaded before and after the mutation.
675
- */
676
- mutationInterceptionFilter?: MutationInterceptionFilter<Schema>;
677
- /**
678
- * Called before an entity is mutated.
679
- * @param args.entity Only available if `loadBeforeMutationEntities` is set to true in the
680
- * return value of {@link RuntimePlugin.mutationInterceptionFilter}.
677
+ * Called before entities are mutated.
681
678
  */
682
679
  beforeEntityMutation?: BeforeEntityMutationCallback<Schema>;
683
680
  /**
684
- * Called after an entity is mutated.
685
- * @param args.beforeMutationEntity Only available if `loadBeforeMutationEntities` is set to true in the
686
- * return value of {@link RuntimePlugin.mutationInterceptionFilter}.
687
- * @param args.afterMutationEntity Only available if `loadAfterMutationEntities` is set to true in the
688
- * return value of {@link RuntimePlugin.mutationInterceptionFilter}.
681
+ * Called after entities are mutated.
689
682
  */
690
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;
691
697
  };
692
698
  type MutationHooksArgs<Schema extends SchemaDef> = {
693
699
  /**
@@ -702,45 +708,41 @@ type MutationHooksArgs<Schema extends SchemaDef> = {
702
708
  * The mutation data. Only available for create and update actions.
703
709
  */
704
710
  queryNode: OperationNode;
705
- };
706
- type MutationInterceptionFilter<Schema extends SchemaDef> = (args: MutationHooksArgs<Schema>) => MaybePromise<MutationInterceptionFilterResult>;
707
- /**
708
- * The result of the hooks interception filter.
709
- */
710
- type MutationInterceptionFilterResult = {
711
- /**
712
- * Whether to intercept the mutation or not.
713
- */
714
- intercept: boolean;
715
- /**
716
- * Whether entities should be loaded before the mutation.
717
- */
718
- loadBeforeMutationEntities?: boolean;
719
711
  /**
720
- * Whether entities should be loaded after the mutation.
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.
721
714
  */
722
- loadAfterMutationEntities?: boolean;
715
+ queryId: string;
723
716
  };
724
717
  type BeforeEntityMutationCallback<Schema extends SchemaDef> = (args: PluginBeforeEntityMutationArgs<Schema>) => MaybePromise<void>;
725
718
  type AfterEntityMutationCallback<Schema extends SchemaDef> = (args: PluginAfterEntityMutationArgs<Schema>) => MaybePromise<void>;
726
719
  type PluginBeforeEntityMutationArgs<Schema extends SchemaDef> = MutationHooksArgs<Schema> & {
727
720
  /**
728
- * Entities that are about to be mutated. Only available if `loadBeforeMutationEntities` is set to
729
- * true in the return value of {@link RuntimePlugin.mutationInterceptionFilter}.
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.
730
723
  */
731
- entities?: unknown[];
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.
731
+ */
732
+ client: ClientContract<Schema>;
732
733
  };
733
734
  type PluginAfterEntityMutationArgs<Schema extends SchemaDef> = MutationHooksArgs<Schema> & {
734
735
  /**
735
- * Entities that are about to be mutated. Only available if `loadBeforeMutationEntities` is set to
736
- * true in the return value of {@link RuntimePlugin.mutationInterceptionFilter}.
736
+ * Loads the entities that have been mutated.
737
737
  */
738
- beforeMutationEntities?: unknown[];
738
+ loadAfterMutationEntities(): Promise<Record<string, unknown>[] | undefined>;
739
739
  /**
740
- * Entities mutated. Only available if `loadAfterMutationEntities` is set to true in the return
741
- * 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.
742
744
  */
743
- afterMutationEntities?: unknown[];
745
+ client: ClientContract<Schema>;
744
746
  };
745
747
  type OnKyselyQueryArgs<Schema extends SchemaDef> = {
746
748
  kysely: ToKysely<Schema>;
@@ -96,7 +96,7 @@ type BatchResult = {
96
96
  count: number;
97
97
  };
98
98
  type WhereInput<Schema extends SchemaDef, Model extends GetModels<Schema>, ScalarOnly extends boolean = false, WithAggregations extends boolean = false> = {
99
- [Key in GetModelFields<Schema, Model> as ScalarOnly extends true ? Key extends RelationFields<Schema, Model> ? never : Key : Key]?: Key extends RelationFields<Schema, Model> ? RelationFilter<Schema, Model, Key> : FieldIsArray<Schema, Model, Key> extends true ? ArrayFilter<Schema, GetModelFieldType<Schema, Model, Key>> : GetModelFieldType<Schema, Model, Key> extends GetEnums<Schema> ? EnumFilter<Schema, GetModelFieldType<Schema, Model, Key>, ModelFieldIsOptional<Schema, Model, Key>> : PrimitiveFilter<Schema, GetModelFieldType<Schema, Model, Key>, ModelFieldIsOptional<Schema, Model, Key>, WithAggregations>;
99
+ [Key in GetModelFields<Schema, Model> as ScalarOnly extends true ? Key extends RelationFields<Schema, Model> ? never : Key : Key]?: Key extends RelationFields<Schema, Model> ? RelationFilter<Schema, Model, Key> : FieldIsArray<Schema, Model, Key> extends true ? ArrayFilter<Schema, GetModelFieldType<Schema, Model, Key>> : GetModelFieldType<Schema, Model, Key> extends GetEnums<Schema> ? EnumFilter<Schema, GetModelFieldType<Schema, Model, Key>, ModelFieldIsOptional<Schema, Model, Key>, WithAggregations> : PrimitiveFilter<Schema, GetModelFieldType<Schema, Model, Key>, ModelFieldIsOptional<Schema, Model, Key>, WithAggregations>;
100
100
  } & {
101
101
  $expr?: (eb: ExpressionBuilder<ToKyselySchema<Schema>, Model>) => OperandExpression<SqlBool>;
102
102
  } & {
@@ -104,12 +104,16 @@ type WhereInput<Schema extends SchemaDef, Model extends GetModels<Schema>, Scala
104
104
  OR?: WhereInput<Schema, Model, ScalarOnly>[];
105
105
  NOT?: OrArray<WhereInput<Schema, Model, ScalarOnly>>;
106
106
  };
107
- type EnumFilter<Schema extends SchemaDef, T extends GetEnums<Schema>, Nullable extends boolean> = NullableIf<keyof GetEnum<Schema, T>, Nullable> | {
107
+ type EnumFilter<Schema extends SchemaDef, T extends GetEnums<Schema>, Nullable extends boolean, WithAggregations extends boolean> = NullableIf<keyof GetEnum<Schema, T>, Nullable> | ({
108
108
  equals?: NullableIf<keyof GetEnum<Schema, T>, Nullable>;
109
109
  in?: (keyof GetEnum<Schema, T>)[];
110
110
  notIn?: (keyof GetEnum<Schema, T>)[];
111
- not?: EnumFilter<Schema, T, Nullable>;
112
- };
111
+ not?: EnumFilter<Schema, T, Nullable, WithAggregations>;
112
+ } & (WithAggregations extends true ? {
113
+ _count?: NumberFilter<Schema, 'Int', false, false>;
114
+ _min?: EnumFilter<Schema, T, false, false>;
115
+ _max?: EnumFilter<Schema, T, false, false>;
116
+ } : {}));
113
117
  type ArrayFilter<Schema extends SchemaDef, T extends string> = {
114
118
  equals?: MapScalarType<Schema, T>[] | null;
115
119
  has?: MapScalarType<Schema, T> | null;
@@ -670,24 +674,26 @@ type OnQueryHookContext<Schema extends SchemaDef> = {
670
674
  };
671
675
  type EntityMutationHooksDef<Schema extends SchemaDef> = {
672
676
  /**
673
- * This callback determines whether a mutation should be intercepted, and if so,
674
- * what data should be loaded before and after the mutation.
675
- */
676
- mutationInterceptionFilter?: MutationInterceptionFilter<Schema>;
677
- /**
678
- * Called before an entity is mutated.
679
- * @param args.entity Only available if `loadBeforeMutationEntities` is set to true in the
680
- * return value of {@link RuntimePlugin.mutationInterceptionFilter}.
677
+ * Called before entities are mutated.
681
678
  */
682
679
  beforeEntityMutation?: BeforeEntityMutationCallback<Schema>;
683
680
  /**
684
- * Called after an entity is mutated.
685
- * @param args.beforeMutationEntity Only available if `loadBeforeMutationEntities` is set to true in the
686
- * return value of {@link RuntimePlugin.mutationInterceptionFilter}.
687
- * @param args.afterMutationEntity Only available if `loadAfterMutationEntities` is set to true in the
688
- * return value of {@link RuntimePlugin.mutationInterceptionFilter}.
681
+ * Called after entities are mutated.
689
682
  */
690
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;
691
697
  };
692
698
  type MutationHooksArgs<Schema extends SchemaDef> = {
693
699
  /**
@@ -702,45 +708,41 @@ type MutationHooksArgs<Schema extends SchemaDef> = {
702
708
  * The mutation data. Only available for create and update actions.
703
709
  */
704
710
  queryNode: OperationNode;
705
- };
706
- type MutationInterceptionFilter<Schema extends SchemaDef> = (args: MutationHooksArgs<Schema>) => MaybePromise<MutationInterceptionFilterResult>;
707
- /**
708
- * The result of the hooks interception filter.
709
- */
710
- type MutationInterceptionFilterResult = {
711
- /**
712
- * Whether to intercept the mutation or not.
713
- */
714
- intercept: boolean;
715
- /**
716
- * Whether entities should be loaded before the mutation.
717
- */
718
- loadBeforeMutationEntities?: boolean;
719
711
  /**
720
- * Whether entities should be loaded after the mutation.
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.
721
714
  */
722
- loadAfterMutationEntities?: boolean;
715
+ queryId: string;
723
716
  };
724
717
  type BeforeEntityMutationCallback<Schema extends SchemaDef> = (args: PluginBeforeEntityMutationArgs<Schema>) => MaybePromise<void>;
725
718
  type AfterEntityMutationCallback<Schema extends SchemaDef> = (args: PluginAfterEntityMutationArgs<Schema>) => MaybePromise<void>;
726
719
  type PluginBeforeEntityMutationArgs<Schema extends SchemaDef> = MutationHooksArgs<Schema> & {
727
720
  /**
728
- * Entities that are about to be mutated. Only available if `loadBeforeMutationEntities` is set to
729
- * true in the return value of {@link RuntimePlugin.mutationInterceptionFilter}.
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.
730
723
  */
731
- entities?: unknown[];
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.
731
+ */
732
+ client: ClientContract<Schema>;
732
733
  };
733
734
  type PluginAfterEntityMutationArgs<Schema extends SchemaDef> = MutationHooksArgs<Schema> & {
734
735
  /**
735
- * Entities that are about to be mutated. Only available if `loadBeforeMutationEntities` is set to
736
- * true in the return value of {@link RuntimePlugin.mutationInterceptionFilter}.
736
+ * Loads the entities that have been mutated.
737
737
  */
738
- beforeMutationEntities?: unknown[];
738
+ loadAfterMutationEntities(): Promise<Record<string, unknown>[] | undefined>;
739
739
  /**
740
- * Entities mutated. Only available if `loadAfterMutationEntities` is set to true in the return
741
- * 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.
742
744
  */
743
- afterMutationEntities?: unknown[];
745
+ client: ClientContract<Schema>;
744
746
  };
745
747
  type OnKyselyQueryArgs<Schema extends SchemaDef> = {
746
748
  kysely: ToKysely<Schema>;