@prisma-next/sql-contract-psl 0.13.0-dev.16 → 0.13.0-dev.18

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/index.d.mts CHANGED
@@ -5,6 +5,7 @@ import { Result } from "@prisma-next/utils/result";
5
5
  import { ParsePslDocumentResult } from "@prisma-next/psl-parser";
6
6
  import { ControlMutationDefaults, ControlMutationDefaults as ControlMutationDefaults$1, DefaultFunctionLoweringContext, DefaultFunctionLoweringHandler, DefaultFunctionRegistry, DefaultFunctionRegistryEntry, MutationDefaultGeneratorDescriptor } from "@prisma-next/framework-components/control";
7
7
  import { ContractSourceDiagnostics } from "@prisma-next/config/config-types";
8
+ import { CodecLookup } from "@prisma-next/framework-components/codec";
8
9
  import { ExtensionPackRef, TargetPackRef } from "@prisma-next/framework-components/components";
9
10
  import { Namespace } from "@prisma-next/framework-components/ir";
10
11
 
@@ -45,6 +46,7 @@ interface InterpretPslDocumentToSqlContractInput {
45
46
  * `SqlUnboundNamespace` singleton.
46
47
  */
47
48
  readonly createNamespace?: (input: SqlNamespaceTablesInput) => Namespace;
49
+ readonly codecLookup?: CodecLookup;
48
50
  }
49
51
  declare function interpretPslDocumentToSqlContract(input: InterpretPslDocumentToSqlContractInput): Result<Contract, ContractSourceDiagnostics>;
50
52
  //#endregion
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.mts","names":[],"sources":["../src/psl-column-resolution.ts","../src/interpreter.ts"],"mappings":";;;;;;;;;;;KA0CY,gBAAA;EAAA,SACD,OAAA;EAAA,SACA,UAAA;EAAA,SACA,OAAA;EAAA,SACA,UAAA,GAAa,MAAM;AAAA;;;UCoDb,sCAAA;EAAA,SACN,QAAA,EAAU,sBAAA;EAAA,SACV,MAAA,EAAQ,aAAA;EAAA,SACR,qBAAA,EAAuB,WAAA,SAAoB,gBAAA;EAAA,SAC3C,sBAAA;EAAA,SACA,yBAAA,YAAqC,gBAAA;EAAA,SACrC,uBAAA,GAA0B,yBAAA;EAAA,SAC1B,sBAAA,GAAyB,sBAAA;ED3DzB;;;AAAmB;;;;ACoD9B;;EDpDW,SCqEA,0BAAA,EAA4B,WAAA,SAAoB,QAAA;EAhBtC;;;;;;;;;EAAA,SA0BV,eAAA,IAAmB,KAAA,EAAO,uBAAA,KAA4B,SAAA;AAAA;AAAA,iBA8sDjD,iCAAA,CACd,KAAA,EAAO,sCAAA,GACN,MAAA,CAAO,QAAA,EAAU,yBAAA"}
1
+ {"version":3,"file":"index.d.mts","names":[],"sources":["../src/psl-column-resolution.ts","../src/interpreter.ts"],"mappings":";;;;;;;;;;;;KA0CY,gBAAA;EAAA,SACD,OAAA;EAAA,SACA,UAAA;EAAA,SACA,OAAA;EAAA,SACA,UAAA,GAAa,MAAM;AAAA;;;UCwDb,sCAAA;EAAA,SACN,QAAA,EAAU,sBAAA;EAAA,SACV,MAAA,EAAQ,aAAA;EAAA,SACR,qBAAA,EAAuB,WAAA,SAAoB,gBAAA;EAAA,SAC3C,sBAAA;EAAA,SACA,yBAAA,YAAqC,gBAAA;EAAA,SACrC,uBAAA,GAA0B,yBAAA;EAAA,SAC1B,sBAAA,GAAyB,sBAAA;ED/DzB;;;AAAmB;;;;ACwD9B;;EDxDW,SCyEA,0BAAA,EAA4B,WAAA,SAAoB,QAAA;EAhBtC;;;;;;;;;EAAA,SA0BV,eAAA,IAAmB,KAAA,EAAO,uBAAA,KAA4B,SAAA;EAAA,SACtD,WAAA,GAAc,WAAA;AAAA;AAAA,iBAywDT,iCAAA,CACd,KAAA,EAAO,sCAAA,GACN,MAAA,CAAO,QAAA,EAAU,yBAAA"}
package/dist/index.mjs CHANGED
@@ -1,2 +1,2 @@
1
- import { t as interpretPslDocumentToSqlContract } from "./interpreter-B_KtZusL.mjs";
1
+ import { t as interpretPslDocumentToSqlContract } from "./interpreter-CQdQo427.mjs";
2
2
  export { interpretPslDocumentToSqlContract };
