@prisma-next/sql-contract-psl 0.13.0-dev.2 → 0.13.0-dev.21

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-BT6dEGeD.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";
@@ -1071,7 +1072,7 @@ const NATIVE_TYPE_SPECS = {
1071
1072
  "db.Uuid": {
1072
1073
  args: "noArgs",
1073
1074
  baseType: "String",
1074
- codecId: null,
1075
+ codecId: "pg/uuid@1",
1075
1076
  nativeType: "uuid"
1076
1077
  },
1077
1078
  "db.SmallInt": {
@@ -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,
@@ -1495,7 +1535,8 @@ function collectResolvedFields(input) {
1495
1535
  });
1496
1536
  continue;
1497
1537
  }
1498
- if (loweredOnCreate) {
1538
+ const fieldUsesNamedType = field.typeRef !== void 0 || namedTypeDescriptors.has(field.typeName);
1539
+ if (loweredOnCreate && !fieldUsesNamedType) {
1499
1540
  const generatedDescriptor = generatorDescriptorById.get(loweredOnCreate.id)?.resolveGeneratedColumnDescriptor?.({ generated: loweredOnCreate });
1500
1541
  if (generatedDescriptor) descriptor = generatedDescriptor;
1501
1542
  }
@@ -1998,6 +2039,41 @@ function processEnumDeclarations(input) {
1998
2039
  enumTypeDescriptors
1999
2040
  };
2000
2041
  }
2042
+ function processEnum2Declarations(input) {
2043
+ const enumHandles = {};
2044
+ const enumTypeDescriptors = /* @__PURE__ */ new Map();
2045
+ if (input.enum2Blocks.length === 0) return {
2046
+ enumHandles,
2047
+ enumTypeDescriptors
2048
+ };
2049
+ const enum2EntityDescriptor = getAuthoringEntity(input.authoringContributions, ["enum2"]);
2050
+ if (!enum2EntityDescriptor) {
2051
+ for (const decl of input.enum2Blocks) input.diagnostics.push({
2052
+ code: "PSL_ENUM2_MISSING_FACTORY",
2053
+ message: `enum2 "${decl.name}" requires an "enum2" entityType factory in the active authoring contributions`,
2054
+ sourceId: input.sourceId,
2055
+ span: decl.span
2056
+ });
2057
+ return {
2058
+ enumHandles,
2059
+ enumTypeDescriptors
2060
+ };
2061
+ }
2062
+ for (const decl of input.enum2Blocks) {
2063
+ const handle = instantiateAuthoringEntityType("enum2", enum2EntityDescriptor, [decl], input.entityContext);
2064
+ if (handle === void 0 || handle === null) continue;
2065
+ const enumHandle = blindCast(handle);
2066
+ enumHandles[decl.name] = enumHandle;
2067
+ enumTypeDescriptors.set(decl.name, {
2068
+ codecId: enumHandle.codecId,
2069
+ nativeType: enumHandle.nativeType
2070
+ });
2071
+ }
2072
+ return {
2073
+ enumHandles,
2074
+ enumTypeDescriptors
2075
+ };
2076
+ }
2001
2077
  function validateNamedTypeAttributes(input) {
2002
2078
  const [dbNativeTypeAttribute, ...extraDbNativeTypeAttributes] = input.allowDbNativeType ? input.declaration.attributes.filter((attribute) => attribute.name.startsWith("db.")) : [];
2003
2079
  let hasUnsupportedNamedTypeAttribute = false;
@@ -2169,7 +2245,8 @@ function buildModelNodeFromPsl(input) {
2169
2245
  generatorDescriptorById: input.generatorDescriptorById,
2170
2246
  diagnostics,
2171
2247
  sourceId,
2172
- scalarTypeDescriptors: input.scalarTypeDescriptors
2248
+ scalarTypeDescriptors: input.scalarTypeDescriptors,
2249
+ ...ifDefined("enum2Handles", input.enum2Handles)
2173
2250
  });
2174
2251
  const inlineIdFields = resolvedFields.filter((field) => field.isId);
