@prisma/orm-family-sql 8.0.0-rc.14-dev.15 → 8.0.0-rc.14-dev.17
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.
- package/dist/contract-prisma7__provider.mjs +15 -25
- package/dist/contract-prisma7__provider.mjs.map +1 -1
- package/dist/contract-psl.d.mts +6 -24
- package/dist/contract-psl.d.mts.map +1 -1
- package/dist/contract-psl.mjs +1 -1
- package/dist/contract-psl__attribute-specs.d.mts +11 -3
- package/dist/contract-psl__attribute-specs.d.mts.map +1 -1
- package/dist/contract-psl__attribute-specs.mjs +2 -2
- package/dist/contract-psl__provider.mjs +12 -5
- package/dist/contract-psl__provider.mjs.map +1 -1
- package/dist/{control-B8LE0HRq.mjs → control-DIVsNXHK.mjs} +4 -2
- package/dist/control-DIVsNXHK.mjs.map +1 -0
- package/dist/{control--13SHD7X.d.mts → control-nLCfcNhb.d.mts} +4 -1
- package/dist/control-nLCfcNhb.d.mts.map +1 -0
- package/dist/family.d.mts +1 -1
- package/dist/family.mjs +1 -1
- package/dist/family__control.d.mts +1 -1
- package/dist/family__control.mjs +1 -1
- package/dist/family__pack.mjs +1 -1
- package/dist/{interpreter-CkCfPqup-DF7bZvNM.mjs → interpreter-C8cP92FD-Co9RsuUU.mjs} +58 -1549
- package/dist/interpreter-C8cP92FD-Co9RsuUU.mjs.map +1 -0
- package/dist/psl-column-resolution-sNpXQygQ-BQr15EoR.d.mts +28 -0
- package/dist/psl-column-resolution-sNpXQygQ-BQr15EoR.d.mts.map +1 -0
- package/dist/psl-field-resolution-_o74AUE4-CKYNRnzL.mjs +1868 -0
- package/dist/psl-field-resolution-_o74AUE4-CKYNRnzL.mjs.map +1 -0
- package/package.json +18 -18
- package/dist/control--13SHD7X.d.mts.map +0 -1
- package/dist/control-B8LE0HRq.mjs.map +0 -1
- package/dist/interpreter-CkCfPqup-DF7bZvNM.mjs.map +0 -1
- package/dist/sql-attribute-specs-DxpHr9Fk-CeWmMP7y.mjs +0 -543
- package/dist/sql-attribute-specs-DxpHr9Fk-CeWmMP7y.mjs.map +0 -1
|
@@ -0,0 +1,1868 @@
|
|
|
1
|
+
import { s as sqlTextFromCanonical, t as SQL_EXPRESSION_DATA_TYPE_ID } from "./sql-expression-BXx0Ilj-.mjs";
|
|
2
|
+
import { n as checkSqlDefaultBody, o as reservedSqlDefaultBody } from "./validators-DvzOZLel.mjs";
|
|
3
|
+
import { a as storedAsListColumn, i as isValueObjectMember } from "./derived-checks-C_y1rk6O-DdqjzGNy.mjs";
|
|
4
|
+
import { structuredError } from "@prisma/orm-framework/utils/structured-error";
|
|
5
|
+
import { instantiateAuthoringTypeConstructor, isAuthoringEntityTypeDescriptor, validateAuthoringHelperArguments } from "@prisma/orm-framework/components/authoring";
|
|
6
|
+
import { InternalError, isInternalError } from "@prisma/orm-framework/utils/internal-error";
|
|
7
|
+
import { ifDefined } from "@prisma/orm-framework/utils/defined";
|
|
8
|
+
import { codecForRef } from "@prisma/orm-framework/components/codec";
|
|
9
|
+
import { blindCast } from "@prisma/orm-framework/utils/casts";
|
|
10
|
+
import { ArrayLiteralAst, FunctionCallAst, IdentifierAst } from "@prisma/orm-framework/psl-parser/syntax";
|
|
11
|
+
import { assertDefined, invariant } from "@prisma/orm-framework/utils/assertions";
|
|
12
|
+
import { describeTaggedLiteralFailure } from "@prisma/orm-framework/components/control";
|
|
13
|
+
import { notOk, ok } from "@prisma/orm-framework/utils/result";
|
|
14
|
+
import { bool, diagnosticSource, entityRef, fieldAttribute, fieldRef, funcCall, identifier, interpretAttribute, leafDiagnostic, list, modelAttribute, nodePslSpan, numLiteral, oneOf, optional, record, referencedFieldRef, str, taggedLiteral, typeReferenceNode } from "@prisma/orm-framework/psl-parser";
|
|
15
|
+
import { instantiatePslFieldPreset, isBareTypeConstructor, mapPslHelperArgs, reportPresetNotCalled, reportTypeConstructorNotCalled } from "@prisma/orm-framework/psl-parser/interpret";
|
|
16
|
+
//#region ../../../2-sql/2-authoring/contract-psl/dist/psl-field-resolution-_o74AUE4.mjs
|
|
17
|
+
function storageName(symbol, physicalNames) {
|
|
18
|
+
const name = physicalNames.get(symbol);
|
|
19
|
+
assertDefined(name, "Physical names are populated for every model and field before lowering");
|
|
20
|
+
return name;
|
|
21
|
+
}
|
|
22
|
+
function getAttribute(attributes, name) {
|
|
23
|
+
return attributes?.find((attribute) => attribute.name === name);
|
|
24
|
+
}
|
|
25
|
+
function formatDbAttributeMigrationMessage(attribute) {
|
|
26
|
+
const renderedArguments = attribute.args.map((argument) => argument.kind === "named" && argument.name !== void 0 ? `${argument.name}: ${argument.value}` : argument.value).join(", ");
|
|
27
|
+
const argumentList = attribute.args.length === 0 ? "" : `(${renderedArguments})`;
|
|
28
|
+
const constructorName = attribute.name.slice(3);
|
|
29
|
+
return `@${attribute.name}${argumentList} is no longer supported; use ${constructorName}${argumentList} in type position`;
|
|
30
|
+
}
|
|
31
|
+
function getNamedArgument(attribute, name) {
|
|
32
|
+
const entry = attribute.args.find((arg) => arg.kind === "named" && arg.name === name);
|
|
33
|
+
if (entry?.kind !== "named") return;
|
|
34
|
+
return entry.value;
|
|
35
|
+
}
|
|
36
|
+
function mapFieldNamesToColumns(input) {
|
|
37
|
+
const columns = [];
|
|
38
|
+
for (const fieldName of input.fieldNames) {
|
|
39
|
+
const field = input.model.fields[fieldName];
|
|
40
|
+
if (field === void 0) {
|
|
41
|
+
input.diagnostics.push({
|
|
42
|
+
code: "PSL_INVALID_ATTRIBUTE_ARGUMENT",
|
|
43
|
+
message: `${input.entityLabel} references unknown field "${input.model.name}.${fieldName}"`,
|
|
44
|
+
...input.source.at(input.span)
|
|
45
|
+
});
|
|
46
|
+
return;
|
|
47
|
+
}
|
|
48
|
+
columns.push(storageName(field, input.physicalNames));
|
|
49
|
+
}
|
|
50
|
+
return columns;
|
|
51
|
+
}
|
|
52
|
+
function contractError(code, message, options) {
|
|
53
|
+
return structuredError(code, message, options);
|
|
54
|
+
}
|
|
55
|
+
/** A written value the column's codec refuses. */
|
|
56
|
+
const PSL_INVALID_DEFAULT_LITERAL = "PSL_INVALID_DEFAULT_LITERAL";
|
|
57
|
+
/** A written value whose data type the receiving type neither is nor casts from. */
|
|
58
|
+
const PSL_VALUE_TYPE_INCOMPATIBLE = "PSL_VALUE_TYPE_INCOMPATIBLE";
|
|
59
|
+
/** A single value written as the default of a column that holds a list. */
|
|
60
|
+
const PSL_DEFAULT_LIST_EXPECTED = "PSL_DEFAULT_LIST_EXPECTED";
|
|
61
|
+
/** The entry a tag names, or `undefined` when no pack registered that tag. */
|
|
62
|
+
function entryForTag(support, tag) {
|
|
63
|
+
for (const [key, entry] of Object.entries(support.entries)) if (entry.written.kind === "tag" && entry.written.tag === tag) return {
|
|
64
|
+
key,
|
|
65
|
+
entry
|
|
66
|
+
};
|
|
67
|
+
}
|
|
68
|
+
/** Every tag a stack registers, in the order the entries were merged, for a diagnostic. */
|
|
69
|
+
function knownTags(support) {
|
|
70
|
+
return Object.values(support.entries).flatMap((entry) => entry.written.kind === "tag" ? [entry.written.tag] : []);
|
|
71
|
+
}
|
|
72
|
+
function entryForPlain(support, syntax) {
|
|
73
|
+
for (const [key, entry] of Object.entries(support.entries)) if (entry.written.kind === "plain" && entry.written.syntax === syntax) return {
|
|
74
|
+
key,
|
|
75
|
+
entry
|
|
76
|
+
};
|
|
77
|
+
}
|
|
78
|
+
/** The text an entry reads: a tag's text, a string's value, or the word a boolean is written as. */
|
|
79
|
+
function plainText(written) {
|
|
80
|
+
switch (written.kind) {
|
|
81
|
+
case "tag": return written.text;
|
|
82
|
+
case "string": return written.text;
|
|
83
|
+
case "boolean": return String(written.value);
|
|
84
|
+
case "number": return written.text;
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
function messageOf(error) {
|
|
88
|
+
return error instanceof Error ? error.message : String(error);
|
|
89
|
+
}
|
|
90
|
+
/** Read one written value through the entry for its syntax. */
|
|
91
|
+
function readValue(support, written, elementIndex) {
|
|
92
|
+
const refuse = (refusal) => ({
|
|
93
|
+
ok: false,
|
|
94
|
+
refusal: blindCast({
|
|
95
|
+
...refusal,
|
|
96
|
+
elementIndex
|
|
97
|
+
})
|
|
98
|
+
});
|
|
99
|
+
const found = written.kind === "tag" ? entryForTag(support, written.tag) : entryForPlain(support, written.kind);
|
|
100
|
+
if (found === void 0) return written.kind === "tag" ? refuse({
|
|
101
|
+
kind: "unknown-tag",
|
|
102
|
+
tag: written.tag,
|
|
103
|
+
known: knownTags(support)
|
|
104
|
+
}) : refuse({
|
|
105
|
+
kind: "unwritable",
|
|
106
|
+
syntax: written.kind
|
|
107
|
+
});
|
|
108
|
+
const form = found.entry.written;
|
|
109
|
+
if (form.kind === "plain" && form.syntax === "number") {
|
|
110
|
+
const text = plainText(written);
|
|
111
|
+
const classified = form.classify(text);
|
|
112
|
+
if (classified === void 0) return refuse({
|
|
113
|
+
kind: "unreadable",
|
|
114
|
+
message: `no data type of this target holds the number ${text}`
|
|
115
|
+
});
|
|
116
|
+
return {
|
|
117
|
+
ok: true,
|
|
118
|
+
typed: classified
|
|
119
|
+
};
|
|
120
|
+
}
|
|
121
|
+
const text = plainText(written);
|
|
122
|
+
try {
|
|
123
|
+
return {
|
|
124
|
+
ok: true,
|
|
125
|
+
typed: {
|
|
126
|
+
type: blindCast(found.key),
|
|
127
|
+
value: form.parse(text)
|
|
128
|
+
}
|
|
129
|
+
};
|
|
130
|
+
} catch (error) {
|
|
131
|
+
if (isInternalError(error)) throw error;
|
|
132
|
+
return refuse({
|
|
133
|
+
kind: "unreadable",
|
|
134
|
+
message: messageOf(error)
|
|
135
|
+
});
|
|
136
|
+
}
|
|
137
|
+
}
|
|
138
|
+
/** Convert a value of one data type into the form another stores, when that type takes it. */
|
|
139
|
+
function castInto(support, columnType, typed, elementIndex) {
|
|
140
|
+
if (typed.type === columnType) return {
|
|
141
|
+
ok: true,
|
|
142
|
+
value: typed.value
|
|
143
|
+
};
|
|
144
|
+
const declaration = support.lookup.get(columnType);
|
|
145
|
+
const cast = declaration?.casts[typed.type];
|
|
146
|
+
if (cast === void 0) return {
|
|
147
|
+
ok: false,
|
|
148
|
+
refusal: {
|
|
149
|
+
kind: "no-cast",
|
|
150
|
+
columnType,
|
|
151
|
+
valueType: typed.type,
|
|
152
|
+
casts: Object.keys(declaration?.casts ?? {}),
|
|
153
|
+
elementIndex
|
|
154
|
+
}
|
|
155
|
+
};
|
|
156
|
+
try {
|
|
157
|
+
return {
|
|
158
|
+
ok: true,
|
|
159
|
+
value: cast(typed.value)
|
|
160
|
+
};
|
|
161
|
+
} catch (error) {
|
|
162
|
+
if (isInternalError(error)) throw error;
|
|
163
|
+
return {
|
|
164
|
+
ok: false,
|
|
165
|
+
refusal: {
|
|
166
|
+
kind: "unreadable",
|
|
167
|
+
message: messageOf(error),
|
|
168
|
+
elementIndex
|
|
169
|
+
}
|
|
170
|
+
};
|
|
171
|
+
}
|
|
172
|
+
}
|
|
173
|
+
/** The column's `typeParams` as the codec reference carries them, so `vector(3)` checks its length. */
|
|
174
|
+
function codecRefTypeParams(typeParams) {
|
|
175
|
+
return typeParams === void 0 ? void 0 : blindCast(typeParams);
|
|
176
|
+
}
|
|
177
|
+
/**
|
|
178
|
+
* The column's codec descriptor, and `read`, which reads a value in its stored JSON form with the column's codec, built with the column's type parameters.
|
|
179
|
+
*/
|
|
180
|
+
function storedValueReader(input) {
|
|
181
|
+
if (input.codecLookup === void 0) throw new InternalError(`Field "${input.fieldPath}": no codec lookup was given, but the column was resolved from a codec descriptor.`);
|
|
182
|
+
const descriptor = input.codecLookup.descriptorFor(input.column.codecId);
|
|
183
|
+
const codec = codecForRef(input.codecLookup, {
|
|
184
|
+
codecId: input.column.codecId,
|
|
185
|
+
...ifDefined("typeParams", codecRefTypeParams(input.column.typeParams))
|
|
186
|
+
});
|
|
187
|
+
if (codec === void 0 || descriptor === void 0) throw new InternalError(`Field "${input.fieldPath}": no codec descriptor is registered for "${input.column.codecId}", but the column was resolved from one.`);
|
|
188
|
+
const read = (value, elementIndex) => {
|
|
189
|
+
try {
|
|
190
|
+
codec.decodeJson(value);
|
|
191
|
+
return {
|
|
192
|
+
ok: true,
|
|
193
|
+
value
|
|
194
|
+
};
|
|
195
|
+
} catch (error) {
|
|
196
|
+
if (isInternalError(error)) throw error;
|
|
197
|
+
return {
|
|
198
|
+
ok: false,
|
|
199
|
+
refusal: {
|
|
200
|
+
kind: "refused-by-codec",
|
|
201
|
+
codecId: input.column.codecId,
|
|
202
|
+
message: messageOf(error),
|
|
203
|
+
elementIndex
|
|
204
|
+
}
|
|
205
|
+
};
|
|
206
|
+
}
|
|
207
|
+
};
|
|
208
|
+
return {
|
|
209
|
+
descriptor,
|
|
210
|
+
read
|
|
211
|
+
};
|
|
212
|
+
}
|
|
213
|
+
/**
|
|
214
|
+
* Reads a value already in its stored JSON form, such as one member of a JSON document default, with the column's codec. Worded as {@link lowerDataTypeDefault} words a codec refusal.
|
|
215
|
+
*/
|
|
216
|
+
function readStoredValue(input) {
|
|
217
|
+
const reading = storedValueReader(input).read(input.value, void 0);
|
|
218
|
+
return reading.ok ? reading : refusalDiagnostic(reading.refusal, input.fieldPath);
|
|
219
|
+
}
|
|
220
|
+
/**
|
|
221
|
+
* Read one `@default(...)` value for a column, refusing in parts so each contract source words its
|
|
222
|
+
* own diagnostic. `isList` selects the check: a list column's elements are each read and cast
|
|
223
|
+
* against the element codec's data type, while a scalar column takes a written list only through
|
|
224
|
+
* its type's list cast — which is how `pgvector.Vector(3) @default([0.1, 0.2])` is read.
|
|
225
|
+
*/
|
|
226
|
+
function readDataTypeDefault(input) {
|
|
227
|
+
const { descriptor, read: readStored } = storedValueReader(input);
|
|
228
|
+
const columnType = descriptor.dataType;
|
|
229
|
+
const readOne = (written, elementIndex) => {
|
|
230
|
+
const read = readValue(input.support, written, elementIndex);
|
|
231
|
+
if (!read.ok) return read;
|
|
232
|
+
const cast = castInto(input.support, columnType, read.typed, elementIndex);
|
|
233
|
+
if (!cast.ok) return cast;
|
|
234
|
+
return readStored(cast.value, elementIndex);
|
|
235
|
+
};
|
|
236
|
+
if (input.written.kind !== "list") {
|
|
237
|
+
if (input.isList) return {
|
|
238
|
+
ok: false,
|
|
239
|
+
refusal: {
|
|
240
|
+
kind: "not-a-list",
|
|
241
|
+
elementIndex: void 0
|
|
242
|
+
}
|
|
243
|
+
};
|
|
244
|
+
return readOne(input.written, void 0);
|
|
245
|
+
}
|
|
246
|
+
if (input.isList) {
|
|
247
|
+
const elements = [];
|
|
248
|
+
for (const [elementIndex, written] of input.written.elements.entries()) {
|
|
249
|
+
if (written.kind === "list") return {
|
|
250
|
+
ok: false,
|
|
251
|
+
refusal: {
|
|
252
|
+
kind: "unreadable",
|
|
253
|
+
message: "a list holds values, not other lists",
|
|
254
|
+
elementIndex
|
|
255
|
+
}
|
|
256
|
+
};
|
|
257
|
+
const element = readOne(written, elementIndex);
|
|
258
|
+
if (!element.ok) return element;
|
|
259
|
+
elements.push(element.value);
|
|
260
|
+
}
|
|
261
|
+
return {
|
|
262
|
+
ok: true,
|
|
263
|
+
value: elements
|
|
264
|
+
};
|
|
265
|
+
}
|
|
266
|
+
return readListIntoScalar({
|
|
267
|
+
...input,
|
|
268
|
+
written: input.written,
|
|
269
|
+
columnType,
|
|
270
|
+
readStored
|
|
271
|
+
});
|
|
272
|
+
}
|
|
273
|
+
/** A written list on a column that is not a list: the column's type takes it through its list cast. */
|
|
274
|
+
function readListIntoScalar(input) {
|
|
275
|
+
const listCast = input.support.lookup.get(input.columnType)?.listCast;
|
|
276
|
+
if (listCast === void 0) return {
|
|
277
|
+
ok: false,
|
|
278
|
+
refusal: {
|
|
279
|
+
kind: "no-cast",
|
|
280
|
+
columnType: input.columnType,
|
|
281
|
+
valueType: "a list",
|
|
282
|
+
casts: Object.keys(input.support.lookup.get(input.columnType)?.casts ?? {}),
|
|
283
|
+
elementIndex: void 0
|
|
284
|
+
}
|
|
285
|
+
};
|
|
286
|
+
const elements = [];
|
|
287
|
+
for (const [elementIndex, written] of input.written.elements.entries()) {
|
|
288
|
+
if (written.kind === "list") return {
|
|
289
|
+
ok: false,
|
|
290
|
+
refusal: {
|
|
291
|
+
kind: "unreadable",
|
|
292
|
+
message: "a list holds values, not other lists",
|
|
293
|
+
elementIndex
|
|
294
|
+
}
|
|
295
|
+
};
|
|
296
|
+
const read = readValue(input.support, written, elementIndex);
|
|
297
|
+
if (!read.ok) return read;
|
|
298
|
+
if (!listCast.of.includes(read.typed.type)) return {
|
|
299
|
+
ok: false,
|
|
300
|
+
refusal: {
|
|
301
|
+
kind: "no-cast",
|
|
302
|
+
columnType: input.columnType,
|
|
303
|
+
valueType: read.typed.type,
|
|
304
|
+
casts: [...listCast.of],
|
|
305
|
+
elementIndex
|
|
306
|
+
}
|
|
307
|
+
};
|
|
308
|
+
elements.push(read.typed.value);
|
|
309
|
+
}
|
|
310
|
+
try {
|
|
311
|
+
return input.readStored(listCast.cast(elements), void 0);
|
|
312
|
+
} catch (error) {
|
|
313
|
+
if (isInternalError(error)) throw error;
|
|
314
|
+
return {
|
|
315
|
+
ok: false,
|
|
316
|
+
refusal: {
|
|
317
|
+
kind: "unreadable",
|
|
318
|
+
message: messageOf(error),
|
|
319
|
+
elementIndex: void 0
|
|
320
|
+
}
|
|
321
|
+
};
|
|
322
|
+
}
|
|
323
|
+
}
|
|
324
|
+
/** Where in a written list a diagnostic is about, for a message: ` at element 2`. */
|
|
325
|
+
function at(elementIndex) {
|
|
326
|
+
return elementIndex === void 0 ? "" : ` at element ${elementIndex + 1}`;
|
|
327
|
+
}
|
|
328
|
+
/** {@link readDataTypeDefault} worded as a PSL diagnostic's code and message. */
|
|
329
|
+
function lowerDataTypeDefault(input) {
|
|
330
|
+
const read = readDataTypeDefault(input);
|
|
331
|
+
return read.ok ? read : refusalDiagnostic(read.refusal, input.fieldPath);
|
|
332
|
+
}
|
|
333
|
+
/** A refusal worded as a PSL diagnostic's code and message. */
|
|
334
|
+
function refusalDiagnostic(refusal, fieldPath) {
|
|
335
|
+
const where = `Field "${fieldPath}"${at(refusal.elementIndex)}`;
|
|
336
|
+
switch (refusal.kind) {
|
|
337
|
+
case "unreadable": return {
|
|
338
|
+
ok: false,
|
|
339
|
+
code: "PSL_INVALID_LITERAL",
|
|
340
|
+
message: `${where}: ${refusal.message}`
|
|
341
|
+
};
|
|
342
|
+
case "unknown-tag": return {
|
|
343
|
+
ok: false,
|
|
344
|
+
code: "PSL_UNKNOWN_LITERAL_TAG",
|
|
345
|
+
message: `Unknown literal tag "${refusal.tag}". Known tags: ${refusal.known.join(", ")}.`
|
|
346
|
+
};
|
|
347
|
+
case "unwritable": return {
|
|
348
|
+
ok: false,
|
|
349
|
+
code: PSL_VALUE_TYPE_INCOMPATIBLE,
|
|
350
|
+
message: `${where}: this target has no data type for a ${refusal.syntax} value`
|
|
351
|
+
};
|
|
352
|
+
case "not-a-list": return {
|
|
353
|
+
ok: false,
|
|
354
|
+
code: PSL_DEFAULT_LIST_EXPECTED,
|
|
355
|
+
message: `${where}: this column holds a list, so its default is a list literal, as in [1, 2]`
|
|
356
|
+
};
|
|
357
|
+
case "no-cast": return {
|
|
358
|
+
ok: false,
|
|
359
|
+
code: PSL_VALUE_TYPE_INCOMPATIBLE,
|
|
360
|
+
message: `${where}: ${refusal.columnType} has no cast from ${refusal.valueType}; ${describeCasts(refusal.casts)}`
|
|
361
|
+
};
|
|
362
|
+
case "refused-by-codec": return {
|
|
363
|
+
ok: false,
|
|
364
|
+
code: PSL_INVALID_DEFAULT_LITERAL,
|
|
365
|
+
message: `${where}: ${refusal.message}`
|
|
366
|
+
};
|
|
367
|
+
}
|
|
368
|
+
}
|
|
369
|
+
function describeCasts(casts) {
|
|
370
|
+
return casts.length === 0 ? "it casts from nothing" : `it casts from ${casts.join(", ")}`;
|
|
371
|
+
}
|
|
372
|
+
function formatSupportedFunctionList(registry) {
|
|
373
|
+
const signatures = Array.from(registry.entries()).sort(([a], [b]) => a.localeCompare(b)).flatMap(([functionName, entry]) => {
|
|
374
|
+
const usageSignatures = entry.usageSignatures?.filter((signature) => signature.length > 0);
|
|
375
|
+
return usageSignatures && usageSignatures.length > 0 ? usageSignatures : [`${functionName}()`];
|
|
376
|
+
});
|
|
377
|
+
return signatures.length > 0 ? signatures.join(", ") : "none";
|
|
378
|
+
}
|
|
379
|
+
/** The diagnostic message for a `dbgenerated(...)` call, which raw SQL tagged literals replaced. */
|
|
380
|
+
function removedDbgeneratedMessage(registry) {
|
|
381
|
+
return `Default function "dbgenerated" was removed. Write the SQL as a tagged literal: @default(sql\`<expression>\`). Supported functions: ${formatSupportedFunctionList(registry)}.`;
|
|
382
|
+
}
|
|
383
|
+
function lowerDefaultFunctionWithRegistry(input) {
|
|
384
|
+
const entry = input.registry.get(input.call.fn);
|
|
385
|
+
if (entry) {
|
|
386
|
+
const result = entry.lower({
|
|
387
|
+
call: input.call,
|
|
388
|
+
context: input.context
|
|
389
|
+
});
|
|
390
|
+
return result.ok ? result : {
|
|
391
|
+
...result,
|
|
392
|
+
kind: "external"
|
|
393
|
+
};
|
|
394
|
+
}
|
|
395
|
+
const supportedFunctionList = formatSupportedFunctionList(input.registry);
|
|
396
|
+
return {
|
|
397
|
+
ok: false,
|
|
398
|
+
kind: "owned",
|
|
399
|
+
diagnostic: {
|
|
400
|
+
code: "PSL_UNKNOWN_DEFAULT_FUNCTION",
|
|
401
|
+
message: `Default function "${input.call.fn}" is not supported in SQL PSL provider v1. Supported functions: ${supportedFunctionList}.`,
|
|
402
|
+
...input.source.at(input.call.span)
|
|
403
|
+
}
|
|
404
|
+
};
|
|
405
|
+
}
|
|
406
|
+
function buildModelAttributeCtx(input) {
|
|
407
|
+
return {
|
|
408
|
+
sources: input.sources,
|
|
409
|
+
selfModel: input.selfModel,
|
|
410
|
+
binder: input.binder,
|
|
411
|
+
symbols: input.symbols
|
|
412
|
+
};
|
|
413
|
+
}
|
|
414
|
+
function buildFieldAttributeCtx(input) {
|
|
415
|
+
return {
|
|
416
|
+
sources: input.sources,
|
|
417
|
+
selfModel: input.selfModel,
|
|
418
|
+
field: input.field,
|
|
419
|
+
binder: input.binder,
|
|
420
|
+
symbols: input.symbols
|
|
421
|
+
};
|
|
422
|
+
}
|
|
423
|
+
function modelAttributeSpecsFrom(modelAttributesByName) {
|
|
424
|
+
const result = Object.create(null);
|
|
425
|
+
for (const [name, descriptor] of modelAttributesByName) result[name] = blindCast(descriptor.spec);
|
|
426
|
+
return result;
|
|
427
|
+
}
|
|
428
|
+
function interpretModelAttribute(input) {
|
|
429
|
+
const result = interpretAttribute(input.node, input.spec, buildModelAttributeCtx({
|
|
430
|
+
symbols: input.symbols,
|
|
431
|
+
selfModel: input.model,
|
|
432
|
+
sources: input.sources,
|
|
433
|
+
binder: input.binder
|
|
434
|
+
}));
|
|
435
|
+
if (!result.ok) {
|
|
436
|
+
input.diagnostics.push(...result.failure);
|
|
437
|
+
return;
|
|
438
|
+
}
|
|
439
|
+
return result.value;
|
|
440
|
+
}
|
|
441
|
+
function interpretFieldAttribute(input) {
|
|
442
|
+
const result = interpretAttribute(input.node, input.spec, buildFieldAttributeCtx({
|
|
443
|
+
symbols: input.symbols,
|
|
444
|
+
selfModel: input.model,
|
|
445
|
+
field: input.field,
|
|
446
|
+
sources: input.sources,
|
|
447
|
+
binder: input.binder
|
|
448
|
+
}));
|
|
449
|
+
if (!result.ok) {
|
|
450
|
+
input.diagnostics.push(...result.failure);
|
|
451
|
+
return;
|
|
452
|
+
}
|
|
453
|
+
return result.value;
|
|
454
|
+
}
|
|
455
|
+
function validateMappedName(value, ctx, attributeNode) {
|
|
456
|
+
return value.name === "" ? [leafDiagnostic(ctx, attributeNode, "Mapped name must not be empty")] : [];
|
|
457
|
+
}
|
|
458
|
+
const mapModelSpec = modelAttribute("map", {
|
|
459
|
+
documentation: "Maps this model to a database table name.",
|
|
460
|
+
positional: [{
|
|
461
|
+
key: "name",
|
|
462
|
+
type: str(),
|
|
463
|
+
documentation: "The nonempty database table name."
|
|
464
|
+
}],
|
|
465
|
+
refine: validateMappedName
|
|
466
|
+
});
|
|
467
|
+
const mapFieldSpec = fieldAttribute("map", {
|
|
468
|
+
documentation: "Maps this field to a database column name.",
|
|
469
|
+
positional: [{
|
|
470
|
+
key: "name",
|
|
471
|
+
type: str(),
|
|
472
|
+
documentation: "The nonempty database column name."
|
|
473
|
+
}],
|
|
474
|
+
refine: validateMappedName
|
|
475
|
+
});
|
|
476
|
+
function nullLiteral() {
|
|
477
|
+
const nullIdentifier = identifier("null", { documentation: "A null list element or nullable scalar default." });
|
|
478
|
+
return {
|
|
479
|
+
kind: "null",
|
|
480
|
+
label: "null",
|
|
481
|
+
parse: (arg, ctx) => {
|
|
482
|
+
const result = nullIdentifier.parse(arg, ctx);
|
|
483
|
+
return result.ok ? ok(null) : result;
|
|
484
|
+
}
|
|
485
|
+
};
|
|
486
|
+
}
|
|
487
|
+
function scalarDefaultArms(isList, registries) {
|
|
488
|
+
const tagsByDocumentation = /* @__PURE__ */ new Map();
|
|
489
|
+
for (const entry of Object.values(registries.dataTypeEntries)) {
|
|
490
|
+
if (entry.written.kind !== "tag") continue;
|
|
491
|
+
const tags = tagsByDocumentation.get(entry.documentation);
|
|
492
|
+
if (tags === void 0) tagsByDocumentation.set(entry.documentation, [entry.written.tag]);
|
|
493
|
+
else tags.push(entry.written.tag);
|
|
494
|
+
}
|
|
495
|
+
const tagArms = () => [...tagsByDocumentation].map(([documentation, tags]) => taggedLiteral(tags, { documentation }));
|
|
496
|
+
const literal = () => oneOf(str(), numLiteral(), bool(), nullLiteral(), ...tagArms());
|
|
497
|
+
const listArm = () => list(literal(), { label: `list of (${literal().label})` });
|
|
498
|
+
const funcArms = [...registries.defaultFunctionRegistry.entries()].map(([name, entry]) => funcCall(name, blindCast(entry.signature)));
|
|
499
|
+
return isList ? [
|
|
500
|
+
listArm(),
|
|
501
|
+
...funcArms,
|
|
502
|
+
...tagArms()
|
|
503
|
+
] : [
|
|
504
|
+
str(),
|
|
505
|
+
numLiteral(),
|
|
506
|
+
bool(),
|
|
507
|
+
nullLiteral(),
|
|
508
|
+
...funcArms,
|
|
509
|
+
...tagArms(),
|
|
510
|
+
listArm()
|
|
511
|
+
];
|
|
512
|
+
}
|
|
513
|
+
/**
|
|
514
|
+
* The `@default` value arms, with a `dbgenerated(...)` call reported as removed before the arms
|
|
515
|
+
* are tried, so the author is told what replaced it instead of being shown the list of arms.
|
|
516
|
+
*/
|
|
517
|
+
function defaultValueArm(arms, registry) {
|
|
518
|
+
const value = arms.length === 1 && arms[0].kind === "list" ? arms[0] : oneOf(...arms);
|
|
519
|
+
return {
|
|
520
|
+
...value,
|
|
521
|
+
parse: (arg, ctx) => FunctionCallAst.cast(arg.syntax)?.path().join(".") === "dbgenerated" ? notOk([leafDiagnostic(ctx, arg, removedDbgeneratedMessage(registry), "PSL_UNKNOWN_DEFAULT_FUNCTION")]) : value.parse(arg, ctx)
|
|
522
|
+
};
|
|
523
|
+
}
|
|
524
|
+
function noEnumMember() {
|
|
525
|
+
return {
|
|
526
|
+
kind: "rejecting",
|
|
527
|
+
label: "enum member",
|
|
528
|
+
message: "Enum declares no members",
|
|
529
|
+
parse: (arg, ctx) => notOk([leafDiagnostic(ctx, arg, "Enum declares no members")])
|
|
530
|
+
};
|
|
531
|
+
}
|
|
532
|
+
function enumMemberNames(ctx) {
|
|
533
|
+
const resolution = ctx.typeResolution;
|
|
534
|
+
if (resolution?.kind !== "block" || resolution.symbol.keyword !== "enum") return void 0;
|
|
535
|
+
const block = resolution.symbol;
|
|
536
|
+
const names = [];
|
|
537
|
+
const seen = /* @__PURE__ */ new Set();
|
|
538
|
+
for (const entry of block.node.entries()) {
|
|
539
|
+
const key = entry.key()?.name();
|
|
540
|
+
if (key === void 0 || seen.has(key)) continue;
|
|
541
|
+
seen.add(key);
|
|
542
|
+
names.push(key);
|
|
543
|
+
}
|
|
544
|
+
return names;
|
|
545
|
+
}
|
|
546
|
+
function enumDefaultArms(members, enumName, isList) {
|
|
547
|
+
const [first, ...rest] = members;
|
|
548
|
+
if (first === void 0) return [noEnumMember()];
|
|
549
|
+
const member = (name) => identifier(name, { documentation: `The \`${name}\` member of enum \`${enumName}\`.` });
|
|
550
|
+
if (isList) return [list(oneOf(member(first), ...rest.map(member), nullLiteral()))];
|
|
551
|
+
return [member(first), ...rest.map(member)];
|
|
552
|
+
}
|
|
553
|
+
function defaultFieldSpec(ctx) {
|
|
554
|
+
const members = enumMemberNames(ctx);
|
|
555
|
+
return fieldAttribute("default", {
|
|
556
|
+
documentation: "Supplies a default value when this field is omitted from a mutation.",
|
|
557
|
+
positional: [{
|
|
558
|
+
key: "value",
|
|
559
|
+
type: defaultValueArm(members === void 0 ? scalarDefaultArms(ctx.field.list, ctx.controlMutationDefaults) : enumDefaultArms(members, ctx.field.typeName, ctx.field.list), ctx.controlMutationDefaults.defaultFunctionRegistry),
|
|
560
|
+
documentation: "A literal, enum member, or registered default function compatible with this field."
|
|
561
|
+
}]
|
|
562
|
+
});
|
|
563
|
+
}
|
|
564
|
+
const idFieldSpec = fieldAttribute("id", {
|
|
565
|
+
documentation: "Makes this field the primary key of the table.",
|
|
566
|
+
named: { map: {
|
|
567
|
+
type: optional(str()),
|
|
568
|
+
documentation: "The database primary-key constraint name."
|
|
569
|
+
} }
|
|
570
|
+
});
|
|
571
|
+
const uniqueFieldSpec = fieldAttribute("unique", {
|
|
572
|
+
documentation: "Requires values in this field to be unique.",
|
|
573
|
+
named: { map: {
|
|
574
|
+
type: optional(str()),
|
|
575
|
+
documentation: "The database unique-constraint name."
|
|
576
|
+
} }
|
|
577
|
+
});
|
|
578
|
+
const noCheckKindArgument = () => oneOf(identifier("membership", { documentation: "Waives the generated check that values belong to the declared domain enum." }), identifier("elementNotNull", { documentation: "Waives the generated check that scalar-list elements are non-null." }));
|
|
579
|
+
/**
|
|
580
|
+
* `@noCheck` waives generated CHECK constraints on one column: bare for every
|
|
581
|
+
* kind the column's shape derives, or naming concrete kinds. Two optional
|
|
582
|
+
* positional slots cover the whole kind vocabulary; a third argument is
|
|
583
|
+
* necessarily a duplicate and fails as excess arity.
|
|
584
|
+
*/
|
|
585
|
+
const noCheckFieldSpec = fieldAttribute("noCheck", {
|
|
586
|
+
documentation: "Waives generated CHECK constraints for this column. With no arguments, waives every generated kind.",
|
|
587
|
+
positional: [{
|
|
588
|
+
key: "first",
|
|
589
|
+
type: optional(noCheckKindArgument()),
|
|
590
|
+
documentation: "The first generated check kind to waive: `membership` or `elementNotNull`."
|
|
591
|
+
}, {
|
|
592
|
+
key: "second",
|
|
593
|
+
type: optional(noCheckKindArgument()),
|
|
594
|
+
documentation: "A second, distinct generated check kind to waive."
|
|
595
|
+
}],
|
|
596
|
+
refine: (value, ctx, attributeNode) => {
|
|
597
|
+
if (value.first !== void 0 && value.first === value.second) return [leafDiagnostic(ctx, attributeNode, "`@noCheck` names the same kind twice")];
|
|
598
|
+
return [];
|
|
599
|
+
}
|
|
600
|
+
});
|
|
601
|
+
const idModelSpec = modelAttribute("id", {
|
|
602
|
+
documentation: "Declares a compound primary key for this table.",
|
|
603
|
+
positional: [{
|
|
604
|
+
key: "fields",
|
|
605
|
+
type: list(fieldRef(), {
|
|
606
|
+
allowEmpty: false,
|
|
607
|
+
unique: true
|
|
608
|
+
}),
|
|
609
|
+
documentation: "The ordered, nonempty list of distinct primary-key fields."
|
|
610
|
+
}],
|
|
611
|
+
named: { map: {
|
|
612
|
+
type: optional(str()),
|
|
613
|
+
documentation: "The database primary-key constraint name."
|
|
614
|
+
} }
|
|
615
|
+
});
|
|
616
|
+
const uniqueModelSpec = modelAttribute("unique", {
|
|
617
|
+
documentation: "Requires the combination of these fields to be unique.",
|
|
618
|
+
positional: [{
|
|
619
|
+
key: "fields",
|
|
620
|
+
type: list(fieldRef(), {
|
|
621
|
+
allowEmpty: false,
|
|
622
|
+
unique: true
|
|
623
|
+
}),
|
|
624
|
+
documentation: "The ordered, nonempty list of distinct fields in the unique constraint."
|
|
625
|
+
}],
|
|
626
|
+
named: { map: {
|
|
627
|
+
type: optional(str()),
|
|
628
|
+
documentation: "The database unique-constraint name."
|
|
629
|
+
} }
|
|
630
|
+
});
|
|
631
|
+
const PSL_INDEX_FIELDS_XOR_EXPRESSION = "PSL_INDEX_FIELDS_XOR_EXPRESSION";
|
|
632
|
+
const PSL_INDEX_EXPRESSION_REQUIRES_NAME = "PSL_INDEX_EXPRESSION_REQUIRES_NAME";
|
|
633
|
+
const PSL_INDEX_NAME_XOR_MAP = "PSL_INDEX_NAME_XOR_MAP";
|
|
634
|
+
const indexModelSpec = modelAttribute("index", {
|
|
635
|
+
documentation: "Declares a database index over fields or a SQL expression, optionally restricted by a predicate.",
|
|
636
|
+
positional: [{
|
|
637
|
+
key: "fields",
|
|
638
|
+
type: optional(list(fieldRef(), {
|
|
639
|
+
allowEmpty: false,
|
|
640
|
+
unique: true
|
|
641
|
+
})),
|
|
642
|
+
documentation: "The ordered list of distinct indexed fields. Mutually exclusive with `expression`."
|
|
643
|
+
}],
|
|
644
|
+
named: {
|
|
645
|
+
expression: {
|
|
646
|
+
type: optional(str()),
|
|
647
|
+
documentation: "The SQL index expression. Requires `name` or `map` and cannot be combined with a fields list."
|
|
648
|
+
},
|
|
649
|
+
where: {
|
|
650
|
+
type: optional(str()),
|
|
651
|
+
documentation: "The SQL predicate restricting rows included in a partial index."
|
|
652
|
+
},
|
|
653
|
+
unique: {
|
|
654
|
+
type: optional(bool()),
|
|
655
|
+
documentation: "Whether the index enforces uniqueness."
|
|
656
|
+
},
|
|
657
|
+
name: {
|
|
658
|
+
type: optional(str()),
|
|
659
|
+
documentation: "The index name. Mutually exclusive with `map`."
|
|
660
|
+
},
|
|
661
|
+
map: {
|
|
662
|
+
type: optional(str()),
|
|
663
|
+
documentation: "The database index name. Mutually exclusive with `name`."
|
|
664
|
+
},
|
|
665
|
+
type: {
|
|
666
|
+
type: optional(str()),
|
|
667
|
+
documentation: "The target-specific index access method."
|
|
668
|
+
},
|
|
669
|
+
options: {
|
|
670
|
+
type: optional(record(str())),
|
|
671
|
+
documentation: "Target-specific index options. Requires an explicit `type`."
|
|
672
|
+
}
|
|
673
|
+
},
|
|
674
|
+
refine: (value, ctx, attributeNode) => {
|
|
675
|
+
const diagnostics = [];
|
|
676
|
+
if (value.fields === void 0 === (value.expression === void 0)) diagnostics.push(leafDiagnostic(ctx, attributeNode, "`@@index` requires exactly one of a fields list or an `expression` argument", PSL_INDEX_FIELDS_XOR_EXPRESSION));
|
|
677
|
+
if (value.expression !== void 0 && value.name === void 0 && value.map === void 0) diagnostics.push(leafDiagnostic(ctx, attributeNode, "`@@index` with an `expression` argument requires a `name` or `map` argument (a default name cannot be derived from an expression)", PSL_INDEX_EXPRESSION_REQUIRES_NAME));
|
|
678
|
+
if (value.name !== void 0 && value.map !== void 0) diagnostics.push(leafDiagnostic(ctx, attributeNode, "`@@index` takes at most one of `name` and `map`", PSL_INDEX_NAME_XOR_MAP));
|
|
679
|
+
if (value.options !== void 0 && value.type === void 0) diagnostics.push(leafDiagnostic(ctx, attributeNode, "`@@index` options argument requires a type argument"));
|
|
680
|
+
return diagnostics;
|
|
681
|
+
}
|
|
682
|
+
});
|
|
683
|
+
const PSL_CHECK_REQUIRES_NAME_OR_MAP = "PSL_CHECK_REQUIRES_NAME_OR_MAP";
|
|
684
|
+
const PSL_CHECK_NAME_XOR_MAP = "PSL_CHECK_NAME_XOR_MAP";
|
|
685
|
+
const PSL_CHECK_EXPRESSION_EMPTY = "PSL_CHECK_EXPRESSION_EMPTY";
|
|
686
|
+
/**
|
|
687
|
+
* A single-table-inheritance variant (`@@base` with no own `@@map`) shares
|
|
688
|
+
* its base model's storage table and has no table of its own to declare a
|
|
689
|
+
* check on — raised from {@link interpretPslDocumentToSqlContract}, not from
|
|
690
|
+
* this spec's own `refine`, because the rule needs the model's `@@base`
|
|
691
|
+
* declaration, which a single attribute's `refine` cannot see.
|
|
692
|
+
*/
|
|
693
|
+
const PSL_CHECK_ON_STI_VARIANT = "PSL_CHECK_ON_STI_VARIANT";
|
|
694
|
+
const checkModelSpec = modelAttribute("check", {
|
|
695
|
+
documentation: "Declares a named database CHECK constraint on this table.",
|
|
696
|
+
named: {
|
|
697
|
+
expression: {
|
|
698
|
+
type: str(),
|
|
699
|
+
documentation: "The nonempty SQL predicate checked for each row."
|
|
700
|
+
},
|
|
701
|
+
name: {
|
|
702
|
+
type: optional(str()),
|
|
703
|
+
documentation: "The constraint name. Exactly one of `name` and `map` is required."
|
|
704
|
+
},
|
|
705
|
+
map: {
|
|
706
|
+
type: optional(str()),
|
|
707
|
+
documentation: "The database constraint name. Exactly one of `name` and `map` is required."
|
|
708
|
+
}
|
|
709
|
+
},
|
|
710
|
+
refine: (value, ctx, attributeNode) => {
|
|
711
|
+
const diagnostics = [];
|
|
712
|
+
if (value.expression.trim().length === 0) diagnostics.push(leafDiagnostic(ctx, attributeNode, "`@@check` expression must not be empty — an empty predicate is not a constraint", PSL_CHECK_EXPRESSION_EMPTY));
|
|
713
|
+
if (value.name === void 0 && value.map === void 0) diagnostics.push(leafDiagnostic(ctx, attributeNode, "`@@check` requires a `name` or `map` argument (a default name cannot be derived — a check has no column tuple to name itself after)", PSL_CHECK_REQUIRES_NAME_OR_MAP));
|
|
714
|
+
if (value.name !== void 0 && value.map !== void 0) diagnostics.push(leafDiagnostic(ctx, attributeNode, "`@@check` takes at most one of `name` and `map`", PSL_CHECK_NAME_XOR_MAP));
|
|
715
|
+
return diagnostics;
|
|
716
|
+
}
|
|
717
|
+
});
|
|
718
|
+
const controlModelSpec = modelAttribute("control", {
|
|
719
|
+
documentation: "Sets how schema management treats this model’s storage.",
|
|
720
|
+
positional: [{
|
|
721
|
+
key: "policy",
|
|
722
|
+
documentation: "The storage control policy: `managed`, `tolerated`, `external`, or `observed`.",
|
|
723
|
+
type: oneOf(identifier("managed", { documentation: "Verifies the declared shape strictly and allows migrations to create, alter, or drop the storage object." }), identifier("tolerated", { documentation: "Verifies declared columns while allowing extra columns. Migrations may create missing storage, but never alter or drop existing storage." }), identifier("external", { documentation: "Verifies declared storage without creating, altering, or dropping it." }), identifier("observed", { documentation: "Reports storage differences as warnings rather than verification failures and never emits migration operations." }))
|
|
724
|
+
}]
|
|
725
|
+
});
|
|
726
|
+
const discriminatorModelSpec = modelAttribute("discriminator", {
|
|
727
|
+
documentation: "Selects the field that identifies inheritance variants of this model.",
|
|
728
|
+
positional: [{
|
|
729
|
+
key: "field",
|
|
730
|
+
type: fieldRef(),
|
|
731
|
+
documentation: "The discriminator field on this model."
|
|
732
|
+
}]
|
|
733
|
+
});
|
|
734
|
+
function baseModelSpec() {
|
|
735
|
+
return modelAttribute("base", {
|
|
736
|
+
documentation: "Declares this model as a variant of a base model.",
|
|
737
|
+
positional: [{
|
|
738
|
+
key: "base",
|
|
739
|
+
type: entityRef({ kind: "model" }),
|
|
740
|
+
documentation: "The base model to inherit from."
|
|
741
|
+
}, {
|
|
742
|
+
key: "value",
|
|
743
|
+
type: str(),
|
|
744
|
+
documentation: "The discriminator value identifying this variant."
|
|
745
|
+
}]
|
|
746
|
+
});
|
|
747
|
+
}
|
|
748
|
+
function relationAttributeSpan(ctx) {
|
|
749
|
+
const node = getAttribute(ctx.field.attributes, "relation")?.node;
|
|
750
|
+
if (node !== void 0) return nodePslSpan(node.syntax, ctx.sources);
|
|
751
|
+
return ctx.field.span;
|
|
752
|
+
}
|
|
753
|
+
function relationInvariants(parsed, ctx) {
|
|
754
|
+
if (parsed.fields !== void 0 !== (parsed.references !== void 0)) return [{
|
|
755
|
+
code: "PSL_INVALID_ATTRIBUTE_SYNTAX",
|
|
756
|
+
message: `Relation field "${ctx.selfModel.name}.${ctx.field.name}" requires fields and references arguments`,
|
|
757
|
+
...diagnosticSource(ctx.sources, ctx.field.node.syntax).at(relationAttributeSpan(ctx))
|
|
758
|
+
}];
|
|
759
|
+
return [];
|
|
760
|
+
}
|
|
761
|
+
const referentialActionArgument = () => oneOf(identifier("NoAction", { documentation: "Rejects a change that would violate the foreign key when the constraint is checked; checking may be deferred when supported and configured." }), identifier("Restrict", { documentation: "Rejects deleting or updating a referenced row while referencing rows remain, without deferring the check." }), identifier("Cascade", { documentation: "Propagates deletion or key updates of a referenced row to its referencing rows." }), identifier("SetNull", { documentation: "Sets referencing foreign-key fields to null when the referenced row is deleted or its key changes. The fields must allow null." }), identifier("SetDefault", { documentation: "Sets referencing foreign-key fields to their defaults when the referenced row is deleted or its key changes. The resulting values must satisfy the foreign key." }));
|
|
762
|
+
const relationFieldSpec = fieldAttribute("relation", {
|
|
763
|
+
documentation: "Defines the relation name, foreign-key fields, and referential actions for this relation.",
|
|
764
|
+
positional: [{
|
|
765
|
+
key: "name",
|
|
766
|
+
type: optional(str()),
|
|
767
|
+
documentation: "The relation name used to pair both sides. May also be supplied by name."
|
|
768
|
+
}],
|
|
769
|
+
named: {
|
|
770
|
+
name: {
|
|
771
|
+
type: optional(str()),
|
|
772
|
+
documentation: "The relation name used to pair both sides. Cannot also be supplied positionally."
|
|
773
|
+
},
|
|
774
|
+
fields: {
|
|
775
|
+
type: optional(list(fieldRef(), {
|
|
776
|
+
allowEmpty: false,
|
|
777
|
+
unique: true
|
|
778
|
+
})),
|
|
779
|
+
documentation: "The ordered local foreign-key fields. Must be supplied together with `references`."
|
|
780
|
+
},
|
|
781
|
+
references: {
|
|
782
|
+
type: optional(list(referencedFieldRef(), {
|
|
783
|
+
allowEmpty: false,
|
|
784
|
+
unique: true
|
|
785
|
+
})),
|
|
786
|
+
documentation: "The corresponding fields on the referenced model. Must be supplied together with `fields`."
|
|
787
|
+
},
|
|
788
|
+
map: {
|
|
789
|
+
type: optional(str()),
|
|
790
|
+
documentation: "The database foreign-key constraint name."
|
|
791
|
+
},
|
|
792
|
+
onDelete: {
|
|
793
|
+
type: optional(referentialActionArgument()),
|
|
794
|
+
documentation: "The referential action when a referenced row is deleted."
|
|
795
|
+
},
|
|
796
|
+
onUpdate: {
|
|
797
|
+
type: optional(referentialActionArgument()),
|
|
798
|
+
documentation: "The referential action when a referenced key is updated."
|
|
799
|
+
},
|
|
800
|
+
index: {
|
|
801
|
+
type: optional(bool()),
|
|
802
|
+
documentation: "Whether to create an index for the relation’s foreign-key fields."
|
|
803
|
+
}
|
|
804
|
+
},
|
|
805
|
+
refine: relationInvariants
|
|
806
|
+
});
|
|
807
|
+
function fieldSpecContext(input) {
|
|
808
|
+
const node = typeReferenceNode(input.field);
|
|
809
|
+
return {
|
|
810
|
+
symbols: input.symbols,
|
|
811
|
+
model: input.model,
|
|
812
|
+
field: input.field,
|
|
813
|
+
typeResolution: node === void 0 ? void 0 : input.binder.symbolForNode(node),
|
|
814
|
+
controlMutationDefaults: input.controlMutationDefaults
|
|
815
|
+
};
|
|
816
|
+
}
|
|
817
|
+
const sqlAttributeSpecs = {
|
|
818
|
+
model: {
|
|
819
|
+
map: () => mapModelSpec,
|
|
820
|
+
id: () => idModelSpec,
|
|
821
|
+
unique: () => uniqueModelSpec,
|
|
822
|
+
index: () => indexModelSpec,
|
|
823
|
+
check: () => checkModelSpec,
|
|
824
|
+
control: () => controlModelSpec,
|
|
825
|
+
discriminator: () => discriminatorModelSpec,
|
|
826
|
+
base: baseModelSpec
|
|
827
|
+
},
|
|
828
|
+
field: {
|
|
829
|
+
map: () => mapFieldSpec,
|
|
830
|
+
id: () => idFieldSpec,
|
|
831
|
+
unique: () => uniqueFieldSpec,
|
|
832
|
+
noCheck: () => noCheckFieldSpec,
|
|
833
|
+
relation: () => relationFieldSpec,
|
|
834
|
+
default: defaultFieldSpec
|
|
835
|
+
}
|
|
836
|
+
};
|
|
837
|
+
/** Each way the default does not match the composite type. */
|
|
838
|
+
function valueObjectDefaultMismatches(input) {
|
|
839
|
+
const mismatches = [];
|
|
840
|
+
const shape = (path, message) => mismatches.push({
|
|
841
|
+
code: PSL_VALUE_TYPE_INCOMPATIBLE,
|
|
842
|
+
message: `Field "${path}": ${message}`
|
|
843
|
+
});
|
|
844
|
+
const checkObject = (value, valueObjectName, path) => {
|
|
845
|
+
const valueObject = input.types.nodes.get(valueObjectName);
|
|
846
|
+
const declared = input.types.declaredMembers.get(valueObjectName);
|
|
847
|
+
if (valueObject === void 0 || declared === void 0) throw new InternalError(`Field "${path}" is typed by the value object "${valueObjectName}", which the contract does not declare.`);
|
|
848
|
+
if (!isJsonObject(value)) {
|
|
849
|
+
shape(path, `a value of "${valueObjectName}" is a JSON object, not ${jsonKind(value)}`);
|
|
850
|
+
return;
|
|
851
|
+
}
|
|
852
|
+
for (const key of Object.keys(value)) if (!declared.has(key)) shape(path, `"${key}" is not a member of "${valueObjectName}"`);
|
|
853
|
+
for (const member of valueObject.fields) checkMember(value[member.fieldName], member, `${path}.${member.fieldName}`);
|
|
854
|
+
};
|
|
855
|
+
const checkMember = (value, member, path) => {
|
|
856
|
+
if (value === void 0) {
|
|
857
|
+
if (!member.nullable) shape(path, "the member is required, and the default has no value for it");
|
|
858
|
+
return;
|
|
859
|
+
}
|
|
860
|
+
if (value === null && member.nullable) return;
|
|
861
|
+
if (member.many === true) {
|
|
862
|
+
if (!Array.isArray(value)) {
|
|
863
|
+
shape(path, `the member is a list, so its value is a JSON array, not ${jsonKind(value)}`);
|
|
864
|
+
return;
|
|
865
|
+
}
|
|
866
|
+
for (const [index, element] of value.entries()) {
|
|
867
|
+
if (element === null && member.elementNullable) continue;
|
|
868
|
+
checkOne(element, member, `${path}[${index}]`, "an element of the member is not null");
|
|
869
|
+
}
|
|
870
|
+
return;
|
|
871
|
+
}
|
|
872
|
+
checkOne(value, member, path, "the member is not optional, so its value is not null");
|
|
873
|
+
};
|
|
874
|
+
const checkOne = (value, member, path, notNull) => {
|
|
875
|
+
if (isValueObjectMember(member)) {
|
|
876
|
+
if (value === null) shape(path, notNull);
|
|
877
|
+
else checkObject(value, member.valueObjectName, path);
|
|
878
|
+
return;
|
|
879
|
+
}
|
|
880
|
+
const reading = readStoredValue({
|
|
881
|
+
value,
|
|
882
|
+
column: member.descriptor,
|
|
883
|
+
codecLookup: input.codecLookup,
|
|
884
|
+
fieldPath: path
|
|
885
|
+
});
|
|
886
|
+
if (!reading.ok) {
|
|
887
|
+
if (value === null) shape(path, notNull);
|
|
888
|
+
else mismatches.push({
|
|
889
|
+
code: reading.code,
|
|
890
|
+
message: reading.message
|
|
891
|
+
});
|
|
892
|
+
return;
|
|
893
|
+
}
|
|
894
|
+
const enumMismatch = enumValueMismatch(value, member, path, input.codecLookup);
|
|
895
|
+
if (enumMismatch !== void 0) mismatches.push(enumMismatch);
|
|
896
|
+
};
|
|
897
|
+
if (input.value === null) {
|
|
898
|
+
if (input.nullable) return mismatches;
|
|
899
|
+
shape(input.fieldPath, input.list ? "the default of a list of value objects is a JSON array, not null" : "the default of a value object is a JSON object, not null");
|
|
900
|
+
return mismatches;
|
|
901
|
+
}
|
|
902
|
+
if (input.list) {
|
|
903
|
+
if (!Array.isArray(input.value)) {
|
|
904
|
+
shape(input.fieldPath, `the default of a list of value objects is a JSON array, not ${jsonKind(input.value)}`);
|
|
905
|
+
return mismatches;
|
|
906
|
+
}
|
|
907
|
+
for (const [index, element] of input.value.entries()) {
|
|
908
|
+
if (element === null && input.elementNullable) continue;
|
|
909
|
+
checkObject(element, input.valueObjectName, `${input.fieldPath}[${index}]`);
|
|
910
|
+
}
|
|
911
|
+
return mismatches;
|
|
912
|
+
}
|
|
913
|
+
if (!isJsonObject(input.value)) {
|
|
914
|
+
shape(input.fieldPath, `the default of a value object is a JSON object, not ${jsonKind(input.value)}`);
|
|
915
|
+
return mismatches;
|
|
916
|
+
}
|
|
917
|
+
checkObject(input.value, input.valueObjectName, input.fieldPath);
|
|
918
|
+
return mismatches;
|
|
919
|
+
}
|
|
920
|
+
/**
|
|
921
|
+
* A member typed by an enum takes only the enum's values, in the form its codec stores them, as a model field of that enum takes only its members.
|
|
922
|
+
*/
|
|
923
|
+
function enumValueMismatch(value, member, path, codecLookup) {
|
|
924
|
+
const handle = member.enumTypeHandle;
|
|
925
|
+
if (handle === void 0) return void 0;
|
|
926
|
+
const codec = codecLookup?.get(handle.codecId);
|
|
927
|
+
const stored = handle.values.map((enumValue) => codec === void 0 ? enumValue : codec.encodeJson(enumValue));
|
|
928
|
+
if (stored.some((storedValue) => storedValue === value)) return void 0;
|
|
929
|
+
return {
|
|
930
|
+
code: PSL_INVALID_DEFAULT_LITERAL,
|
|
931
|
+
message: `Field "${path}": Expected one of: ${stored.map((storedValue) => JSON.stringify(storedValue)).join(" | ")}`
|
|
932
|
+
};
|
|
933
|
+
}
|
|
934
|
+
function isJsonObject(value) {
|
|
935
|
+
return typeof value === "object" && value !== null && !Array.isArray(value);
|
|
936
|
+
}
|
|
937
|
+
function jsonKind(value) {
|
|
938
|
+
if (value === null) return "null";
|
|
939
|
+
if (Array.isArray(value)) return "a JSON array";
|
|
940
|
+
if (typeof value === "object") return "a JSON object";
|
|
941
|
+
return `a JSON ${typeof value}`;
|
|
942
|
+
}
|
|
943
|
+
function toNamedTypeFieldDescriptor(typeRef, descriptor) {
|
|
944
|
+
return {
|
|
945
|
+
codecId: descriptor.codecId,
|
|
946
|
+
nativeType: descriptor.nativeType,
|
|
947
|
+
typeRef
|
|
948
|
+
};
|
|
949
|
+
}
|
|
950
|
+
/**
|
|
951
|
+
* Walks `authoringContributions.entityTypes` segment-by-segment and returns
|
|
952
|
+
* the entity type descriptor at the resolved path, or `undefined` if no
|
|
953
|
+
* descriptor is registered.
|
|
954
|
+
*
|
|
955
|
+
* Used by the PSL interpreter to dispatch declarative entity-shaped
|
|
956
|
+
* declarations (`enum`, future `namespace { … }`, …) through the
|
|
957
|
+
* pack entity-type mechanism — the descriptor's `factory` (or
|
|
958
|
+
* `template`) materialises the IR-class instance without the
|
|
959
|
+
* interpreter knowing target-specific construction.
|
|
960
|
+
*/
|
|
961
|
+
function getAuthoringEntity(contributions, path) {
|
|
962
|
+
let current = contributions?.entityTypes;
|
|
963
|
+
for (const segment of path) {
|
|
964
|
+
if (typeof current !== "object" || current === null || "kind" in current) return;
|
|
965
|
+
current = current[segment];
|
|
966
|
+
}
|
|
967
|
+
return current !== void 0 && isAuthoringEntityTypeDescriptor(current) ? current : void 0;
|
|
968
|
+
}
|
|
969
|
+
function instantiatePslTypeConstructor(input) {
|
|
970
|
+
const helperPath = input.call.path.join(".");
|
|
971
|
+
const args = mapPslHelperArgs({
|
|
972
|
+
args: input.call.args,
|
|
973
|
+
descriptors: input.descriptor.args ?? [],
|
|
974
|
+
helperLabel: `constructor "${helperPath}"`,
|
|
975
|
+
span: input.call.span,
|
|
976
|
+
diagnostics: input.diagnostics,
|
|
977
|
+
source: input.source,
|
|
978
|
+
entityLabel: input.entityLabel
|
|
979
|
+
});
|
|
980
|
+
if (!args) return;
|
|
981
|
+
try {
|
|
982
|
+
validateAuthoringHelperArguments(helperPath, input.descriptor.args, args);
|
|
983
|
+
return instantiateAuthoringTypeConstructor(input.descriptor, args);
|
|
984
|
+
} catch (error) {
|
|
985
|
+
if (isInternalError(error)) throw error;
|
|
986
|
+
const message = error instanceof Error ? error.message : String(error);
|
|
987
|
+
input.diagnostics.push({
|
|
988
|
+
code: "PSL_INVALID_ATTRIBUTE_ARGUMENT",
|
|
989
|
+
message: `${input.entityLabel} constructor "${helperPath}" ${message}`,
|
|
990
|
+
...input.source.at(input.call.span)
|
|
991
|
+
});
|
|
992
|
+
return;
|
|
993
|
+
}
|
|
994
|
+
}
|
|
995
|
+
/**
|
|
996
|
+
* Structural check for a codec descriptor exposing the authoring-time
|
|
997
|
+
* `columnFromEntity` hook a type constructor's `entityRefArg` resolves
|
|
998
|
+
* through (e.g. the `pg/enum@1` codec descriptor). No casts.
|
|
999
|
+
*/
|
|
1000
|
+
function hasColumnFromEntityHook(descriptor) {
|
|
1001
|
+
return "columnFromEntity" in descriptor && typeof descriptor.columnFromEntity === "function";
|
|
1002
|
+
}
|
|
1003
|
+
/**
|
|
1004
|
+
* Resolves a type-constructor call whose descriptor declares an
|
|
1005
|
+
* `entityRefArg` (e.g. `pg.enum(AalLevel)`): extracts the call's sole
|
|
1006
|
+
* positional-argument ref string, resolves it against the field's
|
|
1007
|
+
* namespace's already-lowered extension entities (keyed by the declared
|
|
1008
|
+
* `entityRefArg.entityKind`, then block name), and converts the resolved
|
|
1009
|
+
* entity to column params via the `columnFromEntity` authoring hook on the
|
|
1010
|
+
* codec descriptor registered for `descriptor.output.codecId`. The `nativeType`
|
|
1011
|
+
* / `typeParams.typeName` `columnFromEntity` returns are bare — schema
|
|
1012
|
+
* qualification (e.g. `auth.aal_level`) is a target concern, applied later
|
|
1013
|
+
* when the target builds the field's namespace. A `valueSet` ref is
|
|
1014
|
+
* attached when the same namespace derived a value-set under the same block
|
|
1015
|
+
* name (the generic `deriveValueSet` mechanism), scoped to the field's own
|
|
1016
|
+
* namespace.
|
|
1017
|
+
*/
|
|
1018
|
+
function resolveEntityRefTypeConstructorCall(input) {
|
|
1019
|
+
const entityRefArg = input.descriptor.entityRefArg;
|
|
1020
|
+
if (entityRefArg === void 0) throw new InternalError("resolveEntityRefTypeConstructorCall called with a descriptor that does not declare an entityRefArg. This is an interpreter bug.");
|
|
1021
|
+
const helperPath = input.call.path.join(".");
|
|
1022
|
+
const positionalArgs = input.call.args.filter((arg) => arg.kind === "positional");
|
|
1023
|
+
const ref = positionalArgs[entityRefArg.index]?.value;
|
|
1024
|
+
if (input.call.args.length !== 1 || positionalArgs.length !== 1 || ref === void 0) {
|
|
1025
|
+
input.diagnostics.push({
|
|
1026
|
+
code: "PSL_INVALID_ATTRIBUTE_ARGUMENT",
|
|
1027
|
+
message: `${input.entityLabel} type constructor "${helperPath}" expects exactly one positional argument naming the referenced entity`,
|
|
1028
|
+
...input.source.at(input.call.span)
|
|
1029
|
+
});
|
|
1030
|
+
return NOT_RESOLVED;
|
|
1031
|
+
}
|
|
1032
|
+
const reportUnknownRef = () => {
|
|
1033
|
+
input.diagnostics.push({
|
|
1034
|
+
code: "PSL_UNKNOWN_ENTITY_REF",
|
|
1035
|
+
message: `${input.entityLabel} type constructor "${helperPath}(${ref})" does not resolve — no entity named "${ref}" was found in namespace "${input.namespaceId ?? "(unspecified)"}"`,
|
|
1036
|
+
...input.source.at(input.call.span)
|
|
1037
|
+
});
|
|
1038
|
+
return NOT_RESOLVED;
|
|
1039
|
+
};
|
|
1040
|
+
const entity = input.namespaceExtensionEntities?.[entityRefArg.entityKind]?.[ref];
|
|
1041
|
+
if (entity === void 0) return reportUnknownRef();
|
|
1042
|
+
const codecId = input.descriptor.output.codecId;
|
|
1043
|
+
const codecDescriptor = input.codecLookup?.descriptorFor(codecId);
|
|
1044
|
+
if (codecDescriptor === void 0 || !hasColumnFromEntityHook(codecDescriptor)) throw contractError("CONTRACT.PACK_CONTRIBUTION_INVALID", `Type constructor "${helperPath}" registers codecId "${codecId}" with an entity-ref argument, but its codec descriptor has no "columnFromEntity" authoring hook. This is a contributor bug in the pack registering "${helperPath}", not a user-schema error.`, { meta: {
|
|
1045
|
+
helperPath,
|
|
1046
|
+
codecId
|
|
1047
|
+
} });
|
|
1048
|
+
const resolved = codecDescriptor.columnFromEntity(entity);
|
|
1049
|
+
if (resolved === void 0) return reportUnknownRef();
|
|
1050
|
+
const derivedValueSet = input.namespaceExtensionEntities?.["valueSet"]?.[ref];
|
|
1051
|
+
if (derivedValueSet !== void 0 && input.namespaceId === void 0) {
|
|
1052
|
+
input.diagnostics.push({
|
|
1053
|
+
code: "PSL_INVALID_ATTRIBUTE_ARGUMENT",
|
|
1054
|
+
message: `${input.entityLabel} type constructor "${helperPath}(${ref})" resolves to a value-set-typed entity, but the field has no resolvable namespace to scope the value-set ref to`,
|
|
1055
|
+
...input.source.at(input.call.span)
|
|
1056
|
+
});
|
|
1057
|
+
return NOT_RESOLVED;
|
|
1058
|
+
}
|
|
1059
|
+
const valueSet = derivedValueSet !== void 0 && input.namespaceId !== void 0 ? {
|
|
1060
|
+
plane: "storage",
|
|
1061
|
+
entityKind: "valueSet",
|
|
1062
|
+
namespaceId: input.namespaceId,
|
|
1063
|
+
entityName: ref
|
|
1064
|
+
} : void 0;
|
|
1065
|
+
return {
|
|
1066
|
+
ok: true,
|
|
1067
|
+
descriptor: {
|
|
1068
|
+
codecId,
|
|
1069
|
+
nativeType: resolved.nativeType,
|
|
1070
|
+
...resolved.typeParams !== void 0 ? { typeParams: resolved.typeParams } : {},
|
|
1071
|
+
...valueSet !== void 0 ? { valueSet } : {}
|
|
1072
|
+
}
|
|
1073
|
+
};
|
|
1074
|
+
}
|
|
1075
|
+
const NOT_RESOLVED = { ok: false };
|
|
1076
|
+
function instantiateFieldTypeConstructor(input) {
|
|
1077
|
+
if (input.descriptor.entityRefArg !== void 0) return resolveEntityRefTypeConstructorCall({
|
|
1078
|
+
call: input.call,
|
|
1079
|
+
descriptor: input.descriptor,
|
|
1080
|
+
namespaceId: input.namespaceId,
|
|
1081
|
+
namespaceExtensionEntities: input.namespaceExtensionEntities,
|
|
1082
|
+
codecLookup: input.codecLookup,
|
|
1083
|
+
diagnostics: input.diagnostics,
|
|
1084
|
+
source: input.source,
|
|
1085
|
+
entityLabel: input.entityLabel
|
|
1086
|
+
});
|
|
1087
|
+
const instantiated = instantiatePslTypeConstructor({
|
|
1088
|
+
call: input.call,
|
|
1089
|
+
descriptor: input.descriptor,
|
|
1090
|
+
diagnostics: input.diagnostics,
|
|
1091
|
+
source: input.source,
|
|
1092
|
+
entityLabel: input.entityLabel
|
|
1093
|
+
});
|
|
1094
|
+
return instantiated === void 0 ? NOT_RESOLVED : {
|
|
1095
|
+
ok: true,
|
|
1096
|
+
descriptor: instantiated
|
|
1097
|
+
};
|
|
1098
|
+
}
|
|
1099
|
+
function instantiateFieldPreset(input) {
|
|
1100
|
+
const instantiated = instantiatePslFieldPreset(input);
|
|
1101
|
+
if (!instantiated) return NOT_RESOLVED;
|
|
1102
|
+
const presetContributions = {
|
|
1103
|
+
nullable: instantiated.nullable,
|
|
1104
|
+
id: instantiated.id,
|
|
1105
|
+
unique: instantiated.unique,
|
|
1106
|
+
...instantiated.default !== void 0 ? { default: instantiated.default } : {},
|
|
1107
|
+
...instantiated.executionDefaults !== void 0 ? { executionDefaults: instantiated.executionDefaults } : {}
|
|
1108
|
+
};
|
|
1109
|
+
return {
|
|
1110
|
+
ok: true,
|
|
1111
|
+
descriptor: instantiated.descriptor,
|
|
1112
|
+
presetContributions
|
|
1113
|
+
};
|
|
1114
|
+
}
|
|
1115
|
+
function bareTypeConstructorOf(resolution) {
|
|
1116
|
+
if (resolution?.kind !== "contributedType") return void 0;
|
|
1117
|
+
const { descriptor } = resolution.symbol;
|
|
1118
|
+
return descriptor.kind === "typeConstructor" && isBareTypeConstructor(descriptor) ? descriptor : void 0;
|
|
1119
|
+
}
|
|
1120
|
+
function resolvedKindLabel(resolution) {
|
|
1121
|
+
switch (resolution.kind) {
|
|
1122
|
+
case "compositeType": return "composite type";
|
|
1123
|
+
case "namedType": return "named type";
|
|
1124
|
+
case "block": return resolution.symbol.keyword;
|
|
1125
|
+
case "crossSpace": return "type of another contract space";
|
|
1126
|
+
default: return resolution.kind;
|
|
1127
|
+
}
|
|
1128
|
+
}
|
|
1129
|
+
function resolveFieldTypeDescriptor(input) {
|
|
1130
|
+
const { field, resolution, entityLabel, diagnostics } = input;
|
|
1131
|
+
if (field.malformedType || resolution === void 0) return NOT_RESOLVED;
|
|
1132
|
+
const source = diagnosticSource(input.sources, field.node.syntax);
|
|
1133
|
+
const unsupported = (message) => {
|
|
1134
|
+
diagnostics.push({
|
|
1135
|
+
code: "PSL_UNSUPPORTED_FIELD_TYPE",
|
|
1136
|
+
message,
|
|
1137
|
+
...source.at(field.span)
|
|
1138
|
+
});
|
|
1139
|
+
return NOT_RESOLVED;
|
|
1140
|
+
};
|
|
1141
|
+
const call = field.typeConstructor;
|
|
1142
|
+
switch (resolution.kind) {
|
|
1143
|
+
case "unresolved":
|
|
1144
|
+
case "namespace":
|
|
1145
|
+
case "contributedNamespace": return NOT_RESOLVED;
|
|
1146
|
+
case "contributedType": {
|
|
1147
|
+
const { descriptor, path } = resolution.symbol;
|
|
1148
|
+
const written = path.join(".");
|
|
1149
|
+
if (descriptor.kind === "fieldPreset") {
|
|
1150
|
+
if (call === void 0) {
|
|
1151
|
+
reportPresetNotCalled({
|
|
1152
|
+
entityLabel,
|
|
1153
|
+
presetPath: written,
|
|
1154
|
+
source,
|
|
1155
|
+
span: field.span,
|
|
1156
|
+
diagnostics
|
|
1157
|
+
});
|
|
1158
|
+
return NOT_RESOLVED;
|
|
1159
|
+
}
|
|
1160
|
+
return instantiateFieldPreset({
|
|
1161
|
+
call,
|
|
1162
|
+
descriptor,
|
|
1163
|
+
diagnostics,
|
|
1164
|
+
source,
|
|
1165
|
+
entityLabel
|
|
1166
|
+
});
|
|
1167
|
+
}
|
|
1168
|
+
if (call !== void 0) return instantiateFieldTypeConstructor({
|
|
1169
|
+
...input,
|
|
1170
|
+
call,
|
|
1171
|
+
descriptor,
|
|
1172
|
+
source
|
|
1173
|
+
});
|
|
1174
|
+
if (!isBareTypeConstructor(descriptor)) {
|
|
1175
|
+
reportTypeConstructorNotCalled({
|
|
1176
|
+
entityLabel,
|
|
1177
|
+
path: written,
|
|
1178
|
+
descriptor,
|
|
1179
|
+
source,
|
|
1180
|
+
span: field.span,
|
|
1181
|
+
diagnostics
|
|
1182
|
+
});
|
|
1183
|
+
return NOT_RESOLVED;
|
|
1184
|
+
}
|
|
1185
|
+
return {
|
|
1186
|
+
ok: true,
|
|
1187
|
+
descriptor: instantiateAuthoringTypeConstructor(descriptor, [])
|
|
1188
|
+
};
|
|
1189
|
+
}
|
|
1190
|
+
case "field":
|
|
1191
|
+
case "attribute": throw new InternalError(`The type of ${entityLabel} resolved to a ${resolution.kind}; a type reference never names one. This is a binder bug.`);
|
|
1192
|
+
}
|
|
1193
|
+
if (call !== void 0) return unsupported(`${entityLabel} calls "${call.path.join(".")}", which is a ${resolvedKindLabel(resolution)}, not a type constructor. Remove the arguments.`);
|
|
1194
|
+
switch (resolution.kind) {
|
|
1195
|
+
case "namedType": {
|
|
1196
|
+
const descriptor = input.namedTypeDescriptors.get(resolution.symbol);
|
|
1197
|
+
return descriptor === void 0 ? NOT_RESOLVED : {
|
|
1198
|
+
ok: true,
|
|
1199
|
+
descriptor
|
|
1200
|
+
};
|
|
1201
|
+
}
|
|
1202
|
+
case "block": {
|
|
1203
|
+
if (resolution.symbol.keyword !== "enum") return unsupported(`${entityLabel} is typed by the ${resolution.symbol.keyword} "${resolution.symbol.name}", which is not a column type.`);
|
|
1204
|
+
const descriptor = input.enumTypeDescriptors.get(resolution.symbol);
|
|
1205
|
+
return descriptor === void 0 ? NOT_RESOLVED : {
|
|
1206
|
+
ok: true,
|
|
1207
|
+
descriptor
|
|
1208
|
+
};
|
|
1209
|
+
}
|
|
1210
|
+
case "model":
|
|
1211
|
+
case "compositeType": return unsupported(`${entityLabel} is typed by the ${resolvedKindLabel(resolution)} "${resolution.symbol.name}", which is not a column type.`);
|
|
1212
|
+
case "crossSpace": return unsupported(`${entityLabel} type "${field.typeName}" is a type of contract space "${field.typeContractSpaceId}"; only a relation field can name a type of another contract space.`);
|
|
1213
|
+
}
|
|
1214
|
+
}
|
|
1215
|
+
const PSL_INVALID_DEFAULT_SQL = "PSL_INVALID_DEFAULT_SQL";
|
|
1216
|
+
const TAGGED_LITERAL_CANONICALIZATION_CODES = {
|
|
1217
|
+
nul: "PSL_TAGGED_LITERAL_NUL",
|
|
1218
|
+
"too-large": "PSL_TAGGED_LITERAL_TOO_LARGE"
|
|
1219
|
+
};
|
|
1220
|
+
function readTaggedLiteral(literal, support, source) {
|
|
1221
|
+
const reject = (code, message) => ({
|
|
1222
|
+
ok: false,
|
|
1223
|
+
diagnostic: {
|
|
1224
|
+
code,
|
|
1225
|
+
message,
|
|
1226
|
+
...source.at(literal.span)
|
|
1227
|
+
}
|
|
1228
|
+
});
|
|
1229
|
+
if (entryForTag(support, literal.tag) === void 0) return reject("PSL_UNKNOWN_LITERAL_TAG", `Unknown literal tag "${literal.tag}". Known tags: ${knownTags(support).join(", ")}.`);
|
|
1230
|
+
const { canonicalization } = literal;
|
|
1231
|
+
if (!canonicalization.ok) return reject(TAGGED_LITERAL_CANONICALIZATION_CODES[canonicalization.reason], describeTaggedLiteralFailure(canonicalization.reason));
|
|
1232
|
+
return {
|
|
1233
|
+
ok: true,
|
|
1234
|
+
written: {
|
|
1235
|
+
kind: "tag",
|
|
1236
|
+
tag: literal.tag,
|
|
1237
|
+
text: canonicalization.body
|
|
1238
|
+
}
|
|
1239
|
+
};
|
|
1240
|
+
}
|
|
1241
|
+
function rejectStrictListNullDefault(input) {
|
|
1242
|
+
if (input.field.elementOptional) return false;
|
|
1243
|
+
for (const arg of input.node.argList()?.args() ?? []) {
|
|
1244
|
+
const expression = arg.value();
|
|
1245
|
+
const list = expression ? ArrayLiteralAst.cast(expression.syntax) : void 0;
|
|
1246
|
+
for (const element of list?.elements() ?? []) {
|
|
1247
|
+
const identifier = IdentifierAst.cast(element.syntax);
|
|
1248
|
+
if (identifier?.token()?.text !== "null") continue;
|
|
1249
|
+
input.diagnostics.push({
|
|
1250
|
+
code: "PSL_INVALID_DEFAULT_APPLICABILITY",
|
|
1251
|
+
message: `Field "${input.modelName}.${input.field.name}" has strict list elements and cannot use null in a literal list default. Make the element type nullable or remove null from the default.`,
|
|
1252
|
+
...diagnosticSource(input.sources, identifier.syntax).at()
|
|
1253
|
+
});
|
|
1254
|
+
return true;
|
|
1255
|
+
}
|
|
1256
|
+
}
|
|
1257
|
+
return false;
|
|
1258
|
+
}
|
|
1259
|
+
function lowerDefaultForField(input) {
|
|
1260
|
+
const node = getAttribute(input.field.attributes, "default")?.node;
|
|
1261
|
+
if (node === void 0) return {};
|
|
1262
|
+
const source = diagnosticSource(input.sources, node.syntax);
|
|
1263
|
+
const spec = sqlAttributeSpecs.field.default(fieldSpecContext({
|
|
1264
|
+
symbols: input.symbolTable,
|
|
1265
|
+
model: input.model,
|
|
1266
|
+
field: input.field,
|
|
1267
|
+
binder: input.binder,
|
|
1268
|
+
controlMutationDefaults: {
|
|
1269
|
+
defaultFunctionRegistry: input.defaultFunctionRegistry,
|
|
1270
|
+
dataTypeEntries: input.dataTypeSupport.entries
|
|
1271
|
+
}
|
|
1272
|
+
}));
|
|
1273
|
+
const interpreted = interpretFieldAttribute({
|
|
1274
|
+
symbols: input.symbolTable,
|
|
1275
|
+
node,
|
|
1276
|
+
spec,
|
|
1277
|
+
model: input.model,
|
|
1278
|
+
field: input.field,
|
|
1279
|
+
sources: input.sources,
|
|
1280
|
+
binder: input.binder,
|
|
1281
|
+
diagnostics: input.diagnostics
|
|
1282
|
+
});
|
|
1283
|
+
if (interpreted === void 0) return {};
|
|
1284
|
+
const value = interpreted.value;
|
|
1285
|
+
if (Array.isArray(value) && value.includes(null) && rejectStrictListNullDefault({
|
|
1286
|
+
...input,
|
|
1287
|
+
node
|
|
1288
|
+
})) return {};
|
|
1289
|
+
if (value === null) {
|
|
1290
|
+
if (!input.field.optional) {
|
|
1291
|
+
input.diagnostics.push({
|
|
1292
|
+
code: "PSL_INVALID_DEFAULT_APPLICABILITY",
|
|
1293
|
+
message: `Field "${input.modelName}.${input.fieldName}" is non-nullable and cannot use null as its literal default. Make the field nullable or use a non-null default.`,
|
|
1294
|
+
...source.at()
|
|
1295
|
+
});
|
|
1296
|
+
return {};
|
|
1297
|
+
}
|
|
1298
|
+
return { defaultValue: {
|
|
1299
|
+
kind: "literal",
|
|
1300
|
+
value: null,
|
|
1301
|
+
canonical: true
|
|
1302
|
+
} };
|
|
1303
|
+
}
|
|
1304
|
+
const readsListElements = (written) => input.isListColumn || input.field.list && written.kind === "list";
|
|
1305
|
+
const readAsLiteral = (written) => {
|
|
1306
|
+
const lowered = lowerDataTypeDefault({
|
|
1307
|
+
written,
|
|
1308
|
+
isList: readsListElements(written),
|
|
1309
|
+
column: input.columnDescriptor,
|
|
1310
|
+
codecLookup: input.codecLookup,
|
|
1311
|
+
support: input.dataTypeSupport,
|
|
1312
|
+
fieldPath: `${input.modelName}.${input.fieldName}`
|
|
1313
|
+
});
|
|
1314
|
+
if (!lowered.ok) {
|
|
1315
|
+
input.diagnostics.push({
|
|
1316
|
+
code: lowered.code,
|
|
1317
|
+
message: lowered.message,
|
|
1318
|
+
...source.at()
|
|
1319
|
+
});
|
|
1320
|
+
return {};
|
|
1321
|
+
}
|
|
1322
|
+
let restoredValue = lowered.value;
|
|
1323
|
+
if (Array.isArray(value) && value.includes(null) && Array.isArray(lowered.value)) {
|
|
1324
|
+
let index = 0;
|
|
1325
|
+
const nonNullValues = lowered.value;
|
|
1326
|
+
restoredValue = value.map((element) => element === null ? null : nonNullValues[index++] ?? null);
|
|
1327
|
+
}
|
|
1328
|
+
if (input.valueObjectDefault !== void 0) {
|
|
1329
|
+
const mismatches = valueObjectDefaultMismatches({
|
|
1330
|
+
fieldPath: `${input.modelName}.${input.fieldName}`,
|
|
1331
|
+
value: restoredValue,
|
|
1332
|
+
list: input.field.list,
|
|
1333
|
+
nullable: input.field.optional,
|
|
1334
|
+
elementNullable: input.field.elementOptional,
|
|
1335
|
+
...input.valueObjectDefault,
|
|
1336
|
+
codecLookup: input.codecLookup
|
|
1337
|
+
});
|
|
1338
|
+
for (const { code, message } of mismatches) input.diagnostics.push({
|
|
1339
|
+
code,
|
|
1340
|
+
message,
|
|
1341
|
+
...source.at()
|
|
1342
|
+
});
|
|
1343
|
+
if (mismatches.length > 0) return {};
|
|
1344
|
+
}
|
|
1345
|
+
return { defaultValue: {
|
|
1346
|
+
kind: "literal",
|
|
1347
|
+
value: restoredValue,
|
|
1348
|
+
canonical: true
|
|
1349
|
+
} };
|
|
1350
|
+
};
|
|
1351
|
+
const writtenElement = (element) => {
|
|
1352
|
+
if (typeof element === "string") return {
|
|
1353
|
+
kind: "string",
|
|
1354
|
+
text: element
|
|
1355
|
+
};
|
|
1356
|
+
if (typeof element === "boolean") return {
|
|
1357
|
+
kind: "boolean",
|
|
1358
|
+
value: element
|
|
1359
|
+
};
|
|
1360
|
+
if ("text" in element) return {
|
|
1361
|
+
kind: "number",
|
|
1362
|
+
text: element.text
|
|
1363
|
+
};
|
|
1364
|
+
const literal = readTaggedLiteral(element, input.dataTypeSupport, source);
|
|
1365
|
+
if (!literal.ok) {
|
|
1366
|
+
input.diagnostics.push(literal.diagnostic);
|
|
1367
|
+
return { ok: false };
|
|
1368
|
+
}
|
|
1369
|
+
return literal.written;
|
|
1370
|
+
};
|
|
1371
|
+
const sqlExpressionDefault = (text, span) => {
|
|
1372
|
+
const reserved = reservedSqlDefaultBody(text);
|
|
1373
|
+
const refusal = reserved === void 0 ? checkSqlDefaultBody(text) : `Write @default(${reserved}()) instead of sql\`${reserved}()\`; ${reserved}() is a Prisma default function, not raw SQL.`;
|
|
1374
|
+
if (refusal !== void 0) {
|
|
1375
|
+
input.diagnostics.push({
|
|
1376
|
+
code: PSL_INVALID_DEFAULT_SQL,
|
|
1377
|
+
message: refusal,
|
|
1378
|
+
...source.at(span)
|
|
1379
|
+
});
|
|
1380
|
+
return {};
|
|
1381
|
+
}
|
|
1382
|
+
return { defaultValue: {
|
|
1383
|
+
kind: "function",
|
|
1384
|
+
expression: text
|
|
1385
|
+
} };
|
|
1386
|
+
};
|
|
1387
|
+
if (input.columnDescriptor.valueSet !== void 0) {
|
|
1388
|
+
if (typeof value === "string") return { defaultValue: {
|
|
1389
|
+
kind: "literal",
|
|
1390
|
+
value
|
|
1391
|
+
} };
|
|
1392
|
+
if (Array.isArray(value)) {
|
|
1393
|
+
const members = value.filter((element) => element === null || typeof element === "string");
|
|
1394
|
+
if (members.length === value.length) return { defaultValue: {
|
|
1395
|
+
kind: "literal",
|
|
1396
|
+
value: members
|
|
1397
|
+
} };
|
|
1398
|
+
}
|
|
1399
|
+
}
|
|
1400
|
+
if (Array.isArray(value)) {
|
|
1401
|
+
const elements = [];
|
|
1402
|
+
for (const element of value) {
|
|
1403
|
+
if (element === null) continue;
|
|
1404
|
+
const written = writtenElement(element);
|
|
1405
|
+
if ("ok" in written) return {};
|
|
1406
|
+
elements.push(written);
|
|
1407
|
+
}
|
|
1408
|
+
return readAsLiteral({
|
|
1409
|
+
kind: "list",
|
|
1410
|
+
elements
|
|
1411
|
+
});
|
|
1412
|
+
}
|
|
1413
|
+
if (typeof value === "string") return readAsLiteral({
|
|
1414
|
+
kind: "string",
|
|
1415
|
+
text: value
|
|
1416
|
+
});
|
|
1417
|
+
if (typeof value === "boolean") return readAsLiteral({
|
|
1418
|
+
kind: "boolean",
|
|
1419
|
+
value
|
|
1420
|
+
});
|
|
1421
|
+
if ("text" in value) return readAsLiteral({
|
|
1422
|
+
kind: "number",
|
|
1423
|
+
text: value.text
|
|
1424
|
+
});
|
|
1425
|
+
if ("tag" in value) {
|
|
1426
|
+
const literal = readTaggedLiteral(value, input.dataTypeSupport, source);
|
|
1427
|
+
if (!literal.ok) {
|
|
1428
|
+
input.diagnostics.push(literal.diagnostic);
|
|
1429
|
+
return {};
|
|
1430
|
+
}
|
|
1431
|
+
const read = readValue(input.dataTypeSupport, literal.written, void 0);
|
|
1432
|
+
if (read.ok && read.typed.type === SQL_EXPRESSION_DATA_TYPE_ID) return sqlExpressionDefault(sqlTextFromCanonical(read.typed.value), value.span);
|
|
1433
|
+
return readAsLiteral(literal.written);
|
|
1434
|
+
}
|
|
1435
|
+
if (typeof value === "object") {
|
|
1436
|
+
const context = {
|
|
1437
|
+
sourceId: input.sources.sourceFileFor(node.syntax).filename,
|
|
1438
|
+
modelName: input.modelName,
|
|
1439
|
+
fieldName: input.fieldName,
|
|
1440
|
+
columnCodecId: input.columnDescriptor.codecId
|
|
1441
|
+
};
|
|
1442
|
+
const lowered = lowerDefaultFunctionWithRegistry({
|
|
1443
|
+
call: value,
|
|
1444
|
+
registry: input.defaultFunctionRegistry,
|
|
1445
|
+
context,
|
|
1446
|
+
source
|
|
1447
|
+
});
|
|
1448
|
+
if (!lowered.ok) {
|
|
1449
|
+
if (lowered.kind === "owned") input.diagnostics.push(lowered.diagnostic);
|
|
1450
|
+
else input.diagnostics.pushExternal(lowered.diagnostic);
|
|
1451
|
+
return {};
|
|
1452
|
+
}
|
|
1453
|
+
if (lowered.value.kind === "storage") return { defaultValue: lowered.value.defaultValue };
|
|
1454
|
+
const generatorDescriptor = input.generatorDescriptorById.get(lowered.value.generated.id);
|
|
1455
|
+
if (!generatorDescriptor) {
|
|
1456
|
+
input.diagnostics.push({
|
|
1457
|
+
code: "PSL_INVALID_DEFAULT_APPLICABILITY",
|
|
1458
|
+
message: `Default generator "${lowered.value.generated.id}" is not available in the composed mutation default registry.`,
|
|
1459
|
+
...source.at(value.span)
|
|
1460
|
+
});
|
|
1461
|
+
return {};
|
|
1462
|
+
}
|
|
1463
|
+
if (generatorDescriptor.applicableCodecIds === void 0) {
|
|
1464
|
+
input.diagnostics.push({
|
|
1465
|
+
code: "PSL_INVALID_DEFAULT_APPLICABILITY",
|
|
1466
|
+
message: `Default generator "${generatorDescriptor.id}" is not applicable to "@default(...)" lowering. Use the corresponding field preset (e.g. \`temporal.${generatorDescriptor.id === "timestampNow" ? "updatedAt" : generatorDescriptor.id}()\`) instead.`,
|
|
1467
|
+
...source.at(value.span)
|
|
1468
|
+
});
|
|
1469
|
+
return {};
|
|
1470
|
+
}
|
|
1471
|
+
if (!generatorDescriptor.applicableCodecIds.includes(input.columnDescriptor.codecId)) {
|
|
1472
|
+
input.diagnostics.push({
|
|
1473
|
+
code: "PSL_INVALID_DEFAULT_APPLICABILITY",
|
|
1474
|
+
message: `Default generator "${generatorDescriptor.id}" is not applicable to "${input.modelName}.${input.fieldName}" with codecId "${input.columnDescriptor.codecId}".`,
|
|
1475
|
+
...source.at(value.span)
|
|
1476
|
+
});
|
|
1477
|
+
return {};
|
|
1478
|
+
}
|
|
1479
|
+
return { executionDefaults: { onCreate: lowered.value.generated } };
|
|
1480
|
+
}
|
|
1481
|
+
return {};
|
|
1482
|
+
}
|
|
1483
|
+
function lowerEnumDefaultForField(input) {
|
|
1484
|
+
const { field, model, enumHandle, diagnostics } = input;
|
|
1485
|
+
const node = getAttribute(field.attributes, "default")?.node;
|
|
1486
|
+
if (node === void 0) return {};
|
|
1487
|
+
if (enumHandle.enumMembers.length === 0) return {};
|
|
1488
|
+
const interpreted = interpretFieldAttribute({
|
|
1489
|
+
node,
|
|
1490
|
+
spec: sqlAttributeSpecs.field.default(fieldSpecContext({
|
|
1491
|
+
symbols: input.symbolTable,
|
|
1492
|
+
model,
|
|
1493
|
+
field,
|
|
1494
|
+
binder: input.binder,
|
|
1495
|
+
controlMutationDefaults: {
|
|
1496
|
+
defaultFunctionRegistry: input.defaultFunctionRegistry,
|
|
1497
|
+
dataTypeEntries: input.dataTypeSupport.entries
|
|
1498
|
+
}
|
|
1499
|
+
})),
|
|
1500
|
+
symbols: input.symbolTable,
|
|
1501
|
+
model,
|
|
1502
|
+
field,
|
|
1503
|
+
sources: input.sources,
|
|
1504
|
+
binder: input.binder,
|
|
1505
|
+
diagnostics
|
|
1506
|
+
});
|
|
1507
|
+
if (interpreted === void 0) return {};
|
|
1508
|
+
const member = interpreted.value;
|
|
1509
|
+
if (Array.isArray(member)) {
|
|
1510
|
+
if (member.includes(null) && rejectStrictListNullDefault({
|
|
1511
|
+
...input,
|
|
1512
|
+
node
|
|
1513
|
+
})) return {};
|
|
1514
|
+
return { defaultValue: {
|
|
1515
|
+
kind: "literal",
|
|
1516
|
+
value: blindCast(member.map((entry) => entry === null ? null : enumHandle.enumMembers.find((candidate) => candidate.name === entry)?.value))
|
|
1517
|
+
} };
|
|
1518
|
+
}
|
|
1519
|
+
invariant(typeof member === "string", "the enum @default grammar admits only member identifiers, so the parsed value is a string");
|
|
1520
|
+
const match = enumHandle.enumMembers.find((m) => m.name === member);
|
|
1521
|
+
if (!match) return {};
|
|
1522
|
+
return { defaultValue: {
|
|
1523
|
+
kind: "literal",
|
|
1524
|
+
value: blindCast(match.value)
|
|
1525
|
+
} };
|
|
1526
|
+
}
|
|
1527
|
+
const MODEL_COORDINATE_SEPARATOR = "\0";
|
|
1528
|
+
function modelCoordinateKey(namespaceId, modelName) {
|
|
1529
|
+
return `${namespaceId}${MODEL_COORDINATE_SEPARATOR}${modelName}`;
|
|
1530
|
+
}
|
|
1531
|
+
const REMOVED_ATTRIBUTE_RULES = /* @__PURE__ */ new Map([["updatedAt", {
|
|
1532
|
+
hint: "Use `temporal.updatedAt()` as a field-preset call instead.",
|
|
1533
|
+
suppressWhen: (field) => field.typeConstructor?.path[0] === "temporal"
|
|
1534
|
+
}]]);
|
|
1535
|
+
{
|
|
1536
|
+
const overlap = [...REMOVED_ATTRIBUTE_RULES.keys()].filter((name) => Object.hasOwn(sqlAttributeSpecs.field, name));
|
|
1537
|
+
if (overlap.length > 0) throw new InternalError(`Registered SQL field attributes and REMOVED_ATTRIBUTE_RULES must not overlap. Names in both: ${overlap.join(", ")}`);
|
|
1538
|
+
}
|
|
1539
|
+
function describeUnsupportedSqlAttribute(sources) {
|
|
1540
|
+
return ({ attribute, level, owner, field }) => {
|
|
1541
|
+
if (owner.kind === "compositeType") return void 0;
|
|
1542
|
+
if (level === "model") {
|
|
1543
|
+
const source = diagnosticSource(sources, owner.node.syntax);
|
|
1544
|
+
return {
|
|
1545
|
+
code: "PSL_UNSUPPORTED_MODEL_ATTRIBUTE",
|
|
1546
|
+
message: `Model "${owner.name}" uses unsupported attribute "@@${attribute.name}"`,
|
|
1547
|
+
...source.at(attribute.span)
|
|
1548
|
+
};
|
|
1549
|
+
}
|
|
1550
|
+
if (field === void 0) return void 0;
|
|
1551
|
+
const source = diagnosticSource(sources, field.node.syntax);
|
|
1552
|
+
if (attribute.name.startsWith("db.")) return {
|
|
1553
|
+
code: "PSL_UNSUPPORTED_FIELD_ATTRIBUTE",
|
|
1554
|
+
message: formatDbAttributeMigrationMessage(attribute),
|
|
1555
|
+
...source.at(attribute.span)
|
|
1556
|
+
};
|
|
1557
|
+
const baseMessage = `Field "${owner.name}.${field.name}" uses unsupported attribute "@${attribute.name}"`;
|
|
1558
|
+
const removedRule = REMOVED_ATTRIBUTE_RULES.get(attribute.name);
|
|
1559
|
+
return {
|
|
1560
|
+
code: "PSL_UNSUPPORTED_FIELD_ATTRIBUTE",
|
|
1561
|
+
message: removedRule && !removedRule.suppressWhen(field) ? `${baseMessage}. ${removedRule.hint}` : baseMessage,
|
|
1562
|
+
...source.at(attribute.span)
|
|
1563
|
+
};
|
|
1564
|
+
};
|
|
1565
|
+
}
|
|
1566
|
+
function extractFieldConstraintNames(input) {
|
|
1567
|
+
const idAttribute = getAttribute(input.field.attributes, "id");
|
|
1568
|
+
const uniqueAttribute = getAttribute(input.field.attributes, "unique");
|
|
1569
|
+
const idNode = getAttribute(input.field.attributes, "id")?.node;
|
|
1570
|
+
const idName = idNode === void 0 ? void 0 : interpretFieldAttribute({
|
|
1571
|
+
node: idNode,
|
|
1572
|
+
symbols: input.symbolTable,
|
|
1573
|
+
spec: sqlAttributeSpecs.field.id(),
|
|
1574
|
+
model: input.model,
|
|
1575
|
+
field: input.field,
|
|
1576
|
+
sources: input.sources,
|
|
1577
|
+
binder: input.binder,
|
|
1578
|
+
diagnostics: input.diagnostics
|
|
1579
|
+
})?.map;
|
|
1580
|
+
const uniqueNode = getAttribute(input.field.attributes, "unique")?.node;
|
|
1581
|
+
return {
|
|
1582
|
+
idAttribute,
|
|
1583
|
+
uniqueAttribute,
|
|
1584
|
+
idName,
|
|
1585
|
+
uniqueName: uniqueNode === void 0 ? void 0 : interpretFieldAttribute({
|
|
1586
|
+
node: uniqueNode,
|
|
1587
|
+
symbols: input.symbolTable,
|
|
1588
|
+
spec: sqlAttributeSpecs.field.unique(),
|
|
1589
|
+
model: input.model,
|
|
1590
|
+
field: input.field,
|
|
1591
|
+
sources: input.sources,
|
|
1592
|
+
binder: input.binder,
|
|
1593
|
+
diagnostics: input.diagnostics
|
|
1594
|
+
})?.map
|
|
1595
|
+
};
|
|
1596
|
+
}
|
|
1597
|
+
/**
|
|
1598
|
+
* Interprets a field's `@noCheck` attribute and resolves it against the
|
|
1599
|
+
* column's shape: the bare form becomes every kind the shape derives, and a
|
|
1600
|
+
* named kind that can never apply is a spec-validation diagnostic on the
|
|
1601
|
+
* attribute node. Returns the concrete kinds in canonical ascending order —
|
|
1602
|
+
* the only form the definition tree carries.
|
|
1603
|
+
*/
|
|
1604
|
+
function lowerNoCheckForField(input) {
|
|
1605
|
+
const node = getAttribute(input.field.attributes, "noCheck")?.node;
|
|
1606
|
+
if (node === void 0) return void 0;
|
|
1607
|
+
const interpreted = interpretFieldAttribute({
|
|
1608
|
+
node,
|
|
1609
|
+
spec: sqlAttributeSpecs.field.noCheck(),
|
|
1610
|
+
symbols: input.symbolTable,
|
|
1611
|
+
model: input.model,
|
|
1612
|
+
field: input.field,
|
|
1613
|
+
sources: input.sources,
|
|
1614
|
+
binder: input.binder,
|
|
1615
|
+
diagnostics: input.diagnostics
|
|
1616
|
+
});
|
|
1617
|
+
if (interpreted === void 0) return void 0;
|
|
1618
|
+
const span = getAttribute(input.field.attributes, "noCheck")?.span ?? input.field.span;
|
|
1619
|
+
const subject = `Field "${input.model.name}.${input.field.name}"`;
|
|
1620
|
+
const derivable = [];
|
|
1621
|
+
if (input.isListColumn && !input.elementNullable) derivable.push("elementNotNull");
|
|
1622
|
+
if (input.isDomainEnum) derivable.push("membership");
|
|
1623
|
+
const authored = [interpreted.first, interpreted.second].filter((kind) => kind === "membership" || kind === "elementNotNull");
|
|
1624
|
+
if (authored.length === 0) {
|
|
1625
|
+
if (derivable.length === 0) {
|
|
1626
|
+
input.diagnostics.push({
|
|
1627
|
+
code: "PSL_INVALID_ATTRIBUTE_ARGUMENT",
|
|
1628
|
+
message: `${subject} @noCheck waives nothing — this column's shape derives no generated checks`,
|
|
1629
|
+
...diagnosticSource(input.sources, input.field.node.syntax).at(span)
|
|
1630
|
+
});
|
|
1631
|
+
return;
|
|
1632
|
+
}
|
|
1633
|
+
return derivable;
|
|
1634
|
+
}
|
|
1635
|
+
for (const kind of authored) if (!derivable.includes(kind)) {
|
|
1636
|
+
const explanation = kind === "membership" ? "membership checks are derived only from enum value sets" : "element-non-null checks are derived only for lists whose elements are semantically non-null";
|
|
1637
|
+
input.diagnostics.push({
|
|
1638
|
+
code: "PSL_INVALID_ATTRIBUTE_ARGUMENT",
|
|
1639
|
+
message: `${subject} @noCheck(${kind}) does not apply — ${explanation}`,
|
|
1640
|
+
...diagnosticSource(input.sources, input.field.node.syntax).at(span)
|
|
1641
|
+
});
|
|
1642
|
+
return;
|
|
1643
|
+
}
|
|
1644
|
+
return [...authored].sort();
|
|
1645
|
+
}
|
|
1646
|
+
function collectResolvedFields(input) {
|
|
1647
|
+
const { model, symbolTable, enumTypeDescriptors, namedTypeDescriptors, valueObjectTypes, authoringContributions, binder, targetId, defaultFunctionRegistry, dataTypeSupport, generatorDescriptorById, diagnostics, sources, scalarColumnDescriptors, enumHandles, capabilities, namespaceId, namespaceExtensionEntities, codecLookup } = input;
|
|
1648
|
+
const resolvedFields = [];
|
|
1649
|
+
const valueObjectStorageTypeName = authoringContributions?.valueObjectStorageType;
|
|
1650
|
+
const declaredControlPolicy = getAttribute(model.attributes, "control")?.args.find((arg) => arg.kind === "positional")?.value.trim();
|
|
1651
|
+
const modelDerivesChecks = declaredControlPolicy === void 0 || declaredControlPolicy === "managed";
|
|
1652
|
+
for (const field of Object.values(model.fields)) {
|
|
1653
|
+
const source = diagnosticSource(sources, field.node.syntax);
|
|
1654
|
+
const fieldTypeReference = typeReferenceNode(field);
|
|
1655
|
+
const fieldTypeResolution = fieldTypeReference === void 0 ? void 0 : binder.symbolForNode(fieldTypeReference);
|
|
1656
|
+
const isModelField = fieldTypeResolution?.kind === "model";
|
|
1657
|
+
if (field.list && isModelField) continue;
|
|
1658
|
+
const relationAttribute = getAttribute(field.attributes, "relation");
|
|
1659
|
+
if (isModelField && relationAttribute) continue;
|
|
1660
|
+
if (field.typeContractSpaceId !== void 0 && relationAttribute) continue;
|
|
1661
|
+
if (isModelField) continue;
|
|
1662
|
+
const valueObjectName = fieldTypeResolution?.kind === "compositeType" ? fieldTypeResolution.symbol.name : void 0;
|
|
1663
|
+
const isValueObjectField = valueObjectName !== void 0;
|
|
1664
|
+
const isListField = field.list;
|
|
1665
|
+
const isListColumn = storedAsListColumn({
|
|
1666
|
+
list: isListField,
|
|
1667
|
+
typedByValueObject: isValueObjectField
|
|
1668
|
+
});
|
|
1669
|
+
let descriptor;
|
|
1670
|
+
let presetContributions;
|
|
1671
|
+
const resolveInput = {
|
|
1672
|
+
field,
|
|
1673
|
+
resolution: fieldTypeResolution,
|
|
1674
|
+
enumTypeDescriptors,
|
|
1675
|
+
namedTypeDescriptors,
|
|
1676
|
+
diagnostics,
|
|
1677
|
+
sources,
|
|
1678
|
+
entityLabel: `Field "${model.name}.${field.name}"`,
|
|
1679
|
+
...ifDefined("namespaceId", namespaceId),
|
|
1680
|
+
...ifDefined("namespaceExtensionEntities", namespaceExtensionEntities),
|
|
1681
|
+
...ifDefined("codecLookup", codecLookup)
|
|
1682
|
+
};
|
|
1683
|
+
if (isValueObjectField) {
|
|
1684
|
+
if (valueObjectStorageTypeName === void 0) {
|
|
1685
|
+
diagnostics.push({
|
|
1686
|
+
code: "PSL_UNSUPPORTED_FIELD_TYPE",
|
|
1687
|
+
message: `Field "${model.name}.${field.name}" is typed by the composite type "${field.typeName}", but the adapter of the stack declares no storage type for value objects, so the field has no column to be stored in.`,
|
|
1688
|
+
...source.at(field.span)
|
|
1689
|
+
});
|
|
1690
|
+
continue;
|
|
1691
|
+
}
|
|
1692
|
+
descriptor = scalarColumnDescriptors.get(valueObjectStorageTypeName);
|
|
1693
|
+
if (descriptor === void 0) throw new InternalError(`The stack declares "${valueObjectStorageTypeName}" as its value-object storage type, but it is not one of the stack's scalar types; the control stack checks this when it is assembled.`);
|
|
1694
|
+
} else if (isListField) {
|
|
1695
|
+
if (capabilities["sql"]?.["scalarList"] !== true) {
|
|
1696
|
+
diagnostics.push({
|
|
1697
|
+
code: "PSL_SCALAR_LIST_UNSUPPORTED_TARGET",
|
|
1698
|
+
message: `Field "${model.name}.${field.name}" is a scalar list, but target "${targetId}" does not support scalar lists (the adapter does not report the "scalarList" capability). Remove the list or author it against a target that supports scalar lists.`,
|
|
1699
|
+
...source.at(field.span)
|
|
1700
|
+
});
|
|
1701
|
+
continue;
|
|
1702
|
+
}
|
|
1703
|
+
const resolved = resolveFieldTypeDescriptor(resolveInput);
|
|
1704
|
+
if (!resolved.ok) continue;
|
|
1705
|
+
if (resolved.presetContributions) {
|
|
1706
|
+
diagnostics.push({
|
|
1707
|
+
code: "PSL_PRESET_NOT_LIST",
|
|
1708
|
+
message: `Field "${model.name}.${field.name}" uses a field-preset call as a list element type. Presets cannot be list elements; remove "[]" or use a scalar type.`,
|
|
1709
|
+
...source.at(field.span)
|
|
1710
|
+
});
|
|
1711
|
+
continue;
|
|
1712
|
+
}
|
|
1713
|
+
descriptor = resolved.descriptor;
|
|
1714
|
+
} else {
|
|
1715
|
+
const resolved = resolveFieldTypeDescriptor(resolveInput);
|
|
1716
|
+
if (!resolved.ok) continue;
|
|
1717
|
+
descriptor = resolved.descriptor;
|
|
1718
|
+
presetContributions = resolved.presetContributions;
|
|
1719
|
+
}
|
|
1720
|
+
if (!descriptor) continue;
|
|
1721
|
+
if (presetContributions && field.optional) {
|
|
1722
|
+
diagnostics.push({
|
|
1723
|
+
code: "PSL_PRESET_NOT_OPTIONAL",
|
|
1724
|
+
message: `Field "${model.name}.${field.name}" uses a field-preset call and cannot be optional. Remove "?" or use a different field type.`,
|
|
1725
|
+
...source.at(field.span)
|
|
1726
|
+
});
|
|
1727
|
+
continue;
|
|
1728
|
+
}
|
|
1729
|
+
const defaultAttribute = getAttribute(field.attributes, "default");
|
|
1730
|
+
if (presetContributions && defaultAttribute) {
|
|
1731
|
+
diagnostics.push({
|
|
1732
|
+
code: "PSL_PRESET_AND_DEFAULT_CONFLICT",
|
|
1733
|
+
message: `Field "${model.name}.${field.name}" uses a field-preset call and cannot also declare @default(...). The preset already specifies the default value.`,
|
|
1734
|
+
...source.at(defaultAttribute.span)
|
|
1735
|
+
});
|
|
1736
|
+
continue;
|
|
1737
|
+
}
|
|
1738
|
+
const enumHandle = fieldTypeResolution?.kind === "block" ? enumHandles.get(fieldTypeResolution.symbol) : void 0;
|
|
1739
|
+
const loweredDefault = defaultAttribute ? enumHandle ? lowerEnumDefaultForField({
|
|
1740
|
+
modelName: model.name,
|
|
1741
|
+
fieldName: field.name,
|
|
1742
|
+
field,
|
|
1743
|
+
model,
|
|
1744
|
+
symbolTable,
|
|
1745
|
+
sources: input.sources,
|
|
1746
|
+
binder: input.binder,
|
|
1747
|
+
enumHandle,
|
|
1748
|
+
defaultFunctionRegistry,
|
|
1749
|
+
dataTypeSupport,
|
|
1750
|
+
diagnostics
|
|
1751
|
+
}) : lowerDefaultForField({
|
|
1752
|
+
modelName: model.name,
|
|
1753
|
+
fieldName: field.name,
|
|
1754
|
+
field,
|
|
1755
|
+
model,
|
|
1756
|
+
symbolTable,
|
|
1757
|
+
sources: input.sources,
|
|
1758
|
+
binder: input.binder,
|
|
1759
|
+
columnDescriptor: descriptor,
|
|
1760
|
+
isListColumn,
|
|
1761
|
+
valueObjectDefault: valueObjectName === void 0 ? void 0 : {
|
|
1762
|
+
valueObjectName,
|
|
1763
|
+
types: valueObjectTypes
|
|
1764
|
+
},
|
|
1765
|
+
generatorDescriptorById,
|
|
1766
|
+
defaultFunctionRegistry,
|
|
1767
|
+
dataTypeSupport,
|
|
1768
|
+
codecLookup,
|
|
1769
|
+
diagnostics
|
|
1770
|
+
}) : {};
|
|
1771
|
+
const loweredOnCreate = loweredDefault.executionDefaults?.onCreate;
|
|
1772
|
+
if (isListField && loweredDefault.defaultValue?.kind === "function" && loweredDefault.defaultValue.expression === "autoincrement()") {
|
|
1773
|
+
diagnostics.push({
|
|
1774
|
+
code: "PSL_LIST_AUTOINCREMENT_UNSUPPORTED",
|
|
1775
|
+
message: `Field "${model.name}.${field.name}" is a list and cannot use autoincrement(); it is a Prisma marker for a sequence-backed scalar column, not SQL.`,
|
|
1776
|
+
...source.at(defaultAttribute?.span ?? field.span)
|
|
1777
|
+
});
|
|
1778
|
+
continue;
|
|
1779
|
+
}
|
|
1780
|
+
if (isListField && loweredOnCreate) {
|
|
1781
|
+
const defaultExpression = defaultAttribute?.args.find((arg) => arg.kind === "positional")?.value.trim() ?? "this function";
|
|
1782
|
+
diagnostics.push({
|
|
1783
|
+
code: "PSL_LIST_EXECUTION_DEFAULT_UNSUPPORTED",
|
|
1784
|
+
message: `Field "${model.name}.${field.name}" is a list and cannot use an execution default ("${defaultExpression}"). Lists have no per-element execution-default semantics; use a literal list @default or remove the default.`,
|
|
1785
|
+
...source.at(defaultAttribute?.span ?? field.span)
|
|
1786
|
+
});
|
|
1787
|
+
continue;
|
|
1788
|
+
}
|
|
1789
|
+
if (field.optional && loweredOnCreate) {
|
|
1790
|
+
const generatorDescription = loweredOnCreate.kind === "generator" ? `"${loweredOnCreate.id}"` : "for this field";
|
|
1791
|
+
diagnostics.push({
|
|
1792
|
+
code: "PSL_INVALID_DEFAULT_FUNCTION_ARGUMENT",
|
|
1793
|
+
message: `Field "${model.name}.${field.name}" cannot be optional when using execution default ${generatorDescription}. Remove "?" or use a storage default.`,
|
|
1794
|
+
...source.at(defaultAttribute?.span ?? field.span)
|
|
1795
|
+
});
|
|
1796
|
+
continue;
|
|
1797
|
+
}
|
|
1798
|
+
const mappedColumnName = storageName(field, input.physicalNames);
|
|
1799
|
+
const { idAttribute, uniqueAttribute, idName, uniqueName } = extractFieldConstraintNames({
|
|
1800
|
+
symbolTable: input.symbolTable,
|
|
1801
|
+
model,
|
|
1802
|
+
field,
|
|
1803
|
+
sources: input.sources,
|
|
1804
|
+
binder: input.binder,
|
|
1805
|
+
diagnostics
|
|
1806
|
+
});
|
|
1807
|
+
let isIdField = Boolean(idAttribute);
|
|
1808
|
+
if (idAttribute && isListField) {
|
|
1809
|
+
diagnostics.push({
|
|
1810
|
+
code: "PSL_LIST_ID_UNSUPPORTED",
|
|
1811
|
+
message: `Field "${model.name}.${field.name}" is a list and cannot be a primary key. Remove @id; a list cannot be an identity column.`,
|
|
1812
|
+
...source.at(idAttribute.span)
|
|
1813
|
+
});
|
|
1814
|
+
continue;
|
|
1815
|
+
}
|
|
1816
|
+
if (idAttribute && field.optional) {
|
|
1817
|
+
diagnostics.push({
|
|
1818
|
+
code: "PSL_INVALID_ATTRIBUTE_ARGUMENT",
|
|
1819
|
+
message: `Field "${model.name}.${field.name}" @id cannot be optional; primary key columns must be NOT NULL`,
|
|
1820
|
+
...source.at(idAttribute.span)
|
|
1821
|
+
});
|
|
1822
|
+
isIdField = false;
|
|
1823
|
+
}
|
|
1824
|
+
if (presetContributions && idAttribute && !presetContributions.id) {
|
|
1825
|
+
diagnostics.push({
|
|
1826
|
+
code: "PSL_PRESET_AND_ID_CONFLICT",
|
|
1827
|
+
message: `Field "${model.name}.${field.name}" uses a field-preset call and cannot also declare @id. Use a preset that contributes id semantics, or drop @id.`,
|
|
1828
|
+
...source.at(idAttribute.span)
|
|
1829
|
+
});
|
|
1830
|
+
continue;
|
|
1831
|
+
}
|
|
1832
|
+
const fieldExecutionDefaults = presetContributions?.executionDefaults ?? loweredDefault.executionDefaults;
|
|
1833
|
+
const fieldDefaultValue = presetContributions?.default ?? loweredDefault.defaultValue;
|
|
1834
|
+
const noCheckKinds = modelDerivesChecks ? lowerNoCheckForField({
|
|
1835
|
+
symbolTable: input.symbolTable,
|
|
1836
|
+
model,
|
|
1837
|
+
field,
|
|
1838
|
+
sources: input.sources,
|
|
1839
|
+
binder: input.binder,
|
|
1840
|
+
isListColumn,
|
|
1841
|
+
elementNullable: field.elementOptional,
|
|
1842
|
+
isDomainEnum: enumHandle !== void 0,
|
|
1843
|
+
diagnostics
|
|
1844
|
+
}) : void 0;
|
|
1845
|
+
resolvedFields.push({
|
|
1846
|
+
field,
|
|
1847
|
+
columnName: mappedColumnName,
|
|
1848
|
+
descriptor,
|
|
1849
|
+
nullable: presetContributions?.nullable ?? field.optional,
|
|
1850
|
+
...ifDefined("defaultValue", fieldDefaultValue),
|
|
1851
|
+
...ifDefined("executionDefaults", fieldExecutionDefaults),
|
|
1852
|
+
isId: isIdField || Boolean(presetContributions?.id),
|
|
1853
|
+
isUnique: Boolean(uniqueAttribute) || Boolean(presetContributions?.unique),
|
|
1854
|
+
...ifDefined("idName", idName),
|
|
1855
|
+
...ifDefined("uniqueName", uniqueName),
|
|
1856
|
+
...ifDefined("many", isListField ? true : void 0),
|
|
1857
|
+
...ifDefined("noCheck", noCheckKinds),
|
|
1858
|
+
...ifDefined("elementNullable", field.elementOptional ? true : void 0),
|
|
1859
|
+
...ifDefined("valueObjectTypeName", valueObjectName),
|
|
1860
|
+
...ifDefined("enumTypeHandle", enumHandle)
|
|
1861
|
+
});
|
|
1862
|
+
}
|
|
1863
|
+
return resolvedFields;
|
|
1864
|
+
}
|
|
1865
|
+
//#endregion
|
|
1866
|
+
export { toNamedTypeFieldDescriptor as S, modelCoordinateKey as _, describeUnsupportedSqlAttribute as a, sqlAttributeSpecs as b, getAttribute as c, instantiateFieldTypeConstructor as d, instantiatePslTypeConstructor as f, modelAttributeSpecsFrom as g, mapFieldNamesToColumns as h, contractError as i, getAuthoringEntity as l, interpretModelAttribute as m, bareTypeConstructorOf as n, entryForTag as o, interpretFieldAttribute as p, collectResolvedFields as r, formatDbAttributeMigrationMessage as s, PSL_CHECK_ON_STI_VARIANT as t, getNamedArgument as u, readDataTypeDefault as v, storageName as x, resolveFieldTypeDescriptor as y };
|
|
1867
|
+
|
|
1868
|
+
//# sourceMappingURL=psl-field-resolution-_o74AUE4-CKYNRnzL.mjs.map
|