|
| 1 | +import type { |
| 2 | + CoreCrudOperations, |
| 3 | + CrudArgsMap, |
| 4 | + CrudReturnMap, |
| 5 | + ExtQueryArgsBase, |
| 6 | + ExtResultBase, |
| 7 | + GetSlicedOperations, |
| 8 | + ModelAllowsCreate, |
| 9 | + OperationsRequiringCreate, |
| 10 | + QueryOptions, |
| 11 | +} from '@zenstackhq/orm'; |
| 12 | +import type { GetModels, SchemaDef } from '@zenstackhq/schema'; |
| 13 | + |
| 14 | +/** |
| 15 | + * Operations available in a sequential transaction. |
| 16 | + */ |
| 17 | +type AllowedTransactionOps< |
| 18 | + Schema extends SchemaDef, |
| 19 | + Model extends GetModels<Schema>, |
| 20 | + Options extends QueryOptions<Schema> = QueryOptions<Schema>, |
| 21 | +> = |
| 22 | + ModelAllowsCreate<Schema, Model> extends true |
| 23 | + ? GetSlicedOperations<Schema, Model, Options> & CoreCrudOperations |
| 24 | + : Exclude<GetSlicedOperations<Schema, Model, Options> & CoreCrudOperations, OperationsRequiringCreate>; |
| 25 | + |
| 26 | +/** |
| 27 | + * Represents a single operation to execute within a sequential transaction. |
| 28 | + * |
| 29 | + * The `model`, `op`, and `args` fields are correlated: `op` is constrained to |
| 30 | + * the CRUD operations available on `model` (respecting `Options['slicing']`), and |
| 31 | + * `args` is typed accordingly. |
| 32 | + */ |
| 33 | +export type TransactionOperation< |
| 34 | + Schema extends SchemaDef, |
| 35 | + Options extends QueryOptions<Schema> = QueryOptions<Schema>, |
| 36 | + ExtQueryArgs extends ExtQueryArgsBase = {}, |
| 37 | + ExtResult extends ExtResultBase<Schema> = {}, |
| 38 | +> = { |
| 39 | + [Model in GetModels<Schema>]: { |
| 40 | + [Op in AllowedTransactionOps<Schema, Model, Options>]: {} extends CrudArgsMap< |
| 41 | + Schema, |
| 42 | + Model, |
| 43 | + Options, |
| 44 | + ExtQueryArgs, |
| 45 | + ExtResult |
| 46 | + >[Op] |
| 47 | + ? { model: Model; op: Op; args?: CrudArgsMap<Schema, Model, Options, ExtQueryArgs, ExtResult>[Op] } |
| 48 | + : { model: Model; op: Op; args: CrudArgsMap<Schema, Model, Options, ExtQueryArgs, ExtResult>[Op] }; |
| 49 | + }[AllowedTransactionOps<Schema, Model, Options>]; |
| 50 | +}[GetModels<Schema>]; |
| 51 | + |
| 52 | +/** |
| 53 | + * Maps each operation in a transaction tuple to its precise result type, preserving |
| 54 | + * per-position typing. |
| 55 | + */ |
| 56 | +export type TransactionResults< |
| 57 | + Schema extends SchemaDef, |
| 58 | + Ops extends readonly TransactionOperation<Schema, any, any, any>[], |
| 59 | + Options extends QueryOptions<Schema> = QueryOptions<Schema>, |
| 60 | + ExtResult extends ExtResultBase<Schema> = {}, |
| 61 | +> = { |
| 62 | + [K in keyof Ops]: Ops[K] extends { model: infer M; op: infer O; args?: infer A } |
| 63 | + ? M extends GetModels<Schema> |
| 64 | + ? O extends keyof CrudReturnMap<Schema, M, A, Options, ExtResult> |
| 65 | + ? CrudReturnMap<Schema, M, A, Options, ExtResult>[O] |
| 66 | + : never |
| 67 | + : never |
| 68 | + : never; |
| 69 | +}; |
| 70 | + |
0 commit comments