2175
2252
  if (inlineIdFields.length > 1) diagnostics.push({
@@ -2710,14 +2787,18 @@ function buildModelNodeFromPsl(input) {
2710
2787
  modelNode: {
2711
2788
  modelName: model.name,
2712
2789
  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
- })),
2790
+ fields: resolvedFields.map((resolvedField) => {
2791
+ const enumHandle = input.enum2Handles?.get(resolvedField.field.typeName);
2792
+ return {
2793
+ fieldName: resolvedField.field.name,
2794
+ columnName: resolvedField.columnName,
2795
+ descriptor: resolvedField.descriptor,
2796
+ nullable: resolvedField.field.optional,
2797
+ ...ifDefined("default", resolvedField.defaultValue),
2798
+ ...ifDefined("executionDefaults", resolvedField.executionDefaults),
2799
+ ...ifDefined("enumTypeHandle", enumHandle)
2800
+ };
2801
+ }),
2721
2802
  ...ifDefined("id", primaryKey),
2722
2803
  ...uniqueConstraints.length > 0 ? { uniques: uniqueConstraints } : {},
2723
2804
  ...indexNodes.length > 0 ? { indexes: indexNodes } : {},
@@ -3237,6 +3318,47 @@ function interpretPslDocumentToSqlContract(input) {
3237
3318
  for (const [name, entry] of Object.entries(nsEnumResult.storageTypes)) if (isPostgresEnumStorageEntry(entry)) nsEntries[name] = entry;
3238
3319
  if (Object.keys(nsEntries).length > 0) namespaceEnumStorageTypes[nsId] = nsEntries;
3239
3320
  }
3321
+ const topLevelEnum2s = input.document.ast.namespaces.filter((ns) => ns.name === UNSPECIFIED_PSL_NAMESPACE_NAME).flatMap((ns) => namespacePslExtensionBlocks(ns).filter((b) => b.kind === "enum2"));
3322
+ for (const ns of input.document.ast.namespaces) {
3323
+ if (ns.name === UNSPECIFIED_PSL_NAMESPACE_NAME) continue;
3324
+ const nsEnum2s = namespacePslExtensionBlocks(ns).filter((b) => b.kind === "enum2");
3325
+ if (nsEnum2s.length === 0) continue;
3326
+ for (const decl of nsEnum2s) diagnostics.push({
3327
+ code: "PSL_ENUM2_NAMESPACE_NOT_SUPPORTED",
3328
+ message: `enum2 "${decl.name}" inside namespace "${ns.name}" is not supported; declare enum2 at the top level`,
3329
+ sourceId,
3330
+ span: decl.span
3331
+ });
3332
+ }
3333
+ const enum2Result = processEnum2Declarations({
3334
+ enum2Blocks: topLevelEnum2s,
3335
+ sourceId,
3336
+ authoringContributions: input.authoringContributions,
3337
+ entityContext: {
3338
+ family: input.target.familyId,
3339
+ target: input.target.targetId,
3340
+ ...ifDefined("codecLookup", input.codecLookup),
3341
+ sourceId,
3342
+ diagnostics: { push: (d) => {
3343
+ diagnostics.push(blindCast(d));
3344
+ } }
3345
+ },
3346
+ diagnostics
3347
+ });
3348
+ const collidingEnum2Names = /* @__PURE__ */ new Set();
3349
+ for (const [name, descriptor] of enum2Result.enumTypeDescriptors) if (allEnumTypeDescriptors.has(name)) {
3350
+ collidingEnum2Names.add(name);
3351
+ const collision = topLevelEnum2s.find((e) => e.name === name);
3352
+ diagnostics.push({
3353
+ code: "PSL_ENUM2_DUPLICATE_TYPE_NAME",
3354
+ message: `enum2 "${name}" collides with an existing type name; each type name must be unique`,
3355
+ sourceId,
3356
+ ...ifDefined("span", collision?.span)
3357
+ });
3358
+ } else allEnumTypeDescriptors.set(name, descriptor);
3359
+ const validEnum2Handles = {};
3360
+ for (const [name, handle] of Object.entries(enum2Result.enumHandles)) if (!collidingEnum2Names.has(name)) validEnum2Handles[name] = handle;
3361
+ const enum2HandlesByName = new Map(Object.entries(validEnum2Handles));
3240
3362
  const namedTypeResult = resolveNamedTypeDeclarations({
3241
3363
  declarations: input.document.ast.types?.declarations ?? [],
3242
3364
  sourceId,
@@ -3283,7 +3405,8 @@ function interpretPslDocumentToSqlContract(input) {
3283
3405
  scalarTypeDescriptors: input.scalarTypeDescriptors,
3284
3406
  sourceId,
3285
3407
  diagnostics,
3286
- modelNamespaceIds
3408
+ modelNamespaceIds,
3409
+ ...enum2HandlesByName.size > 0 ? { enum2Handles: enum2HandlesByName } : {}
3287
3410
  });
3288
3411
  modelNodes.push(namespaceId !== void 0 ? {
3289
3412
  ...result.modelNode,
@@ -3336,6 +3459,7 @@ function interpretPslDocumentToSqlContract(input) {
3336
3459
  ...ifDefined("extensionPacks", buildComposedExtensionPackRefs(input.target, [...composedExtensions].sort(compareStrings), input.composedExtensionPackRefs)),
3337
3460
  ...Object.keys(storageTypes).length > 0 ? { storageTypes } : {},
3338
3461
  ...Object.keys(namespaceEnumStorageTypes).length > 0 ? { namespaceTypes: namespaceEnumStorageTypes } : {},
3462
+ ...Object.keys(validEnum2Handles).length > 0 ? { enums: validEnum2Handles } : {},
3339
3463
  ...ifDefined("createNamespace", input.createNamespace),
3340
3464
  models: stiColumnModelNodes.map((model) => ({
3341
3465
  ...model,
@@ -3362,6 +3486,7 @@ function interpretPslDocumentToSqlContract(input) {
3362
3486
  roots: filteredRoots,
3363
3487
  domain: { namespaces: Object.fromEntries(Object.entries(contract.domain.namespaces).map(([namespaceId, namespaceSlice]) => [namespaceId, {
3364
3488
  models: Object.fromEntries(Object.entries(namespaceSlice.models).map(([modelName, model]) => [modelName, patchedModels[modelCoordinateKey(namespaceId, modelName)] ?? model])),
3489
+ ...namespaceSlice.enum !== void 0 ? { enum: namespaceSlice.enum } : {},
3365
3490
  ...namespaceSlice.valueObjects !== void 0 ? { valueObjects: namespaceSlice.valueObjects } : {},
3366
3491
  ...namespaceId === input.target.defaultNamespaceId && Object.keys(valueObjects).length > 0 ? { valueObjects } : {}
3367
3492
  }])) }
@@ -3370,4 +3495,4 @@ function interpretPslDocumentToSqlContract(input) {
3370
3495
  //#endregion
3371
3496
  export { interpretPslDocumentToSqlContract as t };
3372
3497
 
3373
- //# sourceMappingURL=interpreter-B_KtZusL.mjs.map
3498
+ //# sourceMappingURL=interpreter-BT6dEGeD.mjs.map