@prisma-next/sql-contract-psl 0.12.0-dev.78 → 0.12.0-dev.79

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1 +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;;;UCkDb,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;EDzDzB;;;AAAmB;;;;ACkD9B;;EDlDW,SCmEA,0BAAA,EAA4B,WAAA,SAAoB,QAAA;EAhBtC;;;;;;;;;EAAA,SA0BV,eAAA,IAAmB,KAAA,EAAO,uBAAA,KAA4B,SAAA;AAAA;AAAA,iBAurDjD,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;;;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"}
package/dist/index.mjs CHANGED
@@ -1,2 +1,2 @@
1
- import { t as interpretPslDocumentToSqlContract } from "./interpreter-B5_yovSP.mjs";
1
+ import { t as interpretPslDocumentToSqlContract } from "./interpreter-B_KtZusL.mjs";
2
2
  export { interpretPslDocumentToSqlContract };
@@ -1308,6 +1308,10 @@ function resolveColumnDescriptor(field, enumTypeDescriptors, namedTypeDescriptor
1308
1308
  }
1309
1309
  //#endregion
1310
1310
  //#region src/psl-field-resolution.ts
1311
+ const MODEL_COORDINATE_SEPARATOR = "\0";
1312
+ function modelCoordinateKey(namespaceId, modelName) {
1313
+ return `${namespaceId}${MODEL_COORDINATE_SEPARATOR}${modelName}`;
1314
+ }
1311
1315
  const BUILTIN_FIELD_ATTRIBUTE_NAMES = new Set([
1312
1316
  "id",
1313
1317
  "unique",
@@ -1540,9 +1544,9 @@ function collectResolvedFields(input) {
1540
1544
  }
1541
1545
  return resolvedFields;
1542
1546
  }
1543
- function buildModelMappings(models, diagnostics, sourceId) {
1547
+ function buildModelMappings(modelEntries, defaultNamespaceId, diagnostics, sourceId) {
1544
1548
  const result = /* @__PURE__ */ new Map();
1545
- for (const model of models) {
1549
+ for (const { model, namespaceId } of modelEntries) {
1546
1550
  const tableName = parseMapName({
1547
1551
  attribute: getAttribute(model.attributes, "map"),
1548
1552
  defaultValue: lowerFirst(model.name),
@@ -1563,7 +1567,7 @@ function buildModelMappings(models, diagnostics, sourceId) {
1563
1567
  });
1564
1568
  fieldColumns.set(field.name, columnName);
1565
1569
  }
1566
- result.set(model.name, {
1570
+ result.set(modelCoordinateKey(namespaceId ?? defaultNamespaceId, model.name), {
1567
1571
  model,
1568
1572
  tableName,
1569
1573
  fieldColumns
@@ -1717,6 +1721,7 @@ function indexFkRelations(input) {
1717
1721
  fieldName: relation.declaringFieldName,
1718
1722
  toModel: relation.targetModelName,
1719
1723
  toTable: relation.targetTableName,
1724
+ ...ifDefined("toNamespaceId", relation.targetNamespaceId),
1720
1725
  cardinality: "N:1",
1721
1726
  on: {
1722
1727
  parentTable: relation.declaringTableName,
@@ -2592,16 +2597,15 @@ function buildModelNodeFromPsl(input) {
2592
2597
  });
2593
2598
  continue;
2594
2599
  }
2595
- if (fieldTypeNamespaceId !== void 0) {
2596
- if (input.modelNamespaceIds.get(fieldTypeName) !== (fieldTypeNamespaceId === "unbound" ? "__unbound__" : fieldTypeNamespaceId)) {
2597
- diagnostics.push({
2598
- code: "PSL_INVALID_RELATION_TARGET",
2599
- message: `Relation field "${model.name}.${relationAttribute.field.name}" references unknown model "${qualifiedTypeName}"`,
2600
- sourceId,
2601
- span: relationAttribute.field.span
2602
- });
2603
- continue;
2604
- }
2600
+ const normalizedQualifier = fieldTypeNamespaceId === void 0 ? void 0 : fieldTypeNamespaceId === "unbound" ? "__unbound__" : fieldTypeNamespaceId;
2601
+ if (normalizedQualifier !== void 0 && !input.modelMappingsByCoordinate.has(modelCoordinateKey(normalizedQualifier, fieldTypeName))) {
2602
+ diagnostics.push({
2603
+ code: "PSL_INVALID_RELATION_TARGET",
2604
+ message: `Relation field "${model.name}.${relationAttribute.field.name}" references unknown model "${qualifiedTypeName}"`,
2605
+ sourceId,
2606
+ span: relationAttribute.field.span
2607
+ });
2608
+ continue;
2605
2609
  }
2606
2610
  const parsedRelation = parseRelationAttribute({
2607
2611
  attribute: relationAttribute.relation,
@@ -2620,7 +2624,7 @@ function buildModelNodeFromPsl(input) {
2620
2624
  });
2621
2625
  continue;
2622
2626
  }
2623
- const targetMapping = input.modelMappings.get(fieldTypeName);
2627
+ const targetMapping = normalizedQualifier !== void 0 ? input.modelMappingsByCoordinate.get(modelCoordinateKey(normalizedQualifier, fieldTypeName)) : input.modelMappings.get(fieldTypeName);
2624
2628
  if (!targetMapping) {
2625
2629
  diagnostics.push({
2626
2630
  code: "PSL_INVALID_RELATION_TARGET",
@@ -2677,7 +2681,7 @@ function buildModelNodeFromPsl(input) {
2677
2681
  span: relationAttribute.field.span,
2678
2682
  diagnostics
2679
2683
  }) : void 0;
2680
- const targetNamespaceId = input.modelNamespaceIds.get(targetMapping.model.name);
2684
+ const targetNamespaceId = normalizedQualifier !== void 0 ? normalizedQualifier : input.modelNamespaceIds.get(targetMapping.model.name);
2681
2685
  foreignKeyNodes.push({
2682
2686
  columns: localColumns,
2683
2687
  references: {
@@ -2696,6 +2700,7 @@ function buildModelNodeFromPsl(input) {
2696
2700
  declaringTableName: tableName,
2697
2701
  targetModelName: targetMapping.model.name,
2698
2702
  targetTableName: targetMapping.tableName,
2703
+ ...ifDefined("targetNamespaceId", targetNamespaceId),
2699
2704
  ...ifDefined("relationName", parsedRelation.relationName),
2700
2705
  localColumns,
2701
2706
  referencedColumns
@@ -2888,12 +2893,14 @@ function collectPolymorphismDeclarations(models, sourceId, diagnostics) {
2888
2893
  }
2889
2894
  function resolvePolymorphism(models, discriminatorDeclarations, baseDeclarations, modelNames, modelMappings, modelNamespaceIds, defaultNamespaceId, syntheticPkFieldsByVariant, stiBaseFieldsByBase, sourceId, diagnostics) {
2890
2895
  let patched = models;
2896
+ const coordinateFor = (modelName) => modelCoordinateKey(modelNamespaceIds.get(modelName) ?? defaultNamespaceId, modelName);
2891
2897
  for (const [baseName, fieldNames] of stiBaseFieldsByBase) {
2892
- const baseModel = patched[baseName];
2898
+ const baseKey = coordinateFor(baseName);
2899
+ const baseModel = patched[baseKey];
2893
2900
  if (!baseModel || fieldNames.length === 0) continue;
2894
2901
  patched = {
2895
2902
  ...patched,
2896
- [baseName]: stripStorageOnlyDomainFields(baseModel, fieldNames)
2903
+ [baseKey]: stripStorageOnlyDomainFields(baseModel, fieldNames)
2897
2904
  };
2898
2905
  }
2899
2906
  for (const [modelName, decl] of discriminatorDeclarations) {
@@ -2906,7 +2913,7 @@ function resolvePolymorphism(models, discriminatorDeclarations, baseDeclarations
2906
2913
  });
2907
2914
  continue;
2908
2915
  }
2909
- const model = patched[modelName];
2916
+ const model = patched[coordinateFor(modelName)];
2910
2917
  if (!model) continue;
2911
2918
  if (!Object.hasOwn(model.fields, decl.fieldName)) {
2912
2919
  diagnostics.push({
@@ -2945,7 +2952,7 @@ function resolvePolymorphism(models, discriminatorDeclarations, baseDeclarations
2945
2952
  }
2946
2953
  patched = {
2947
2954
  ...patched,
2948
- [modelName]: {
2955
+ [coordinateFor(modelName)]: {
2949
2956
  ...model,
2950
2957
  discriminator: { field: decl.fieldName },
2951
2958
  variants
@@ -2972,7 +2979,7 @@ function resolvePolymorphism(models, discriminatorDeclarations, baseDeclarations
2972
2979
  continue;
2973
2980
  }
2974
2981
  if (discriminatorDeclarations.has(variantName)) continue;
2975
- const variantModel = patched[variantName];
2982
+ const variantModel = patched[coordinateFor(variantName)];
2976
2983
  if (!variantModel) continue;
2977
2984
  const baseMapping = modelMappings.get(baseDecl.baseName);
2978
2985
  const variantMapping = modelMappings.get(variantName);
@@ -2987,7 +2994,7 @@ function resolvePolymorphism(models, discriminatorDeclarations, baseDeclarations
2987
2994
  };
2988
2995
  patched = {
2989
2996
  ...patched,
2990
- [variantName]: stripStorageOnlyDomainFields(patchedVariant, syntheticPkFieldsByVariant.get(variantName) ?? [])
2997
+ [coordinateFor(variantName)]: stripStorageOnlyDomainFields(patchedVariant, syntheticPkFieldsByVariant.get(variantName) ?? [])
2991
2998
  };
2992
2999
  }
2993
3000
  return patched;
@@ -3165,6 +3172,7 @@ function interpretPslDocumentToSqlContract(input) {
3165
3172
  diagnostics
3166
3173
  });
3167
3174
  const models = [];
3175
+ const modelEntries = [];
3168
3176
  const modelNamespaceIds = /* @__PURE__ */ new Map();
3169
3177
  for (const namespace of input.document.ast.namespaces) {
3170
3178
  const resolvedNamespaceId = resolveNamespaceIdForSqlTarget({
@@ -3173,9 +3181,14 @@ function interpretPslDocumentToSqlContract(input) {
3173
3181
  });
3174
3182
  for (const model of namespace.models) {
3175
3183
  models.push(model);
3184
+ modelEntries.push({
3185
+ model,
3186
+ namespaceId: resolvedNamespaceId
3187
+ });
3176
3188
  if (resolvedNamespaceId !== void 0) modelNamespaceIds.set(model.name, resolvedNamespaceId);
3177
3189
  }
3178
3190
  }
3191
+ const defaultNamespaceId = input.target.defaultNamespaceId;
3179
3192
  const topLevelEnums = input.document.ast.namespaces.filter((ns) => ns.name === UNSPECIFIED_PSL_NAMESPACE_NAME).flatMap((ns) => ns.enums);
3180
3193
  const namedNamespaceEnumsByNsId = /* @__PURE__ */ new Map();
3181
3194
  for (const ns of input.document.ast.namespaces) {
@@ -3239,19 +3252,23 @@ function interpretPslDocumentToSqlContract(input) {
3239
3252
  ...enumResult.storageTypes,
3240
3253
  ...namedTypeResult.storageTypes
3241
3254
  };
3242
- const modelMappings = buildModelMappings(models, diagnostics, sourceId);
3255
+ const modelMappingsByCoordinate = buildModelMappings(modelEntries, defaultNamespaceId, diagnostics, sourceId);
3256
+ const modelMappings = /* @__PURE__ */ new Map();
3257
+ for (const mapping of modelMappingsByCoordinate.values()) modelMappings.set(mapping.model.name, mapping);
3243
3258
  const modelNodes = [];
3244
3259
  const fkRelationMetadata = [];
3245
3260
  const backrelationCandidates = [];
3246
3261
  const modelResolvedFields = /* @__PURE__ */ new Map();
3247
3262
  const crossSpaceRelationsByModel = /* @__PURE__ */ new Map();
3248
- for (const model of models) {
3249
- const mapping = modelMappings.get(model.name);
3263
+ for (const { model, namespaceId } of modelEntries) {
3264
+ const coordinate = modelCoordinateKey(namespaceId ?? defaultNamespaceId, model.name);
3265
+ const mapping = modelMappingsByCoordinate.get(coordinate);
3250
3266
  if (!mapping) continue;
3251
3267
  const result = buildModelNodeFromPsl({
3252
3268
  model,
3253
3269
  mapping,
3254
3270
  modelMappings,
3271
+ modelMappingsByCoordinate,
3255
3272
  modelNames,
3256
3273
  compositeTypeNames,
3257
3274
  enumTypeDescriptors: allEnumTypeDescriptors,
@@ -3268,14 +3285,13 @@ function interpretPslDocumentToSqlContract(input) {
3268
3285
  diagnostics,
3269
3286
  modelNamespaceIds
3270
3287
  });
3271
- const resolvedNamespaceId = modelNamespaceIds.get(model.name);
3272
- modelNodes.push(resolvedNamespaceId !== void 0 ? {
3288
+ modelNodes.push(namespaceId !== void 0 ? {
3273
3289
  ...result.modelNode,
3274
- namespaceId: resolvedNamespaceId
3290
+ namespaceId
3275
3291
  } : result.modelNode);
3276
3292
  fkRelationMetadata.push(...result.fkRelationMetadata);
3277
3293
  backrelationCandidates.push(...result.backrelationCandidates);
3278
- modelResolvedFields.set(model.name, result.resolvedFields);
3294
+ modelResolvedFields.set(coordinate, result.resolvedFields);
3279
3295
  if (result.crossSpaceRelations.length > 0) {
3280
3296
  const existing = crossSpaceRelationsByModel.get(model.name) ?? [];
3281
3297
  crossSpaceRelationsByModel.set(model.name, [...existing, ...result.crossSpaceRelations]);
@@ -3327,9 +3343,10 @@ function interpretPslDocumentToSqlContract(input) {
3327
3343
  }))
3328
3344
  });
3329
3345
  const modelsForPatch = {};
3330
- for (const namespaceSlice of Object.values(contract.domain.namespaces)) for (const [modelName, model] of Object.entries(namespaceSlice.models)) {
3331
- if (Object.hasOwn(modelsForPatch, modelName)) throw new Error(`duplicate model name "${modelName}" across domain namespaces during PSL interpretation`);
3332
- modelsForPatch[modelName] = model;
3346
+ for (const [namespaceId, namespaceSlice] of Object.entries(contract.domain.namespaces)) for (const [modelName, model] of Object.entries(namespaceSlice.models)) {
3347
+ const coordinate = modelCoordinateKey(namespaceId, modelName);
3348
+ if (Object.hasOwn(modelsForPatch, coordinate)) throw new Error(`duplicate model "${namespaceId}.${modelName}" during PSL interpretation`);
3349
+ modelsForPatch[coordinate] = model;
3333
3350
  }
3334
3351
  let patchedModels = patchModelDomainFields(modelsForPatch, modelResolvedFields);
3335
3352
  const polyDiagnostics = [];
@@ -3344,7 +3361,7 @@ function interpretPslDocumentToSqlContract(input) {
3344
3361
  ...contract,
3345
3362
  roots: filteredRoots,
3346
3363
  domain: { namespaces: Object.fromEntries(Object.entries(contract.domain.namespaces).map(([namespaceId, namespaceSlice]) => [namespaceId, {
3347
- models: Object.fromEntries(Object.entries(namespaceSlice.models).map(([modelName, model]) => [modelName, patchedModels[modelName] ?? model])),
3364
+ models: Object.fromEntries(Object.entries(namespaceSlice.models).map(([modelName, model]) => [modelName, patchedModels[modelCoordinateKey(namespaceId, modelName)] ?? model])),
3348
3365
  ...namespaceSlice.valueObjects !== void 0 ? { valueObjects: namespaceSlice.valueObjects } : {},
3349
3366
  ...namespaceId === input.target.defaultNamespaceId && Object.keys(valueObjects).length > 0 ? { valueObjects } : {}
3350
3367
  }])) }
@@ -3353,4 +3370,4 @@ function interpretPslDocumentToSqlContract(input) {
3353
3370
  //#endregion
3354
3371
  export { interpretPslDocumentToSqlContract as t };
3355
3372
 
3356
- //# sourceMappingURL=interpreter-B5_yovSP.mjs.map
3373
+ //# sourceMappingURL=interpreter-B_KtZusL.mjs.map