@baseplate-dev/fastify-generators 0.5.3 → 0.6.0

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 (165) hide show
  1. package/CHANGELOG.md +57 -0
  2. package/dist/constants/fastify-packages.d.ts +14 -14
  3. package/dist/constants/fastify-packages.js +14 -14
  4. package/dist/constants/fastify-packages.js.map +1 -1
  5. package/dist/generators/auth/auth-context/templates/module/types/auth-context.types.ts +1 -0
  6. package/dist/generators/auth/auth-context/templates/module/utils/auth-context.utils.ts +3 -1
  7. package/dist/generators/core/fastify/fastify.generator.d.ts.map +1 -1
  8. package/dist/generators/core/fastify/fastify.generator.js +3 -1
  9. package/dist/generators/core/fastify/fastify.generator.js.map +1 -1
  10. package/dist/generators/pothos/index.d.ts +1 -0
  11. package/dist/generators/pothos/index.d.ts.map +1 -1
  12. package/dist/generators/pothos/index.js +1 -0
  13. package/dist/generators/pothos/index.js.map +1 -1
  14. package/dist/generators/pothos/pothos-auth/pothos-auth.generator.d.ts +5 -0
  15. package/dist/generators/pothos/pothos-auth/pothos-auth.generator.d.ts.map +1 -1
  16. package/dist/generators/pothos/pothos-auth/pothos-auth.generator.js +8 -0
  17. package/dist/generators/pothos/pothos-auth/pothos-auth.generator.js.map +1 -1
  18. package/dist/generators/pothos/pothos-prisma-count-query/index.d.ts +2 -0
  19. package/dist/generators/pothos/pothos-prisma-count-query/index.d.ts.map +1 -0
  20. package/dist/generators/pothos/pothos-prisma-count-query/index.js +2 -0
  21. package/dist/generators/pothos/pothos-prisma-count-query/index.js.map +1 -0
  22. package/dist/generators/pothos/pothos-prisma-count-query/pothos-prisma-count-query.generator.d.ts +15 -0
  23. package/dist/generators/pothos/pothos-prisma-count-query/pothos-prisma-count-query.generator.d.ts.map +1 -0
  24. package/dist/generators/pothos/pothos-prisma-count-query/pothos-prisma-count-query.generator.js +97 -0
  25. package/dist/generators/pothos/pothos-prisma-count-query/pothos-prisma-count-query.generator.js.map +1 -0
  26. package/dist/generators/pothos/pothos-prisma-crud-mutation/pothos-prisma-crud-mutation.generator.d.ts.map +1 -1
  27. package/dist/generators/pothos/pothos-prisma-crud-mutation/pothos-prisma-crud-mutation.generator.js +2 -5
  28. package/dist/generators/pothos/pothos-prisma-crud-mutation/pothos-prisma-crud-mutation.generator.js.map +1 -1
  29. package/dist/generators/pothos/pothos-prisma-find-query/pothos-prisma-find-query.generator.d.ts +3 -0
  30. package/dist/generators/pothos/pothos-prisma-find-query/pothos-prisma-find-query.generator.d.ts.map +1 -1
  31. package/dist/generators/pothos/pothos-prisma-find-query/pothos-prisma-find-query.generator.js +44 -10
  32. package/dist/generators/pothos/pothos-prisma-find-query/pothos-prisma-find-query.generator.js.map +1 -1
  33. package/dist/generators/pothos/pothos-prisma-list-query/pothos-prisma-list-query.generator.d.ts +3 -0
  34. package/dist/generators/pothos/pothos-prisma-list-query/pothos-prisma-list-query.generator.d.ts.map +1 -1
  35. package/dist/generators/pothos/pothos-prisma-list-query/pothos-prisma-list-query.generator.js +32 -3
  36. package/dist/generators/pothos/pothos-prisma-list-query/pothos-prisma-list-query.generator.js.map +1 -1
  37. package/dist/generators/pothos/pothos-prisma-object/pothos-prisma-object.generator.d.ts +7 -1
  38. package/dist/generators/pothos/pothos-prisma-object/pothos-prisma-object.generator.d.ts.map +1 -1
  39. package/dist/generators/pothos/pothos-prisma-object/pothos-prisma-object.generator.js +73 -13
  40. package/dist/generators/pothos/pothos-prisma-object/pothos-prisma-object.generator.js.map +1 -1
  41. package/dist/generators/prisma/_shared/build-data-helpers/generate-authorize-fragment.d.ts +20 -0
  42. package/dist/generators/prisma/_shared/build-data-helpers/generate-authorize-fragment.d.ts.map +1 -0
  43. package/dist/generators/prisma/_shared/build-data-helpers/generate-authorize-fragment.js +28 -0
  44. package/dist/generators/prisma/_shared/build-data-helpers/generate-authorize-fragment.js.map +1 -0
  45. package/dist/generators/prisma/_shared/build-data-helpers/generate-operation-callbacks.d.ts +41 -50
  46. package/dist/generators/prisma/_shared/build-data-helpers/generate-operation-callbacks.d.ts.map +1 -1
  47. package/dist/generators/prisma/_shared/build-data-helpers/generate-operation-callbacks.js +35 -29
  48. package/dist/generators/prisma/_shared/build-data-helpers/generate-operation-callbacks.js.map +1 -1
  49. package/dist/generators/prisma/_shared/build-data-helpers/generate-relation-build-data.d.ts +2 -0
  50. package/dist/generators/prisma/_shared/build-data-helpers/generate-relation-build-data.d.ts.map +1 -1
  51. package/dist/generators/prisma/_shared/build-data-helpers/generate-relation-build-data.js +3 -3
  52. package/dist/generators/prisma/_shared/build-data-helpers/generate-relation-build-data.js.map +1 -1
  53. package/dist/generators/prisma/_shared/build-data-helpers/index.d.ts +1 -0
  54. package/dist/generators/prisma/_shared/build-data-helpers/index.d.ts.map +1 -1
  55. package/dist/generators/prisma/_shared/build-data-helpers/index.js +1 -0
  56. package/dist/generators/prisma/_shared/build-data-helpers/index.js.map +1 -1
  57. package/dist/generators/prisma/data-utils/data-utils.generator.d.ts +25 -3
  58. package/dist/generators/prisma/data-utils/data-utils.generator.d.ts.map +1 -1
  59. package/dist/generators/prisma/data-utils/generated/index.d.ts +67 -18
  60. package/dist/generators/prisma/data-utils/generated/index.d.ts.map +1 -1
  61. package/dist/generators/prisma/data-utils/generated/template-paths.d.ts +3 -1
  62. package/dist/generators/prisma/data-utils/generated/template-paths.d.ts.map +1 -1
  63. package/dist/generators/prisma/data-utils/generated/template-paths.js +3 -1
  64. package/dist/generators/prisma/data-utils/generated/template-paths.js.map +1 -1
  65. package/dist/generators/prisma/data-utils/generated/ts-import-providers.d.ts +75 -9
  66. package/dist/generators/prisma/data-utils/generated/ts-import-providers.d.ts.map +1 -1
  67. package/dist/generators/prisma/data-utils/generated/ts-import-providers.js +26 -6
  68. package/dist/generators/prisma/data-utils/generated/ts-import-providers.js.map +1 -1
  69. package/dist/generators/prisma/data-utils/generated/typed-templates.d.ts +84 -30
  70. package/dist/generators/prisma/data-utils/generated/typed-templates.d.ts.map +1 -1
  71. package/dist/generators/prisma/data-utils/generated/typed-templates.js +56 -11
  72. package/dist/generators/prisma/data-utils/generated/typed-templates.js.map +1 -1
  73. package/dist/generators/prisma/data-utils/templates/src/utils/data-operations/commit-operations.ts +365 -0
  74. package/dist/generators/prisma/data-utils/templates/src/utils/data-operations/compose-operations.ts +131 -0
  75. package/dist/generators/prisma/data-utils/templates/src/utils/data-operations/field-definitions.ts +26 -30
  76. package/dist/generators/prisma/data-utils/templates/src/utils/data-operations/field-utils.ts +201 -0
  77. package/dist/generators/prisma/data-utils/templates/src/utils/data-operations/relation-helpers.ts +21 -26
  78. package/dist/generators/prisma/data-utils/templates/src/utils/data-operations/types.ts +372 -14
  79. package/dist/generators/prisma/index.d.ts +2 -0
  80. package/dist/generators/prisma/index.d.ts.map +1 -1
  81. package/dist/generators/prisma/index.js +2 -0
  82. package/dist/generators/prisma/index.js.map +1 -1
  83. package/dist/generators/prisma/prisma-authorizer-utils/generated/index.d.ts +50 -6
  84. package/dist/generators/prisma/prisma-authorizer-utils/generated/index.d.ts.map +1 -1
  85. package/dist/generators/prisma/prisma-authorizer-utils/generated/template-renderers.d.ts +25 -3
  86. package/dist/generators/prisma/prisma-authorizer-utils/generated/template-renderers.d.ts.map +1 -1
  87. package/dist/generators/prisma/prisma-authorizer-utils/generated/typed-templates.d.ts +50 -6
  88. package/dist/generators/prisma/prisma-authorizer-utils/generated/typed-templates.d.ts.map +1 -1
  89. package/dist/generators/prisma/prisma-authorizer-utils/prisma-authorizer-utils.generator.d.ts +25 -3
  90. package/dist/generators/prisma/prisma-authorizer-utils/prisma-authorizer-utils.generator.d.ts.map +1 -1
  91. package/dist/generators/prisma/prisma-data-create/prisma-data-create.generator.d.ts +26 -3
  92. package/dist/generators/prisma/prisma-data-create/prisma-data-create.generator.d.ts.map +1 -1
  93. package/dist/generators/prisma/prisma-data-create/prisma-data-create.generator.js +51 -25
  94. package/dist/generators/prisma/prisma-data-create/prisma-data-create.generator.js.map +1 -1
  95. package/dist/generators/prisma/prisma-data-delete/prisma-data-delete.generator.d.ts +31 -3
  96. package/dist/generators/prisma/prisma-data-delete/prisma-data-delete.generator.d.ts.map +1 -1
  97. package/dist/generators/prisma/prisma-data-delete/prisma-data-delete.generator.js +48 -14
  98. package/dist/generators/prisma/prisma-data-delete/prisma-data-delete.generator.js.map +1 -1
  99. package/dist/generators/prisma/prisma-data-nested-field/prisma-data-nested-field.generator.d.ts +25 -3
  100. package/dist/generators/prisma/prisma-data-nested-field/prisma-data-nested-field.generator.d.ts.map +1 -1
  101. package/dist/generators/prisma/prisma-data-service/prisma-data-service.generator.d.ts +25 -3
  102. package/dist/generators/prisma/prisma-data-service/prisma-data-service.generator.d.ts.map +1 -1
  103. package/dist/generators/prisma/prisma-data-update/prisma-data-update.generator.d.ts +31 -3
  104. package/dist/generators/prisma/prisma-data-update/prisma-data-update.generator.d.ts.map +1 -1
  105. package/dist/generators/prisma/prisma-data-update/prisma-data-update.generator.js +63 -23
  106. package/dist/generators/prisma/prisma-data-update/prisma-data-update.generator.js.map +1 -1
  107. package/dist/generators/prisma/prisma-model-authorizer/prisma-model-authorizer.generator.d.ts +2 -0
  108. package/dist/generators/prisma/prisma-model-authorizer/prisma-model-authorizer.generator.d.ts.map +1 -1
  109. package/dist/generators/prisma/prisma-model-authorizer/prisma-model-authorizer.generator.js +86 -53
  110. package/dist/generators/prisma/prisma-model-authorizer/prisma-model-authorizer.generator.js.map +1 -1
  111. package/dist/generators/prisma/prisma-model-query-filter/index.d.ts +2 -0
  112. package/dist/generators/prisma/prisma-model-query-filter/index.d.ts.map +1 -0
  113. package/dist/generators/prisma/prisma-model-query-filter/index.js +2 -0
  114. package/dist/generators/prisma/prisma-model-query-filter/index.js.map +1 -0
  115. package/dist/generators/prisma/prisma-model-query-filter/prisma-model-query-filter.generator.d.ts +57 -0
  116. package/dist/generators/prisma/prisma-model-query-filter/prisma-model-query-filter.generator.d.ts.map +1 -0
  117. package/dist/generators/prisma/prisma-model-query-filter/prisma-model-query-filter.generator.js +124 -0
  118. package/dist/generators/prisma/prisma-model-query-filter/prisma-model-query-filter.generator.js.map +1 -0
  119. package/dist/generators/prisma/prisma-query-filter-utils/generated/index.d.ts +348 -0
  120. package/dist/generators/prisma/prisma-query-filter-utils/generated/index.d.ts.map +1 -0
  121. package/dist/generators/prisma/prisma-query-filter-utils/generated/index.js +13 -0
  122. package/dist/generators/prisma/prisma-query-filter-utils/generated/index.js.map +1 -0
  123. package/dist/generators/prisma/prisma-query-filter-utils/generated/template-paths.d.ts +13 -0
  124. package/dist/generators/prisma/prisma-query-filter-utils/generated/template-paths.d.ts.map +1 -0
  125. package/dist/generators/prisma/prisma-query-filter-utils/generated/template-paths.js +25 -0
  126. package/dist/generators/prisma/prisma-query-filter-utils/generated/template-paths.js.map +1 -0
  127. package/dist/generators/prisma/prisma-query-filter-utils/generated/template-renderers.d.ts +123 -0
  128. package/dist/generators/prisma/prisma-query-filter-utils/generated/template-renderers.d.ts.map +1 -0
  129. package/dist/generators/prisma/prisma-query-filter-utils/generated/template-renderers.js +46 -0
  130. package/dist/generators/prisma/prisma-query-filter-utils/generated/template-renderers.js.map +1 -0
  131. package/dist/generators/prisma/prisma-query-filter-utils/generated/ts-import-providers.d.ts +57 -0
  132. package/dist/generators/prisma/prisma-query-filter-utils/generated/ts-import-providers.d.ts.map +1 -0
  133. package/dist/generators/prisma/prisma-query-filter-utils/generated/ts-import-providers.js +38 -0
  134. package/dist/generators/prisma/prisma-query-filter-utils/generated/ts-import-providers.js.map +1 -0
  135. package/dist/generators/prisma/prisma-query-filter-utils/generated/typed-templates.d.ts +401 -0
  136. package/dist/generators/prisma/prisma-query-filter-utils/generated/typed-templates.d.ts.map +1 -0
  137. package/dist/generators/prisma/prisma-query-filter-utils/generated/typed-templates.js +43 -0
  138. package/dist/generators/prisma/prisma-query-filter-utils/generated/typed-templates.js.map +1 -0
  139. package/dist/generators/prisma/prisma-query-filter-utils/index.d.ts +4 -0
  140. package/dist/generators/prisma/prisma-query-filter-utils/index.d.ts.map +1 -0
  141. package/dist/generators/prisma/prisma-query-filter-utils/index.js +3 -0
  142. package/dist/generators/prisma/prisma-query-filter-utils/index.js.map +1 -0
  143. package/dist/generators/prisma/prisma-query-filter-utils/prisma-query-filter-utils.generator.d.ts +145 -0
  144. package/dist/generators/prisma/prisma-query-filter-utils/prisma-query-filter-utils.generator.d.ts.map +1 -0
  145. package/dist/generators/prisma/prisma-query-filter-utils/prisma-query-filter-utils.generator.js +32 -0
  146. package/dist/generators/prisma/prisma-query-filter-utils/prisma-query-filter-utils.generator.js.map +1 -0
  147. package/dist/generators/prisma/prisma-query-filter-utils/templates/src/utils/query-filters.ts +185 -0
  148. package/dist/generators/prisma/prisma-query-filter-utils/templates/src/utils/query-helpers.ts +70 -0
  149. package/dist/types/service-dto-kinds.d.ts +0 -6
  150. package/dist/types/service-dto-kinds.d.ts.map +1 -1
  151. package/dist/types/service-dto-kinds.js +0 -6
  152. package/dist/types/service-dto-kinds.js.map +1 -1
  153. package/dist/writers/pothos/helpers.d.ts +1 -0
  154. package/dist/writers/pothos/helpers.d.ts.map +1 -1
  155. package/dist/writers/pothos/helpers.js +1 -0
  156. package/dist/writers/pothos/helpers.js.map +1 -1
  157. package/dist/writers/pothos/scalar-fields.d.ts +3 -1
  158. package/dist/writers/pothos/scalar-fields.d.ts.map +1 -1
  159. package/dist/writers/pothos/scalar-fields.js +1 -0
  160. package/dist/writers/pothos/scalar-fields.js.map +1 -1
  161. package/dist/writers/prisma-schema/fields.d.ts +1 -1
  162. package/dist/writers/prisma-schema/fields.js +2 -2
  163. package/dist/writers/prisma-schema/fields.js.map +1 -1
  164. package/package.json +8 -8
  165. package/dist/generators/prisma/data-utils/templates/src/utils/data-operations/define-operations.ts +0 -1134
