@postxl/schema 1.9.0 → 1.10.1

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.
@@ -157,6 +157,11 @@ export declare const createFieldJSONDecoder: (params: {
157
157
  excelName: z.ZodOptional<z.ZodString>;
158
158
  prismaRelationFieldName: z.ZodOptional<z.ZodString>;
159
159
  deepClone: z.ZodOptional<z.ZodBoolean>;
160
+ onDelete: z.ZodOptional<z.ZodEnum<{
161
+ cascade: "cascade";
162
+ setNull: "setNull";
163
+ restrict: "restrict";
164
+ }>>;
160
165
  label: z.ZodOptional<z.ZodString>;
161
166
  description: z.ZodOptional<z.ZodString>;
162
167
  isReadonly: z.ZodOptional<z.ZodBoolean>;
@@ -171,6 +176,7 @@ export declare const createFieldJSONDecoder: (params: {
171
176
  prismaRelationFieldName: (string & z.core.$brand<"PXL.DatabaseFieldName">) | undefined;
172
177
  excelName: string & z.core.$brand<"PXL.ExcelColumnName">;
173
178
  deepClone: boolean;
179
+ onDelete: "cascade" | "setNull" | "restrict";
174
180
  description: string | undefined;
175
181
  isReadonly: boolean;
176
182
  isRequired: boolean;
@@ -184,6 +190,7 @@ export declare const createFieldJSONDecoder: (params: {
184
190
  excelName?: string | undefined;
185
191
  prismaRelationFieldName?: string | undefined;
186
192
  deepClone?: boolean | undefined;
193
+ onDelete?: "cascade" | "setNull" | "restrict" | undefined;
187
194
  label?: string | undefined;
188
195
  description?: string | undefined;
189
196
  isReadonly?: boolean | undefined;
@@ -269,6 +276,11 @@ export declare const zFieldJSONInput: (params: {
269
276
  excelName: z.ZodOptional<z.ZodString>;
270
277
  prismaRelationFieldName: z.ZodOptional<z.ZodString>;
271
278
  deepClone: z.ZodOptional<z.ZodBoolean>;
279
+ onDelete: z.ZodOptional<z.ZodEnum<{
280
+ cascade: "cascade";
281
+ setNull: "setNull";
282
+ restrict: "restrict";
283
+ }>>;
272
284
  label: z.ZodOptional<z.ZodString>;
273
285
  description: z.ZodOptional<z.ZodString>;
274
286
  isReadonly: z.ZodOptional<z.ZodBoolean>;
@@ -4,6 +4,14 @@ import * as Branded from './shared/brands';
4
4
  import { FieldCommon, FieldTransformer } from './shared/types';
5
5
  export type FieldKindRelation = 'relation';
6
6
  export declare const fieldKindRelation: FieldKindRelation;
7
+ /**
8
+ * Behavior applied to entities carrying this relation when the referenced
9
+ * entity is deleted. Cascade deletion is handled in the update service,
10
+ * not in the database, so it also works with in-memory repositories and
11
+ * keeps internal caches consistent.
12
+ */
13
+ export type OnDeleteBehavior = 'cascade' | 'setNull' | 'restrict';
14
+ export declare const onDeleteBehaviors: readonly ["cascade", "setNull", "restrict"];
7
15
  export type FieldRelation = FieldCommon<FieldKindRelation> & {
8
16
  type: Branded.IdType;
9
17
  referencedModelName: ModelName;
@@ -20,6 +28,14 @@ export type FieldRelation = FieldCommon<FieldKindRelation> & {
20
28
  * Note: Setting this to true on a relation to a readonly model will cause a validation error.
21
29
  */
22
30
  deepClone: boolean;
31
+ /**
32
+ * Behavior when the referenced entity is deleted.
33
+ *
34
+ * - `cascade` (default): this entity is also deleted.
35
+ * - `setNull`: the foreign key is set to null. Only valid for optional relations.
36
+ * - `restrict`: the parent deletion is rejected if any referencing entities exist.
37
+ */
38
+ onDelete: OnDeleteBehavior;
23
39
  /**
24
40
  * Name of the unbranded TypeScript type of the field, e.g. string
25
41
  *
@@ -39,6 +55,11 @@ export declare const createRelationJSONDecoder: ({ modelNames }: {
39
55
  excelName: z.ZodOptional<z.ZodString>;
40
56
  prismaRelationFieldName: z.ZodOptional<z.ZodString>;
41
57
  deepClone: z.ZodOptional<z.ZodBoolean>;
58
+ onDelete: z.ZodOptional<z.ZodEnum<{
59
+ cascade: "cascade";
60
+ setNull: "setNull";
61
+ restrict: "restrict";
62
+ }>>;
42
63
  label: z.ZodOptional<z.ZodString>;
43
64
  description: z.ZodOptional<z.ZodString>;
44
65
  isReadonly: z.ZodOptional<z.ZodBoolean>;
@@ -55,6 +76,7 @@ export declare function fieldRelationJSONTransformer(fieldInput: FieldRelationJS
55
76
  prismaRelationFieldName: (string & z.core.$brand<"PXL.DatabaseFieldName">) | undefined;
56
77
  excelName: string & z.core.$brand<"PXL.ExcelColumnName">;
57
78
  deepClone: boolean;
79
+ onDelete: "cascade" | "setNull" | "restrict";
58
80
  description: string | undefined;
59
81
  isReadonly: boolean;
60
82
  isRequired: boolean;
@@ -33,7 +33,7 @@ var __importStar = (this && this.__importStar) || (function () {
33
33
  };
34
34
  })();
35
35
  Object.defineProperty(exports, "__esModule", { value: true });
36
- exports.fieldRelationTransformer = exports.createRelationJSONDecoder = exports.fieldKindRelation = void 0;
36
+ exports.fieldRelationTransformer = exports.createRelationJSONDecoder = exports.onDeleteBehaviors = exports.fieldKindRelation = void 0;
37
37
  exports.fieldRelationJSONTransformer = fieldRelationJSONTransformer;
38
38
  const zod_1 = require("zod");
39
39
  const utils_1 = require("@postxl/utils");
@@ -42,6 +42,7 @@ const Branded = __importStar(require("./shared/brands"));
42
42
  const Decoders = __importStar(require("./shared/decoders"));
43
43
  const utils_2 = require("./shared/utils");
44
44
  exports.fieldKindRelation = 'relation';
45
+ exports.onDeleteBehaviors = ['cascade', 'setNull', 'restrict'];
45
46
  const createRelationJSONDecoder = ({ modelNames }) => {
46
47
  return zod_1.z.object({
47
48
  name: Decoders.zFieldPropertyName.refine((name) => name.endsWith('Id'), {
@@ -72,6 +73,10 @@ const createRelationJSONDecoder = ({ modelNames }) => {
72
73
  .boolean()
73
74
  .optional()
74
75
  .describe('If true, when cloning an entity, this relation will be deep cloned. If false or undefined, the clone will reference the same entity.'),
76
+ onDelete: zod_1.z
77
+ .enum(exports.onDeleteBehaviors)
78
+ .optional()
79
+ .describe('Behavior when the referenced entity is deleted: "cascade" (default) deletes this entity too, "setNull" nulls the foreign key (optional relations only), "restrict" rejects the deletion if any referencing entities exist.'),
75
80
  label: Decoders.zFieldPropertyLabel,
76
81
  description: Decoders.zFieldPropertyDescription,
77
82
  isReadonly: Decoders.zFieldPropertyIsReadonly,
@@ -95,6 +100,7 @@ function fieldRelationJSONTransformer(fieldInput) {
95
100
  : undefined,
96
101
  excelName: Branded.toExcelColumnName(fieldInput.excelName ?? fieldInput.label ?? fieldInput.name),
97
102
  deepClone: fieldInput.deepClone ?? false,
103
+ onDelete: fieldInput.onDelete ?? 'cascade',
98
104
  description: fieldInput.description,
99
105
  isReadonly: fieldInput.isReadonly ?? false,
100
106
  isRequired,
@@ -120,6 +126,14 @@ const fieldRelationTransformer = ({ fieldInput, model, projectSchema }, ctx) =>
120
126
  });
121
127
  return zod_1.z.NEVER;
122
128
  }
129
+ // Validate that onDelete 'setNull' is only used on optional relations
130
+ if (fieldInput.onDelete === 'setNull' && fieldInput.isRequired) {
131
+ ctx.addIssue({
132
+ code: zod_1.z.ZodIssueCode.custom,
133
+ message: `Field ${model.name}.${fieldInput.name} has onDelete='setNull' but the relation is required. Only optional relations (e.g. "${referencedModel.name}?") can have their foreign key set to null.`,
134
+ });
135
+ return zod_1.z.NEVER;
136
+ }
123
137
  const idField = (0, utils_2.getIdField)(model);
124
138
  if (idField === undefined) {
125
139
  ctx.addIssue({