@baseplate-dev/fastify-generators 0.6.4 → 0.6.5

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.
Files changed (130) hide show
  1. package/CHANGELOG.md +21 -0
  2. package/dist/constants/fastify-packages.d.ts +3 -3
  3. package/dist/constants/fastify-packages.js +3 -3
  4. package/dist/generators/auth/auth-roles/auth-roles.generator.d.ts +1 -0
  5. package/dist/generators/auth/auth-roles/auth-roles.generator.d.ts.map +1 -1
  6. package/dist/generators/auth/auth-roles/auth-roles.generator.js +5 -1
  7. package/dist/generators/auth/auth-roles/auth-roles.generator.js.map +1 -1
  8. package/dist/generators/auth/auth-roles/templates/module/constants/auth-roles.constants.ts +4 -3
  9. package/dist/generators/pothos/pothos-prisma-enum/pothos-prisma-enum.generator.d.ts +1 -0
  10. package/dist/generators/pothos/pothos-prisma-enum/pothos-prisma-enum.generator.d.ts.map +1 -1
  11. package/dist/generators/pothos/pothos-prisma-enum/pothos-prisma-enum.generator.js +11 -2
  12. package/dist/generators/pothos/pothos-prisma-enum/pothos-prisma-enum.generator.js.map +1 -1
  13. package/dist/generators/prisma/_shared/build-data-helpers/build-schema-fragments.d.ts +47 -0
  14. package/dist/generators/prisma/_shared/build-data-helpers/build-schema-fragments.d.ts.map +1 -0
  15. package/dist/generators/prisma/_shared/build-data-helpers/build-schema-fragments.js +56 -0
  16. package/dist/generators/prisma/_shared/build-data-helpers/build-schema-fragments.js.map +1 -0
  17. package/dist/generators/prisma/_shared/build-data-helpers/build-transform-operation-parts.d.ts +71 -0
  18. package/dist/generators/prisma/_shared/build-data-helpers/build-transform-operation-parts.d.ts.map +1 -0
  19. package/dist/generators/prisma/_shared/build-data-helpers/build-transform-operation-parts.js +153 -0
  20. package/dist/generators/prisma/_shared/build-data-helpers/build-transform-operation-parts.js.map +1 -0
  21. package/dist/generators/prisma/_shared/build-data-helpers/generate-authorization-statements.d.ts +46 -0
  22. package/dist/generators/prisma/_shared/build-data-helpers/generate-authorization-statements.d.ts.map +1 -0
  23. package/dist/generators/prisma/_shared/build-data-helpers/generate-authorization-statements.js +44 -0
  24. package/dist/generators/prisma/_shared/build-data-helpers/generate-authorization-statements.js.map +1 -0
  25. package/dist/generators/prisma/_shared/build-data-helpers/generate-relation-build-data.d.ts +6 -0
  26. package/dist/generators/prisma/_shared/build-data-helpers/generate-relation-build-data.d.ts.map +1 -1
  27. package/dist/generators/prisma/_shared/build-data-helpers/generate-relation-build-data.js +13 -1
  28. package/dist/generators/prisma/_shared/build-data-helpers/generate-relation-build-data.js.map +1 -1
  29. package/dist/generators/prisma/_shared/build-data-helpers/generate-where-type.d.ts +24 -0
  30. package/dist/generators/prisma/_shared/build-data-helpers/generate-where-type.d.ts.map +1 -0
  31. package/dist/generators/prisma/_shared/build-data-helpers/generate-where-type.js +49 -0
  32. package/dist/generators/prisma/_shared/build-data-helpers/generate-where-type.js.map +1 -0
  33. package/dist/generators/prisma/_shared/field-definition-generators/generate-scalar-input-field.d.ts +1 -4
  34. package/dist/generators/prisma/_shared/field-definition-generators/generate-scalar-input-field.d.ts.map +1 -1
  35. package/dist/generators/prisma/_shared/field-definition-generators/generate-scalar-input-field.js +10 -2
  36. package/dist/generators/prisma/_shared/field-definition-generators/generate-scalar-input-field.js.map +1 -1
  37. package/dist/generators/prisma/_shared/field-definition-generators/types.d.ts +47 -1
  38. package/dist/generators/prisma/_shared/field-definition-generators/types.d.ts.map +1 -1
  39. package/dist/generators/prisma/data-utils/data-utils.generator.d.ts +17 -74
  40. package/dist/generators/prisma/data-utils/data-utils.generator.d.ts.map +1 -1
  41. package/dist/generators/prisma/data-utils/generated/index.d.ts +50 -145
  42. package/dist/generators/prisma/data-utils/generated/index.d.ts.map +1 -1
  43. package/dist/generators/prisma/data-utils/generated/template-paths.d.ts +5 -6
  44. package/dist/generators/prisma/data-utils/generated/template-paths.d.ts.map +1 -1
  45. package/dist/generators/prisma/data-utils/generated/template-paths.js +5 -6
  46. package/dist/generators/prisma/data-utils/generated/template-paths.js.map +1 -1
  47. package/dist/generators/prisma/data-utils/generated/template-renderers.d.ts +0 -21
  48. package/dist/generators/prisma/data-utils/generated/template-renderers.d.ts.map +1 -1
  49. package/dist/generators/prisma/data-utils/generated/template-renderers.js +1 -7
  50. package/dist/generators/prisma/data-utils/generated/template-renderers.js.map +1 -1
  51. package/dist/generators/prisma/data-utils/generated/ts-import-providers.d.ts +51 -159
  52. package/dist/generators/prisma/data-utils/generated/ts-import-providers.d.ts.map +1 -1
  53. package/dist/generators/prisma/data-utils/generated/ts-import-providers.js +32 -64
  54. package/dist/generators/prisma/data-utils/generated/ts-import-providers.js.map +1 -1
  55. package/dist/generators/prisma/data-utils/generated/typed-templates.d.ts +66 -142
  56. package/dist/generators/prisma/data-utils/generated/typed-templates.d.ts.map +1 -1
  57. package/dist/generators/prisma/data-utils/generated/typed-templates.js +47 -98
  58. package/dist/generators/prisma/data-utils/generated/typed-templates.js.map +1 -1
  59. package/dist/generators/prisma/data-utils/templates/src/utils/data-operations/define-transformer.ts +130 -0
  60. package/dist/generators/prisma/data-utils/templates/src/utils/data-operations/execute-transform-plan.ts +108 -0
  61. package/dist/generators/prisma/data-utils/templates/src/utils/data-operations/nested-transformers.ts +364 -0
  62. package/dist/generators/prisma/data-utils/templates/src/utils/data-operations/prepare-transformers.ts +73 -0
  63. package/dist/generators/prisma/data-utils/templates/src/utils/data-operations/prisma-types.ts +6 -83
  64. package/dist/generators/prisma/data-utils/templates/src/utils/data-operations/transformer-types.ts +118 -0
  65. package/dist/generators/prisma/prisma-authorizer-utils/generated/index.d.ts +34 -106
  66. package/dist/generators/prisma/prisma-authorizer-utils/generated/index.d.ts.map +1 -1
  67. package/dist/generators/prisma/prisma-authorizer-utils/generated/template-renderers.d.ts +17 -53
  68. package/dist/generators/prisma/prisma-authorizer-utils/generated/template-renderers.d.ts.map +1 -1
  69. package/dist/generators/prisma/prisma-authorizer-utils/generated/typed-templates.d.ts +34 -106
  70. package/dist/generators/prisma/prisma-authorizer-utils/generated/typed-templates.d.ts.map +1 -1
  71. package/dist/generators/prisma/prisma-authorizer-utils/prisma-authorizer-utils.generator.d.ts +17 -53
  72. package/dist/generators/prisma/prisma-authorizer-utils/prisma-authorizer-utils.generator.d.ts.map +1 -1
  73. package/dist/generators/prisma/prisma-authorizer-utils/templates/src/utils/authorizers.ts +11 -11
  74. package/dist/generators/prisma/prisma-data-create/prisma-data-create.generator.d.ts +39 -49
  75. package/dist/generators/prisma/prisma-data-create/prisma-data-create.generator.d.ts.map +1 -1
  76. package/dist/generators/prisma/prisma-data-create/prisma-data-create.generator.js +82 -45
  77. package/dist/generators/prisma/prisma-data-create/prisma-data-create.generator.js.map +1 -1
  78. package/dist/generators/prisma/prisma-data-delete/prisma-data-delete.generator.d.ts +39 -52
  79. package/dist/generators/prisma/prisma-data-delete/prisma-data-delete.generator.d.ts.map +1 -1
  80. package/dist/generators/prisma/prisma-data-delete/prisma-data-delete.generator.js +48 -32
  81. package/dist/generators/prisma/prisma-data-delete/prisma-data-delete.generator.js.map +1 -1
  82. package/dist/generators/prisma/prisma-data-nested-field/nested-field-writer.d.ts +16 -2
  83. package/dist/generators/prisma/prisma-data-nested-field/nested-field-writer.d.ts.map +1 -1
  84. package/dist/generators/prisma/prisma-data-nested-field/nested-field-writer.js +282 -138
  85. package/dist/generators/prisma/prisma-data-nested-field/nested-field-writer.js.map +1 -1
  86. package/dist/generators/prisma/prisma-data-nested-field/prisma-data-nested-field.generator.d.ts +19 -55
  87. package/dist/generators/prisma/prisma-data-nested-field/prisma-data-nested-field.generator.d.ts.map +1 -1
  88. package/dist/generators/prisma/prisma-data-nested-field/prisma-data-nested-field.generator.js +18 -4
  89. package/dist/generators/prisma/prisma-data-nested-field/prisma-data-nested-field.generator.js.map +1 -1
  90. package/dist/generators/prisma/prisma-data-service/prisma-data-service.generator.d.ts +37 -92
  91. package/dist/generators/prisma/prisma-data-service/prisma-data-service.generator.d.ts.map +1 -1
  92. package/dist/generators/prisma/prisma-data-service/prisma-data-service.generator.js +86 -29
  93. package/dist/generators/prisma/prisma-data-service/prisma-data-service.generator.js.map +1 -1
  94. package/dist/generators/prisma/prisma-data-update/prisma-data-update.generator.d.ts +40 -52
  95. package/dist/generators/prisma/prisma-data-update/prisma-data-update.generator.d.ts.map +1 -1
  96. package/dist/generators/prisma/prisma-data-update/prisma-data-update.generator.js +102 -49
  97. package/dist/generators/prisma/prisma-data-update/prisma-data-update.generator.js.map +1 -1
  98. package/dist/generators/prisma/prisma-query-filter-utils/generated/index.d.ts +51 -159
  99. package/dist/generators/prisma/prisma-query-filter-utils/generated/index.d.ts.map +1 -1
  100. package/dist/generators/prisma/prisma-query-filter-utils/generated/template-renderers.d.ts +17 -53
  101. package/dist/generators/prisma/prisma-query-filter-utils/generated/template-renderers.d.ts.map +1 -1
  102. package/dist/generators/prisma/prisma-query-filter-utils/generated/typed-templates.d.ts +68 -212
  103. package/dist/generators/prisma/prisma-query-filter-utils/generated/typed-templates.d.ts.map +1 -1
  104. package/dist/generators/prisma/prisma-query-filter-utils/prisma-query-filter-utils.generator.d.ts +17 -53
  105. package/dist/generators/prisma/prisma-query-filter-utils/prisma-query-filter-utils.generator.d.ts.map +1 -1
  106. package/dist/generators/prisma/prisma-relation-field/prisma-relation-field.generator.js +1 -1
  107. package/dist/generators/prisma/prisma-relation-field/prisma-relation-field.generator.js.map +1 -1
  108. package/dist/writers/prisma-schema/fields.d.ts +5 -1
  109. package/dist/writers/prisma-schema/fields.d.ts.map +1 -1
  110. package/dist/writers/prisma-schema/fields.js +8 -2
  111. package/dist/writers/prisma-schema/fields.js.map +1 -1
  112. package/package.json +8 -8
  113. package/dist/generators/prisma/_shared/build-data-helpers/generate-authorize-fragment.d.ts +0 -20
  114. package/dist/generators/prisma/_shared/build-data-helpers/generate-authorize-fragment.d.ts.map +0 -1
  115. package/dist/generators/prisma/_shared/build-data-helpers/generate-authorize-fragment.js +0 -28
  116. package/dist/generators/prisma/_shared/build-data-helpers/generate-authorize-fragment.js.map +0 -1
  117. package/dist/generators/prisma/_shared/build-data-helpers/generate-operation-callbacks.d.ts +0 -130
  118. package/dist/generators/prisma/_shared/build-data-helpers/generate-operation-callbacks.d.ts.map +0 -1
  119. package/dist/generators/prisma/_shared/build-data-helpers/generate-operation-callbacks.js +0 -221
  120. package/dist/generators/prisma/_shared/build-data-helpers/generate-operation-callbacks.js.map +0 -1
  121. package/dist/generators/prisma/_shared/build-data-helpers/index.d.ts +0 -4
  122. package/dist/generators/prisma/_shared/build-data-helpers/index.d.ts.map +0 -1
  123. package/dist/generators/prisma/_shared/build-data-helpers/index.js +0 -4
  124. package/dist/generators/prisma/_shared/build-data-helpers/index.js.map +0 -1
  125. package/dist/generators/prisma/data-utils/templates/src/utils/data-operations/commit-operations.ts +0 -366
  126. package/dist/generators/prisma/data-utils/templates/src/utils/data-operations/compose-operations.ts +0 -131
  127. package/dist/generators/prisma/data-utils/templates/src/utils/data-operations/field-definitions.ts +0 -777
  128. package/dist/generators/prisma/data-utils/templates/src/utils/data-operations/field-utils.ts +0 -201
  129. package/dist/generators/prisma/data-utils/templates/src/utils/data-operations/prisma-utils.ts +0 -90
  130. package/dist/generators/prisma/data-utils/templates/src/utils/data-operations/types.ts +0 -721