@@ -0,0 +1,201 @@
1
+ // @ts-nocheck
2
+
3
+ import type {
4
+ AnyFieldDefinition,
5
+ AnyOperationHooks,
6
+ DataOperationType,
7
+ InferFieldOutput,
8
+ InferFieldsCreateOutput,
9
+ InferFieldsOutput,
10
+ InferFieldsUpdateOutput,
11
+ InferInput,
12
+ InferInputSchema,
13
+ } from '$types';
14
+ import type { ServiceContext } from '%serviceContextImports';
15
+
16
+ import { z } from 'zod';
17
+
18
+ /**
19
+ * Invokes an array of hooks with the provided context.
20
+ *
21
+ * All hooks are executed in parallel using `Promise.all`. If no hooks are provided
22
+ * or the array is empty, this function returns immediately.
23
+ *
24
+ * @template TContext - The context type passed to each hook
25
+ * @param hooks - Optional array of async hook functions to invoke
26
+ * @param context - The context object passed to each hook
27
+ * @returns Promise that resolves when all hooks have completed
28
+ *
29
+ * @example
30
+ * ```typescript
31
+ * await invokeHooks(config.hooks?.beforeExecute, {
32
+ * operation: 'create',
33
+ * serviceContext: ctx,
34
+ * tx: transaction,
35
+ * });
36
+ * ```
37
+ */
38
+ export async function invokeHooks<TContext>(
39
+ hooks: ((ctx: TContext) => Promise<void>)[] | undefined,
40
+ context: TContext,
41
+ ): Promise<void> {
42
+ if (!hooks || hooks.length === 0) return;
43
+ await Promise.all(hooks.map((hook) => hook(context)));
44
+ }
45
+
46
+ /**
47
+ * Transforms field definitions into Prisma create/update data structures.
48
+ *
49
+ * This function processes each field definition by:
50
+ * 1. Validating the input value against the field's schema
51
+ * 2. Transforming the value into Prisma-compatible create/update data
52
+ * 3. Collecting hooks from each field for execution during the operation lifecycle
53
+ *
54
+ * @template TFields - Record of field definitions
55
+ * @param fields - Field definitions to process
56
+ * @param input - Input data to validate and transform
57
+ * @param options - Transformation options
58
+ * @param options.serviceContext - Service context with user, request info
59
+ * @param options.operation - Type of operation (create, update, upsert, delete)
60
+ * @param options.allowOptionalFields - Whether to allow undefined field values
61
+ * @param options.loadExisting - Function to load existing model data
62
+ * @returns Object containing transformed data and collected hooks
63
+ *
64
+ * @example
65
+ * ```typescript
66
+ * const { data, hooks } = await transformFields(
67
+ * { name: scalarField(z.string()), email: scalarField(z.email()) },
68
+ * { name: 'John', email: 'john@example.com' },
69
+ * {
70
+ * serviceContext: ctx,
71
+ * operation: 'create',
72
+ * allowOptionalFields: false,
73
+ * loadExisting: () => Promise.resolve(undefined),
74
+ * },
75
+ * );
76
+ * ```
77
+ */
78
+ export async function transformFields<
79
+ TFields extends Record<string, AnyFieldDefinition>,
80
+ >(
81
+ fields: TFields,
82
+ input: InferInput<TFields>,
83
+ {
84
+ serviceContext,
85
+ operation,
86
+ allowOptionalFields,
87
+ loadExisting,
88
+ }: {
89
+ serviceContext: ServiceContext;
90
+ operation: DataOperationType;
91
+ allowOptionalFields: boolean;
92
+ loadExisting: () => Promise<object | undefined>;
93
+ },
94
+ ): Promise<{
95
+ data: InferFieldsOutput<TFields>;
96
+ hooks: AnyOperationHooks;
97
+ }> {
98
+ const hooks: Required<AnyOperationHooks> = {
99
+ beforeExecute: [],
100
+ afterExecute: [],
101
+ afterCommit: [],
102
+ };
103
+
104
+ const data = {} as {
105
+ [K in keyof TFields]: InferFieldOutput<TFields[K]>;
106
+ };
107
+
108
+ for (const [key, field] of Object.entries(fields)) {
109
+ const fieldKey = key as keyof typeof input;
110
+ const value = input[fieldKey];
111
+
112
+ if (allowOptionalFields && value === undefined) continue;
113
+
114
+ const result = await field.processInput(value, {
115
+ operation,
116
+ serviceContext,
117
+ fieldName: fieldKey as string,
118
+ loadExisting,
119
+ });
120
+
121
+ if (result.data !== undefined) {
122
+ data[fieldKey as keyof TFields] = result.data as InferFieldOutput<
123
+ TFields[keyof TFields]
124
+ >;
125
+ }
126
+
127
+ if (result.hooks) {
128
+ hooks.beforeExecute.push(...(result.hooks.beforeExecute ?? []));
129
+ hooks.afterExecute.push(...(result.hooks.afterExecute ?? []));
130
+ hooks.afterCommit.push(...(result.hooks.afterCommit ?? []));
131
+ }
132
+ }
133
+
134
+ const create = {} as InferFieldsCreateOutput<TFields>;
135
+ const update = {} as InferFieldsUpdateOutput<TFields>;
136
+ for (const [key, value] of Object.entries<
137
+ InferFieldOutput<TFields[keyof TFields]>
138
+ >(data)) {
139
+ if (value.create !== undefined) {
140
+ create[key as keyof TFields] =
141
+ value.create as InferFieldsCreateOutput<TFields>[keyof TFields];
142
+ }
143
+ if (value.update !== undefined) {
144
+ update[key as keyof TFields] =
145
+ value.update as InferFieldsUpdateOutput<TFields>[keyof TFields];
146
+ }
147
+ }
148
+
149
+ return { data: { create, update }, hooks };
150
+ }
151
+
152
+ /**
153
+ * =========================================
154
+ * Schema Generation Utilities
155
+ * =========================================
156
+ */
157
+
158
+ /**
159
+ * Generates a Zod schema for create operations from field definitions.
160
+ *
161
+ * Extracts the Zod schema from each field definition and combines them
162
+ * into a single object schema. This schema can be used for validation
163
+ * in GraphQL resolvers, REST endpoints, tRPC procedures, or OpenAPI documentation.
164
+ *
165
+ * @template TFields - Record of field definitions
166
+ * @param fields - Field definitions to extract schemas from
167
+ * @returns Zod object schema preserving each field's optionality
168
+ */
169
+ export function generateCreateSchema<
170
+ TFields extends Record<string, AnyFieldDefinition>,
171
+ >(fields: TFields): InferInputSchema<TFields> {
172
+ const shape = Object.fromEntries(
173
+ Object.entries(fields).map(([key, field]) => [key, field.schema]),
174
+ ) as {
175
+ [K in keyof TFields]: TFields[K]['schema'];
176
+ };
177
+
178
+ return z.object(shape) as InferInputSchema<TFields>;
179
+ }
180
+
181
+ type PartialSchema<T extends z.ZodObject<z.ZodRawShape>> = z.ZodObject<{
182
+ [K in keyof T['shape']]: z.ZodOptional<T['shape'][K]>;
183
+ }>;
184
+
185
+ /**
186
+ * Generates a Zod schema for update operations from field definitions.
187
+ *
188
+ * Wraps {@link generateCreateSchema} with `.partial()` so every field becomes
189
+ * optional, allowing callers to supply only the fields they want to change.
190
+ *
191
+ * @template TFields - Record of field definitions
192
+ * @param fields - Field definitions to extract schemas from
193
+ * @returns Partial Zod object schema where all fields are optional
194
+ */
195
+ export function generateUpdateSchema<
196
+ TFields extends Record<string, AnyFieldDefinition>,
197
+ >(fields: TFields): PartialSchema<InferInputSchema<TFields>> {
198
+ return generateCreateSchema(fields).partial() as PartialSchema<
199
+ InferInputSchema<TFields>
200
+ >;
201
+ }
@@ -108,7 +108,7 @@ function connectCreate<
108
108
  * owner: relationHelpers.connectUpdate({ userId: null, tenantId: 'tenant-1' })
