@postxl/schema 2.0.1 → 2.1.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.
@@ -1,5 +1,31 @@
1
1
  import { z } from 'zod';
2
2
  export declare const zModelNameJSON: z.ZodString;
3
+ /**
4
+ * A single composite index entry. Two input shapes are accepted and normalized to
5
+ * the canonical `{ fields, unique, map }` object:
6
+ *
7
+ * - Array shorthand `["a", "b"]` → a multi-column UNIQUE index (`unique: true`),
8
+ * preserving the historical behavior of the `indexes` field.
9
+ * - Object `{ fields, unique?, map? }` → `unique` defaults to `false` (a plain
10
+ * `@@index`), with optional `map` to pin the database constraint name.
11
+ */
12
+ export declare const zModelIndexJSON: z.ZodUnion<readonly [z.ZodPipe<z.ZodArray<z.core.$ZodBranded<z.ZodString, "PXL.FieldName", "out">>, z.ZodTransform<{
13
+ fields: (string & z.core.$brand<"PXL.FieldName">)[];
14
+ unique: boolean;
15
+ map: string | undefined;
16
+ }, (string & z.core.$brand<"PXL.FieldName">)[]>>, z.ZodPipe<z.ZodObject<{
17
+ fields: z.ZodArray<z.core.$ZodBranded<z.ZodString, "PXL.FieldName", "out">>;
18
+ unique: z.ZodOptional<z.ZodBoolean>;
19
+ map: z.ZodOptional<z.ZodString>;
20
+ }, z.core.$strip>, z.ZodTransform<{
21
+ fields: (string & z.core.$brand<"PXL.FieldName">)[];
22
+ unique: boolean;
23
+ map: string | undefined;
24
+ }, {
25
+ fields: (string & z.core.$brand<"PXL.FieldName">)[];
26
+ unique?: boolean | undefined;
27
+ map?: string | undefined;
28
+ }>>]>;
3
29
  export declare const zModelJSON: z.ZodPreprocess<z.ZodObject<{
4
30
  name: z.ZodString;
5
31
  description: z.ZodOptional<z.ZodString>;
@@ -38,7 +64,23 @@ export declare const zModelJSON: z.ZodPreprocess<z.ZodObject<{
38
64
  labelField: z.ZodOptional<z.ZodString>;
39
65
  keyField: z.ZodOptional<z.ZodString>;
40
66
  defaultSort: z.ZodOptional<z.ZodArray<z.core.$ZodBranded<z.ZodString, "PXL.FieldName", "out">>>;
41
- indexes: z.ZodOptional<z.ZodArray<z.ZodArray<z.core.$ZodBranded<z.ZodString, "PXL.FieldName", "out">>>>;
67
+ indexes: z.ZodOptional<z.ZodArray<z.ZodUnion<readonly [z.ZodPipe<z.ZodArray<z.core.$ZodBranded<z.ZodString, "PXL.FieldName", "out">>, z.ZodTransform<{
68
+ fields: (string & z.core.$brand<"PXL.FieldName">)[];
69
+ unique: boolean;
70
+ map: string | undefined;
71
+ }, (string & z.core.$brand<"PXL.FieldName">)[]>>, z.ZodPipe<z.ZodObject<{
72
+ fields: z.ZodArray<z.core.$ZodBranded<z.ZodString, "PXL.FieldName", "out">>;
73
+ unique: z.ZodOptional<z.ZodBoolean>;
74
+ map: z.ZodOptional<z.ZodString>;
75
+ }, z.core.$strip>, z.ZodTransform<{
76
+ fields: (string & z.core.$brand<"PXL.FieldName">)[];
77
+ unique: boolean;
78
+ map: string | undefined;
79
+ }, {
80
+ fields: (string & z.core.$brand<"PXL.FieldName">)[];
81
+ unique?: boolean | undefined;
82
+ map?: string | undefined;
83
+ }>>]>>>;
42
84
  seed: z.ZodOptional<z.ZodArray<z.ZodAny>>;
43
85
  source: z.ZodOptional<z.ZodRecord<z.ZodString, z.ZodUnknown>>;
44
86
  faker: z.ZodOptional<z.ZodObject<{
@@ -1,6 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.zModelJSON = exports.zModelNameJSON = void 0;
3
+ exports.zModelJSON = exports.zModelIndexJSON = exports.zModelNameJSON = void 0;
4
4
  const zod_1 = require("zod");
5
5
  const utils_1 = require("@postxl/utils");
6
6
  const brands_1 = require("../field/shared/brands");
@@ -17,6 +17,33 @@ exports.zModelNameJSON = zod_1.z
17
17
  error: (issue) => `Model name "${(0, utils_1.red)(issue.input)}" must be PascalCase, i.e. ${(0, utils_1.toPascalCase)(issue.input)}!`,
18
18
  })
19
19
  .describe('Name of the model.');
20
+ const zModelIndexFields = zod_1.z.array(brands_1.zFieldName).min(1, { error: 'Index must have at least 1 field' });
21
+ /**
22
+ * A single composite index entry. Two input shapes are accepted and normalized to
23
+ * the canonical `{ fields, unique, map }` object:
24
+ *
25
+ * - Array shorthand `["a", "b"]` → a multi-column UNIQUE index (`unique: true`),
26
+ * preserving the historical behavior of the `indexes` field.
27
+ * - Object `{ fields, unique?, map? }` → `unique` defaults to `false` (a plain
28
+ * `@@index`), with optional `map` to pin the database constraint name.
29
+ */
30
+ exports.zModelIndexJSON = zod_1.z.union([
31
+ zModelIndexFields.transform((fields) => ({ fields, unique: true, map: undefined })),
32
+ zod_1.z
33
+ .object({
34
+ fields: zModelIndexFields.describe('The field names that make up the index, in index order.'),
35
+ unique: zod_1.z
36
+ .boolean()
37
+ .optional()
38
+ .describe('Emit a `@@unique` multi-column constraint instead of a plain `@@index`. Defaults to `false`.'),
39
+ map: zod_1.z
40
+ .string()
41
+ .min(1, { error: 'Index `map` must not be empty' })
42
+ .optional()
43
+ .describe('Explicit database constraint/index name (Prisma `map:`). Match an existing name to avoid drift.'),
44
+ })
45
+ .transform(({ fields, unique, map }) => ({ fields, unique: unique ?? false, map })),
46
+ ]);
20
47
  exports.zModelJSON = zod_1.z.preprocess((input) => {
21
48
  if (typeof input === 'object' && input !== null) {
22
49
  const original = input;
@@ -98,9 +125,17 @@ The key field is used in navigation, Excel import, and other places where a huma
98
125
  Each entry references a field name and is applied ascending in the given order.`),
99
126
  indexes: zod_1.z
100
127
  //
101
- .array(zod_1.z.array(brands_1.zFieldName).min(2, { error: 'Index must have at least 2 fields' }))
102
- .optional()
103
- .describe('Each entry is an array of field names that are used to create an index.'),
128
+ .array(exports.zModelIndexJSON)
129
+ .optional().describe(`Composite (multi-column) indexes for the model.
130
+
131
+ Each entry is either:
132
+ - a shorthand array of field names (\`["a", "b"]\`), which creates a multi-column UNIQUE index, or
133
+ - an object \`{ "fields": ["a", "b"], "unique": false, "map": "my_index_name" }\` where
134
+ \`unique\` defaults to \`false\` (a plain \`@@index\`) and \`map\` optionally pins the
135
+ database constraint/index name (use it to match an existing constraint so Prisma
136
+ reports no drift).
137
+
138
+ Field order is preserved verbatim in the generated Prisma index.`),
104
139
  seed: zod_1.z
105
140
  .array(zod_1.z.any())
106
141
  .optional()
@@ -16,7 +16,15 @@ export declare function modelJSONTransformer(input: ModelJSON, ctx: z.Refinement
16
16
  isReadonly: boolean;
17
17
  excelName: string;
18
18
  description: string;
19
- indexes: (string & z.core.$brand<"PXL.FieldName">)[][];
19
+ indexes: ({
20
+ fields: (string & z.core.$brand<"PXL.FieldName">)[];
21
+ unique: boolean;
22
+ map: string | undefined;
23
+ } | {
24
+ fields: (string & z.core.$brand<"PXL.FieldName">)[];
25
+ unique: boolean;
26
+ map: string | undefined;
27
+ })[];
20
28
  seed: any[];
21
29
  faker: {
22
30
  seed: number;
@@ -58,7 +66,15 @@ declare function addDefaults({ description, fields, standardFields, seed, faker,
58
66
  name: string;
59
67
  }[];
60
68
  standardFields: string[];
61
- indexes: (string & z.core.$brand<"PXL.FieldName">)[][];
69
+ indexes: ({
70
+ fields: (string & z.core.$brand<"PXL.FieldName">)[];
71
+ unique: boolean;
72
+ map: string | undefined;
73
+ } | {
74
+ fields: (string & z.core.$brand<"PXL.FieldName">)[];
75
+ unique: boolean;
76
+ map: string | undefined;
77
+ })[];
62
78
  seed: any[];
63
79
  faker: {
64
80
  seed: number;
@@ -112,7 +128,15 @@ declare function brandModel({ name, schema, databaseName, standardFields, defaul
112
128
  [x: string]: unknown;
113
129
  name: string;
114
130
  }[];
115
- indexes: (string & z.core.$brand<"PXL.FieldName">)[][];
131
+ indexes: ({
132
+ fields: (string & z.core.$brand<"PXL.FieldName">)[];
133
+ unique: boolean;
134
+ map: string | undefined;
135
+ } | {
136
+ fields: (string & z.core.$brand<"PXL.FieldName">)[];
137
+ unique: boolean;
138
+ map: string | undefined;
139
+ })[];
116
140
  seed: any[];
117
141
  faker: {
118
142
  seed: number;
@@ -160,7 +184,15 @@ declare function combineFields({ fields, standardFields, ...model }: BrandedMode
160
184
  isReadonly: boolean;
161
185
  excelName: string;
162
186
  description: string;
163
- indexes: (string & z.core.$brand<"PXL.FieldName">)[][];
187
+ indexes: ({
188
+ fields: (string & z.core.$brand<"PXL.FieldName">)[];
189
+ unique: boolean;
190
+ map: string | undefined;
191
+ } | {
192
+ fields: (string & z.core.$brand<"PXL.FieldName">)[];
193
+ unique: boolean;
194
+ map: string | undefined;
195
+ })[];
164
196
  seed: any[];
165
197
  faker: {
166
198
  seed: number;
@@ -128,16 +128,16 @@ function combineFields({ fields, standardFields, ...model }, ctx) {
128
128
  function validateIndexes({ fields, indexes, ...model }, ctx) {
129
129
  const checkedIndexes = new Set();
130
130
  for (const index of indexes) {
131
- for (const fieldName of index) {
131
+ for (const fieldName of index.fields) {
132
132
  if (!fields[fieldName]) {
133
133
  ctx.addIssue({
134
134
  code: zod_1.z.ZodIssueCode.custom,
135
- message: `Index [${index.join(',')} ]in model ${model.name} references unknown field ${fieldName}!`,
135
+ message: `Index [${index.fields.join(',')} ]in model ${model.name} references unknown field ${fieldName}!`,
136
136
  });
137
137
  return zod_1.z.NEVER;
138
138
  }
139
139
  }
140
- const indexName = index.toSorted((a, b) => a.localeCompare(b)).join(',');
140
+ const indexName = index.fields.toSorted((a, b) => a.localeCompare(b)).join(',');
141
141
  if (checkedIndexes.has(indexName)) {
142
142
  ctx.addIssue({
143
143
  code: zod_1.z.ZodIssueCode.custom,
@@ -47,5 +47,21 @@ export type Model = {
47
47
  };
48
48
  auth?: AuthRuleSet;
49
49
  };
50
- export type ModelIndex = Field.FieldName[];
50
+ export type ModelIndex = {
51
+ /**
52
+ * The field names that make up the index, in the order they should be indexed.
53
+ * Order is preserved verbatim in the generated Prisma `@@index`/`@@unique`.
54
+ */
55
+ fields: Field.FieldName[];
56
+ /**
57
+ * When `true`, the index is emitted as a `@@unique([...])` multi-column unique
58
+ * constraint; otherwise as a non-unique `@@index([...])`.
59
+ */
60
+ unique: boolean;
61
+ /**
62
+ * Optional explicit constraint/index name (Prisma `map:`). Set this to match an
63
+ * existing database constraint name so Prisma sees a no-op instead of drift.
64
+ */
65
+ map: string | undefined;
66
+ };
51
67
  export type Seed = Record<string, unknown>;
@@ -110,7 +110,23 @@ export declare const zProjectSchemaJSON: z.ZodPreprocess<z.ZodObject<{
110
110
  labelField: z.ZodOptional<z.ZodString>;
111
111
  keyField: z.ZodOptional<z.ZodString>;
112
112
  defaultSort: z.ZodOptional<z.ZodArray<z.core.$ZodBranded<z.ZodString, "PXL.FieldName", "out">>>;
113
- indexes: z.ZodOptional<z.ZodArray<z.ZodArray<z.core.$ZodBranded<z.ZodString, "PXL.FieldName", "out">>>>;
113
+ indexes: z.ZodOptional<z.ZodArray<z.ZodUnion<readonly [z.ZodPipe<z.ZodArray<z.core.$ZodBranded<z.ZodString, "PXL.FieldName", "out">>, z.ZodTransform<{
114
+ fields: (string & z.core.$brand<"PXL.FieldName">)[];
115
+ unique: boolean;
116
+ map: string | undefined;
117
+ }, (string & z.core.$brand<"PXL.FieldName">)[]>>, z.ZodPipe<z.ZodObject<{
118
+ fields: z.ZodArray<z.core.$ZodBranded<z.ZodString, "PXL.FieldName", "out">>;
119
+ unique: z.ZodOptional<z.ZodBoolean>;
120
+ map: z.ZodOptional<z.ZodString>;
121
+ }, z.core.$strip>, z.ZodTransform<{
122
+ fields: (string & z.core.$brand<"PXL.FieldName">)[];
123
+ unique: boolean;
124
+ map: string | undefined;
125
+ }, {
126
+ fields: (string & z.core.$brand<"PXL.FieldName">)[];
127
+ unique?: boolean | undefined;
128
+ map?: string | undefined;
129
+ }>>]>>>;
114
130
  seed: z.ZodOptional<z.ZodArray<z.ZodAny>>;
115
131
  source: z.ZodOptional<z.ZodRecord<z.ZodString, z.ZodUnknown>>;
116
132
  faker: z.ZodOptional<z.ZodObject<{
@@ -89,7 +89,23 @@ export declare const zProjectSchema: z.ZodPipe<z.ZodPreprocess<z.ZodObject<{
89
89
  labelField: z.ZodOptional<z.ZodString>;
90
90
  keyField: z.ZodOptional<z.ZodString>;
91
91
  defaultSort: z.ZodOptional<z.ZodArray<z.core.$ZodBranded<z.ZodString, "PXL.FieldName", "out">>>;
92
- indexes: z.ZodOptional<z.ZodArray<z.ZodArray<z.core.$ZodBranded<z.ZodString, "PXL.FieldName", "out">>>>;
92
+ indexes: z.ZodOptional<z.ZodArray<z.ZodUnion<readonly [z.ZodPipe<z.ZodArray<z.core.$ZodBranded<z.ZodString, "PXL.FieldName", "out">>, z.ZodTransform<{
93
+ fields: (string & z.core.$brand<"PXL.FieldName">)[];
94
+ unique: boolean;
95
+ map: string | undefined;
96
+ }, (string & z.core.$brand<"PXL.FieldName">)[]>>, z.ZodPipe<z.ZodObject<{
97
+ fields: z.ZodArray<z.core.$ZodBranded<z.ZodString, "PXL.FieldName", "out">>;
98
+ unique: z.ZodOptional<z.ZodBoolean>;
99
+ map: z.ZodOptional<z.ZodString>;
100
+ }, z.core.$strip>, z.ZodTransform<{
101
+ fields: (string & z.core.$brand<"PXL.FieldName">)[];
102
+ unique: boolean;
103
+ map: string | undefined;
104
+ }, {
105
+ fields: (string & z.core.$brand<"PXL.FieldName">)[];
106
+ unique?: boolean | undefined;
107
+ map?: string | undefined;
108
+ }>>]>>>;
93
109
  seed: z.ZodOptional<z.ZodArray<z.ZodAny>>;
94
110
  source: z.ZodOptional<z.ZodRecord<z.ZodString, z.ZodUnknown>>;
95
111
  faker: z.ZodOptional<z.ZodObject<{
@@ -197,7 +213,15 @@ export declare const zProjectSchema: z.ZodPipe<z.ZodPreprocess<z.ZodObject<{
197
213
  labelField?: string | undefined;
198
214
  keyField?: string | undefined;
199
215
  defaultSort?: (string & z.core.$brand<"PXL.FieldName">)[] | undefined;
200
- indexes?: (string & z.core.$brand<"PXL.FieldName">)[][] | undefined;
216
+ indexes?: ({
217
+ fields: (string & z.core.$brand<"PXL.FieldName">)[];
218
+ unique: boolean;
219
+ map: string | undefined;
220
+ } | {
221
+ fields: (string & z.core.$brand<"PXL.FieldName">)[];
222
+ unique: boolean;
223
+ map: string | undefined;
224
+ })[] | undefined;
201
225
  seed?: any[] | undefined;
202
226
  source?: Record<string, unknown> | undefined;
203
227
  faker?: {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@postxl/schema",
3
- "version": "2.0.1",
3
+ "version": "2.1.0",
4
4
  "description": "Decoders for PXL Schema definitions and validation for PXL code generation framework",
5
5
  "main": "./dist/index.js",
6
6
  "module": "./dist/index.js",