@@ -1,721 +0,0 @@
1
- // @ts-nocheck
2
-
3
- import type {
4
- GetPayload,
5
- ModelInclude,
6
- ModelPropName,
7
- WhereUniqueInput,
8
- } from '$prismaTypes';
9
- import type {
10
- GlobalRoleCheck,
11
- InstanceRoleCheck,
12
- } from '%authorizerUtilsImports';
13
- import type { Prisma } from '%prismaGeneratedImports';
14
- import type { ServiceContext } from '%serviceContextImports';
15
- import type { z } from 'zod';
16
-
17
- /**
18
- * Type of data operation being performed.
19
- *
20
- * - **create**: Inserting a new record
21
- * - **update**: Modifying an existing record
22
- * - **upsert**: Creating or updating a record (used internally for nested relations)
23
- * - **delete**: Removing a record
24
- */
25
- export type DataOperationType = 'create' | 'update' | 'upsert' | 'delete';
26
-
27
- /* eslint-disable @typescript-eslint/no-explicit-any -- to allow any generic types */
28
-
29
- /**
30
- * =========================================
31
- * Operation Contexts and Hooks
32
- * =========================================
33
- */
34
-
35
- /**
36
- * Context object provided to operation hooks and authorization functions.
37
- *
38
- * Contains information about the operation being performed and provides
39
- * access to the service context and existing data.
40
- *
41
- * @template TModel - The Prisma model type
42
- * @template TConfig - Configuration object with hasResult flag
43
- */
44
- export interface OperationContext<
45
- TModel,
46
- TConfig extends {
47
- hasResult: boolean;
48
- },
49
- > {
50
- /** Type of operation being performed */
51
- operation: DataOperationType;
52
- /** Service context with user info, request details, etc. */
53
- serviceContext: ServiceContext;
54
- /** Function to load the existing model data (for update/delete operations) */
55
- loadExisting: () => Promise<TModel | undefined>;
56
- /** The operation result (only available after execution) */
57
- result: TConfig['hasResult'] extends true ? TModel : undefined;
58
- }
59
-
60
- /**
61
- * Context object provided to hooks that run inside a transaction.
62
- *
63
- * Extends {@link OperationContext} with access to the Prisma transaction client,
64
- * allowing hooks to perform additional database operations within the same transaction.
65
- *
66
- * @template TModel - The Prisma model type
67
- * @template TConfig - Configuration object with hasResult flag
68
- */
69
- export interface TransactionalOperationContext<
70
- TModel,
71
- TConfig extends { hasResult: boolean },
72
- > extends OperationContext<TModel, TConfig> {
73
- /** Prisma transaction client for performing database operations */
74
- tx: Prisma.TransactionClient;
75
- }
76
-
77
- /**
78
- * Lifecycle hooks for data operations.
79
- *
80
- * Hooks allow you to execute custom logic at different points in the operation lifecycle:
81
- * - **beforeExecute**: Runs inside transaction, before the database operation
82
- * - **afterExecute**: Runs inside transaction, after the database operation (has access to result)
83
- * - **afterCommit**: Runs outside transaction, after successful commit (for side effects)
84
- *
85
- * @template TModel - The Prisma model type
86
- *
87
- * @example
88
- * ```typescript
89
- * const hooks: OperationHooks<User> = {
90
- * beforeExecute: [
91
- * async (ctx) => {
92
- * // Validate business rules before saving
93
- * },
94
- * ],
95
- * afterExecute: [
96
- * async (ctx) => {
97
- * // Update related records within same transaction
98
- * await ctx.tx.auditLog.create({
99
- * data: { action: 'user_created', userId: ctx.result.id },
100
- * });
101
- * },
102
- * ],
103
- * afterCommit: [
104
- * async (ctx) => {
105
- * // Send email notification (outside transaction)
106
- * await emailService.sendWelcome(ctx.result.email);
107
- * },
108
- * ],
109
- * };
110
- * ```
111
- */
112
- export interface OperationHooks<TModel> {
113
- /** Hooks that run inside transaction, before the database operation */
114
- beforeExecute?: ((
115
- context: TransactionalOperationContext<TModel, { hasResult: false }>,
116
- ) => Promise<void>)[];
117
- /** Hooks that run inside transaction, after the database operation */
118
- afterExecute?: ((
119
- context: TransactionalOperationContext<TModel, { hasResult: true }>,
120
- ) => Promise<void>)[];
121
- /** Hooks that run outside transaction, after successful commit */
122
- afterCommit?: ((
123
- context: OperationContext<TModel, { hasResult: true }>,
124
- ) => Promise<void>)[];
125
- }
126
-
127
- export type AnyOperationHooks = OperationHooks<any>;
128
-
129
- /**
130
- * =========================================
131
- * Field Types
132
- * =========================================
133
- */
134
-
135
- /**
136
- * Context provided to field `processInput` functions.
137
- *
138
- * Contains information about the operation and provides access to
139
- * existing data and service context.
140
- */
141
- export interface FieldContext {
142
- /** Type of operation being performed */
143
- operation: DataOperationType;
144
- /** Name of the field being processed */
145
- fieldName: string;
146
- /** Service context with user info, request details, etc. */
147
- serviceContext: ServiceContext;
148
- /** Function to load existing model data (for updates) */
149
- loadExisting: () => Promise<object | undefined>;
150
- }
151
-
152
- /**
153
- * Transformed field data for create and update operations.
154
- *
155
- * Fields can produce different data structures for create vs. update operations.
156
- *
157
- * @template TCreateOutput - Data type for create operations
158
- * @template TUpdateOutput - Data type for update operations
159
- */
160
- export interface FieldTransformData<TCreateOutput, TUpdateOutput> {
161
- /** Data to use when creating a new record */
162
- create?: TCreateOutput;
163
- /** Data to use when updating an existing record */
164
- update?: TUpdateOutput;
165
- }
166
-
167
- /**
168
- * Result of field processing, including transformed data and optional hooks.
169
- *
170
- * Hooks allow fields to perform side effects during the operation lifecycle.
171
- *
172
- * @template TCreateOutput - Data type for create operations
173
- * @template TUpdateOutput - Data type for update operations
174
- */
175
- export interface FieldTransformResult<TCreateOutput, TUpdateOutput> {
176
- /** Transformed field data for create and update operations */
177
- data?: FieldTransformData<TCreateOutput, TUpdateOutput>;
178
-
179
- /** Optional hooks to execute during operation lifecycle */
180
- hooks?: AnyOperationHooks;
181
- }
182
-
183
- /**
184
- * Field definition for validating and transforming input values.
185
- *
186
- * A field definition specifies how to process a single input field:
187
- * - Validate the input value using a Zod schema
188
- * - Transform it into Prisma-compatible create/update data
189
- * - Optionally attach hooks for side effects
190
- *
191
- * @template TInputSchema - The Zod schema type for validation
192
- * @template TCreateOutput - Output type for create operations
193
- * @template TUpdateOutput - Output type for update operations
194
- *
195
- * @example
196
- * ```typescript
197
- * const nameField: FieldDefinition<z.ZodString, string, string> = {
198
- * zodSchema: z.string().min(1),
199
- * processInput: (value, ctx) => {
200
- * const validated = z.string().min(1).parse(value);
201
- * return {
202
- * data: {
203
- * create: validated,
204
- * update: validated,
205
- * },
206
- * };
207
- * },
208
- * };
209
- * ```
210
- */
211
- export interface FieldDefinition<
212
- TInputSchema extends z.ZodType,
213
- TCreateOutput,
214
- TUpdateOutput,
215
- > {
216
- /**
217
- * The Zod schema for validating this field's input.
218
- * This schema can be extracted and reused for validation in other contexts
219
- * (e.g., GraphQL mutations, REST endpoints, tRPC procedures).
220
- */
221
- schema: TInputSchema;
222
-
223
- /**
224
- * Processes and transforms an already-validated input value into
225
- * Prisma-compatible create/update data.
226
- *
227
- * @param value - The validated input value to process
228
- * @param ctx - Context about the operation
229
- * @returns Transformed data and optional hooks
230
- */
231
- processInput: (
232
- value: z.output<TInputSchema>,
233
- ctx: FieldContext,
234
- ) =>
235
- | Promise<FieldTransformResult<TCreateOutput, TUpdateOutput>>
236
- | FieldTransformResult<TCreateOutput, TUpdateOutput>;
237
- }
238
-
239
- /** Type alias for any field definition (used for generic constraints) */
240
- export type AnyFieldDefinition = FieldDefinition<any, any, any>;
241
-
242
- /**
243
- * =========================================
244
- * Type Inference Utilities
245
- * =========================================
246
- */
247
-
248
- /** Identity type that expands type aliases for better IDE tooltips */
249
- type Identity<T> = T extends object
250
- ? {} & {
251
- [P in keyof T]: T[P];
252
- }
253
- : T;
254
-
255
- /**
256
- * Infers the input schema from a record of field definitions.
257
- *
258
- * Creates an object type where:
259
- * - Each key corresponds to a field name
260
- * - Each value type is the field's Zod schema type
261
- *
262
- * @template TFields - Record of field definitions
263
- *
264
- * @example
265
- * ```typescript
266
- * const fields = {
267
- * name: scalarField(z.string()),
268
- * email: scalarField(z.email().optional()),
269
- * };
270
- *
271
- * type InputSchema = InferInputSchema<typeof fields>;
272
- * // { name: z.ZodString; email?: z.ZodString | undefined }
273
- * ```
274
- */
275
- export type InferInputSchema<
276
- TFields extends Record<string, AnyFieldDefinition>,
277
- > = z.ZodObject<{
278
- [K in keyof TFields]: TFields[K] extends FieldDefinition<
279
- infer TInputSchema,
280
- any,
281
- any
282
- >
283
- ? TInputSchema
284
- : never;
285
- }>;
286
-
287
- /**
288
- * Infers the input type from a record of field definitions.
289
- *
290
- * Creates an object type where:
291
- * - Each key corresponds to a field name
292
- * - Each value type is the field's Zod schema type
293
- * - Fields accepting undefined become optional properties
294
- *
295
- * @template TFields - Record of field definitions
296
- *
297
- * @example
298
- * ```typescript
299
- * const fields = {
300
- * name: scalarField(z.string()),
301
- * email: scalarField(z.email().optional()),
302
- * };
303
- *
304
- * type Input = InferInput<typeof fields>;
305
- * // { name: string; email?: string | undefined }
306
- * ```
307
- */
308
- export type InferInput<TFields extends Record<string, AnyFieldDefinition>> =
309
- z.output<InferInputSchema<TFields>>;
310
-
311
- /**
312
- * Infers the output type (create and update) from a single field definition.
313
- *
314
- * @template TField - A field definition
315
- */
316
- export type InferFieldOutput<TField extends FieldDefinition<any, any, any>> =
317
- TField extends FieldDefinition<any, infer TCreateOutput, infer TUpdateOutput>
318
- ? {
319
- create: TCreateOutput;
320
- update: TUpdateOutput;
321
- }
322
- : never;
323
-
324
- /**
325
- * Infers the create output type from a record of field definitions.
326
- *
327
- * Creates an object type where each property is the field's create output type.
328
- *
329
- * @template TFields - Record of field definitions
330
- */
331
- export type InferFieldsCreateOutput<
332
- TFields extends Record<string, AnyFieldDefinition>,
333
- > = Identity<{
334
- [K in keyof TFields]: InferFieldOutput<TFields[K]>['create'];
335
- }>;
336
-
337
- /**
338
- * Infers the update output type from a record of field definitions.
339
- *
340
- * Creates an object type where each property is the field's update output type
341
- * or undefined (since updates are partial).
342
- *
343
- * @template TFields - Record of field definitions
344
- */
345
- export type InferFieldsUpdateOutput<
346
- TFields extends Record<string, AnyFieldDefinition>,
347
- > = Identity<{
348
- [K in keyof TFields]: InferFieldOutput<TFields[K]>['update'] | undefined;
349
- }>;
350
-
351
- /**
352
- * Combined create and update output types for a set of fields.
353
- *
354
- * @template TFields - Record of field definitions
355
- */
356
- export interface InferFieldsOutput<
357
- TFields extends Record<string, AnyFieldDefinition>,
358
- > {
359
- /** Field outputs for create operations */
360
- create: InferFieldsCreateOutput<TFields>;
361
- /** Field outputs for update operations */
362
- update: InferFieldsUpdateOutput<TFields>;
363
- }
364
-
365
- /**
366
- * =========================================
367
- * Operation Plans
368
- * =========================================
369
- */
370
-
371
- type HookPhase = keyof Required<AnyOperationHooks>;
372
- type HookFn<TPhase extends HookPhase> =
373
- Required<AnyOperationHooks>[TPhase][number];
374
-
375
- /**
376
- * Immutable plan for a create operation, returned by `composeCreate`.
377
- *
378
- * Contains the resolved field data, collected hooks, and service context.
379
- * Use `mapData` and `addHook` to derive new plans — the original is never mutated.
380
- */
381
- export class CreatePlan<
382
- TModelName extends ModelPropName,
383
- TFields extends Record<string, AnyFieldDefinition>,
384
- > {
385
- /** The Prisma model name */
386
- readonly model: TModelName;
387
-
388
- /** Resolved field data for the create operation */
389
- readonly data: InferFieldsCreateOutput<TFields>;
390
-
391
- /** Collected hooks from field processing and any `addHook` calls */
392
- readonly hooks: Readonly<Required<AnyOperationHooks>>;
393
-
394
- /** Service context for the operation */
395
- readonly serviceContext: ServiceContext;
396
-
397
- constructor(args: {
398
- model: TModelName;
399
- data: InferFieldsCreateOutput<TFields>;
400
- hooks: Required<AnyOperationHooks>;
401
- serviceContext: ServiceContext;
402
- }) {
403
- this.model = args.model;
404
- this.data = args.data;
405
- this.hooks = args.hooks;
406
- this.serviceContext = args.serviceContext;
407
- }
408
-
409
- /** Return a new plan with transformed data. */
410
- mapData(
411
- fn: (
412
- data: InferFieldsCreateOutput<TFields>,
413
- ) => InferFieldsCreateOutput<TFields>,
414
- ): CreatePlan<TModelName, TFields> {
415
- return new CreatePlan({
416
- model: this.model,
417
- data: fn(this.data),
418
- hooks: this.hooks,
419
- serviceContext: this.serviceContext,
420
- });
421
- }
422
-
423
- /** Return a new plan with an additional hook appended to the given phase. */
424
- addHook<TPhase extends HookPhase>(
425
- phase: TPhase,
426
- hook: HookFn<TPhase>,
427
- ): CreatePlan<TModelName, TFields> {
428
- return new CreatePlan({
429
- model: this.model,
430
- data: this.data,
431
- hooks: {
432
- ...this.hooks,
433
- [phase]: [...this.hooks[phase], hook],
434
- },
435
- serviceContext: this.serviceContext,
436
- });
437
- }
438
- }
439
-
440
- /**
441
- * Immutable plan for an update operation, returned by `composeUpdate`.
442
- *
443
- * Contains the resolved field data, collected hooks, existing item access,
444
- * and service context. Use `mapData` and `addHook` to derive new plans —
445
- * the original is never mutated.
446
- */
447
- export class UpdatePlan<
448
- TModelName extends ModelPropName,
449
- TFields extends Record<string, AnyFieldDefinition>,
450
- > {
451
- /** The Prisma model name */
452
- readonly model: TModelName;
453
-
454
- /** Resolved field data for the update operation */
455
- readonly data: InferFieldsUpdateOutput<TFields>;
456
-
457
- /** Collected hooks from field processing and any `addHook` calls */
458
- readonly hooks: Readonly<Required<AnyOperationHooks>>;
459
-
460
- /** Service context for the operation */
461
- readonly serviceContext: ServiceContext;
462
-
463
- /**
464
- * Lazy loader for the existing item. Caches the result.
465
- * Call this if you need to inspect the existing item for diffing.
466
- */
467
- readonly loadExisting: () => Promise<GetPayload<TModelName>>;
468
-
469
- constructor(args: {
470
- model: TModelName;
471
- data: InferFieldsUpdateOutput<TFields>;
472
- hooks: Required<AnyOperationHooks>;
473
- serviceContext: ServiceContext;
474
- loadExisting: () => Promise<GetPayload<TModelName>>;
475
- }) {
476
- this.model = args.model;
477
- this.data = args.data;
478
- this.hooks = args.hooks;
479
- this.serviceContext = args.serviceContext;
480
- this.loadExisting = args.loadExisting;
481
- }
482
-
483
- /** Return a new plan with transformed data. */
484
- mapData(
485
- fn: (
486
- data: InferFieldsUpdateOutput<TFields>,
487
- ) => InferFieldsUpdateOutput<TFields>,
488
- ): UpdatePlan<TModelName, TFields> {
489
- return new UpdatePlan({
490
- model: this.model,
491
- data: fn(this.data),
492
- hooks: this.hooks,
493
- serviceContext: this.serviceContext,
494
- loadExisting: this.loadExisting,
495
- });
496
- }
497
-
498
- /** Return a new plan with an additional hook appended to the given phase. */
499
- addHook<TPhase extends HookPhase>(
500
- phase: TPhase,
501
- hook: HookFn<TPhase>,
502
- ): UpdatePlan<TModelName, TFields> {
503
- return new UpdatePlan({
504
- model: this.model,
505
- data: this.data,
506
- hooks: {
507
- ...this.hooks,
508
- [phase]: [...this.hooks[phase], hook],
509
- },
510
- serviceContext: this.serviceContext,
511
- loadExisting: this.loadExisting,
512
- });
513
- }
514
- }
515
-
516
- /**
517
- * =========================================
518
- * Compose Configs
519
- * =========================================
520
- */
521
-
522
- /**
523
- * Configuration for `composeCreate`.
524
- */
525
- export interface ComposeCreateConfig<
526
- TModelName extends ModelPropName,
527
- TFields extends Record<string, AnyFieldDefinition>,
528
- > {
529
- model: TModelName;
530
- fields: TFields;
531
- input: InferInput<TFields>;
532
- context: ServiceContext;
533
-
534
- /**
535
- * Optional authorization checks. Only global roles are allowed for create
536
- * since there is no existing instance to check against.
537
- * Runs before field processing — fails fast on unauthorized access.
538
- */
539
- authorize?: GlobalRoleCheck[];
540
- }
541
-
542
- /**
543
- * Configuration for `composeUpdate`.
544
- *
545
- * The `loadExisting` function is a cached lazy loader for the existing item.
546
- * It is called by field processing (e.g., nested relations) when they need
547
- * to read the current state. The caller defines this function to control
548
- * how the item is fetched (e.g., with extra includes).
549
- */
550
- export interface ComposeUpdateConfig<
551
- TModelName extends ModelPropName,
552
- TFields extends Record<string, AnyFieldDefinition>,
553
- > {
554
- model: TModelName;
555
- fields: TFields;
556
- input: Partial<InferInput<TFields>>;
557
- context: ServiceContext;
558
- loadExisting: () => Promise<GetPayload<TModelName>>;
559
-
560
- /**
561
- * Optional authorization checks. Supports both global roles (strings)
562
- * and instance-level checks (functions that receive the existing item).
563
- * Runs before field processing — fails fast on unauthorized access.
564
- */
565
- authorize?: (GlobalRoleCheck | InstanceRoleCheck<GetPayload<TModelName>>)[];
566
- }
567
-
568
- /**
569
- * =========================================
570
- * Data Service Input Types
571
- * =========================================
572
- */
573
-
574
- /**
575
- * Input type for data service create functions.
576
- */
577
- export interface DataCreateInput<
578
- TModelName extends ModelPropName,
579
- TFields extends Record<string, AnyFieldDefinition>,
580
- TIncludeArgs extends ModelInclude<TModelName> = ModelInclude<TModelName>,
581
- > {
582
- data: InferInput<TFields>;
583
- query?: TIncludeArgs;
584
- context: ServiceContext;
585
- }
586
-
587
- /**
588
- * Input type for data service update functions.
589
- */
590
- export interface DataUpdateInput<
591
- TModelName extends ModelPropName,
592
- TFields extends Record<string, AnyFieldDefinition>,
593
- TIncludeArgs extends ModelInclude<TModelName> = ModelInclude<TModelName>,
594
- > {
595
- where: WhereUniqueInput<TModelName>;
596
- data: Partial<InferInput<TFields>>;
597
- query?: TIncludeArgs;
598
- context: ServiceContext;
599
- }
600
-
601
- /**
602
- * Input type for data service delete functions.
603
- */
604
- export interface DataDeleteInput<
605
- TModelName extends ModelPropName,
606
- TIncludeArgs extends ModelInclude<TModelName> = ModelInclude<TModelName>,
607
- > {
608
- where: WhereUniqueInput<TModelName>;
609
- query?: TIncludeArgs;
610
- context: ServiceContext;
611
- }
612
-
613
- /**
614
- * =========================================
615
- * Commit Configs
616
- * =========================================
617
- */
618
-
619
- /**
620
- * Configuration for `commitCreate`.
621
- */
622
- export interface CommitCreateConfig<
623
- TModelName extends ModelPropName,
624
- TFields extends Record<string, AnyFieldDefinition>,
625
- TIncludeArgs extends ModelInclude<TModelName>,
626
- > {
627
- /** Prisma query arguments (include) to shape returned data */
628
- query?: TIncludeArgs;
629
-
630
- /**
631
- * Optional override for the default refetch behavior.
632
- * By default, records are refetched using `findUnique({ where: { id } })`
633
- * via a generic Prisma delegate. Provide this if you need custom refetch logic.
634
- */
635
- refetchWithQuery?: (
636
- result: GetPayload<TModelName>,
637
- query: TIncludeArgs,
638
- ) => Promise<GetPayload<TModelName, TIncludeArgs>>;
639
-
640
- /** Execute the Prisma create operation inside the transaction */
641
- execute: (args: {
642
- tx: Prisma.TransactionClient;
643
- data: InferFieldsCreateOutput<TFields>;
644
- query: ModelInclude<TModelName>;
645
- serviceContext: ServiceContext;
646
- }) => Promise<GetPayload<TModelName, ModelInclude<TModelName>>>;
647
- }
648
-
649
- /**
650
- * Configuration for `commitUpdate`.
651
- */
652
- export interface CommitUpdateConfig<
653
- TModelName extends ModelPropName,
654
- TFields extends Record<string, AnyFieldDefinition>,
655
- TIncludeArgs extends ModelInclude<TModelName>,
656
- > {
657
- /** Prisma query arguments (include) to shape returned data */
658
- query?: TIncludeArgs;
659
-
660
- /**
661
- * Optional override for the default refetch behavior.
662
- * By default, records are refetched using `findUnique({ where: { id } })`
663
- * via a generic Prisma delegate. Provide this if you need custom refetch logic.
664
- */
665
- refetchWithQuery?: (
666
- result: GetPayload<TModelName>,
667
- query: TIncludeArgs,
668
- ) => Promise<GetPayload<TModelName, TIncludeArgs>>;
669
-
670
- /** Execute the Prisma update operation inside the transaction */
671
- execute: (args: {
672
- tx: Prisma.TransactionClient;
673
- data: InferFieldsUpdateOutput<TFields>;
674
- query: ModelInclude<TModelName>;
675
- serviceContext: ServiceContext;
676
- }) => Promise<GetPayload<TModelName, ModelInclude<TModelName>>>;
677
- }
678
-
679
- /**
680
- * Configuration for `commitDelete`.
681
- *
682
- * Delete has no compose step since there are no fields to process.
683
- * Authorization, hooks, and execution are all handled directly.
684
- */
685
- export interface CommitDeleteConfig<
686
- TModelName extends ModelPropName,
687
- TIncludeArgs extends ModelInclude<TModelName>,
688
- > {
689
- /** The Prisma model name */
690
- model: TModelName;
691
-
692
- /** Prisma query arguments (include) to shape returned data */
693
- query?: TIncludeArgs;
694
-
695
- /** Service context for the operation */
696
- context: ServiceContext;
697
-
698
- /**
699
- * Optional authorization checks. Supports both global roles (strings)
700
- * and instance-level checks (functions that receive the existing item).
701
- * Runs before hooks and delete execution.
702
- */
703
- authorize?: (GlobalRoleCheck | InstanceRoleCheck<GetPayload<TModelName>>)[];
704
-
705
- /** Optional hooks to run during the delete lifecycle */
706
- hooks?: Partial<AnyOperationHooks>;
707
-
708
- /**
709
- * Lazy loader for the existing item. Required when using instance-level
710
- * authorization or hooks that need to inspect the record before deletion.
711
- * The result is cached so the item is fetched at most once.
712
- */
713
- loadExisting?: () => Promise<GetPayload<TModelName>>;
714
-
715
- /** Execute the Prisma delete operation inside the transaction */
716
- execute: (args: {
717
- tx: Prisma.TransactionClient;
718
- query: ModelInclude<TModelName>;
719
- serviceContext: ServiceContext;
720
- }) => Promise<GetPayload<TModelName, ModelInclude<TModelName>>>;
721
- }