109
109
  *
110
110
  * @example
111
- * // Composite key - no change (if any field is undefined)
111
+ * // Composite key - no change (if any field is undefined, the entire relation is unchanged)
112
112
  * owner: relationHelpers.connectUpdate({ userId: undefined, tenantId: 'tenant-1' })
113
113
  */
114
114
  function connectUpdate<
@@ -151,33 +151,28 @@ function connectUpdate<
151
151
  * support for both single-field and composite key relations.
152
152
  *
153
153
  * @example
154
- * // In a create operation with single field relations
155
- * buildData: ({ todoListId, assigneeId, ...rest }) => ({
156
- * todoList: relationHelpers.connectCreate({ id: todoListId }),
157
- * assignee: relationHelpers.connectCreate({ id: assigneeId }), // undefined if assigneeId is null
158
- * ...rest,
159
- * })
154
+ * // In a create execute function with single field relations
155
+ * execute: async ({ tx, data: { todoListId, assigneeId, ...rest }, query }) =>
156
+ * tx.todoItem.create({
157
+ * data: {
158
+ * todoList: relationHelpers.connectCreate({ id: todoListId }),
159
+ * assignee: relationHelpers.connectCreate({ id: assigneeId }),
160
+ * ...rest,
161
+ * },
162
+ * ...query,
163
+ * })
160
164
  *