@@ -1,5 +1,6 @@
1
1
  import { crossRef } from "@prisma-next/contract/types";
2
2
  import { hasRegisteredFieldNamespace, instantiateAuthoringEntityType, instantiateAuthoringFieldPreset, instantiateAuthoringTypeConstructor, isAuthoringEntityTypeDescriptor, isAuthoringFieldPresetDescriptor, isAuthoringTypeConstructorDescriptor, validateAuthoringHelperArguments } from "@prisma-next/framework-components/authoring";
3
+ import { namespacePslExtensionBlocks } from "@prisma-next/framework-components/psl-ast";
3
4
  import { isPostgresEnumStorageEntry } from "@prisma-next/sql-contract/types";
4
5
  import { buildSqlContractFromDefinition } from "@prisma-next/sql-contract-ts/contract-builder";
5
6
  import { blindCast } from "@prisma-next/utils/casts";
@@ -1308,6 +1309,37 @@ function resolveColumnDescriptor(field, enumTypeDescriptors, namedTypeDescriptor
1308
1309
  }
1309
1310
  //#endregion
1310
1311
  //#region src/psl-field-resolution.ts
1312
+ function lowerEnum2DefaultForField(input) {
1313
+ const expressionEntry = getPositionalArgumentEntry(input.defaultAttribute);
1314
+ if (!expressionEntry) return {};
1315
+ const raw = expressionEntry.value.trim();
1316
+ const isQuotedString = /^(['"]).*\1$/.test(raw);
1317
+ const isFunctionCall = raw.includes("(") && raw.endsWith(")");
1318
+ if (isQuotedString || isFunctionCall) {
1319
+ input.diagnostics.push({
1320
+ code: "PSL_ENUM2_DEFAULT_MUST_BE_MEMBER_NAME",
1321
+ message: `Field "${input.modelName}.${input.fieldName}" @default on an enum2 field must name a member (e.g. @default(Low)), not a raw value or function.`,
1322
+ sourceId: input.sourceId,
1323
+ span: input.defaultAttribute.span
1324
+ });
1325
+ return {};
1326
+ }
1327
+ const match = input.enumHandle.enumMembers.find((m) => m.name === raw);
1328
+ if (!match) {
1329
+ const validNames = input.enumHandle.enumMembers.map((m) => m.name).join(", ");
1330
+ input.diagnostics.push({
1331
+ code: "PSL_ENUM2_UNKNOWN_DEFAULT_MEMBER",
1332
+ message: `Field "${input.modelName}.${input.fieldName}" @default(${raw}) does not name a member of ${input.enumHandle.enumName}. Valid members: ${validNames}.`,
1333
+ sourceId: input.sourceId,
1334
+ span: input.defaultAttribute.span
1335
+ });
1336
+ return {};
1337
+ }
1338
+ return { defaultValue: {
1339
+ kind: "literal",
1340
+ value: blindCast(match.value)
1341
+ } };
1342
+ }
1311
1343
  const MODEL_COORDINATE_SEPARATOR = "\0";
1312
1344
  function modelCoordinateKey(namespaceId, modelName) {
1313
1345
  return `${namespaceId}${MODEL_COORDINATE_SEPARATOR}${modelName}`;
@@ -1381,7 +1413,7 @@ function extractFieldConstraintNames(input) {
1381
1413
  };
1382
1414
  }
1383
1415
  function collectResolvedFields(input) {
1384
- const { model, mapping, enumTypeDescriptors, namedTypeDescriptors, modelNames, compositeTypeNames, composedExtensions, authoringContributions, familyId, targetId, defaultFunctionRegistry, generatorDescriptorById, diagnostics, sourceId, scalarTypeDescriptors } = input;
1416
+ const { model, mapping, enumTypeDescriptors, namedTypeDescriptors, modelNames, compositeTypeNames, composedExtensions, authoringContributions, familyId, targetId, defaultFunctionRegistry, generatorDescriptorById, diagnostics, sourceId, scalarTypeDescriptors, enum2Handles } = input;
1385
1417
  const resolvedFields = [];
1386
1418
  for (const field of model.fields) {
1387
1419
  const isModelField = modelNames.has(field.typeName);
@@ -1474,7 +1506,15 @@ function collectResolvedFields(input) {
1474
1506
  });
1475
1507
  continue;
1476
1508
  }
1477
- const loweredDefault = defaultAttribute ? lowerDefaultForField({
1509
+ const enum2Handle = enum2Handles?.get(field.typeName);
1510
+ const loweredDefault = defaultAttribute ? enum2Handle ? lowerEnum2DefaultForField({
1511
+ modelName: model.name,
1512
+ fieldName: field.name,
1513
+ defaultAttribute,
1514
+ enumHandle: enum2Handle,
1515
+ sourceId,
1516
+ diagnostics
1517
+ }) : lowerDefaultForField({
1478
1518
  modelName: model.name,
1479
1519
  fieldName: field.name,
1480
1520
  defaultAttribute,
@@ -1998,6 +2038,41 @@ function processEnumDeclarations(input) {
1998
2038
  enumTypeDescriptors
1999
2039
  };
2000
2040
  }
2041
+ function processEnum2Declarations(input) {
2042
+ const enumHandles = {};
2043
+ const enumTypeDescriptors = /* @__PURE__ */ new Map();
2044
+ if (input.enum2Blocks.length === 0) return {
2045
+ enumHandles,
2046
+ enumTypeDescriptors
2047
+ };
2048
+ const enum2EntityDescriptor = getAuthoringEntity(input.authoringContributions, ["enum2"]);
2049
+ if (!enum2EntityDescriptor) {
2050
+ for (const decl of input.enum2Blocks) input.diagnostics.push({
2051
+ code: "PSL_ENUM2_MISSING_FACTORY",
2052
+ message: `enum2 "${decl.name}" requires an "enum2" entityType factory in the active authoring contributions`,
2053
+ sourceId: input.sourceId,
2054
+ span: decl.span
2055
+ });
2056
+ return {
2057
+ enumHandles,
2058
+ enumTypeDescriptors
2059
+ };
2060
+ }
2061
+ for (const decl of input.enum2Blocks) {
2062
+ const handle = instantiateAuthoringEntityType("enum2", enum2EntityDescriptor, [decl], input.entityContext);
2063
+ if (handle === void 0 || handle === null) continue;
2064
+ const enumHandle = blindCast(handle);
2065
+ enumHandles[decl.name] = enumHandle;
2066
+ enumTypeDescriptors.set(decl.name, {
2067
+ codecId: enumHandle.codecId,
2068
+ nativeType: enumHandle.nativeType
2069
+ });
2070
+ }
2071
+ return {
2072
+ enumHandles,
2073
+ enumTypeDescriptors
2074
+ };
2075
+ }
2001
2076
  function validateNamedTypeAttributes(input) {
2002
2077
  const [dbNativeTypeAttribute, ...extraDbNativeTypeAttributes] = input.allowDbNativeType ? input.declaration.attributes.filter((attribute) => attribute.name.startsWith("db.")) : [];
2003
2078
  let hasUnsupportedNamedTypeAttribute = false;
@@ -2169,7 +2244,8 @@ function buildModelNodeFromPsl(input) {
2169
2244
  generatorDescriptorById: input.generatorDescriptorById,
2170
2245
  diagnostics,
2171
2246
  sourceId,
2172
- scalarTypeDescriptors: input.scalarTypeDescriptors
2247
+ scalarTypeDescriptors: input.scalarTypeDescriptors,
2248
+ ...ifDefined("enum2Handles", input.enum2Handles)
2173
2249
  });
2174
2250
  const inlineIdFields = resolvedFields.filter((field) => field.isId);
2175
2251
  if (inlineIdFields.length > 1) diagnostics.push({
@@ -2710,14 +2786,18 @@ function buildModelNodeFromPsl(input) {
2710
2786
  modelNode: {
2711
2787
  modelName: model.name,
2712
2788
  tableName,
2713
- fields: resolvedFields.map((resolvedField) => ({
2714
- fieldName: resolvedField.field.name,
2715
- columnName: resolvedField.columnName,
2716
- descriptor: resolvedField.descriptor,
2717
- nullable: resolvedField.field.optional,
2718
- ...ifDefined("default", resolvedField.defaultValue),
2719
- ...ifDefined("executionDefaults", resolvedField.executionDefaults)
2720
- })),
2789
+ fields: resolvedFields.map((resolvedField) => {
2790
+ const enumHandle = input.enum2Handles?.get(resolvedField.field.typeName);
2791
+ return {
2792
+ fieldName: resolvedField.field.name,
2793
+ columnName: resolvedField.columnName,
2794
+ descriptor: resolvedField.descriptor,
2795
+ nullable: resolvedField.field.optional,
2796
+ ...ifDefined("default", resolvedField.defaultValue),
2797
+ ...ifDefined("executionDefaults", resolvedField.executionDefaults),
2798
+ ...ifDefined("enumTypeHandle", enumHandle)
2799
+ };
2800
+ }),
2721
2801
  ...ifDefined("id", primaryKey),
2722
2802
  ...uniqueConstraints.length > 0 ? { uniques: uniqueConstraints } : {},
2723
2803
  ...indexNodes.length > 0 ? { indexes: indexNodes } : {},
@@ -3237,6 +3317,47 @@ function interpretPslDocumentToSqlContract(input) {
3237
3317
  for (const [name, entry] of Object.entries(nsEnumResult.storageTypes)) if (isPostgresEnumStorageEntry(entry)) nsEntries[name] = entry;
3238
3318
  if (Object.keys(nsEntries).length > 0) namespaceEnumStorageTypes[nsId] = nsEntries;
3239
3319
  }
3320
+ const topLevelEnum2s = input.document.ast.namespaces.filter((ns) => ns.name === UNSPECIFIED_PSL_NAMESPACE_NAME).flatMap((ns) => namespacePslExtensionBlocks(ns).filter((b) => b.kind === "enum2"));
3321
+ for (const ns of input.document.ast.namespaces) {
3322
+ if (ns.name === UNSPECIFIED_PSL_NAMESPACE_NAME) continue;
3323
+ const nsEnum2s = namespacePslExtensionBlocks(ns).filter((b) => b.kind === "enum2");
3324
+ if (nsEnum2s.length === 0) continue;
3325
+ for (const decl of nsEnum2s) diagnostics.push({
3326
+ code: "PSL_ENUM2_NAMESPACE_NOT_SUPPORTED",
3327
+ message: `enum2 "${decl.name}" inside namespace "${ns.name}" is not supported; declare enum2 at the top level`,
3328
+ sourceId,
3329
+ span: decl.span
3330
+ });
3331
+ }
3332
+ const enum2Result = processEnum2Declarations({
3333
+ enum2Blocks: topLevelEnum2s,
3334
+ sourceId,
3335
+ authoringContributions: input.authoringContributions,
3336
+ entityContext: {
3337
+ family: input.target.familyId,
3338
+ target: input.target.targetId,
3339
+ ...ifDefined("codecLookup", input.codecLookup),
3340
+ sourceId,
3341
+ diagnostics: { push: (d) => {
3342
+ diagnostics.push(blindCast(d));
3343
+ } }
3344
+ },
3345
+ diagnostics
3346
+ });
3347
+ const collidingEnum2Names = /* @__PURE__ */ new Set();
3348
+ for (const [name, descriptor] of enum2Result.enumTypeDescriptors) if (allEnumTypeDescriptors.has(name)) {
3349
+ collidingEnum2Names.add(name);
3350
+ const collision = topLevelEnum2s.find((e) => e.name === name);
3351
+ diagnostics.push({
3352
+ code: "PSL_ENUM2_DUPLICATE_TYPE_NAME",
3353
+ message: `enum2 "${name}" collides with an existing type name; each type name must be unique`,
3354
+ sourceId,
3355
+ ...ifDefined("span", collision?.span)
3356
+ });
3357
+ } else allEnumTypeDescriptors.set(name, descriptor);
3358
+ const validEnum2Handles = {};
3359
+ for (const [name, handle] of Object.entries(enum2Result.enumHandles)) if (!collidingEnum2Names.has(name)) validEnum2Handles[name] = handle;
3360
+ const enum2HandlesByName = new Map(Object.entries(validEnum2Handles));
3240
3361
  const namedTypeResult = resolveNamedTypeDeclarations({
3241
3362
  declarations: input.document.ast.types?.declarations ?? [],
3242
3363
  sourceId,
@@ -3283,7 +3404,8 @@ function interpretPslDocumentToSqlContract(input) {
3283
3404
  scalarTypeDescriptors: input.scalarTypeDescriptors,
3284
3405
  sourceId,
3285
3406
  diagnostics,
3286
- modelNamespaceIds
3407
+ modelNamespaceIds,
3408
+ ...enum2HandlesByName.size > 0 ? { enum2Handles: enum2HandlesByName } : {}
3287
3409
  });
3288
3410
  modelNodes.push(namespaceId !== void 0 ? {
3289
3411
  ...result.modelNode,
@@ -3336,6 +3458,7 @@ function interpretPslDocumentToSqlContract(input) {
3336
3458
  ...ifDefined("extensionPacks", buildComposedExtensionPackRefs(input.target, [...composedExtensions].sort(compareStrings), input.composedExtensionPackRefs)),
3337
3459
  ...Object.keys(storageTypes).length > 0 ? { storageTypes } : {},
3338
3460
  ...Object.keys(namespaceEnumStorageTypes).length > 0 ? { namespaceTypes: namespaceEnumStorageTypes } : {},
3461
+ ...Object.keys(validEnum2Handles).length > 0 ? { enums: validEnum2Handles } : {},
3339
3462
  ...ifDefined("createNamespace", input.createNamespace),
3340
3463
  models: stiColumnModelNodes.map((model) => ({
3341
3464
  ...model,
@@ -3362,6 +3485,7 @@ function interpretPslDocumentToSqlContract(input) {
3362
3485
  roots: filteredRoots,
3363
3486
  domain: { namespaces: Object.fromEntries(Object.entries(contract.domain.namespaces).map(([namespaceId, namespaceSlice]) => [namespaceId, {
3364
3487
  models: Object.fromEntries(Object.entries(namespaceSlice.models).map(([modelName, model]) => [modelName, patchedModels[modelCoordinateKey(namespaceId, modelName)] ?? model])),
3488
+ ...namespaceSlice.enum !== void 0 ? { enum: namespaceSlice.enum } : {},
3365
3489
  ...namespaceSlice.valueObjects !== void 0 ? { valueObjects: namespaceSlice.valueObjects } : {},
3366
3490
  ...namespaceId === input.target.defaultNamespaceId && Object.keys(valueObjects).length > 0 ? { valueObjects } : {}
3367
3491
  }])) }
@@ -3370,4 +3494,4 @@ function interpretPslDocumentToSqlContract(input) {
3370
3494
  //#endregion
3371
3495
  export { interpretPslDocumentToSqlContract as t };
3372
3496
 
3373
- //# sourceMappingURL=interpreter-B_KtZusL.mjs.map
3497
+ //# sourceMappingURL=interpreter-CQdQo427.mjs.map