@prisma-next/mongo-contract-psl 0.9.0 → 0.10.0-dev.1

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,4 +1,4 @@
1
- import { t as interpretPslDocumentToMongoContract } from "../interpreter-DgpmmXva.mjs";
1
+ import { t as interpretPslDocumentToMongoContract } from "../interpreter-DzLp9JOt.mjs";
2
2
  import { notOk, ok } from "@prisma-next/utils/result";
3
3
  import { parsePslDocument } from "@prisma-next/psl-parser";
4
4
  import { readFile } from "node:fs/promises";
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.mts","names":[],"sources":["../src/interpreter.ts"],"mappings":";;;;;;;UAgCiB,wCAAA;EAAA,SACN,QAAA,EAAU,sBAAA;EAAA,SACV,qBAAA,EAAuB,WAAA;EAAA,SACvB,WAAA,GAAc,WAAA;AAAA;AAAA,iBAsvBT,mCAAA,CACd,KAAA,EAAO,wCAAA,GACN,MAAA,CAAO,QAAA,EAAU,yBAAA"}
1
+ {"version":3,"file":"index.d.mts","names":[],"sources":["../src/interpreter.ts"],"mappings":";;;;;;;UAuCiB,wCAAA;EAAA,SACN,QAAA,EAAU,sBAAA;EAAA,SACV,qBAAA,EAAuB,WAAA;EAAA,SACvB,WAAA,GAAc,WAAA;AAAA;AAAA,iBA0xBT,mCAAA,CACd,KAAA,EAAO,wCAAA,GACN,MAAA,CAAO,QAAA,EAAU,yBAAA"}
package/dist/index.mjs CHANGED
@@ -1,2 +1,2 @@
1
- import { t as interpretPslDocumentToMongoContract } from "./interpreter-DgpmmXva.mjs";
1
+ import { t as interpretPslDocumentToMongoContract } from "./interpreter-DzLp9JOt.mjs";
2
2
  export { interpretPslDocumentToMongoContract };
@@ -1,4 +1,5 @@
1
1
  import { computeProfileHash, computeStorageHash } from "@prisma-next/contract/hashing";
2
+ import { UNBOUND_NAMESPACE_ID } from "@prisma-next/framework-components/ir";
2
3
  import { MongoCollection, MongoIndex, MongoValidator, applyPolymorphicScopeToMongoIndex } from "@prisma-next/mongo-contract";
3
4
  import { notOk, ok } from "@prisma-next/utils/result";
4
5
  import { getPositionalArgument, parseQuotedStringLiteral } from "@prisma-next/psl-parser";
@@ -171,6 +172,32 @@ function stripQuotes(value) {
171
172
  }
172
173
  //#endregion
173
174
  //#region src/interpreter.ts
175
+ /**
176
+ * Name of the framework-parser synthesised bucket for top-level
177
+ * declarations. Re-declared locally so the interpreter does not have to
178
+ * import from `@prisma-next/framework-components/psl-ast`.
179
+ */
180
+ const UNSPECIFIED_PSL_NAMESPACE_NAME = "__unspecified__";
181
+ /**
182
+ * Mongo FR16c validation: Mongo's authoring DSL exposes the connection's
183
+ * database as the only namespace surface today, so the PSL interpreter
184
+ * rejects every explicit `namespace { … }` block. The implicit
185
+ * `__unspecified__` bucket (top-level declarations) is the only
186
+ * namespace Mongo accepts. `namespace unbound { … }` is rejected too —
187
+ * Mongo has no late-binding namespace concept on the PSL surface (the
188
+ * database name comes from the connection string, not from PSL).
189
+ */
190
+ function validateNamespaceBlocksForMongoTarget(input) {
191
+ for (const namespace of input.namespaces) {
192
+ if (namespace.name === UNSPECIFIED_PSL_NAMESPACE_NAME) continue;
193
+ input.diagnostics.push({
194
+ code: "PSL_UNSUPPORTED_NAMESPACE_BLOCK",
195
+ message: `Mongo does not support \`namespace ${namespace.name} { … }\` blocks (the database is bound by the connection string; declare models at the document top level instead).`,
196
+ sourceId: input.sourceId,
197
+ span: namespace.span
198
+ });
199
+ }
200
+ }
174
201
  function fkRelationPairKey(declaringModel, targetModel) {
175
202
  return `${declaringModel}::${targetModel}`;
176
203
  }