161
165
  * @example
162
- * // In a create operation with composite key relation
163
- * buildData: ({ userId, tenantId, ...rest }) => ({
164
- * owner: relationHelpers.connectCreate({ userId, tenantId }), // undefined if any field is null
165
- * ...rest,
166
- * })
167
- *
168
- * @example
169
- * // In an update operation with single field
170
- * buildData: ({ assigneeId, ...rest }) => ({
171
- * assignee: relationHelpers.connectUpdate({ id: assigneeId }),
172
- * ...rest,
173
- * })
174
- *
175
- * @example
176
- * // In an update operation with composite key
177
- * buildData: ({ userId, tenantId, ...rest }) => ({
178
- * owner: relationHelpers.connectUpdate({ userId, tenantId }),
179
- * ...rest,
180
- * })
166
+ * // In an update execute function with single field
167
+ * execute: async ({ tx, data: { assigneeId, ...rest }, query }) =>
168
+ * tx.todoItem.update({
169
+ * where,
170
+ * data: {
171
+ * assignee: relationHelpers.connectUpdate({ id: assigneeId }),
172
+ * ...rest,
173
+ * },
174
+ * ...query,
175
+ * })
181
176
  */
182
177
  export const relationHelpers = {
183
178
  /**
@@ -1,5 +1,15 @@
1
1
  // @ts-nocheck
2
2
 
3
+ import type {
4
+ GetPayload,
5
+ ModelPropName,
6
+ ModelQuery,
7
+ WhereUniqueInput,
8
+ } from '$prismaTypes';
9
+ import type {
10
+ GlobalRoleCheck,
11
+ InstanceRoleCheck,
12
+ } from '%authorizerUtilsImports';
3
13
  import type { PrismaClient } from '%prismaGeneratedImports';
4
14
  import type { ServiceContext } from '%serviceContextImports';
5
15
  import type { ITXClientDenyList } from '@prisma/client/runtime/client';
@@ -166,22 +176,14 @@ export interface FieldTransformData<TCreateOutput, TUpdateOutput> {
166
176
  /**
167
177
  * Result of field processing, including transformed data and optional hooks.
168
178
  *
169
- * The data can be either synchronous or asynchronous (resolved inside transaction).
170
179
  * Hooks allow fields to perform side effects during the operation lifecycle.
171
180
  *
172
181
  * @template TCreateOutput - Data type for create operations
173
182
  * @template TUpdateOutput - Data type for update operations
174
183
  */
175
184
  export interface FieldTransformResult<TCreateOutput, TUpdateOutput> {
176
- /**
177
- * Transformed field data or an async function that resolves to field data.
178
- * Async functions are resolved inside the transaction, allowing access to tx client.
179
- */
180
- data?:
181
- | FieldTransformData<TCreateOutput, TUpdateOutput>
182
- | ((
183
- tx: PrismaTransaction,
184
- ) => Promise<FieldTransformData<TCreateOutput, TUpdateOutput>>);
185
+ /** Transformed field data for create and update operations */
186
+ data?: FieldTransformData<TCreateOutput, TUpdateOutput>;
185
187
 
186
188
  /** Optional hooks to execute during operation lifecycle */
187
189
  hooks?: AnyOperationHooks;
@@ -228,10 +230,8 @@ export interface FieldDefinition<
228
230
  schema: TInputSchema;
229
231
 
230
232
  /**
231
- * Processes and transforms an input value.
232
- *
233
- * Note: Validation happens at the operation level (defineCreateOperation/defineUpdateOperation),
234
- * not at the field level. This function receives already-validated input.
233
+ * Processes and transforms an already-validated input value into
234
+ * Prisma-compatible create/update data.
235
235
  *
236
236
  * @param value - The validated input value to process
237
237
  * @param ctx - Context about the operation
@@ -370,3 +370,361 @@ export interface InferFieldsOutput<
370
370
  /** Field outputs for update operations */
371
371
  update: InferFieldsUpdateOutput<TFields>;
372
372
  }
373
+
374
+ /**
375
+ * =========================================
376
+ * Operation Plans
377
+ * =========================================
378
+ */
379
+
380
+ type HookPhase = keyof Required<AnyOperationHooks>;
381
+ type HookFn<TPhase extends HookPhase> =
382
+ Required<AnyOperationHooks>[TPhase][number];
383
+
384
+ /**
385
+ * Immutable plan for a create operation, returned by `composeCreate`.
386
+ *
387
+ * Contains the resolved field data, collected hooks, and service context.
388
+ * Use `mapData` and `addHook` to derive new plans — the original is never mutated.
389
+ */
390
+ export class CreatePlan<
391
+ TModelName extends ModelPropName,
392
+ TFields extends Record<string, AnyFieldDefinition>,
393
+ > {
394
+ /** The Prisma model name */
395
+ readonly model: TModelName;
396
+
397
+ /** Resolved field data for the create operation */
398
+ readonly data: InferFieldsCreateOutput<TFields>;
399
+
400
+ /** Collected hooks from field processing and any `addHook` calls */
401
+ readonly hooks: Readonly<Required<AnyOperationHooks>>;
402
+
403
+ /** Service context for the operation */
404
+ readonly serviceContext: ServiceContext;
405
+
406
+ constructor(args: {
407
+ model: TModelName;
408
+ data: InferFieldsCreateOutput<TFields>;
409
+ hooks: Required<AnyOperationHooks>;
410
+ serviceContext: ServiceContext;
411
+ }) {
412
+ this.model = args.model;
413
+ this.data = args.data;
414
+ this.hooks = args.hooks;
415
+ this.serviceContext = args.serviceContext;
416
+ }
417
+
418
+ /** Return a new plan with transformed data. */
419
+ mapData(
420
+ fn: (
421
+ data: InferFieldsCreateOutput<TFields>,
422
+ ) => InferFieldsCreateOutput<TFields>,
423
+ ): CreatePlan<TModelName, TFields> {
424
+ return new CreatePlan({
425
+ model: this.model,
426
+ data: fn(this.data),
427
+ hooks: this.hooks,
428
+ serviceContext: this.serviceContext,
429
+ });
430
+ }
431
+
432
+ /** Return a new plan with an additional hook appended to the given phase. */
433
+ addHook<TPhase extends HookPhase>(
434
+ phase: TPhase,
435
+ hook: HookFn<TPhase>,
436
+ ): CreatePlan<TModelName, TFields> {
437
+ return new CreatePlan({
438
+ model: this.model,
439
+ data: this.data,
440
+ hooks: {
441
+ ...this.hooks,
442
+ [phase]: [...this.hooks[phase], hook],
443
+ },
444
+ serviceContext: this.serviceContext,
445
+ });
446
+ }
447
+ }
448
+
449
+ /**
450
+ * Immutable plan for an update operation, returned by `composeUpdate`.
451
+ *
452
+ * Contains the resolved field data, collected hooks, existing item access,
453
+ * and service context. Use `mapData` and `addHook` to derive new plans —
454
+ * the original is never mutated.
455
+ */
456
+ export class UpdatePlan<
457
+ TModelName extends ModelPropName,
458
+ TFields extends Record<string, AnyFieldDefinition>,
459
+ > {
460
+ /** The Prisma model name */
461
+ readonly model: TModelName;
462
+
463
+ /** Resolved field data for the update operation */
464
+ readonly data: InferFieldsUpdateOutput<TFields>;
465
+
466
+ /** Collected hooks from field processing and any `addHook` calls */
467
+ readonly hooks: Readonly<Required<AnyOperationHooks>>;
468
+
469
+ /** Service context for the operation */
470
+ readonly serviceContext: ServiceContext;
471
+
472
+ /**
473
+ * Lazy loader for the existing item. Caches the result.
474
+ * Call this if you need to inspect the existing item for diffing.
475
+ */
476
+ readonly loadExisting: () => Promise<GetPayload<TModelName>>;
477
+
478
+ constructor(args: {
479
+ model: TModelName;
480
+ data: InferFieldsUpdateOutput<TFields>;
481
+ hooks: Required<AnyOperationHooks>;
482
+ serviceContext: ServiceContext;
483
+ loadExisting: () => Promise<GetPayload<TModelName>>;
484
+ }) {
485
+ this.model = args.model;
486
+ this.data = args.data;
487
+ this.hooks = args.hooks;
488
+ this.serviceContext = args.serviceContext;
489
+ this.loadExisting = args.loadExisting;
490
+ }
491
+
492
+ /** Return a new plan with transformed data. */
493
+ mapData(
494
+ fn: (
495
+ data: InferFieldsUpdateOutput<TFields>,
496
+ ) => InferFieldsUpdateOutput<TFields>,
497
+ ): UpdatePlan<TModelName, TFields> {
498
+ return new UpdatePlan({
499
+ model: this.model,
500
+ data: fn(this.data),
501
+ hooks: this.hooks,
502
+ serviceContext: this.serviceContext,
503
+ loadExisting: this.loadExisting,
504
+ });
505
+ }
506
+
507
+ /** Return a new plan with an additional hook appended to the given phase. */
508
+ addHook<TPhase extends HookPhase>(
509
+ phase: TPhase,
510
+ hook: HookFn<TPhase>,
511
+ ): UpdatePlan<TModelName, TFields> {
512
+ return new UpdatePlan({
513
+ model: this.model,
514
+ data: this.data,
515
+ hooks: {
516
+ ...this.hooks,
517
+ [phase]: [...this.hooks[phase], hook],
518
+ },
519
+ serviceContext: this.serviceContext,
520
+ loadExisting: this.loadExisting,
521
+ });
522
+ }
523
+ }
524
+
525
+ /**
526
+ * =========================================
527
+ * Compose Configs
528
+ * =========================================
529
+ */
530
+
531
+ /**
532
+ * Configuration for `composeCreate`.
533
+ */
534
+ export interface ComposeCreateConfig<
535
+ TModelName extends ModelPropName,
536
+ TFields extends Record<string, AnyFieldDefinition>,
537
+ > {
538
+ model: TModelName;
539
+ fields: TFields;
540
+ input: InferInput<TFields>;
541
+ context: ServiceContext;
542
+
543
+ /**
544
+ * Optional authorization checks. Only global roles are allowed for create
545
+ * since there is no existing instance to check against.
546
+ * Runs before field processing — fails fast on unauthorized access.
547
+ */
548
+ authorize?: GlobalRoleCheck[];
549
+ }
550
+
551
+ /**
552
+ * Configuration for `composeUpdate`.
553
+ *
554
+ * The `loadExisting` function is a cached lazy loader for the existing item.
555
+ * It is called by field processing (e.g., nested relations) when they need
556
+ * to read the current state. The caller defines this function to control
557
+ * how the item is fetched (e.g., with extra includes).
558
+ */
559
+ export interface ComposeUpdateConfig<
560
+ TModelName extends ModelPropName,
561
+ TFields extends Record<string, AnyFieldDefinition>,
562
+ > {
563
+ model: TModelName;
564
+ fields: TFields;
565
+ input: Partial<InferInput<TFields>>;
566
+ context: ServiceContext;
567
+ loadExisting: () => Promise<GetPayload<TModelName>>;
568
+
569
+ /**
570
+ * Optional authorization checks. Supports both global roles (strings)
571
+ * and instance-level checks (functions that receive the existing item).
572
+ * Runs before field processing — fails fast on unauthorized access.
573
+ */
574
+ authorize?: (GlobalRoleCheck | InstanceRoleCheck<GetPayload<TModelName>>)[];
575
+ }
576
+
577
+ /**
578
+ * =========================================
579
+ * Data Service Input Types
580
+ * =========================================
581
+ */
582
+
583
+ /**
584
+ * Input type for data service create functions.
585
+ */
586
+ export interface DataCreateInput<
587
+ TModelName extends ModelPropName,
588
+ TFields extends Record<string, AnyFieldDefinition>,
589
+ TQueryArgs extends ModelQuery<TModelName> = ModelQuery<TModelName>,
590
+ > {
591
+ data: InferInput<TFields>;
592
+ query?: TQueryArgs;
593
+ context: ServiceContext;
594
+ }
595
+
596
+ /**
597
+ * Input type for data service update functions.
598
+ */
599
+ export interface DataUpdateInput<
600
+ TModelName extends ModelPropName,
601
+ TFields extends Record<string, AnyFieldDefinition>,
602
+ TQueryArgs extends ModelQuery<TModelName> = ModelQuery<TModelName>,
603
+ > {
604
+ where: WhereUniqueInput<TModelName>;
605
+ data: Partial<InferInput<TFields>>;
606
+ query?: TQueryArgs;
607
+ context: ServiceContext;
608
+ }
609
+
610
+ /**
611
+ * Input type for data service delete functions.
612
+ */
613
+ export interface DataDeleteInput<
614
+ TModelName extends ModelPropName,
615
+ TQueryArgs extends ModelQuery<TModelName> = ModelQuery<TModelName>,
616
+ > {
617
+ where: WhereUniqueInput<TModelName>;
618
+ query?: TQueryArgs;
619
+ context: ServiceContext;
620
+ }
621
+
622
+ /**
623
+ * =========================================
624
+ * Commit Configs
625
+ * =========================================
626
+ */
627
+
628
+ /**
629
+ * Configuration for `commitCreate`.
630
+ */
631
+ export interface CommitCreateConfig<
632
+ TModelName extends ModelPropName,
633
+ TFields extends Record<string, AnyFieldDefinition>,
634
+ TQueryArgs extends ModelQuery<TModelName>,
635
+ > {
636
+ /** Prisma query arguments (include) to shape returned data */
637
+ query?: TQueryArgs;
638
+
639
+ /**
640
+ * Optional override for the default refetch behavior.
641
+ * By default, records are refetched using `findUnique({ where: { id } })`
642
+ * via a generic Prisma delegate. Provide this if you need custom refetch logic.
643
+ */
644
+ refetchWithQuery?: (
645
+ result: GetPayload<TModelName>,
646
+ query: TQueryArgs,
647
+ ) => Promise<GetPayload<TModelName>>;
648
+
649
+ /** Execute the Prisma create operation inside the transaction */
650
+ execute: (args: {
651
+ tx: PrismaTransaction;
652
+ data: InferFieldsCreateOutput<TFields>;
653
+ query: { include: NonNullable<TQueryArgs['include']> };
654
+ serviceContext: ServiceContext;
655
+ }) => Promise<GetPayload<TModelName>>;
656
+ }
657
+
658
+ /**
659
+ * Configuration for `commitUpdate`.
660
+ */
661
+ export interface CommitUpdateConfig<
662
+ TModelName extends ModelPropName,
663
+ TFields extends Record<string, AnyFieldDefinition>,
664
+ TQueryArgs extends ModelQuery<TModelName>,
665
+ > {
666
+ /** Prisma query arguments (include) to shape returned data */
667
+ query?: TQueryArgs;
668
+
669
+ /**
670
+ * Optional override for the default refetch behavior.
671
+ * By default, records are refetched using `findUnique({ where: { id } })`
672
+ * via a generic Prisma delegate. Provide this if you need custom refetch logic.
673
+ */
674
+ refetchWithQuery?: (
675
+ result: GetPayload<TModelName>,
676
+ query: TQueryArgs,
677
+ ) => Promise<GetPayload<TModelName>>;
678
+
679
+ /** Execute the Prisma update operation inside the transaction */
680
+ execute: (args: {
681
+ tx: PrismaTransaction;
682
+ data: InferFieldsUpdateOutput<TFields>;
683
+ query: { include: NonNullable<TQueryArgs['include']> };
684
+ serviceContext: ServiceContext;
685
+ }) => Promise<GetPayload<TModelName>>;
686
+ }
687
+
688
+ /**
689
+ * Configuration for `commitDelete`.
690
+ *
691
+ * Delete has no compose step since there are no fields to process.
692
+ * Authorization, hooks, and execution are all handled directly.
693
+ */
694
+ export interface CommitDeleteConfig<
695
+ TModelName extends ModelPropName,
696
+ TQueryArgs extends ModelQuery<TModelName>,
697
+ > {
698
+ /** The Prisma model name */
699
+ model: TModelName;
700
+
701
+ /** Prisma query arguments (include) to shape returned data */
702
+ query?: TQueryArgs;
703
+
704
+ /** Service context for the operation */
705
+ context: ServiceContext;
706
+
707
+ /**
708
+ * Optional authorization checks. Supports both global roles (strings)
709
+ * and instance-level checks (functions that receive the existing item).
710
+ * Runs before hooks and delete execution.
711
+ */
712
+ authorize?: (GlobalRoleCheck | InstanceRoleCheck<GetPayload<TModelName>>)[];
713
+
714
+ /** Optional hooks to run during the delete lifecycle */
715
+ hooks?: Partial<AnyOperationHooks>;
716
+
717
+ /**
718
+ * Lazy loader for the existing item. Required when using instance-level
719
+ * authorization or hooks that need to inspect the record before deletion.
720
+ * The result is cached so the item is fetched at most once.
721
+ */
722
+ loadExisting?: () => Promise<GetPayload<TModelName>>;
723
+
724
+ /** Execute the Prisma delete operation inside the transaction */
725
+ execute: (args: {
726
+ tx: PrismaTransaction;
727
+ query: { include: NonNullable<TQueryArgs['include']> };
728
+ serviceContext: ServiceContext;
729
+ }) => Promise<GetPayload<TModelName>>;
730
+ }
@@ -11,8 +11,10 @@ export * from './prisma-field/index.js';
11
11
  export * from './prisma-model-authorizer/index.js';
12
12
  export * from './prisma-model-id/index.js';
13
13
  export * from './prisma-model-index/index.js';
14
+ export * from './prisma-model-query-filter/index.js';
14
15
  export * from './prisma-model-unique/index.js';
15
16
  export * from './prisma-model/index.js';
17
+ export * from './prisma-query-filter-utils/index.js';
16
18
  export * from './prisma-relation-field/index.js';
17
19
  export * from './prisma/index.js';
18
20
  //# sourceMappingURL=index.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/generators/prisma/index.ts"],"names":[],"mappings":"AAAA,cAAc,uBAAuB,CAAC;AACtC,cAAc,uBAAuB,CAAC;AACtC,cAAc,oCAAoC,CAAC;AACnD,cAAc,+BAA+B,CAAC;AAC9C,cAAc,+BAA+B,CAAC;AAC9C,cAAc,qCAAqC,CAAC;AACpD,cAAc,gCAAgC,CAAC;AAC/C,cAAc,+BAA+B,CAAC;AAC9C,cAAc,wBAAwB,CAAC;AACvC,cAAc,yBAAyB,CAAC;AACxC,cAAc,oCAAoC,CAAC;AACnD,cAAc,4BAA4B,CAAC;AAC3C,cAAc,+BAA+B,CAAC;AAC9C,cAAc,gCAAgC,CAAC;AAC/C,cAAc,yBAAyB,CAAC;AACxC,cAAc,kCAAkC,CAAC;AACjD,cAAc,mBAAmB,CAAC"}
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/generators/prisma/index.ts"],"names":[],"mappings":"AAAA,cAAc,uBAAuB,CAAC;AACtC,cAAc,uBAAuB,CAAC;AACtC,cAAc,oCAAoC,CAAC;AACnD,cAAc,+BAA+B,CAAC;AAC9C,cAAc,+BAA+B,CAAC;AAC9C,cAAc,qCAAqC,CAAC;AACpD,cAAc,gCAAgC,CAAC;AAC/C,cAAc,+BAA+B,CAAC;AAC9C,cAAc,wBAAwB,CAAC;AACvC,cAAc,yBAAyB,CAAC;AACxC,cAAc,oCAAoC,CAAC;AACnD,cAAc,4BAA4B,CAAC;AAC3C,cAAc,+BAA+B,CAAC;AAC9C,cAAc,sCAAsC,CAAC;AACrD,cAAc,gCAAgC,CAAC;AAC/C,cAAc,yBAAyB,CAAC;AACxC,cAAc,sCAAsC,CAAC;AACrD,cAAc,kCAAkC,CAAC;AACjD,cAAc,mBAAmB,CAAC"}