@@ -188,7 +215,8 @@ function resolveCollectionName(model) {
188
215
  function collectPolymorphismDeclarations(document, sourceId, diagnostics) {
189
216
  const discriminatorDeclarations = /* @__PURE__ */ new Map();
190
217
  const baseDeclarations = /* @__PURE__ */ new Map();
191
- for (const pslModel of document.ast.models) for (const attr of pslModel.attributes) {
218
+ const allPslModels = document.ast.namespaces.flatMap((ns) => ns.models);
219
+ for (const pslModel of allPslModels) for (const attr of pslModel.attributes) {
192
220
  if (attr.name === "discriminator") {
193
221
  const fieldName = getPositionalArgument(attr);
194
222
  if (!fieldName) {
@@ -253,6 +281,7 @@ function collectPolymorphismDeclarations(document, sourceId, diagnostics) {
253
281
  }
254
282
  function resolvePolymorphism(input) {
255
283
  const { discriminatorDeclarations, baseDeclarations, modelNames, sourceId, document, indexSpans, modelIndexesByName } = input;
284
+ const allPslModels = document.ast.namespaces.flatMap((ns) => ns.models);
256
285
  let patched = input.models;
257
286
  let roots = input.roots;
258
287
  let collections = input.collections;
@@ -269,7 +298,7 @@ function resolvePolymorphism(input) {
269
298
  }
270
299
  const model = patched[modelName];
271
300
  if (!model) continue;
272
- const pslModel = document.ast.models.find((m) => m.name === modelName);
301
+ const pslModel = allPslModels.find((m) => m.name === modelName);
273
302
  const mappedDiscriminatorField = pslModel ? resolveFieldMappings(pslModel).pslNameToMapped.get(decl.fieldName) ?? decl.fieldName : decl.fieldName;
274
303
  if (!Object.hasOwn(model.fields, mappedDiscriminatorField)) {
275
304
  diagnostics.push({
@@ -324,7 +353,7 @@ function resolvePolymorphism(input) {
324
353
  }
325
354
  if (discriminatorDeclarations.has(variantName)) continue;
326
355
  const baseModel = patched[baseDecl.baseName];
327
- const variantPslModel = document.ast.models.find((m) => m.name === variantName);
356
+ const variantPslModel = allPslModels.find((m) => m.name === variantName);
328
357
  if (!variantPslModel) continue;
329
358
  if (getMapName(variantPslModel.attributes) !== void 0 && baseModel && baseDecl.collectionName !== baseModel.storage.collection) {
330
359
  diagnostics.push({
@@ -724,8 +753,15 @@ function interpretPslDocumentToMongoContract(input) {
724
753
  const { document, scalarTypeDescriptors, codecLookup } = input;
725
754
  const sourceId = document.ast.sourceId;
726
755
  const diagnostics = [];
727
- const modelNames = new Set(document.ast.models.map((m) => m.name));
728
- const compositeTypeNames = new Set(document.ast.compositeTypes.map((ct) => ct.name));
756
+ validateNamespaceBlocksForMongoTarget({
757
+ namespaces: document.ast.namespaces,
758
+ sourceId,
759
+ diagnostics
760
+ });
761
+ const allModels = document.ast.namespaces.flatMap((ns) => ns.models);
762
+ const allCompositeTypes = document.ast.namespaces.flatMap((ns) => ns.compositeTypes);
763
+ const modelNames = new Set(allModels.map((m) => m.name));
764
+ const compositeTypeNames = new Set(allCompositeTypes.map((ct) => ct.name));
729
765
  const models = {};
730
766
  const collections = {};
731
767
  const roots = {};
@@ -733,7 +769,7 @@ function interpretPslDocumentToMongoContract(input) {
733
769
  const indexSpans = /* @__PURE__ */ new Map();
734
770
  const modelIndexesByName = /* @__PURE__ */ new Map();
735
771
  const backrelationCandidates = [];
736
- for (const pslModel of document.ast.models) {
772
+ for (const pslModel of allModels) {
737
773
  const collectionName = resolveCollectionName(pslModel);
738
774
  const fieldMappings = resolveFieldMappings(pslModel);
739
775
  const fields = {};
@@ -754,7 +790,7 @@ function interpretPslDocumentToMongoContract(input) {
754
790
  }
755
791
  if (relation?.fields && relation?.references) {
756
792
  const localMapped = relation.fields.map((f) => fieldMappings.pslNameToMapped.get(f) ?? f);
757
- const targetModel = document.ast.models.find((m) => m.name === field.typeName);
793
+ const targetModel = allModels.find((m) => m.name === field.typeName);
758
794
  const targetFieldMappings = targetModel ? resolveFieldMappings(targetModel) : void 0;
759
795
  const targetMapped = relation.references.map((f) => targetFieldMappings?.pslNameToMapped.get(f) ?? f);
760
796
  relations[field.name] = {
@@ -800,7 +836,7 @@ function interpretPslDocumentToMongoContract(input) {
800
836
  roots[collectionName] = pslModel.name;
801
837
  }
802
838
  const valueObjects = {};
803
- for (const compositeType of document.ast.compositeTypes) {
839
+ for (const compositeType of allCompositeTypes) {
804
840
  const fields = {};
805
841
  for (const field of compositeType.fields) {
806
842
  const resolved = resolveNonRelationField(field, compositeType.name, compositeTypeNames, scalarTypeDescriptors, sourceId, diagnostics);
@@ -892,7 +928,10 @@ function interpretPslDocumentToMongoContract(input) {
892
928
  if (coll["options"] !== void 0) input.options = coll["options"];
893
929
  collectionsAsClasses[name] = new MongoCollection(input);
894
930
  }
895
- const storageWithoutHash = { collections: collectionsAsClasses };
931
+ const storageWithoutHash = { namespaces: { [UNBOUND_NAMESPACE_ID]: {
932
+ id: UNBOUND_NAMESPACE_ID,
933
+ collections: collectionsAsClasses
934
+ } } };
896
935
  const storageHash = computeStorageHash({
897
936
  target,
898
937
  targetFamily,
@@ -922,4 +961,4 @@ function interpretPslDocumentToMongoContract(input) {
922
961
  //#endregion
923
962
  export { interpretPslDocumentToMongoContract as t };
924
963
 
925
- //# sourceMappingURL=interpreter-DgpmmXva.mjs.map
964
+ //# sourceMappingURL=interpreter-DzLp9JOt.mjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"interpreter-DzLp9JOt.mjs","names":[],"sources":["../src/derive-json-schema.ts","../src/psl-helpers.ts","../src/interpreter.ts"],"sourcesContent":["import type { ContractField, ContractValueObject } from '@prisma-next/contract/types';\nimport type { CodecLookup } from '@prisma-next/framework-components/codec';\nimport { MongoValidator } from '@prisma-next/mongo-contract';\n\nfunction resolveBsonType(\n codecId: string,\n codecLookup: CodecLookup | undefined,\n): string | undefined {\n return codecLookup?.targetTypesFor(codecId)?.[0];\n}\n\nfunction fieldToBsonSchema(\n field: ContractField,\n valueObjects: Record<string, ContractValueObject> | undefined,\n codecLookup: CodecLookup | undefined,\n): Record<string, unknown> | undefined {\n if (field.type.kind === 'scalar') {\n const bsonType = resolveBsonType(field.type.codecId, codecLookup);\n if (!bsonType) return undefined;\n\n if ('many' in field && field.many) {\n return { bsonType: 'array', items: { bsonType } };\n }\n\n if (field.nullable) {\n return { bsonType: ['null', bsonType] };\n }\n\n return { bsonType };\n }\n\n if (field.type.kind === 'valueObject') {\n const vo = valueObjects?.[field.type.name];\n if (!vo) return undefined;\n const voSchema = deriveObjectSchema(vo.fields, valueObjects, codecLookup);\n if ('many' in field && field.many) {\n return { bsonType: 'array', items: voSchema };\n }\n if (field.nullable) {\n return { oneOf: [{ bsonType: 'null' }, voSchema] };\n }\n return voSchema;\n }\n\n return undefined;\n}\n\nfunction deriveObjectSchema(\n fields: Record<string, ContractField>,\n valueObjects: Record<string, ContractValueObject> | undefined,\n codecLookup: CodecLookup | undefined,\n): Record<string, unknown> {\n const properties: Record<string, unknown> = {};\n const required: string[] = [];\n\n for (const [fieldName, field] of Object.entries(fields)) {\n const schema = fieldToBsonSchema(field, valueObjects, codecLookup);\n if (schema) {\n properties[fieldName] = schema;\n if (!field.nullable) {\n required.push(fieldName);\n }\n }\n }\n\n const result: Record<string, unknown> = {\n bsonType: 'object',\n properties,\n };\n if (required.length > 0) {\n result['required'] = required.sort();\n }\n return result;\n}\n\nexport function deriveJsonSchema(\n fields: Record<string, ContractField>,\n valueObjects?: Record<string, ContractValueObject>,\n codecLookup?: CodecLookup,\n): MongoValidator {\n return new MongoValidator({\n jsonSchema: deriveObjectSchema(fields, valueObjects, codecLookup),\n validationLevel: 'strict',\n validationAction: 'error',\n });\n}\n\nexport interface PolymorphicVariant {\n readonly discriminatorValue: string;\n readonly fields: Record<string, ContractField>;\n}\n\nexport function derivePolymorphicJsonSchema(\n baseFields: Record<string, ContractField>,\n discriminatorField: string,\n variants: readonly PolymorphicVariant[],\n valueObjects?: Record<string, ContractValueObject>,\n codecLookup?: CodecLookup,\n): MongoValidator {\n const baseSchema = deriveObjectSchema(baseFields, valueObjects, codecLookup);\n\n const oneOf: Record<string, unknown>[] = [];\n for (const variant of variants) {\n const variantOnlyFields: Record<string, ContractField> = {};\n for (const [name, field] of Object.entries(variant.fields)) {\n if (!(name in baseFields)) {\n variantOnlyFields[name] = field;\n }\n }\n\n const entry: Record<string, unknown> = {\n properties: {\n [discriminatorField]: { enum: [variant.discriminatorValue] },\n },\n };\n\n const variantProperties: Record<string, unknown> = {};\n const variantRequired: string[] = [discriminatorField];\n for (const [name, field] of Object.entries(variantOnlyFields)) {\n const schema = fieldToBsonSchema(field, valueObjects, codecLookup);\n if (schema) {\n variantProperties[name] = schema;\n if (!field.nullable) {\n variantRequired.push(name);\n }\n }\n }\n\n if (Object.keys(variantProperties).length > 0) {\n (entry['properties'] as Record<string, unknown>) = {\n ...(entry['properties'] as Record<string, unknown>),\n ...variantProperties,\n };\n }\n entry['required'] = variantRequired.sort();\n\n oneOf.push(entry);\n }\n\n const jsonSchema = { ...baseSchema };\n if (oneOf.length > 0) {\n jsonSchema['oneOf'] = oneOf;\n }\n\n return new MongoValidator({\n jsonSchema,\n validationLevel: 'strict',\n validationAction: 'error',\n });\n}\n","import type { PslAttribute, PslAttributeArgument } from '@prisma-next/psl-parser';\nimport { getPositionalArgument, parseQuotedStringLiteral } from '@prisma-next/psl-parser';\n\nexport { getPositionalArgument, parseQuotedStringLiteral };\n\nexport function getNamedArgument(attr: PslAttribute, name: string): string | undefined {\n const arg = attr.args.find((a) => a.kind === 'named' && a.name === name);\n return arg?.value;\n}\n\nexport function parseFieldList(value: string): readonly string[] {\n const inner = value.replace(/^\\[/, '').replace(/\\]$/, '').trim();\n if (inner.length === 0) return [];\n return splitTopLevel(inner).map((s) => s.trim());\n}\n\nexport interface ParsedIndexField {\n readonly name: string;\n readonly isWildcard: boolean;\n readonly direction?: number;\n}\n\nexport function parseIndexFieldList(value: string): readonly ParsedIndexField[] {\n const segments = parseFieldList(value);\n return segments.map(parseIndexFieldSegment);\n}\n\nfunction parseIndexFieldSegment(segment: string): ParsedIndexField {\n const wildcardMatch = segment.match(/^wildcard\\(\\s*(.*?)\\s*\\)$/);\n if (wildcardMatch) {\n const scope = wildcardMatch[1] ?? '';\n return {\n name: scope.length > 0 ? `${scope}.$**` : '$**',\n isWildcard: true,\n };\n }\n\n const modifierMatch = segment.match(/^(\\w+)\\(\\s*sort:\\s*(\\w+)\\s*\\)$/);\n if (modifierMatch) {\n const fieldName = modifierMatch[1] ?? segment;\n const sortValue = modifierMatch[2];\n return {\n name: fieldName,\n isWildcard: false,\n direction: sortValue === 'Desc' ? -1 : 1,\n };\n }\n\n return { name: segment, isWildcard: false };\n}\n\nfunction splitTopLevel(input: string): string[] {\n const parts: string[] = [];\n let depth = 0;\n let start = 0;\n for (let i = 0; i < input.length; i++) {\n const ch = input[i];\n if (ch === '(' || ch === '[' || ch === '{') depth++;\n else if (ch === ')' || ch === ']' || ch === '}') depth = Math.max(0, depth - 1);\n else if (ch === ',' && depth === 0) {\n parts.push(input.slice(start, i));\n start = i + 1;\n }\n }\n parts.push(input.slice(start));\n return parts;\n}\n\nexport function lowerFirst(value: string): string {\n if (value.length === 0) return value;\n return value[0]?.toLowerCase() + value.slice(1);\n}\n\nexport function getAttribute(\n attributes: readonly PslAttribute[],\n name: string,\n): PslAttribute | undefined {\n return attributes.find((attr) => attr.name === name);\n}\n\nexport function getMapName(attributes: readonly PslAttribute[]): string | undefined {\n const mapAttr = getAttribute(attributes, 'map');\n if (!mapAttr) return undefined;\n const arg = mapAttr.args[0];\n if (!arg) return undefined;\n return stripQuotes(arg.value);\n}\n\nexport interface ParsedRelationAttribute {\n readonly relationName?: string;\n readonly fields?: readonly string[];\n readonly references?: readonly string[];\n}\n\nexport function parseRelationAttribute(\n attributes: readonly PslAttribute[],\n): ParsedRelationAttribute | undefined {\n const relationAttr = getAttribute(attributes, 'relation');\n if (!relationAttr) return undefined;\n\n let relationName: string | undefined;\n let fieldsArg: PslAttributeArgument | undefined;\n let referencesArg: PslAttributeArgument | undefined;\n\n for (const arg of relationAttr.args) {\n if (arg.kind === 'positional') {\n relationName = stripQuotes(arg.value);\n } else if (arg.name === 'name') {\n relationName = stripQuotes(arg.value);\n } else if (arg.name === 'fields') {\n fieldsArg = arg;\n } else if (arg.name === 'references') {\n referencesArg = arg;\n }\n }\n\n const fields = fieldsArg ? parseFieldList(fieldsArg.value) : undefined;\n const references = referencesArg ? parseFieldList(referencesArg.value) : undefined;\n\n return {\n ...(relationName !== undefined ? { relationName } : {}),\n ...(fields !== undefined ? { fields } : {}),\n ...(references !== undefined ? { references } : {}),\n };\n}\n\nfunction stripQuotes(value: string): string {\n if (value.startsWith('\"') && value.endsWith('\"')) {\n return value.slice(1, -1);\n }\n return value;\n}\n","import type {\n ContractSourceDiagnostic,\n ContractSourceDiagnostics,\n} from '@prisma-next/config/config-types';\nimport { computeProfileHash, computeStorageHash } from '@prisma-next/contract/hashing';\nimport type {\n Contract,\n ContractField,\n ContractReferenceRelation,\n ContractValueObject,\n} from '@prisma-next/contract/types';\nimport type { CodecLookup } from '@prisma-next/framework-components/codec';\nimport { UNBOUND_NAMESPACE_ID } from '@prisma-next/framework-components/ir';\nimport {\n applyPolymorphicScopeToMongoIndex,\n MongoCollection,\n MongoIndex,\n type MongoIndexKeyDirection,\n} from '@prisma-next/mongo-contract';\nimport type {\n ParsePslDocumentResult,\n PslField,\n PslModel,\n PslNamespace,\n PslSpan,\n} from '@prisma-next/psl-parser';\nimport { notOk, ok, type Result } from '@prisma-next/utils/result';\nimport { deriveJsonSchema, derivePolymorphicJsonSchema } from './derive-json-schema';\nimport {\n getAttribute,\n getMapName,\n getNamedArgument,\n getPositionalArgument,\n lowerFirst,\n parseIndexFieldList,\n parseQuotedStringLiteral,\n parseRelationAttribute,\n} from './psl-helpers';\n\nexport interface InterpretPslDocumentToMongoContractInput {\n readonly document: ParsePslDocumentResult;\n readonly scalarTypeDescriptors: ReadonlyMap<string, string>;\n readonly codecLookup?: CodecLookup;\n}\n\n/**\n * Name of the framework-parser synthesised bucket for top-level\n * declarations. Re-declared locally so the interpreter does not have to\n * import from `@prisma-next/framework-components/psl-ast`.\n */\nconst UNSPECIFIED_PSL_NAMESPACE_NAME = '__unspecified__';\n\n/**\n * Mongo FR16c validation: Mongo's authoring DSL exposes the connection's\n * database as the only namespace surface today, so the PSL interpreter\n * rejects every explicit `namespace { … }` block. The implicit\n * `__unspecified__` bucket (top-level declarations) is the only\n * namespace Mongo accepts. `namespace unbound { … }` is rejected too —\n * Mongo has no late-binding namespace concept on the PSL surface (the\n * database name comes from the connection string, not from PSL).\n */\nfunction validateNamespaceBlocksForMongoTarget(input: {\n readonly namespaces: readonly PslNamespace[];\n readonly sourceId: string;\n readonly diagnostics: ContractSourceDiagnostic[];\n}): void {\n for (const namespace of input.namespaces) {\n if (namespace.name === UNSPECIFIED_PSL_NAMESPACE_NAME) {\n continue;\n }\n input.diagnostics.push({\n code: 'PSL_UNSUPPORTED_NAMESPACE_BLOCK',\n message: `Mongo does not support \\`namespace ${namespace.name} { … }\\` blocks (the database is bound by the connection string; declare models at the document top level instead).`,\n sourceId: input.sourceId,\n span: namespace.span,\n });\n }\n}\n\ninterface FieldMappings {\n readonly pslNameToMapped: Map<string, string>;\n}\n\ninterface FkRelation {\n readonly declaringModel: string;\n readonly fieldName: string;\n readonly targetModel: string;\n readonly relationName?: string;\n readonly localFields: readonly string[];\n readonly targetFields: readonly string[];\n}\n\nfunction fkRelationPairKey(declaringModel: string, targetModel: string): string {\n return `${declaringModel}::${targetModel}`;\n}\n\nfunction resolveFieldMappings(model: PslModel): FieldMappings {\n const pslNameToMapped = new Map<string, string>();\n for (const field of model.fields) {\n const mapped = getMapName(field.attributes) ?? field.name;\n pslNameToMapped.set(field.name, mapped);\n }\n return { pslNameToMapped };\n}\n\nfunction resolveCollectionName(model: PslModel): string {\n return getMapName(model.attributes) ?? lowerFirst(model.name);\n}\n\ninterface MongoModelEntry {\n readonly fields: Record<string, ContractField>;\n readonly relations: Record<string, ContractReferenceRelation>;\n readonly storage: { readonly collection: string };\n readonly discriminator?: { readonly field: string };\n readonly variants?: Record<string, { readonly value: string }>;\n readonly base?: string;\n}\n\ntype DiscriminatorDeclaration = { readonly fieldName: string; readonly span: PslModel['span'] };\ntype BaseDeclaration = {\n readonly baseName: string;\n readonly value: string;\n readonly collectionName: string;\n readonly span: PslModel['span'];\n};\n\nfunction collectPolymorphismDeclarations(\n document: ParsePslDocumentResult,\n sourceId: string,\n diagnostics: ContractSourceDiagnostic[],\n): {\n discriminatorDeclarations: Map<string, DiscriminatorDeclaration>;\n baseDeclarations: Map<string, BaseDeclaration>;\n} {\n const discriminatorDeclarations = new Map<string, DiscriminatorDeclaration>();\n const baseDeclarations = new Map<string, BaseDeclaration>();\n const allPslModels = document.ast.namespaces.flatMap((ns) => ns.models);\n\n for (const pslModel of allPslModels) {\n for (const attr of pslModel.attributes) {\n if (attr.name === 'discriminator') {\n const fieldName = getPositionalArgument(attr);\n if (!fieldName) {\n diagnostics.push({\n code: 'PSL_INVALID_ATTRIBUTE_ARGUMENT',\n message: `Model \"${pslModel.name}\" @@discriminator requires a field name argument`,\n sourceId,\n span: attr.span,\n });\n continue;\n }\n const discField = pslModel.fields.find((f) => f.name === fieldName);\n if (discField && discField.typeName !== 'String') {\n diagnostics.push({\n code: 'PSL_INVALID_ATTRIBUTE_ARGUMENT',\n message: `Discriminator field \"${fieldName}\" on model \"${pslModel.name}\" must be of type String, but is \"${discField.typeName}\"`,\n sourceId,\n span: attr.span,\n });\n continue;\n }\n discriminatorDeclarations.set(pslModel.name, { fieldName, span: attr.span });\n }\n if (attr.name === 'base') {\n const baseName = getPositionalArgument(attr, 0);\n const rawValue = getPositionalArgument(attr, 1);\n if (!baseName || !rawValue) {\n diagnostics.push({\n code: 'PSL_INVALID_ATTRIBUTE_ARGUMENT',\n message: `Model \"${pslModel.name}\" @@base requires two arguments: base model name and discriminator value`,\n sourceId,\n span: attr.span,\n });\n continue;\n }\n const value = parseQuotedStringLiteral(rawValue);\n if (value === undefined) {\n diagnostics.push({\n code: 'PSL_INVALID_ATTRIBUTE_ARGUMENT',\n message: `Model \"${pslModel.name}\" @@base discriminator value must be a quoted string literal`,\n sourceId,\n span: attr.span,\n });\n continue;\n }\n const collectionName = resolveCollectionName(pslModel);\n baseDeclarations.set(pslModel.name, { baseName, value, collectionName, span: attr.span });\n }\n }\n }\n\n return { discriminatorDeclarations, baseDeclarations };\n}\n\nfunction resolvePolymorphism(input: {\n models: Record<string, MongoModelEntry>;\n roots: Record<string, string>;\n collections: Record<string, Record<string, unknown>>;\n document: ParsePslDocumentResult;\n discriminatorDeclarations: Map<string, DiscriminatorDeclaration>;\n baseDeclarations: Map<string, BaseDeclaration>;\n modelNames: ReadonlySet<string>;\n indexSpans: Map<MongoIndex, PslSpan>;\n modelIndexesByName: Map<string, readonly MongoIndex[]>;\n sourceId: string;\n}): {\n models: Record<string, MongoModelEntry>;\n roots: Record<string, string>;\n collections: Record<string, Record<string, unknown>>;\n diagnostics: ContractSourceDiagnostic[];\n} {\n const {\n discriminatorDeclarations,\n baseDeclarations,\n modelNames,\n sourceId,\n document,\n indexSpans,\n modelIndexesByName,\n } = input;\n const allPslModels = document.ast.namespaces.flatMap((ns) => ns.models);\n let patched = input.models;\n let roots = input.roots;\n let collections = input.collections;\n const diagnostics: ContractSourceDiagnostic[] = [];\n\n for (const [modelName, decl] of discriminatorDeclarations) {\n if (baseDeclarations.has(modelName)) {\n diagnostics.push({\n code: 'PSL_DISCRIMINATOR_AND_BASE',\n message: `Model \"${modelName}\" cannot have both @@discriminator and @@base`,\n sourceId,\n span: decl.span,\n });\n continue;\n }\n\n const model = patched[modelName];\n if (!model) continue;\n\n const pslModel = allPslModels.find((m) => m.name === modelName);\n const mappedDiscriminatorField = pslModel\n ? (resolveFieldMappings(pslModel).pslNameToMapped.get(decl.fieldName) ?? decl.fieldName)\n : decl.fieldName;\n\n if (!Object.hasOwn(model.fields, mappedDiscriminatorField)) {\n diagnostics.push({\n code: 'PSL_DISCRIMINATOR_FIELD_NOT_FOUND',\n message: `Discriminator field \"${decl.fieldName}\" is not a field on model \"${modelName}\"`,\n sourceId,\n span: decl.span,\n });\n continue;\n }\n\n const variants: Record<string, { readonly value: string }> = {};\n for (const [variantName, baseDecl] of baseDeclarations) {\n if (baseDecl.baseName !== modelName) continue;\n variants[variantName] = { value: baseDecl.value };\n }\n\n if (Object.keys(variants).length === 0) {\n diagnostics.push({\n code: 'PSL_ORPHANED_DISCRIMINATOR',\n message: `Model \"${modelName}\" has @@discriminator but no variant models declare @@base(${modelName}, ...)`,\n sourceId,\n span: decl.span,\n });\n continue;\n }\n\n patched = {\n ...patched,\n [modelName]: { ...model, discriminator: { field: mappedDiscriminatorField }, variants },\n };\n }\n\n for (const [variantName, baseDecl] of baseDeclarations) {\n if (!modelNames.has(baseDecl.baseName)) {\n diagnostics.push({\n code: 'PSL_BASE_TARGET_NOT_FOUND',\n message: `Model \"${variantName}\" @@base references non-existent model \"${baseDecl.baseName}\"`,\n sourceId,\n span: baseDecl.span,\n });\n continue;\n }\n\n if (!discriminatorDeclarations.has(baseDecl.baseName)) {\n diagnostics.push({\n code: 'PSL_ORPHANED_BASE',\n message: `Model \"${variantName}\" declares @@base(${baseDecl.baseName}, ...) but \"${baseDecl.baseName}\" has no @@discriminator`,\n sourceId,\n span: baseDecl.span,\n });\n continue;\n }\n\n if (discriminatorDeclarations.has(variantName)) {\n continue;\n }\n\n const baseModel = patched[baseDecl.baseName];\n const variantPslModel = allPslModels.find((m) => m.name === variantName);\n if (!variantPslModel) continue;\n const hasExplicitMap = getMapName(variantPslModel.attributes) !== undefined;\n\n if (hasExplicitMap && baseModel && baseDecl.collectionName !== baseModel.storage.collection) {\n diagnostics.push({\n code: 'PSL_MONGO_VARIANT_SEPARATE_COLLECTION',\n message: `Mongo variant \"${variantName}\" cannot use a different collection than its base \"${baseDecl.baseName}\". Mongo only supports single-collection polymorphism.`,\n sourceId,\n span: baseDecl.span,\n });\n continue;\n }\n\n const baseCollection = baseModel?.storage.collection ?? baseDecl.collectionName;\n const variantModel = patched[variantName];\n if (variantModel) {\n patched = {\n ...patched,\n [variantName]: {\n ...variantModel,\n base: baseDecl.baseName,\n storage: { collection: baseCollection },\n },\n };\n }\n\n const variantCollectionName = resolveCollectionName(variantPslModel);\n if (roots[variantCollectionName] === variantName) {\n if (variantCollectionName === baseCollection && baseModel) {\n roots = { ...roots, [variantCollectionName]: baseDecl.baseName };\n } else {\n roots = Object.fromEntries(\n Object.entries(roots).filter(([key]) => key !== variantCollectionName),\n );\n }\n }\n\n const variantOwnIndexes = modelIndexesByName.get(variantName) ?? [];\n const baseColl = collections[baseCollection];\n\n const baseModelEntry = patched[baseDecl.baseName];\n const discriminatorField = baseModelEntry?.discriminator?.field;\n const scopedVariantIndexes: MongoIndex[] = [];\n if (discriminatorField) {\n for (const idx of variantOwnIndexes) {\n const result = applyPolymorphicScopeToMongoIndex(idx, {\n discriminatorField,\n discriminatorValue: baseDecl.value,\n });\n if (result.kind === 'conflict') {\n const span = indexSpans.get(idx) ?? baseDecl.span;\n diagnostics.push({\n code: 'PSL_INVALID_INDEX',\n message: `Variant \"${variantName}\" index conflicts with discriminator scope: ${result.reason}`,\n sourceId,\n span,\n });\n continue;\n }\n if (result.index !== idx) {\n indexSpans.set(result.index, indexSpans.get(idx) ?? baseDecl.span);\n }\n scopedVariantIndexes.push(result.index);\n }\n } else {\n scopedVariantIndexes.push(...variantOwnIndexes);\n }\n\n if (variantCollectionName !== baseCollection) {\n const filtered = Object.fromEntries(\n Object.entries(collections).filter(([key]) => key !== variantCollectionName),\n );\n if (scopedVariantIndexes.length > 0 && baseColl) {\n const baseIndexes = (baseColl['indexes'] ?? []) as MongoIndex[];\n collections = {\n ...filtered,\n [baseCollection]: {\n ...baseColl,\n indexes: [...baseIndexes, ...scopedVariantIndexes],\n },\n };\n } else {\n collections = filtered;\n }\n } else if (baseColl) {\n const existingIndexes = (baseColl['indexes'] ?? []) as MongoIndex[];\n const variantIndexSet = new Set<MongoIndex>(variantOwnIndexes);\n const withoutUnscopedVariants = existingIndexes.filter((idx) => !variantIndexSet.has(idx));\n const mergedIndexes = [...withoutUnscopedVariants];\n for (const idx of scopedVariantIndexes) {\n const idxKey = canonicalJson(idx);\n const isDuplicate = withoutUnscopedVariants.some(\n (existing) => canonicalJson(existing) === idxKey,\n );\n if (!isDuplicate) {\n mergedIndexes.push(idx);\n }\n }\n if (\n mergedIndexes.length !== existingIndexes.length ||\n mergedIndexes.some((idx, i) => idx !== existingIndexes[i])\n ) {\n const next: Record<string, unknown> = { ...baseColl };\n if (mergedIndexes.length > 0) {\n next['indexes'] = mergedIndexes;\n } else {\n delete next['indexes'];\n }\n collections = { ...collections, [baseCollection]: next };\n }\n }\n }\n\n return { models: patched, roots, collections, diagnostics };\n}\n\n// Property-order-stable serialization for structural equality of plain\n// JSON-compatible values. Used for comparing MongoIndex shapes in\n// the variant-merge dedup path where a future change to the spread order\n// would otherwise produce JSON-stringify mismatches even though the\n// indexes are structurally identical.\nfunction canonicalJson(value: unknown): string {\n if (Array.isArray(value)) {\n return `[${value.map(canonicalJson).join(',')}]`;\n }\n if (value && typeof value === 'object') {\n return `{${Object.entries(value as Record<string, unknown>)\n .sort(([left], [right]) => left.localeCompare(right))\n .map(([key, entry]) => `${JSON.stringify(key)}:${canonicalJson(entry)}`)\n .join(',')}}`;\n }\n return JSON.stringify(value);\n}\n\nfunction parseIndexDirection(raw: string | undefined): MongoIndexKeyDirection {\n if (!raw) return 1;\n const stripped = raw.replace(/^[\"']/, '').replace(/[\"']$/, '');\n const num = Number(stripped);\n if (num === 1 || num === -1) return num;\n if (['text', '2dsphere', '2d', 'hashed'].includes(stripped))\n return stripped as MongoIndexKeyDirection;\n return 1;\n}\n\nfunction parseNumericArg(raw: string | undefined): number | undefined {\n if (!raw) return undefined;\n const n = Number(raw);\n return Number.isFinite(n) ? n : undefined;\n}\n\nfunction parseBooleanArg(raw: string | undefined): boolean | undefined {\n if (raw === 'true') return true;\n if (raw === 'false') return false;\n return undefined;\n}\n\nfunction parseJsonArg(raw: string | undefined): Record<string, unknown> | undefined {\n if (!raw) return undefined;\n const stripped = raw.replace(/^[\"']/, '').replace(/[\"']$/, '').replace(/\\\\\"/g, '\"');\n try {\n const parsed = JSON.parse(stripped);\n if (typeof parsed === 'object' && parsed !== null && !Array.isArray(parsed)) {\n return parsed as Record<string, unknown>;\n }\n } catch {\n // not valid JSON\n }\n return undefined;\n}\n\nfunction parseCollation(\n attr: import('@prisma-next/psl-parser').PslAttribute,\n): Record<string, unknown> | null | undefined {\n const locale = stripQuotesHelper(getNamedArgument(attr, 'collationLocale'));\n if (!locale) {\n const hasAnyCollationArg =\n getNamedArgument(attr, 'collationStrength') != null ||\n getNamedArgument(attr, 'collationCaseLevel') != null ||\n getNamedArgument(attr, 'collationCaseFirst') != null ||\n getNamedArgument(attr, 'collationNumericOrdering') != null ||\n getNamedArgument(attr, 'collationAlternate') != null ||\n getNamedArgument(attr, 'collationMaxVariable') != null ||\n getNamedArgument(attr, 'collationBackwards') != null ||\n getNamedArgument(attr, 'collationNormalization') != null;\n return hasAnyCollationArg ? null : undefined;\n }\n\n const collation: Record<string, unknown> = { locale };\n const strength = parseNumericArg(getNamedArgument(attr, 'collationStrength'));\n if (strength != null) collation['strength'] = strength;\n const caseLevel = parseBooleanArg(getNamedArgument(attr, 'collationCaseLevel'));\n if (caseLevel != null) collation['caseLevel'] = caseLevel;\n const caseFirst = stripQuotesHelper(getNamedArgument(attr, 'collationCaseFirst'));\n if (caseFirst != null) collation['caseFirst'] = caseFirst;\n const numericOrdering = parseBooleanArg(getNamedArgument(attr, 'collationNumericOrdering'));\n if (numericOrdering != null) collation['numericOrdering'] = numericOrdering;\n const alternate = stripQuotesHelper(getNamedArgument(attr, 'collationAlternate'));\n if (alternate != null) collation['alternate'] = alternate;\n const maxVariable = stripQuotesHelper(getNamedArgument(attr, 'collationMaxVariable'));\n if (maxVariable != null) collation['maxVariable'] = maxVariable;\n const backwards = parseBooleanArg(getNamedArgument(attr, 'collationBackwards'));\n if (backwards != null) collation['backwards'] = backwards;\n const normalization = parseBooleanArg(getNamedArgument(attr, 'collationNormalization'));\n if (normalization != null) collation['normalization'] = normalization;\n return collation;\n}\n\nfunction stripQuotesHelper(raw: string | undefined): string | undefined {\n if (!raw) return undefined;\n return raw.replace(/^[\"']/, '').replace(/[\"']$/, '');\n}\n\nfunction parseProjectionList(\n raw: string | undefined,\n value: 0 | 1,\n): Record<string, 0 | 1> | undefined {\n if (!raw) return undefined;\n const stripped = raw.replace(/^[\"']/, '').replace(/[\"']$/, '');\n const inner = stripped.replace(/^\\[/, '').replace(/\\]$/, '').trim();\n if (inner.length === 0) return undefined;\n const fields = inner\n .split(',')\n .map((s) => s.trim())\n .filter((s) => s.length > 0);\n const result: Record<string, 0 | 1> = {};\n for (const f of fields) {\n result[f] = value;\n }\n return result;\n}\n\nfunction collectIndexes(\n pslModel: PslModel,\n fieldMappings: FieldMappings,\n modelNames: ReadonlySet<string>,\n sourceId: string,\n diagnostics: ContractSourceDiagnostic[],\n indexSpans: Map<MongoIndex, PslSpan>,\n): MongoIndex[] {\n const indexes: MongoIndex[] = [];\n let textIndexCount = 0;\n\n // Storage-indexable PSL field names — i.e. all declared fields except\n // relation fields (which don't materialize a column on this model). The\n // index field-existence check (PSL_INDEX_FIELD_NOT_FOUND) consults this\n // rather than fieldMappings.pslNameToMapped because the latter contains\n // every PSL field including relation fields.\n const indexableFieldNames = new Set<string>();\n for (const f of pslModel.fields) {\n if (modelNames.has(f.typeName)) continue;\n indexableFieldNames.add(f.name);\n }\n\n for (const field of pslModel.fields) {\n if (modelNames.has(field.typeName)) continue;\n const uniqueAttr = getAttribute(field.attributes, 'unique');\n if (!uniqueAttr) continue;\n const mappedName = fieldMappings.pslNameToMapped.get(field.name) ?? field.name;\n const fieldUniqueIndex = new MongoIndex({\n keys: [{ field: mappedName, direction: 1 }],\n unique: true,\n });\n indexes.push(fieldUniqueIndex);\n indexSpans.set(fieldUniqueIndex, uniqueAttr.span);\n }\n\n for (const attr of pslModel.attributes) {\n const isIndex = attr.name === 'index';\n const isUnique = attr.name === 'unique';\n const isTextIndex = attr.name === 'textIndex';\n if (!isIndex && !isUnique && !isTextIndex) continue;\n\n const fieldsArg = getPositionalArgument(attr, 0);\n if (!fieldsArg) continue;\n const parsedFields = parseIndexFieldList(fieldsArg);\n if (parsedFields.length === 0) continue;\n\n const hasWildcard = parsedFields.some((f) => f.isWildcard);\n const wildcardCount = parsedFields.filter((f) => f.isWildcard).length;\n\n if (wildcardCount > 1) {\n diagnostics.push({\n code: 'PSL_INVALID_INDEX',\n message: 'An index can contain at most one wildcard() field',\n sourceId,\n span: attr.span,\n });\n continue;\n }\n\n if (isUnique && hasWildcard) {\n diagnostics.push({\n code: 'PSL_INVALID_INDEX',\n message: 'Unique indexes cannot use wildcard() fields',\n sourceId,\n span: attr.span,\n });\n continue;\n }\n\n if (isTextIndex) {\n textIndexCount++;\n if (textIndexCount > 1) {\n diagnostics.push({\n code: 'PSL_INVALID_INDEX',\n message: `Only one @@textIndex is allowed per collection (model \"${pslModel.name}\")`,\n sourceId,\n span: attr.span,\n });\n continue;\n }\n\n if (hasWildcard) {\n diagnostics.push({\n code: 'PSL_INVALID_INDEX',\n message:\n 'wildcard() fields cannot be combined with type: hashed/2dsphere/2d or @@textIndex',\n sourceId,\n span: attr.span,\n });\n continue;\n }\n }\n\n const typeArg = getNamedArgument(attr, 'type');\n const defaultDirection: MongoIndexKeyDirection = isTextIndex\n ? 'text'\n : parseIndexDirection(typeArg);\n\n if (\n hasWildcard &&\n typeof defaultDirection === 'string' &&\n ['hashed', '2dsphere', '2d'].includes(defaultDirection)\n ) {\n diagnostics.push({\n code: 'PSL_INVALID_INDEX',\n message: `wildcard() fields cannot be combined with type: ${defaultDirection}`,\n sourceId,\n span: attr.span,\n });\n continue;\n }\n\n if (defaultDirection === 'hashed' && parsedFields.length > 1) {\n diagnostics.push({\n code: 'PSL_INVALID_INDEX',\n message: 'Hashed indexes must have exactly one field',\n sourceId,\n span: attr.span,\n });\n continue;\n }\n\n let missingField: string | undefined;\n for (const pf of parsedFields) {\n let fieldNameForLookup: string | undefined;\n if (pf.isWildcard) {\n const wildcardMatch = pf.name.match(/^(.+)\\.\\$\\*\\*$/);\n fieldNameForLookup = wildcardMatch ? wildcardMatch[1] : undefined;\n } else {\n fieldNameForLookup = pf.name;\n }\n if (fieldNameForLookup === undefined || fieldNameForLookup.length === 0) continue;\n if (!indexableFieldNames.has(fieldNameForLookup)) {\n missingField = fieldNameForLookup;\n break;\n }\n }\n if (missingField !== undefined) {\n diagnostics.push({\n code: 'PSL_INDEX_FIELD_NOT_FOUND',\n message: `Index on model \"${pslModel.name}\" references unknown field \"${missingField}\"`,\n sourceId,\n span: attr.span,\n });\n continue;\n }\n\n const keys = parsedFields.map((pf) => {\n const mappedName = pf.isWildcard\n ? pf.name.replace(/^(.+)\\.\\$\\*\\*$/, (_, prefix: string) => {\n const mapped = fieldMappings.pslNameToMapped.get(prefix);\n return mapped ? `${mapped}.$**` : `${prefix}.$**`;\n })\n : (fieldMappings.pslNameToMapped.get(pf.name) ?? pf.name);\n const direction: MongoIndexKeyDirection =\n pf.direction != null ? (pf.direction as MongoIndexKeyDirection) : defaultDirection;\n return { field: mappedName, direction };\n });\n\n const unique = isUnique ? true : undefined;\n const sparse = isTextIndex ? undefined : parseBooleanArg(getNamedArgument(attr, 'sparse'));\n const expireAfterSeconds = isTextIndex\n ? undefined\n : parseNumericArg(getNamedArgument(attr, 'expireAfterSeconds'));\n\n if (hasWildcard && expireAfterSeconds != null) {\n diagnostics.push({\n code: 'PSL_INVALID_INDEX',\n message: 'expireAfterSeconds cannot be combined with wildcard() fields',\n sourceId,\n span: attr.span,\n });\n continue;\n }\n\n const partialFilterExpression = parseJsonArg(getNamedArgument(attr, 'filter'));\n\n const includeArg = getNamedArgument(attr, 'include');\n const excludeArg = getNamedArgument(attr, 'exclude');\n\n if (includeArg != null && excludeArg != null) {\n diagnostics.push({\n code: 'PSL_INVALID_INDEX',\n message: 'Cannot specify both include and exclude on the same index',\n sourceId,\n span: attr.span,\n });\n continue;\n }\n\n if ((includeArg != null || excludeArg != null) && !hasWildcard) {\n diagnostics.push({\n code: 'PSL_INVALID_INDEX',\n message:\n 'include/exclude options are only valid when the index contains a wildcard() field',\n sourceId,\n span: attr.span,\n });\n continue;\n }\n\n const wildcardProjection =\n includeArg != null\n ? parseProjectionList(includeArg, 1)\n : excludeArg != null\n ? parseProjectionList(excludeArg, 0)\n : undefined;\n\n const collation = parseCollation(attr);\n if (collation === null) {\n diagnostics.push({\n code: 'PSL_INVALID_INDEX',\n message: 'collationLocale is required when using collation options',\n sourceId,\n span: attr.span,\n });\n continue;\n }\n\n const rawWeights = parseJsonArg(getNamedArgument(attr, 'weights'));\n let weights: Record<string, number> | undefined;\n if (rawWeights) {\n weights = {};\n for (const [k, v] of Object.entries(rawWeights)) {\n if (typeof v === 'number') weights[k] = v;\n }\n }\n\n const rawDefaultLang = isTextIndex\n ? getNamedArgument(attr, 'language')\n : getNamedArgument(attr, 'default_language');\n const default_language = stripQuotesHelper(rawDefaultLang);\n\n const rawLangOverride = getNamedArgument(attr, 'languageOverride');\n const language_override = stripQuotesHelper(rawLangOverride);\n\n const index = new MongoIndex({\n keys,\n ...(unique != null && { unique }),\n ...(sparse != null && { sparse }),\n ...(expireAfterSeconds != null && { expireAfterSeconds }),\n ...(partialFilterExpression != null && { partialFilterExpression }),\n ...(wildcardProjection != null && { wildcardProjection }),\n ...(collation != null && { collation }),\n ...(weights != null && { weights }),\n ...(default_language != null && { default_language }),\n ...(language_override != null && { language_override }),\n });\n\n indexes.push(index);\n indexSpans.set(index, attr.span);\n }\n\n return indexes;\n}\n\nfunction isRelationField(field: PslField, modelNames: ReadonlySet<string>): boolean {\n return modelNames.has(field.typeName);\n}\n\nfunction resolveFieldCodecId(\n field: PslField,\n scalarTypeDescriptors: ReadonlyMap<string, string>,\n): string | undefined {\n return scalarTypeDescriptors.get(field.typeName);\n}\n\nfunction resolveNonRelationField(\n field: PslField,\n ownerName: string,\n compositeTypeNames: ReadonlySet<string>,\n scalarTypeDescriptors: ReadonlyMap<string, string>,\n sourceId: string,\n diagnostics: ContractSourceDiagnostic[],\n): ContractField | undefined {\n if (compositeTypeNames.has(field.typeName)) {\n const result: ContractField = {\n type: { kind: 'valueObject', name: field.typeName },\n nullable: field.optional,\n };\n return field.list ? { ...result, many: true } : result;\n }\n\n const codecId = resolveFieldCodecId(field, scalarTypeDescriptors);\n if (!codecId) {\n diagnostics.push({\n code: 'PSL_UNSUPPORTED_FIELD_TYPE',\n message: `Field \"${ownerName}.${field.name}\" type \"${field.typeName}\" is not supported in Mongo PSL interpreter`,\n sourceId,\n span: field.span,\n });\n return undefined;\n }\n\n const result: ContractField = {\n type: { kind: 'scalar', codecId },\n nullable: field.optional,\n };\n return field.list ? { ...result, many: true } : result;\n}\n\nexport function interpretPslDocumentToMongoContract(\n input: InterpretPslDocumentToMongoContractInput,\n): Result<Contract, ContractSourceDiagnostics> {\n const { document, scalarTypeDescriptors, codecLookup } = input;\n const sourceId = document.ast.sourceId;\n const diagnostics: ContractSourceDiagnostic[] = [];\n validateNamespaceBlocksForMongoTarget({\n namespaces: document.ast.namespaces,\n sourceId,\n diagnostics,\n });\n // Mongo lowers only the implicit `__unspecified__` bucket today —\n // explicit `namespace { … }` blocks were rejected above. The IR\n // collection map remains flat (and the Mongo target represents\n // databases via `MongoTargetDatabase`, populated at compose time by\n // the connection string rather than authoring-time PSL).\n const allModels = document.ast.namespaces.flatMap((ns) => ns.models);\n const allCompositeTypes = document.ast.namespaces.flatMap((ns) => ns.compositeTypes);\n const modelNames = new Set(allModels.map((m) => m.name));\n const compositeTypeNames = new Set(allCompositeTypes.map((ct) => ct.name));\n\n const models: Record<string, MongoModelEntry> = {};\n const collections: Record<string, Record<string, unknown>> = {};\n const roots: Record<string, string> = {};\n const allFkRelations: FkRelation[] = [];\n const indexSpans = new Map<MongoIndex, PslSpan>();\n const modelIndexesByName = new Map<string, readonly MongoIndex[]>();\n\n interface BackrelationCandidate {\n readonly modelName: string;\n readonly fieldName: string;\n readonly targetModelName: string;\n readonly relationName?: string;\n readonly cardinality: '1:1' | '1:N';\n readonly field: PslField;\n }\n const backrelationCandidates: BackrelationCandidate[] = [];\n\n for (const pslModel of allModels) {\n const collectionName = resolveCollectionName(pslModel);\n const fieldMappings = resolveFieldMappings(pslModel);\n\n const fields: Record<string, ContractField> = {};\n const relations: Record<string, ContractReferenceRelation> = {};\n\n for (const field of pslModel.fields) {\n if (isRelationField(field, modelNames)) {\n const relation = parseRelationAttribute(field.attributes);\n\n if (field.list || !(relation?.fields && relation?.references)) {\n backrelationCandidates.push({\n modelName: pslModel.name,\n fieldName: field.name,\n targetModelName: field.typeName,\n ...(relation?.relationName !== undefined\n ? { relationName: relation.relationName }\n : {}),\n cardinality: field.list ? '1:N' : '1:1',\n field,\n });\n continue;\n }\n\n if (relation?.fields && relation?.references) {\n const localMapped = relation.fields.map((f) => fieldMappings.pslNameToMapped.get(f) ?? f);\n\n const targetModel = allModels.find((m) => m.name === field.typeName);\n const targetFieldMappings = targetModel ? resolveFieldMappings(targetModel) : undefined;\n const targetMapped = relation.references.map(\n (f) => targetFieldMappings?.pslNameToMapped.get(f) ?? f,\n );\n\n relations[field.name] = {\n to: field.typeName,\n cardinality: 'N:1' as const,\n on: {\n localFields: localMapped,\n targetFields: targetMapped,\n },\n };\n\n allFkRelations.push({\n declaringModel: pslModel.name,\n fieldName: field.name,\n targetModel: field.typeName,\n ...(relation.relationName !== undefined ? { relationName: relation.relationName } : {}),\n localFields: localMapped,\n targetFields: targetMapped,\n });\n }\n continue;\n }\n\n const resolved = resolveNonRelationField(\n field,\n pslModel.name,\n compositeTypeNames,\n scalarTypeDescriptors,\n sourceId,\n diagnostics,\n );\n if (!resolved) continue;\n\n const mappedName = fieldMappings.pslNameToMapped.get(field.name) ?? field.name;\n fields[mappedName] = resolved;\n }\n\n const isVariantModel = pslModel.attributes.some((attr) => attr.name === 'base');\n const hasIdField = pslModel.fields.some((f) => getAttribute(f.attributes, 'id') !== undefined);\n if (!hasIdField && !isVariantModel) {\n diagnostics.push({\n code: 'PSL_MISSING_ID_FIELD',\n message: `Model \"${pslModel.name}\" has no field with @id attribute. Every model must have exactly one @id field.`,\n sourceId,\n });\n }\n\n models[pslModel.name] = { fields, relations, storage: { collection: collectionName } };\n const modelIndexes = collectIndexes(\n pslModel,\n fieldMappings,\n modelNames,\n sourceId,\n diagnostics,\n indexSpans,\n );\n modelIndexesByName.set(pslModel.name, modelIndexes);\n const existingColl = collections[collectionName];\n if (existingColl && modelIndexes.length > 0) {\n const existingIndexes = (existingColl['indexes'] ?? []) as MongoIndex[];\n collections[collectionName] = { indexes: [...existingIndexes, ...modelIndexes] };\n } else if (!existingColl) {\n collections[collectionName] = modelIndexes.length > 0 ? { indexes: modelIndexes } : {};\n }\n roots[collectionName] = pslModel.name;\n }\n\n const valueObjects: Record<string, ContractValueObject> = {};\n for (const compositeType of allCompositeTypes) {\n const fields: Record<string, ContractField> = {};\n for (const field of compositeType.fields) {\n const resolved = resolveNonRelationField(\n field,\n compositeType.name,\n compositeTypeNames,\n scalarTypeDescriptors,\n sourceId,\n diagnostics,\n );\n if (!resolved) continue;\n fields[field.name] = resolved;\n }\n valueObjects[compositeType.name] = { fields };\n }\n\n const fkRelationsByPair = new Map<string, FkRelation[]>();\n for (const fk of allFkRelations) {\n const key = fkRelationPairKey(fk.declaringModel, fk.targetModel);\n const existing = fkRelationsByPair.get(key);\n if (existing) {\n existing.push(fk);\n } else {\n fkRelationsByPair.set(key, [fk]);\n }\n }\n\n for (const candidate of backrelationCandidates) {\n const pairKey = fkRelationPairKey(candidate.targetModelName, candidate.modelName);\n const pairMatches = fkRelationsByPair.get(pairKey) ?? [];\n const matches = candidate.relationName\n ? pairMatches.filter((r) => r.relationName === candidate.relationName)\n : [...pairMatches];\n\n if (matches.length === 0) {\n diagnostics.push({\n code: 'PSL_ORPHANED_BACKRELATION',\n message: `Backrelation list field \"${candidate.modelName}.${candidate.fieldName}\" has no matching FK-side relation on model \"${candidate.targetModelName}\". Add @relation(fields: [...], references: [...]) on the FK-side relation or use an explicit join model for many-to-many.`,\n sourceId,\n span: candidate.field.span,\n });\n continue;\n }\n if (matches.length > 1) {\n diagnostics.push({\n code: 'PSL_AMBIGUOUS_BACKRELATION',\n message: `Backrelation list field \"${candidate.modelName}.${candidate.fieldName}\" matches multiple FK-side relations on model \"${candidate.targetModelName}\". Add @relation(\"...\") to both sides to disambiguate.`,\n sourceId,\n span: candidate.field.span,\n });\n continue;\n }\n\n const fk = matches[0];\n if (!fk) continue;\n const modelEntry = models[candidate.modelName];\n if (!modelEntry) continue;\n modelEntry.relations[candidate.fieldName] = {\n to: candidate.targetModelName,\n cardinality: candidate.cardinality,\n on: {\n localFields: fk.targetFields,\n targetFields: fk.localFields,\n },\n };\n }\n\n const { discriminatorDeclarations, baseDeclarations } = collectPolymorphismDeclarations(\n document,\n sourceId,\n diagnostics,\n );\n const polyResult = resolvePolymorphism({\n models,\n roots,\n collections,\n document,\n discriminatorDeclarations,\n baseDeclarations,\n modelNames,\n indexSpans,\n modelIndexesByName,\n sourceId,\n });\n\n if (diagnostics.length > 0 || polyResult.diagnostics.length > 0) {\n return notOk({\n summary: 'PSL to Mongo contract interpretation failed',\n diagnostics: [...diagnostics, ...polyResult.diagnostics],\n });\n }\n\n const resolvedModels = polyResult.models;\n const resolvedCollections = polyResult.collections;\n\n for (const [, modelEntry] of Object.entries(resolvedModels)) {\n if (modelEntry.base) continue;\n\n const collectionName = modelEntry.storage.collection;\n const coll = resolvedCollections[collectionName];\n if (!coll) continue;\n\n if (modelEntry.discriminator && modelEntry.variants) {\n const variantEntries = Object.entries(modelEntry.variants).map(\n ([variantName, { value }]) => ({\n discriminatorValue: value,\n fields: resolvedModels[variantName]?.fields ?? {},\n }),\n );\n coll['validator'] = derivePolymorphicJsonSchema(\n modelEntry.fields,\n modelEntry.discriminator.field,\n variantEntries,\n valueObjects,\n codecLookup,\n );\n } else {\n coll['validator'] = deriveJsonSchema(modelEntry.fields, valueObjects, codecLookup);\n }\n }\n\n const target = 'mongo';\n const targetFamily = 'mongo';\n const collectionsAsClasses: Record<string, MongoCollection> = {};\n for (const [name, coll] of Object.entries(resolvedCollections)) {\n const input: {\n indexes?: ReadonlyArray<MongoIndex>;\n validator?: unknown;\n options?: unknown;\n } = {};\n if (coll['indexes'] !== undefined) {\n input.indexes = coll['indexes'] as ReadonlyArray<MongoIndex>;\n }\n if (coll['validator'] !== undefined) {\n input.validator = coll['validator'];\n }\n if (coll['options'] !== undefined) {\n input.options = coll['options'];\n }\n // input.validator/options are arktype-validated JSON shapes; MongoCollection\n // constructor normalises them into MongoValidator / MongoCollectionOptions\n // instances. The narrow cast is bounded to the field-typed input record.\n collectionsAsClasses[name] = new MongoCollection(\n input as ConstructorParameters<typeof MongoCollection>[0],\n );\n }\n const storageWithoutHash = {\n namespaces: {\n [UNBOUND_NAMESPACE_ID]: {\n id: UNBOUND_NAMESPACE_ID,\n collections: collectionsAsClasses,\n },\n },\n };\n const storageHash = computeStorageHash({ target, targetFamily, storage: storageWithoutHash });\n const capabilities: Record<string, Record<string, boolean>> = {};\n\n return ok({\n targetFamily,\n target,\n roots: polyResult.roots,\n models: polyResult.models,\n ...(Object.keys(valueObjects).length > 0 ? { valueObjects } : {}),\n storage: { ...storageWithoutHash, storageHash },\n extensionPacks: {},\n capabilities,\n profileHash: computeProfileHash({ target, targetFamily, capabilities }),\n meta: {},\n });\n}\n"],"mappings":";;;;;;AAIA,SAAS,gBACP,SACA,aACoB;CACpB,OAAO,aAAa,eAAe,QAAQ,GAAG;;AAGhD,SAAS,kBACP,OACA,cACA,aACqC;CACrC,IAAI,MAAM,KAAK,SAAS,UAAU;EAChC,MAAM,WAAW,gBAAgB,MAAM,KAAK,SAAS,YAAY;EACjE,IAAI,CAAC,UAAU,OAAO,KAAA;EAEtB,IAAI,UAAU,SAAS,MAAM,MAC3B,OAAO;GAAE,UAAU;GAAS,OAAO,EAAE,UAAU;GAAE;EAGnD,IAAI,MAAM,UACR,OAAO,EAAE,UAAU,CAAC,QAAQ,SAAS,EAAE;EAGzC,OAAO,EAAE,UAAU;;CAGrB,IAAI,MAAM,KAAK,SAAS,eAAe;EACrC,MAAM,KAAK,eAAe,MAAM,KAAK;EACrC,IAAI,CAAC,IAAI,OAAO,KAAA;EAChB,MAAM,WAAW,mBAAmB,GAAG,QAAQ,cAAc,YAAY;EACzE,IAAI,UAAU,SAAS,MAAM,MAC3B,OAAO;GAAE,UAAU;GAAS,OAAO;GAAU;EAE/C,IAAI,MAAM,UACR,OAAO,EAAE,OAAO,CAAC,EAAE,UAAU,QAAQ,EAAE,SAAS,EAAE;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Q;GAAc;GAAc,CAAC;EACvE,MAAM,EAAE;EACT,CAAC"}
package/package.json CHANGED
@@ -1,22 +1,22 @@
1
1
  {
2
2
  "name": "@prisma-next/mongo-contract-psl",
3
- "version": "0.9.0",
3
+ "version": "0.10.0-dev.1",
4
4
  "license": "Apache-2.0",
5
5
  "type": "module",
6
6
  "sideEffects": false,
7
7
  "description": "PSL-to-Mongo ContractIR interpreter for Prisma Next",
8
8
  "dependencies": {
9
- "@prisma-next/config": "0.9.0",
10
- "@prisma-next/contract": "0.9.0",
11
- "@prisma-next/framework-components": "0.9.0",
12
- "@prisma-next/mongo-contract": "0.9.0",
13
- "@prisma-next/psl-parser": "0.9.0",
14
- "@prisma-next/utils": "0.9.0",
9
+ "@prisma-next/config": "0.10.0-dev.1",
10
+ "@prisma-next/contract": "0.10.0-dev.1",
11
+ "@prisma-next/framework-components": "0.10.0-dev.1",
12
+ "@prisma-next/mongo-contract": "0.10.0-dev.1",
13
+ "@prisma-next/psl-parser": "0.10.0-dev.1",
14
+ "@prisma-next/utils": "0.10.0-dev.1",
15
15
  "pathe": "^2.0.3"
16
16
  },
17
17
  "devDependencies": {
18
- "@prisma-next/tsconfig": "0.9.0",
19
- "@prisma-next/tsdown": "0.9.0",
18
+ "@prisma-next/tsconfig": "0.10.0-dev.1",
19
+ "@prisma-next/tsdown": "0.10.0-dev.1",
20
20
  "tsdown": "0.22.0",
21
21
  "typescript": "5.9.3",
22
22
  "vitest": "4.1.6"
@@ -10,13 +10,20 @@ import type {
10
10
  ContractValueObject,
11
11
  } from '@prisma-next/contract/types';
12
12
  import type { CodecLookup } from '@prisma-next/framework-components/codec';
13
+ import { UNBOUND_NAMESPACE_ID } from '@prisma-next/framework-components/ir';
13
14
  import {
14
15
  applyPolymorphicScopeToMongoIndex,
15
16
  MongoCollection,
16
17
  MongoIndex,
17
18
  type MongoIndexKeyDirection,
18
19
  } from '@prisma-next/mongo-contract';
19
- import type { ParsePslDocumentResult, PslField, PslModel, PslSpan } from '@prisma-next/psl-parser';
20
+ import type {
21
+ ParsePslDocumentResult,
22
+ PslField,
23
+ PslModel,
24
+ PslNamespace,
25
+ PslSpan,
26
+ } from '@prisma-next/psl-parser';
20
27
  import { notOk, ok, type Result } from '@prisma-next/utils/result';
21
28
  import { deriveJsonSchema, derivePolymorphicJsonSchema } from './derive-json-schema';
22
29
  import {
@@ -36,6 +43,40 @@ export interface InterpretPslDocumentToMongoContractInput {
36
43
  readonly codecLookup?: CodecLookup;
37
44
  }
38
45
 
46
+ /**
47
+ * Name of the framework-parser synthesised bucket for top-level
48
+ * declarations. Re-declared locally so the interpreter does not have to
49
+ * import from `@prisma-next/framework-components/psl-ast`.
50
+ */
51
+ const UNSPECIFIED_PSL_NAMESPACE_NAME = '__unspecified__';
52
+
53
+ /**
54
+ * Mongo FR16c validation: Mongo's authoring DSL exposes the connection's
55
+ * database as the only namespace surface today, so the PSL interpreter
56
+ * rejects every explicit `namespace { … }` block. The implicit
57
+ * `__unspecified__` bucket (top-level declarations) is the only
58
+ * namespace Mongo accepts. `namespace unbound { … }` is rejected too —
59
+ * Mongo has no late-binding namespace concept on the PSL surface (the
60
+ * database name comes from the connection string, not from PSL).
61
+ */
62
+ function validateNamespaceBlocksForMongoTarget(input: {
63
+ readonly namespaces: readonly PslNamespace[];
64
+ readonly sourceId: string;
65
+ readonly diagnostics: ContractSourceDiagnostic[];
66
+ }): void {
67
+ for (const namespace of input.namespaces) {
68
+ if (namespace.name === UNSPECIFIED_PSL_NAMESPACE_NAME) {
69
+ continue;
70
+ }
71
+ input.diagnostics.push({
72
+ code: 'PSL_UNSUPPORTED_NAMESPACE_BLOCK',
73
+ message: `Mongo does not support \`namespace ${namespace.name} { … }\` blocks (the database is bound by the connection string; declare models at the document top level instead).`,
74
+ sourceId: input.sourceId,
75
+ span: namespace.span,
76
+ });
77
+ }
78
+ }
79
+
39
80
  interface FieldMappings {
40
81
  readonly pslNameToMapped: Map<string, string>;
41
82
  }
@@ -93,8 +134,9 @@ function collectPolymorphismDeclarations(
93
134
  } {
94
135
  const discriminatorDeclarations = new Map<string, DiscriminatorDeclaration>();
95
136
  const baseDeclarations = new Map<string, BaseDeclaration>();
137
+ const allPslModels = document.ast.namespaces.flatMap((ns) => ns.models);
96
138
 
97
- for (const pslModel of document.ast.models) {
139
+ for (const pslModel of allPslModels) {
98
140
  for (const attr of pslModel.attributes) {
99
141
  if (attr.name === 'discriminator') {
100
142
  const fieldName = getPositionalArgument(attr);
@@ -176,6 +218,7 @@ function resolvePolymorphism(input: {
176
218
  indexSpans,
177
219
  modelIndexesByName,
178
220
  } = input;
221
+ const allPslModels = document.ast.namespaces.flatMap((ns) => ns.models);
179
222
  let patched = input.models;
180
223
  let roots = input.roots;
181
224
  let collections = input.collections;
@@ -195,7 +238,7 @@ function resolvePolymorphism(input: {
195
238
  const model = patched[modelName];
196
239
  if (!model) continue;
197
240
 
198
- const pslModel = document.ast.models.find((m) => m.name === modelName);
241
+ const pslModel = allPslModels.find((m) => m.name === modelName);
199
242
  const mappedDiscriminatorField = pslModel
200
243
  ? (resolveFieldMappings(pslModel).pslNameToMapped.get(decl.fieldName) ?? decl.fieldName)
201
244
  : decl.fieldName;
@@ -258,7 +301,7 @@ function resolvePolymorphism(input: {
258
301
  }
259
302
 
260
303
  const baseModel = patched[baseDecl.baseName];
261
- const variantPslModel = document.ast.models.find((m) => m.name === variantName);
304
+ const variantPslModel = allPslModels.find((m) => m.name === variantName);
262
305
  if (!variantPslModel) continue;
263
306
  const hasExplicitMap = getMapName(variantPslModel.attributes) !== undefined;
264
307
 
@@ -797,8 +840,20 @@ export function interpretPslDocumentToMongoContract(
797
840
  const { document, scalarTypeDescriptors, codecLookup } = input;
798
841
  const sourceId = document.ast.sourceId;
799
842
  const diagnostics: ContractSourceDiagnostic[] = [];
800
- const modelNames = new Set(document.ast.models.map((m) => m.name));
801
- const compositeTypeNames = new Set(document.ast.compositeTypes.map((ct) => ct.name));
843
+ validateNamespaceBlocksForMongoTarget({
844
+ namespaces: document.ast.namespaces,
845
+ sourceId,
846
+ diagnostics,
847
+ });
848
+ // Mongo lowers only the implicit `__unspecified__` bucket today —
849
+ // explicit `namespace { … }` blocks were rejected above. The IR
850
+ // collection map remains flat (and the Mongo target represents
851
+ // databases via `MongoTargetDatabase`, populated at compose time by
852
+ // the connection string rather than authoring-time PSL).
853
+ const allModels = document.ast.namespaces.flatMap((ns) => ns.models);
854
+ const allCompositeTypes = document.ast.namespaces.flatMap((ns) => ns.compositeTypes);
855
+ const modelNames = new Set(allModels.map((m) => m.name));
856
+ const compositeTypeNames = new Set(allCompositeTypes.map((ct) => ct.name));
802
857
 
803
858
  const models: Record<string, MongoModelEntry> = {};
804
859
  const collections: Record<string, Record<string, unknown>> = {};
@@ -817,7 +872,7 @@ export function interpretPslDocumentToMongoContract(
817
872
  }
818
873
  const backrelationCandidates: BackrelationCandidate[] = [];
819
874
 
820
- for (const pslModel of document.ast.models) {
875
+ for (const pslModel of allModels) {
821
876
  const collectionName = resolveCollectionName(pslModel);
822
877
  const fieldMappings = resolveFieldMappings(pslModel);
823
878
 
@@ -845,7 +900,7 @@ export function interpretPslDocumentToMongoContract(
845
900
  if (relation?.fields && relation?.references) {
846
901
  const localMapped = relation.fields.map((f) => fieldMappings.pslNameToMapped.get(f) ?? f);
847
902
 
848
- const targetModel = document.ast.models.find((m) => m.name === field.typeName);
903
+ const targetModel = allModels.find((m) => m.name === field.typeName);
849
904
  const targetFieldMappings = targetModel ? resolveFieldMappings(targetModel) : undefined;
850
905
  const targetMapped = relation.references.map(
851
906
  (f) => targetFieldMappings?.pslNameToMapped.get(f) ?? f,
@@ -917,7 +972,7 @@ export function interpretPslDocumentToMongoContract(
917
972
  }
918
973
 
919
974
  const valueObjects: Record<string, ContractValueObject> = {};
920
- for (const compositeType of document.ast.compositeTypes) {
975
+ for (const compositeType of allCompositeTypes) {
921
976
  const fields: Record<string, ContractField> = {};
922
977
  for (const field of compositeType.fields) {
923
978
  const resolved = resolveNonRelationField(
@@ -1064,7 +1119,14 @@ export function interpretPslDocumentToMongoContract(
1064
1119
  input as ConstructorParameters<typeof MongoCollection>[0],
1065
1120
  );
1066
1121
  }
1067
- const storageWithoutHash = { collections: collectionsAsClasses };
1122
+ const storageWithoutHash = {
1123
+ namespaces: {
1124
+ [UNBOUND_NAMESPACE_ID]: {
1125
+ id: UNBOUND_NAMESPACE_ID,
1126
+ collections: collectionsAsClasses,
1127
+ },
1128
+ },
1129
+ };
1068
1130
  const storageHash = computeStorageHash({ target, targetFamily, storage: storageWithoutHash });
1069
1131
  const capabilities: Record<string, Record<string, boolean>> = {};
1070
1132
 
@@ -1 +0,0 @@
1
- {"version":3,"file":"interpreter-DgpmmXva.mjs","names":[],"sources":["../src/derive-json-schema.ts","../src/psl-helpers.ts","../src/interpreter.ts"],"sourcesContent":["import type { ContractField, ContractValueObject } from '@prisma-next/contract/types';\nimport type { CodecLookup } from '@prisma-next/framework-components/codec';\nimport { MongoValidator } from '@prisma-next/mongo-contract';\n\nfunction resolveBsonType(\n codecId: string,\n codecLookup: CodecLookup | undefined,\n): string | undefined {\n return codecLookup?.targetTypesFor(codecId)?.[0];\n}\n\nfunction fieldToBsonSchema(\n field: ContractField,\n valueObjects: Record<string, ContractValueObject> | undefined,\n codecLookup: CodecLookup | undefined,\n): Record<string, unknown> | undefined {\n if (field.type.kind === 'scalar') {\n const bsonType = resolveBsonType(field.type.codecId, codecLookup);\n if (!bsonType) return undefined;\n\n if ('many' in field && field.many) {\n return { bsonType: 'array', items: { bsonType } };\n }\n\n if (field.nullable) {\n return { bsonType: ['null', bsonType] };\n }\n\n return { bsonType };\n }\n\n if (field.type.kind === 'valueObject') {\n const vo = valueObjects?.[field.type.name];\n if (!vo) return undefined;\n const voSchema = deriveObjectSchema(vo.fields, valueObjects, codecLookup);\n if ('many' in field && field.many) {\n return { bsonType: 'array', items: voSchema };\n }\n if (field.nullable) {\n return { oneOf: [{ bsonType: 'null' }, voSchema] };\n }\n return voSchema;\n }\n\n return undefined;\n}\n\nfunction deriveObjectSchema(\n fields: Record<string, ContractField>,\n valueObjects: Record<string, ContractValueObject> | undefined,\n codecLookup: CodecLookup | undefined,\n): Record<string, unknown> {\n const properties: Record<string, unknown> = {};\n const required: string[] = [];\n\n for (const [fieldName, field] of Object.entries(fields)) {\n const schema = fieldToBsonSchema(field, valueObjects, codecLookup);\n if (schema) {\n properties[fieldName] = schema;\n if (!field.nullable) {\n required.push(fieldName);\n }\n }\n }\n\n const result: Record<string, unknown> = {\n bsonType: 'object',\n properties,\n };\n if (required.length > 0) {\n result['required'] = required.sort();\n }\n return result;\n}\n\nexport function deriveJsonSchema(\n fields: Record<string, ContractField>,\n valueObjects?: Record<string, ContractValueObject>,\n codecLookup?: CodecLookup,\n): MongoValidator {\n return new MongoValidator({\n jsonSchema: deriveObjectSchema(fields, valueObjects, codecLookup),\n validationLevel: 'strict',\n validationAction: 'error',\n });\n}\n\nexport interface PolymorphicVariant {\n readonly discriminatorValue: string;\n readonly fields: Record<string, ContractField>;\n}\n\nexport function derivePolymorphicJsonSchema(\n baseFields: Record<string, ContractField>,\n discriminatorField: string,\n variants: readonly PolymorphicVariant[],\n valueObjects?: Record<string, ContractValueObject>,\n codecLookup?: CodecLookup,\n): MongoValidator {\n const baseSchema = deriveObjectSchema(baseFields, valueObjects, codecLookup);\n\n const oneOf: Record<string, unknown>[] = [];\n for (const variant of variants) {\n const variantOnlyFields: Record<string, ContractField> = {};\n for (const [name, field] of Object.entries(variant.fields)) {\n if (!(name in baseFields)) {\n variantOnlyFields[name] = field;\n }\n }\n\n const entry: Record<string, unknown> = {\n properties: {\n [discriminatorField]: { enum: [variant.discriminatorValue] },\n },\n };\n\n const variantProperties: Record<string, unknown> = {};\n const variantRequired: string[] = [discriminatorField];\n for (const [name, field] of Object.entries(variantOnlyFields)) {\n const schema = fieldToBsonSchema(field, valueObjects, codecLookup);\n if (schema) {\n variantProperties[name] = schema;\n if (!field.nullable) {\n variantRequired.push(name);\n }\n }\n }\n\n if (Object.keys(variantProperties).length > 0) {\n (entry['properties'] as Record<string, unknown>) = {\n ...(entry['properties'] as Record<string, unknown>),\n ...variantProperties,\n };\n }\n entry['required'] = variantRequired.sort();\n\n oneOf.push(entry);\n }\n\n const jsonSchema = { ...baseSchema };\n if (oneOf.length > 0) {\n jsonSchema['oneOf'] = oneOf;\n }\n\n return new MongoValidator({\n jsonSchema,\n validationLevel: 'strict',\n validationAction: 'error',\n });\n}\n","import type { PslAttribute, PslAttributeArgument } from '@prisma-next/psl-parser';\nimport { getPositionalArgument, parseQuotedStringLiteral } from '@prisma-next/psl-parser';\n\nexport { getPositionalArgument, parseQuotedStringLiteral };\n\nexport function getNamedArgument(attr: PslAttribute, name: string): string | undefined {\n const arg = attr.args.find((a) => a.kind === 'named' && a.name === name);\n return arg?.value;\n}\n\nexport function parseFieldList(value: string): readonly string[] {\n const inner = value.replace(/^\\[/, '').replace(/\\]$/, '').trim();\n if (inner.length === 0) return [];\n return splitTopLevel(inner).map((s) => s.trim());\n}\n\nexport interface ParsedIndexField {\n readonly name: string;\n readonly isWildcard: boolean;\n readonly direction?: number;\n}\n\nexport function parseIndexFieldList(value: string): readonly ParsedIndexField[] {\n const segments = parseFieldList(value);\n return segments.map(parseIndexFieldSegment);\n}\n\nfunction parseIndexFieldSegment(segment: string): ParsedIndexField {\n const wildcardMatch = segment.match(/^wildcard\\(\\s*(.*?)\\s*\\)$/);\n if (wildcardMatch) {\n const scope = wildcardMatch[1] ?? '';\n return {\n name: scope.length > 0 ? `${scope}.$**` : '$**',\n isWildcard: true,\n };\n }\n\n const modifierMatch = segment.match(/^(\\w+)\\(\\s*sort:\\s*(\\w+)\\s*\\)$/);\n if (modifierMatch) {\n const fieldName = modifierMatch[1] ?? segment;\n const sortValue = modifierMatch[2];\n return {\n name: fieldName,\n isWildcard: false,\n direction: sortValue === 'Desc' ? -1 : 1,\n };\n }\n\n return { name: segment, isWildcard: false };\n}\n\nfunction splitTopLevel(input: string): string[] {\n const parts: string[] = [];\n let depth = 0;\n let start = 0;\n for (let i = 0; i < input.length; i++) {\n const ch = input[i];\n if (ch === '(' || ch === '[' || ch === '{') depth++;\n else if (ch === ')' || ch === ']' || ch === '}') depth = Math.max(0, depth - 1);\n else if (ch === ',' && depth === 0) {\n parts.push(input.slice(start, i));\n start = i + 1;\n }\n }\n parts.push(input.slice(start));\n return parts;\n}\n\nexport function lowerFirst(value: string): string {\n if (value.length === 0) return value;\n return value[0]?.toLowerCase() + value.slice(1);\n}\n\nexport function getAttribute(\n attributes: readonly PslAttribute[],\n name: string,\n): PslAttribute | undefined {\n return attributes.find((attr) => attr.name === name);\n}\n\nexport function getMapName(attributes: readonly PslAttribute[]): string | undefined {\n const mapAttr = getAttribute(attributes, 'map');\n if (!mapAttr) return undefined;\n const arg = mapAttr.args[0];\n if (!arg) return undefined;\n return stripQuotes(arg.value);\n}\n\nexport interface ParsedRelationAttribute {\n readonly relationName?: string;\n readonly fields?: readonly string[];\n readonly references?: readonly string[];\n}\n\nexport function parseRelationAttribute(\n attributes: readonly PslAttribute[],\n): ParsedRelationAttribute | undefined {\n const relationAttr = getAttribute(attributes, 'relation');\n if (!relationAttr) return undefined;\n\n let relationName: string | undefined;\n let fieldsArg: PslAttributeArgument | undefined;\n let referencesArg: PslAttributeArgument | undefined;\n\n for (const arg of relationAttr.args) {\n if (arg.kind === 'positional') {\n relationName = stripQuotes(arg.value);\n } else if (arg.name === 'name') {\n relationName = stripQuotes(arg.value);\n } else if (arg.name === 'fields') {\n fieldsArg = arg;\n } else if (arg.name === 'references') {\n referencesArg = arg;\n }\n }\n\n const fields = fieldsArg ? parseFieldList(fieldsArg.value) : undefined;\n const references = referencesArg ? parseFieldList(referencesArg.value) : undefined;\n\n return {\n ...(relationName !== undefined ? { relationName } : {}),\n ...(fields !== undefined ? { fields } : {}),\n ...(references !== undefined ? { references } : {}),\n };\n}\n\nfunction stripQuotes(value: string): string {\n if (value.startsWith('\"') && value.endsWith('\"')) {\n return value.slice(1, -1);\n }\n return value;\n}\n","import type {\n ContractSourceDiagnostic,\n ContractSourceDiagnostics,\n} from '@prisma-next/config/config-types';\nimport { computeProfileHash, computeStorageHash } from '@prisma-next/contract/hashing';\nimport type {\n Contract,\n ContractField,\n ContractReferenceRelation,\n ContractValueObject,\n} from '@prisma-next/contract/types';\nimport type { CodecLookup } from '@prisma-next/framework-components/codec';\nimport {\n applyPolymorphicScopeToMongoIndex,\n MongoCollection,\n MongoIndex,\n type MongoIndexKeyDirection,\n} from '@prisma-next/mongo-contract';\nimport type { ParsePslDocumentResult, PslField, PslModel, PslSpan } from '@prisma-next/psl-parser';\nimport { notOk, ok, type Result } from '@prisma-next/utils/result';\nimport { deriveJsonSchema, derivePolymorphicJsonSchema } from './derive-json-schema';\nimport {\n getAttribute,\n getMapName,\n getNamedArgument,\n getPositionalArgument,\n lowerFirst,\n parseIndexFieldList,\n parseQuotedStringLiteral,\n parseRelationAttribute,\n} from './psl-helpers';\n\nexport interface InterpretPslDocumentToMongoContractInput {\n readonly document: ParsePslDocumentResult;\n readonly scalarTypeDescriptors: ReadonlyMap<string, string>;\n readonly codecLookup?: CodecLookup;\n}\n\ninterface FieldMappings {\n readonly pslNameToMapped: Map<string, string>;\n}\n\ninterface FkRelation {\n readonly declaringModel: string;\n readonly fieldName: string;\n readonly targetModel: string;\n readonly relationName?: string;\n readonly localFields: readonly string[];\n readonly targetFields: readonly string[];\n}\n\nfunction fkRelationPairKey(declaringModel: string, targetModel: string): string {\n return `${declaringModel}::${targetModel}`;\n}\n\nfunction resolveFieldMappings(model: PslModel): FieldMappings {\n const pslNameToMapped = new Map<string, string>();\n for (const field of model.fields) {\n const mapped = getMapName(field.attributes) ?? field.name;\n pslNameToMapped.set(field.name, mapped);\n }\n return { pslNameToMapped };\n}\n\nfunction resolveCollectionName(model: PslModel): string {\n return getMapName(model.attributes) ?? lowerFirst(model.name);\n}\n\ninterface MongoModelEntry {\n readonly fields: Record<string, ContractField>;\n readonly relations: Record<string, ContractReferenceRelation>;\n readonly storage: { readonly collection: string };\n readonly discriminator?: { readonly field: string };\n readonly variants?: Record<string, { readonly value: string }>;\n readonly base?: string;\n}\n\ntype DiscriminatorDeclaration = { readonly fieldName: string; readonly span: PslModel['span'] };\ntype BaseDeclaration = {\n readonly baseName: string;\n readonly value: string;\n readonly collectionName: string;\n readonly span: PslModel['span'];\n};\n\nfunction collectPolymorphismDeclarations(\n document: ParsePslDocumentResult,\n sourceId: string,\n diagnostics: ContractSourceDiagnostic[],\n): {\n discriminatorDeclarations: Map<string, DiscriminatorDeclaration>;\n baseDeclarations: Map<string, BaseDeclaration>;\n} {\n const discriminatorDeclarations = new Map<string, DiscriminatorDeclaration>();\n const baseDeclarations = new Map<string, BaseDeclaration>();\n\n for (const pslModel of document.ast.models) {\n for (const attr of pslModel.attributes) {\n if (attr.name === 'discriminator') {\n const fieldName = getPositionalArgument(attr);\n if (!fieldName) {\n diagnostics.push({\n code: 'PSL_INVALID_ATTRIBUTE_ARGUMENT',\n message: `Model \"${pslModel.name}\" @@discriminator requires a field name argument`,\n sourceId,\n span: attr.span,\n });\n continue;\n }\n const discField = pslModel.fields.find((f) => f.name === fieldName);\n if (discField && discField.typeName !== 'String') {\n diagnostics.push({\n code: 'PSL_INVALID_ATTRIBUTE_ARGUMENT',\n message: `Discriminator field \"${fieldName}\" on model \"${pslModel.name}\" must be of type String, but is \"${discField.typeName}\"`,\n sourceId,\n span: attr.span,\n });\n continue;\n }\n discriminatorDeclarations.set(pslModel.name, { fieldName, span: attr.span });\n }\n if (attr.name === 'base') {\n const baseName = getPositionalArgument(attr, 0);\n const rawValue = getPositionalArgument(attr, 1);\n if (!baseName || !rawValue) {\n diagnostics.push({\n code: 'PSL_INVALID_ATTRIBUTE_ARGUMENT',\n message: `Model \"${pslModel.name}\" @@base requires two arguments: base model name and discriminator value`,\n sourceId,\n span: attr.span,\n });\n continue;\n }\n const value = parseQuotedStringLiteral(rawValue);\n if (value === undefined) {\n diagnostics.push({\n code: 'PSL_INVALID_ATTRIBUTE_ARGUMENT',\n message: `Model \"${pslModel.name}\" @@base discriminator value must be a quoted string literal`,\n sourceId,\n span: attr.span,\n });\n continue;\n }\n const collectionName = resolveCollectionName(pslModel);\n baseDeclarations.set(pslModel.name, { baseName, value, collectionName, span: attr.span });\n }\n }\n }\n\n return { discriminatorDeclarations, baseDeclarations };\n}\n\nfunction resolvePolymorphism(input: {\n models: Record<string, MongoModelEntry>;\n roots: Record<string, string>;\n collections: Record<string, Record<string, unknown>>;\n document: ParsePslDocumentResult;\n discriminatorDeclarations: Map<string, DiscriminatorDeclaration>;\n baseDeclarations: Map<string, BaseDeclaration>;\n modelNames: ReadonlySet<string>;\n indexSpans: Map<MongoIndex, PslSpan>;\n modelIndexesByName: Map<string, readonly MongoIndex[]>;\n sourceId: string;\n}): {\n models: Record<string, MongoModelEntry>;\n roots: Record<string, string>;\n collections: Record<string, Record<string, unknown>>;\n diagnostics: ContractSourceDiagnostic[];\n} {\n const {\n discriminatorDeclarations,\n baseDeclarations,\n modelNames,\n sourceId,\n document,\n indexSpans,\n modelIndexesByName,\n } = input;\n let patched = input.models;\n let roots = input.roots;\n let collections = input.collections;\n const diagnostics: ContractSourceDiagnostic[] = [];\n\n for (const [modelName, decl] of discriminatorDeclarations) {\n if (baseDeclarations.has(modelName)) {\n diagnostics.push({\n code: 'PSL_DISCRIMINATOR_AND_BASE',\n message: `Model \"${modelName}\" cannot have both @@discriminator and @@base`,\n sourceId,\n span: decl.span,\n });\n continue;\n }\n\n const model = patched[modelName];\n if (!model) continue;\n\n const pslModel = document.ast.models.find((m) => m.name === modelName);\n const mappedDiscriminatorField = pslModel\n ? (resolveFieldMappings(pslModel).pslNameToMapped.get(decl.fieldName) ?? decl.fieldName)\n : decl.fieldName;\n\n if (!Object.hasOwn(model.fields, mappedDiscriminatorField)) {\n diagnostics.push({\n code: 'PSL_DISCRIMINATOR_FIELD_NOT_FOUND',\n message: `Discriminator field \"${decl.fieldName}\" is not a field on model \"${modelName}\"`,\n sourceId,\n span: decl.span,\n });\n continue;\n }\n\n const variants: Record<string, { readonly value: string }> = {};\n for (const [variantName, baseDecl] of baseDeclarations) {\n if (baseDecl.baseName !== modelName) continue;\n variants[variantName] = { value: baseDecl.value };\n }\n\n if (Object.keys(variants).length === 0) {\n diagnostics.push({\n code: 'PSL_ORPHANED_DISCRIMINATOR',\n message: `Model \"${modelName}\" has @@discriminator but no variant models declare @@base(${modelName}, ...)`,\n sourceId,\n span: decl.span,\n });\n continue;\n }\n\n patched = {\n ...patched,\n [modelName]: { ...model, discriminator: { field: mappedDiscriminatorField }, variants },\n };\n }\n\n for (const [variantName, baseDecl] of baseDeclarations) {\n if (!modelNames.has(baseDecl.baseName)) {\n diagnostics.push({\n code: 'PSL_BASE_TARGET_NOT_FOUND',\n message: `Model \"${variantName}\" @@base references non-existent model \"${baseDecl.baseName}\"`,\n sourceId,\n span: baseDecl.span,\n });\n continue;\n }\n\n if (!discriminatorDeclarations.has(baseDecl.baseName)) {\n diagnostics.push({\n code: 'PSL_ORPHANED_BASE',\n message: `Model \"${variantName}\" declares @@base(${baseDecl.baseName}, ...) but \"${baseDecl.baseName}\" has no @@discriminator`,\n sourceId,\n span: baseDecl.span,\n });\n continue;\n }\n\n if (discriminatorDeclarations.has(variantName)) {\n continue;\n }\n\n const baseModel = patched[baseDecl.baseName];\n const variantPslModel = document.ast.models.find((m) => m.name === variantName);\n if (!variantPslModel) continue;\n const hasExplicitMap = getMapName(variantPslModel.attributes) !== undefined;\n\n if (hasExplicitMap && baseModel && baseDecl.collectionName !== baseModel.storage.collection) {\n diagnostics.push({\n code: 'PSL_MONGO_VARIANT_SEPARATE_COLLECTION',\n message: `Mongo variant \"${variantName}\" cannot use a different collection than its base \"${baseDecl.baseName}\". Mongo only supports single-collection polymorphism.`,\n sourceId,\n span: baseDecl.span,\n });\n continue;\n }\n\n const baseCollection = baseModel?.storage.collection ?? baseDecl.collectionName;\n const variantModel = patched[variantName];\n if (variantModel) {\n patched = {\n ...patched,\n [variantName]: {\n ...variantModel,\n base: baseDecl.baseName,\n storage: { collection: baseCollection },\n },\n };\n }\n\n const variantCollectionName = resolveCollectionName(variantPslModel);\n if (roots[variantCollectionName] === variantName) {\n if (variantCollectionName === baseCollection && baseModel) {\n roots = { ...roots, [variantCollectionName]: baseDecl.baseName };\n } else {\n roots = Object.fromEntries(\n Object.entries(roots).filter(([key]) => key !== variantCollectionName),\n );\n }\n }\n\n const variantOwnIndexes = modelIndexesByName.get(variantName) ?? [];\n const baseColl = collections[baseCollection];\n\n const baseModelEntry = patched[baseDecl.baseName];\n const discriminatorField = baseModelEntry?.discriminator?.field;\n const scopedVariantIndexes: MongoIndex[] = [];\n if (discriminatorField) {\n for (const idx of variantOwnIndexes) {\n const result = applyPolymorphicScopeToMongoIndex(idx, {\n discriminatorField,\n discriminatorValue: baseDecl.value,\n });\n if (result.kind === 'conflict') {\n const span = indexSpans.get(idx) ?? baseDecl.span;\n diagnostics.push({\n code: 'PSL_INVALID_INDEX',\n message: `Variant \"${variantName}\" index conflicts with discriminator scope: ${result.reason}`,\n sourceId,\n span,\n });\n continue;\n }\n if (result.index !== idx) {\n indexSpans.set(result.index, indexSpans.get(idx) ?? baseDecl.span);\n }\n scopedVariantIndexes.push(result.index);\n }\n } else {\n scopedVariantIndexes.push(...variantOwnIndexes);\n }\n\n if (variantCollectionName !== baseCollection) {\n const filtered = Object.fromEntries(\n Object.entries(collections).filter(([key]) => key !== variantCollectionName),\n );\n if (scopedVariantIndexes.length > 0 && baseColl) {\n const baseIndexes = (baseColl['indexes'] ?? []) as MongoIndex[];\n collections = {\n ...filtered,\n [baseCollection]: {\n ...baseColl,\n indexes: [...baseIndexes, ...scopedVariantIndexes],\n },\n };\n } else {\n collections = filtered;\n }\n } else if (baseColl) {\n const existingIndexes = (baseColl['indexes'] ?? []) as MongoIndex[];\n const variantIndexSet = new Set<MongoIndex>(variantOwnIndexes);\n const withoutUnscopedVariants = existingIndexes.filter((idx) => !variantIndexSet.has(idx));\n const mergedIndexes = [...withoutUnscopedVariants];\n for (const idx of scopedVariantIndexes) {\n const idxKey = canonicalJson(idx);\n const isDuplicate = withoutUnscopedVariants.some(\n (existing) => canonicalJson(existing) === idxKey,\n );\n if (!isDuplicate) {\n mergedIndexes.push(idx);\n }\n }\n if (\n mergedIndexes.length !== existingIndexes.length ||\n mergedIndexes.some((idx, i) => idx !== existingIndexes[i])\n ) {\n const next: Record<string, unknown> = { ...baseColl };\n if (mergedIndexes.length > 0) {\n next['indexes'] = mergedIndexes;\n } else {\n delete next['indexes'];\n }\n collections = { ...collections, [baseCollection]: next };\n }\n }\n }\n\n return { models: patched, roots, collections, diagnostics };\n}\n\n// Property-order-stable serialization for structural equality of plain\n// JSON-compatible values. Used for comparing MongoIndex shapes in\n// the variant-merge dedup path where a future change to the spread order\n// would otherwise produce JSON-stringify mismatches even though the\n// indexes are structurally identical.\nfunction canonicalJson(value: unknown): string {\n if (Array.isArray(value)) {\n return `[${value.map(canonicalJson).join(',')}]`;\n }\n if (value && typeof value === 'object') {\n return `{${Object.entries(value as Record<string, unknown>)\n .sort(([left], [right]) => left.localeCompare(right))\n .map(([key, entry]) => `${JSON.stringify(key)}:${canonicalJson(entry)}`)\n .join(',')}}`;\n }\n return JSON.stringify(value);\n}\n\nfunction parseIndexDirection(raw: string | undefined): MongoIndexKeyDirection {\n if (!raw) return 1;\n const stripped = raw.replace(/^[\"']/, '').replace(/[\"']$/, '');\n const num = Number(stripped);\n if (num === 1 || num === -1) return num;\n if (['text', '2dsphere', '2d', 'hashed'].includes(stripped))\n return stripped as MongoIndexKeyDirection;\n return 1;\n}\n\nfunction parseNumericArg(raw: string | undefined): number | undefined {\n if (!raw) return undefined;\n const n = Number(raw);\n return Number.isFinite(n) ? n : undefined;\n}\n\nfunction parseBooleanArg(raw: string | undefined): boolean | undefined {\n if (raw === 'true') return true;\n if (raw === 'false') return false;\n return undefined;\n}\n\nfunction parseJsonArg(raw: string | undefined): Record<string, unknown> | undefined {\n if (!raw) return undefined;\n const stripped = raw.replace(/^[\"']/, '').replace(/[\"']$/, '').replace(/\\\\\"/g, '\"');\n try {\n const parsed = JSON.parse(stripped);\n if (typeof parsed === 'object' && parsed !== null && !Array.isArray(parsed)) {\n return parsed as Record<string, unknown>;\n }\n } catch {\n // not valid JSON\n }\n return undefined;\n}\n\nfunction parseCollation(\n attr: import('@prisma-next/psl-parser').PslAttribute,\n): Record<string, unknown> | null | undefined {\n const locale = stripQuotesHelper(getNamedArgument(attr, 'collationLocale'));\n if (!locale) {\n const hasAnyCollationArg =\n getNamedArgument(attr, 'collationStrength') != null ||\n getNamedArgument(attr, 'collationCaseLevel') != null ||\n getNamedArgument(attr, 'collationCaseFirst') != null ||\n getNamedArgument(attr, 'collationNumericOrdering') != null ||\n getNamedArgument(attr, 'collationAlternate') != null ||\n getNamedArgument(attr, 'collationMaxVariable') != null ||\n getNamedArgument(attr, 'collationBackwards') != null ||\n getNamedArgument(attr, 'collationNormalization') != null;\n return hasAnyCollationArg ? null : undefined;\n }\n\n const collation: Record<string, unknown> = { locale };\n const strength = parseNumericArg(getNamedArgument(attr, 'collationStrength'));\n if (strength != null) collation['strength'] = strength;\n const caseLevel = parseBooleanArg(getNamedArgument(attr, 'collationCaseLevel'));\n if (caseLevel != null) collation['caseLevel'] = caseLevel;\n const caseFirst = stripQuotesHelper(getNamedArgument(attr, 'collationCaseFirst'));\n if (caseFirst != null) collation['caseFirst'] = caseFirst;\n const numericOrdering = parseBooleanArg(getNamedArgument(attr, 'collationNumericOrdering'));\n if (numericOrdering != null) collation['numericOrdering'] = numericOrdering;\n const alternate = stripQuotesHelper(getNamedArgument(attr, 'collationAlternate'));\n if (alternate != null) collation['alternate'] = alternate;\n const maxVariable = stripQuotesHelper(getNamedArgument(attr, 'collationMaxVariable'));\n if (maxVariable != null) collation['maxVariable'] = maxVariable;\n const backwards = parseBooleanArg(getNamedArgument(attr, 'collationBackwards'));\n if (backwards != null) collation['backwards'] = backwards;\n const normalization = parseBooleanArg(getNamedArgument(attr, 'collationNormalization'));\n if (normalization != null) collation['normalization'] = normalization;\n return collation;\n}\n\nfunction stripQuotesHelper(raw: string | undefined): string | undefined {\n if (!raw) return undefined;\n return raw.replace(/^[\"']/, '').replace(/[\"']$/, '');\n}\n\nfunction parseProjectionList(\n raw: string | undefined,\n value: 0 | 1,\n): Record<string, 0 | 1> | undefined {\n if (!raw) return undefined;\n const stripped = raw.replace(/^[\"']/, '').replace(/[\"']$/, '');\n const inner = stripped.replace(/^\\[/, '').replace(/\\]$/, '').trim();\n if (inner.length === 0) return undefined;\n const fields = inner\n .split(',')\n .map((s) => s.trim())\n .filter((s) => s.length > 0);\n const result: Record<string, 0 | 1> = {};\n for (const f of fields) {\n result[f] = value;\n }\n return result;\n}\n\nfunction collectIndexes(\n pslModel: PslModel,\n fieldMappings: FieldMappings,\n modelNames: ReadonlySet<string>,\n sourceId: string,\n diagnostics: ContractSourceDiagnostic[],\n indexSpans: Map<MongoIndex, PslSpan>,\n): MongoIndex[] {\n const indexes: MongoIndex[] = [];\n let textIndexCount = 0;\n\n // Storage-indexable PSL field names — i.e. all declared fields except\n // relation fields (which don't materialize a column on this model). The\n // index field-existence check (PSL_INDEX_FIELD_NOT_FOUND) consults this\n // rather than fieldMappings.pslNameToMapped because the latter contains\n // every PSL field including relation fields.\n const indexableFieldNames = new Set<string>();\n for (const f of pslModel.fields) {\n if (modelNames.has(f.typeName)) continue;\n indexableFieldNames.add(f.name);\n }\n\n for (const field of pslModel.fields) {\n if (modelNames.has(field.typeName)) continue;\n const uniqueAttr = getAttribute(field.attributes, 'unique');\n if (!uniqueAttr) continue;\n const mappedName = fieldMappings.pslNameToMapped.get(field.name) ?? field.name;\n const fieldUniqueIndex = new MongoIndex({\n keys: [{ field: mappedName, direction: 1 }],\n unique: true,\n });\n indexes.push(fieldUniqueIndex);\n indexSpans.set(fieldUniqueIndex, uniqueAttr.span);\n }\n\n for (const attr of pslModel.attributes) {\n const isIndex = attr.name === 'index';\n const isUnique = attr.name === 'unique';\n const isTextIndex = attr.name === 'textIndex';\n if (!isIndex && !isUnique && !isTextIndex) continue;\n\n const fieldsArg = getPositionalArgument(attr, 0);\n if (!fieldsArg) continue;\n const parsedFields = parseIndexFieldList(fieldsArg);\n if (parsedFields.length === 0) continue;\n\n const hasWildcard = parsedFields.some((f) => f.isWildcard);\n const wildcardCount = parsedFields.filter((f) => f.isWildcard).length;\n\n if (wildcardCount > 1) {\n diagnostics.push({\n code: 'PSL_INVALID_INDEX',\n message: 'An index can contain at most one wildcard() field',\n sourceId,\n span: attr.span,\n });\n continue;\n }\n\n if (isUnique && hasWildcard) {\n diagnostics.push({\n code: 'PSL_INVALID_INDEX',\n message: 'Unique indexes cannot use wildcard() fields',\n sourceId,\n span: attr.span,\n });\n continue;\n }\n\n if (isTextIndex) {\n textIndexCount++;\n if (textIndexCount > 1) {\n diagnostics.push({\n code: 'PSL_INVALID_INDEX',\n message: `Only one @@textIndex is allowed per collection (model \"${pslModel.name}\")`,\n sourceId,\n span: attr.span,\n });\n continue;\n }\n\n if (hasWildcard) {\n diagnostics.push({\n code: 'PSL_INVALID_INDEX',\n message:\n 'wildcard() fields cannot be combined with type: hashed/2dsphere/2d or @@textIndex',\n sourceId,\n span: attr.span,\n });\n continue;\n }\n }\n\n const typeArg = getNamedArgument(attr, 'type');\n const defaultDirection: MongoIndexKeyDirection = isTextIndex\n ? 'text'\n : parseIndexDirection(typeArg);\n\n if (\n hasWildcard &&\n typeof defaultDirection === 'string' &&\n ['hashed', '2dsphere', '2d'].includes(defaultDirection)\n ) {\n diagnostics.push({\n code: 'PSL_INVALID_INDEX',\n message: `wildcard() fields cannot be combined with type: ${defaultDirection}`,\n sourceId,\n span: attr.span,\n });\n continue;\n }\n\n if (defaultDirection === 'hashed' && parsedFields.length > 1) {\n diagnostics.push({\n code: 'PSL_INVALID_INDEX',\n message: 'Hashed indexes must have exactly one field',\n sourceId,\n span: attr.span,\n });\n continue;\n }\n\n let missingField: string | undefined;\n for (const pf of parsedFields) {\n let fieldNameForLookup: string | undefined;\n if (pf.isWildcard) {\n const wildcardMatch = pf.name.match(/^(.+)\\.\\$\\*\\*$/);\n fieldNameForLookup = wildcardMatch ? wildcardMatch[1] : undefined;\n } else {\n fieldNameForLookup = pf.name;\n }\n if (fieldNameForLookup === undefined || fieldNameForLookup.length === 0) continue;\n if (!indexableFieldNames.has(fieldNameForLookup)) {\n missingField = fieldNameForLookup;\n break;\n }\n }\n if (missingField !== undefined) {\n diagnostics.push({\n code: 'PSL_INDEX_FIELD_NOT_FOUND',\n message: `Index on model \"${pslModel.name}\" references unknown field \"${missingField}\"`,\n sourceId,\n span: attr.span,\n });\n continue;\n }\n\n const keys = parsedFields.map((pf) => {\n const mappedName = pf.isWildcard\n ? pf.name.replace(/^(.+)\\.\\$\\*\\*$/, (_, prefix: string) => {\n const mapped = fieldMappings.pslNameToMapped.get(prefix);\n return mapped ? `${mapped}.$**` : `${prefix}.$**`;\n })\n : (fieldMappings.pslNameToMapped.get(pf.name) ?? pf.name);\n const direction: MongoIndexKeyDirection =\n pf.direction != null ? (pf.direction as MongoIndexKeyDirection) : defaultDirection;\n return { field: mappedName, direction };\n });\n\n const unique = isUnique ? true : undefined;\n const sparse = isTextIndex ? undefined : parseBooleanArg(getNamedArgument(attr, 'sparse'));\n const expireAfterSeconds = isTextIndex\n ? undefined\n : parseNumericArg(getNamedArgument(attr, 'expireAfterSeconds'));\n\n if (hasWildcard && expireAfterSeconds != null) {\n diagnostics.push({\n code: 'PSL_INVALID_INDEX',\n message: 'expireAfterSeconds cannot be combined with wildcard() fields',\n sourceId,\n span: attr.span,\n });\n continue;\n }\n\n const partialFilterExpression = parseJsonArg(getNamedArgument(attr, 'filter'));\n\n const includeArg = getNamedArgument(attr, 'include');\n const excludeArg = getNamedArgument(attr, 'exclude');\n\n if (includeArg != null && excludeArg != null) {\n diagnostics.push({\n code: 'PSL_INVALID_INDEX',\n message: 'Cannot specify both include and exclude on the same index',\n sourceId,\n span: attr.span,\n });\n continue;\n }\n\n if ((includeArg != null || excludeArg != null) && !hasWildcard) {\n diagnostics.push({\n code: 'PSL_INVALID_INDEX',\n message:\n 'include/exclude options are only valid when the index contains a wildcard() field',\n sourceId,\n span: attr.span,\n });\n continue;\n }\n\n const wildcardProjection =\n includeArg != null\n ? parseProjectionList(includeArg, 1)\n : excludeArg != null\n ? parseProjectionList(excludeArg, 0)\n : undefined;\n\n const collation = parseCollation(attr);\n if (collation === null) {\n diagnostics.push({\n code: 'PSL_INVALID_INDEX',\n message: 'collationLocale is required when using collation options',\n sourceId,\n span: attr.span,\n });\n continue;\n }\n\n const rawWeights = parseJsonArg(getNamedArgument(attr, 'weights'));\n let weights: Record<string, number> | undefined;\n if (rawWeights) {\n weights = {};\n for (const [k, v] of Object.entries(rawWeights)) {\n if (typeof v === 'number') weights[k] = v;\n }\n }\n\n const rawDefaultLang = isTextIndex\n ? getNamedArgument(attr, 'language')\n : getNamedArgument(attr, 'default_language');\n const default_language = stripQuotesHelper(rawDefaultLang);\n\n const rawLangOverride = getNamedArgument(attr, 'languageOverride');\n const language_override = stripQuotesHelper(rawLangOverride);\n\n const index = new MongoIndex({\n keys,\n ...(unique != null && { unique }),\n ...(sparse != null && { sparse }),\n ...(expireAfterSeconds != null && { expireAfterSeconds }),\n ...(partialFilterExpression != null && { partialFilterExpression }),\n ...(wildcardProjection != null && { wildcardProjection }),\n ...(collation != null && { collation }),\n ...(weights != null && { weights }),\n ...(default_language != null && { default_language }),\n ...(language_override != null && { language_override }),\n });\n\n indexes.push(index);\n indexSpans.set(index, attr.span);\n }\n\n return indexes;\n}\n\nfunction isRelationField(field: PslField, modelNames: ReadonlySet<string>): boolean {\n return modelNames.has(field.typeName);\n}\n\nfunction resolveFieldCodecId(\n field: PslField,\n scalarTypeDescriptors: ReadonlyMap<string, string>,\n): string | undefined {\n return scalarTypeDescriptors.get(field.typeName);\n}\n\nfunction resolveNonRelationField(\n field: PslField,\n ownerName: string,\n compositeTypeNames: ReadonlySet<string>,\n scalarTypeDescriptors: ReadonlyMap<string, string>,\n sourceId: string,\n diagnostics: ContractSourceDiagnostic[],\n): ContractField | undefined {\n if (compositeTypeNames.has(field.typeName)) {\n const result: ContractField = {\n type: { kind: 'valueObject', name: field.typeName },\n nullable: field.optional,\n };\n return field.list ? { ...result, many: true } : result;\n }\n\n const codecId = resolveFieldCodecId(field, scalarTypeDescriptors);\n if (!codecId) {\n diagnostics.push({\n code: 'PSL_UNSUPPORTED_FIELD_TYPE',\n message: `Field \"${ownerName}.${field.name}\" type \"${field.typeName}\" is not supported in Mongo PSL interpreter`,\n sourceId,\n span: field.span,\n });\n return undefined;\n }\n\n const result: ContractField = {\n type: { kind: 'scalar', codecId },\n nullable: field.optional,\n };\n return field.list ? { ...result, many: true } : result;\n}\n\nexport function interpretPslDocumentToMongoContract(\n input: InterpretPslDocumentToMongoContractInput,\n): Result<Contract, ContractSourceDiagnostics> {\n const { document, scalarTypeDescriptors, codecLookup } = input;\n const sourceId = document.ast.sourceId;\n const diagnostics: ContractSourceDiagnostic[] = [];\n const modelNames = new Set(document.ast.models.map((m) => m.name));\n const compositeTypeNames = new Set(document.ast.compositeTypes.map((ct) => ct.name));\n\n const models: Record<string, MongoModelEntry> = {};\n const collections: Record<string, Record<string, unknown>> = {};\n const roots: Record<string, string> = {};\n const allFkRelations: FkRelation[] = [];\n const indexSpans = new Map<MongoIndex, PslSpan>();\n const modelIndexesByName = new Map<string, readonly MongoIndex[]>();\n\n interface BackrelationCandidate {\n readonly modelName: string;\n readonly fieldName: string;\n readonly targetModelName: string;\n readonly relationName?: string;\n readonly cardinality: '1:1' | '1:N';\n readonly field: PslField;\n }\n const backrelationCandidates: BackrelationCandidate[] = [];\n\n for (const pslModel of document.ast.models) {\n const collectionName = resolveCollectionName(pslModel);\n const fieldMappings = resolveFieldMappings(pslModel);\n\n const fields: Record<string, ContractField> = {};\n const relations: Record<string, ContractReferenceRelation> = {};\n\n for (const field of pslModel.fields) {\n if (isRelationField(field, modelNames)) {\n const relation = parseRelationAttribute(field.attributes);\n\n if (field.list || !(relation?.fields && relation?.references)) {\n backrelationCandidates.push({\n modelName: pslModel.name,\n fieldName: field.name,\n targetModelName: field.typeName,\n ...(relation?.relationName !== undefined\n ? { relationName: relation.relationName }\n : {}),\n cardinality: field.list ? '1:N' : '1:1',\n field,\n });\n continue;\n }\n\n if (relation?.fields && relation?.references) {\n const localMapped = relation.fields.map((f) => fieldMappings.pslNameToMapped.get(f) ?? f);\n\n const targetModel = document.ast.models.find((m) => m.name === field.typeName);\n const targetFieldMappings = targetModel ? resolveFieldMappings(targetModel) : undefined;\n const targetMapped = relation.references.map(\n (f) => targetFieldMappings?.pslNameToMapped.get(f) ?? f,\n );\n\n relations[field.name] = {\n to: field.typeName,\n cardinality: 'N:1' as const,\n on: {\n localFields: localMapped,\n targetFields: targetMapped,\n },\n };\n\n allFkRelations.push({\n declaringModel: pslModel.name,\n fieldName: field.name,\n targetModel: field.typeName,\n ...(relation.relationName !== undefined ? { relationName: relation.relationName } : {}),\n localFields: localMapped,\n targetFields: targetMapped,\n });\n }\n continue;\n }\n\n const resolved = resolveNonRelationField(\n field,\n pslModel.name,\n compositeTypeNames,\n scalarTypeDescriptors,\n sourceId,\n diagnostics,\n );\n if (!resolved) continue;\n\n const mappedName = fieldMappings.pslNameToMapped.get(field.name) ?? field.name;\n fields[mappedName] = resolved;\n }\n\n const isVariantModel = pslModel.attributes.some((attr) => attr.name === 'base');\n const hasIdField = pslModel.fields.some((f) => getAttribute(f.attributes, 'id') !== undefined);\n if (!hasIdField && !isVariantModel) {\n diagnostics.push({\n code: 'PSL_MISSING_ID_FIELD',\n message: `Model \"${pslModel.name}\" has no field with @id attribute. Every model must have exactly one @id field.`,\n sourceId,\n });\n }\n\n models[pslModel.name] = { fields, relations, storage: { collection: collectionName } };\n const modelIndexes = collectIndexes(\n pslModel,\n fieldMappings,\n modelNames,\n sourceId,\n diagnostics,\n indexSpans,\n );\n modelIndexesByName.set(pslModel.name, modelIndexes);\n const existingColl = collections[collectionName];\n if (existingColl && modelIndexes.length > 0) {\n const existingIndexes = (existingColl['indexes'] ?? []) as MongoIndex[];\n collections[collectionName] = { indexes: [...existingIndexes, ...modelIndexes] };\n } else if (!existingColl) {\n collections[collectionName] = modelIndexes.length > 0 ? { indexes: modelIndexes } : {};\n }\n roots[collectionName] = pslModel.name;\n }\n\n const valueObjects: Record<string, ContractValueObject> = {};\n for (const compositeType of document.ast.compositeTypes) {\n const fields: Record<string, ContractField> = {};\n for (const field of compositeType.fields) {\n const resolved = resolveNonRelationField(\n field,\n compositeType.name,\n compositeTypeNames,\n scalarTypeDescriptors,\n sourceId,\n diagnostics,\n );\n if (!resolved) continue;\n fields[field.name] = resolved;\n }\n valueObjects[compositeType.name] = { fields };\n }\n\n const fkRelationsByPair = new Map<string, FkRelation[]>();\n for (const fk of allFkRelations) {\n const key = fkRelationPairKey(fk.declaringModel, fk.targetModel);\n const existing = fkRelationsByPair.get(key);\n if (existing) {\n existing.push(fk);\n } else {\n fkRelationsByPair.set(key, [fk]);\n }\n }\n\n for (const candidate of backrelationCandidates) {\n const pairKey = fkRelationPairKey(candidate.targetModelName, candidate.modelName);\n const pairMatches = fkRelationsByPair.get(pairKey) ?? [];\n const matches = candidate.relationName\n ? pairMatches.filter((r) => r.relationName === candidate.relationName)\n : [...pairMatches];\n\n if (matches.length === 0) {\n diagnostics.push({\n code: 'PSL_ORPHANED_BACKRELATION',\n message: `Backrelation list field \"${candidate.modelName}.${candidate.fieldName}\" has no matching FK-side relation on model \"${candidate.targetModelName}\". Add @relation(fields: [...], references: [...]) on the FK-side relation or use an explicit join model for many-to-many.`,\n sourceId,\n span: candidate.field.span,\n });\n continue;\n }\n if (matches.length > 1) {\n diagnostics.push({\n code: 'PSL_AMBIGUOUS_BACKRELATION',\n message: `Backrelation list field \"${candidate.modelName}.${candidate.fieldName}\" matches multiple FK-side relations on model \"${candidate.targetModelName}\". Add @relation(\"...\") to both sides to disambiguate.`,\n sourceId,\n span: candidate.field.span,\n });\n continue;\n }\n\n const fk = matches[0];\n if (!fk) continue;\n const modelEntry = models[candidate.modelName];\n if (!modelEntry) continue;\n modelEntry.relations[candidate.fieldName] = {\n to: candidate.targetModelName,\n cardinality: candidate.cardinality,\n on: {\n localFields: fk.targetFields,\n targetFields: fk.localFields,\n },\n };\n }\n\n const { discriminatorDeclarations, baseDeclarations } = collectPolymorphismDeclarations(\n document,\n sourceId,\n diagnostics,\n );\n const polyResult = resolvePolymorphism({\n models,\n roots,\n collections,\n document,\n discriminatorDeclarations,\n baseDeclarations,\n modelNames,\n indexSpans,\n modelIndexesByName,\n sourceId,\n });\n\n if (diagnostics.length > 0 || polyResult.diagnostics.length > 0) {\n return notOk({\n summary: 'PSL to Mongo contract interpretation failed',\n diagnostics: [...diagnostics, ...polyResult.diagnostics],\n });\n }\n\n const resolvedModels = polyResult.models;\n const resolvedCollections = polyResult.collections;\n\n for (const [, modelEntry] of Object.entries(resolvedModels)) {\n if (modelEntry.base) continue;\n\n const collectionName = modelEntry.storage.collection;\n const coll = resolvedCollections[collectionName];\n if (!coll) continue;\n\n if (modelEntry.discriminator && modelEntry.variants) {\n const variantEntries = Object.entries(modelEntry.variants).map(\n ([variantName, { value }]) => ({\n discriminatorValue: value,\n fields: resolvedModels[variantName]?.fields ?? {},\n }),\n );\n coll['validator'] = derivePolymorphicJsonSchema(\n modelEntry.fields,\n modelEntry.discriminator.field,\n variantEntries,\n valueObjects,\n codecLookup,\n );\n } else {\n coll['validator'] = deriveJsonSchema(modelEntry.fields, valueObjects, codecLookup);\n }\n }\n\n const target = 'mongo';\n const targetFamily = 'mongo';\n const collectionsAsClasses: Record<string, MongoCollection> = {};\n for (const [name, coll] of Object.entries(resolvedCollections)) {\n const input: {\n indexes?: ReadonlyArray<MongoIndex>;\n validator?: unknown;\n options?: unknown;\n } = {};\n if (coll['indexes'] !== undefined) {\n input.indexes = coll['indexes'] as ReadonlyArray<MongoIndex>;\n }\n if (coll['validator'] !== undefined) {\n input.validator = coll['validator'];\n }\n if (coll['options'] !== undefined) {\n input.options = coll['options'];\n }\n // input.validator/options are arktype-validated JSON shapes; MongoCollection\n // constructor normalises them into MongoValidator / MongoCollectionOptions\n // instances. The narrow cast is bounded to the field-typed input record.\n collectionsAsClasses[name] = new MongoCollection(\n input as ConstructorParameters<typeof MongoCollection>[0],\n );\n }\n const storageWithoutHash = { collections: collectionsAsClasses };\n const storageHash = computeStorageHash({ target, targetFamily, storage: storageWithoutHash });\n const capabilities: Record<string, Record<string, boolean>> = {};\n\n return ok({\n targetFamily,\n target,\n roots: polyResult.roots,\n models: polyResult.models,\n ...(Object.keys(valueObjects).length > 0 ? { valueObjects } : {}),\n storage: { ...storageWithoutHash, storageHash },\n extensionPacks: {},\n capabilities,\n profileHash: computeProfileHash({ target, targetFamily, capabilities }),\n meta: {},\n });\n}\n"],"mappings":";;;;;AAIA,SAAS,gBACP,SACA,aACoB;CACpB,OAAO,aAAa,eAAe,QAAQ,GAAG;;AAGhD,SAAS,kBACP,OACA,cACA,aACqC;CACrC,IAAI,MAAM,KAAK,SAAS,UAAU;EAChC,MAAM,WAAW,gBAAgB,MAAM,KAAK,SAAS,YAAY;EACjE,IAAI,CAAC,UAAU,OAAO,KAAA;EAEtB,IAAI,UAAU,SAAS,MAAM,MAC3B,OAAO;GAAE,UAAU;GAAS,OAAO,EAAE,UAAU;GAAE;EAGnD,IAAI,MAAM,UACR,OAAO,EAAE,UAAU,CAAC,QAAQ,SAAS,EAAE;EAGzC,OAAO,EAAE,UAAU;;CAGrB,IAAI,MAAM,KAAK,SAAS,eAAe;EACrC,MAAM,KAAK,eAAe,MAAM,KAAK;EACrC,IAAI,CAAC,IAAI,OAAO,KAAA;EAChB,MAAM,WAAW,mBAAmB,GAAG,QAAQ,cAAc,YAAY;EACzE,IAAI,UAAU,SAAS,MAAM,MAC3B,OAAO;GAAE,UAAU;GAAS,OAAO;GAAU;EAE/C,IAAI,MAAM,UACR,OAAO,EAAE,OAAO,CAAC,EAAE,UAAU,QAAQ,EAAE,SAAS,EAAE;EAEpD,OAAO;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