@prisma-next/mongo-contract-psl 0.5.0-dev.6 → 0.5.0-dev.60
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/exports/provider.mjs +1 -1
- package/dist/index.d.mts.map +1 -1
- package/dist/index.mjs +1 -1
- package/dist/{interpreter-CWOjqYYA.mjs → interpreter-Dm_5ijze.mjs} +92 -23
- package/dist/interpreter-Dm_5ijze.mjs.map +1 -0
- package/package.json +8 -7
- package/src/interpreter.ts +144 -21
- package/dist/interpreter-CWOjqYYA.mjs.map +0 -1
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { t as interpretPslDocumentToMongoContract } from "../interpreter-
|
|
1
|
+
import { t as interpretPslDocumentToMongoContract } from "../interpreter-Dm_5ijze.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";
|
package/dist/index.d.mts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.mts","names":[],"sources":["../src/interpreter.ts"],"sourcesContent":[],"mappings":";;;;;;;
|
|
1
|
+
{"version":3,"file":"index.d.mts","names":[],"sources":["../src/interpreter.ts"],"sourcesContent":[],"mappings":";;;;;;;UA+BiB,wCAAA;qBACI;EADJ,SAAA,qBAAA,EAEiB,WAFuB,CAAA,MAAA,EAAA,MAAA,CAAA;EACpC,SAAA,WAAA,CAAA,EAEI,WAFJ;;AAEI,iBAsvBT,mCAAA,CAtvBS,KAAA,EAuvBhB,wCAvvBgB,CAAA,EAwvBtB,MAxvBsB,CAwvBf,QAxvBe,EAwvBL,yBAxvBK,CAAA"}
|
package/dist/index.mjs
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { computeProfileHash, computeStorageHash } from "@prisma-next/contract/hashing";
|
|
2
|
+
import { applyPolymorphicScopeToMongoIndex } from "@prisma-next/mongo-contract";
|
|
2
3
|
import { notOk, ok } from "@prisma-next/utils/result";
|
|
3
4
|
import { getPositionalArgument, parseQuotedStringLiteral } from "@prisma-next/psl-parser";
|
|
4
5
|
|
|
@@ -254,7 +255,7 @@ function collectPolymorphismDeclarations(document, sourceId, diagnostics) {
|
|
|
254
255
|
};
|
|
255
256
|
}
|
|
256
257
|
function resolvePolymorphism(input) {
|
|
257
|
-
const { discriminatorDeclarations, baseDeclarations, modelNames, sourceId, document } = input;
|
|
258
|
+
const { discriminatorDeclarations, baseDeclarations, modelNames, sourceId, document, indexSpans, modelIndexesByName } = input;
|
|
258
259
|
let patched = input.models;
|
|
259
260
|
let roots = input.roots;
|
|
260
261
|
let collections = input.collections;
|
|
@@ -353,31 +354,59 @@ function resolvePolymorphism(input) {
|
|
|
353
354
|
[variantCollectionName]: baseDecl.baseName
|
|
354
355
|
};
|
|
355
356
|
else roots = Object.fromEntries(Object.entries(roots).filter(([key]) => key !== variantCollectionName));
|
|
356
|
-
const
|
|
357
|
+
const variantOwnIndexes = modelIndexesByName.get(variantName) ?? [];
|
|
357
358
|
const baseColl = collections[baseCollection];
|
|
359
|
+
const discriminatorField = patched[baseDecl.baseName]?.discriminator?.field;
|
|
360
|
+
const scopedVariantIndexes = [];
|
|
361
|
+
if (discriminatorField) for (const idx of variantOwnIndexes) {
|
|
362
|
+
const result = applyPolymorphicScopeToMongoIndex(idx, {
|
|
363
|
+
discriminatorField,
|
|
364
|
+
discriminatorValue: baseDecl.value
|
|
365
|
+
});
|
|
366
|
+
if (result.kind === "conflict") {
|
|
367
|
+
const span = indexSpans.get(idx) ?? baseDecl.span;
|
|
368
|
+
diagnostics.push({
|
|
369
|
+
code: "PSL_INVALID_INDEX",
|
|
370
|
+
message: `Variant "${variantName}" index conflicts with discriminator scope: ${result.reason}`,
|
|
371
|
+
sourceId,
|
|
372
|
+
span
|
|
373
|
+
});
|
|
374
|
+
continue;
|
|
375
|
+
}
|
|
376
|
+
if (result.index !== idx) indexSpans.set(result.index, indexSpans.get(idx) ?? baseDecl.span);
|
|
377
|
+
scopedVariantIndexes.push(result.index);
|
|
378
|
+
}
|
|
379
|
+
else scopedVariantIndexes.push(...variantOwnIndexes);
|
|
358
380
|
if (variantCollectionName !== baseCollection) {
|
|
359
381
|
const filtered = Object.fromEntries(Object.entries(collections).filter(([key]) => key !== variantCollectionName));
|
|
360
|
-
if (
|
|
382
|
+
if (scopedVariantIndexes.length > 0 && baseColl) {
|
|
361
383
|
const baseIndexes = baseColl["indexes"] ?? [];
|
|
362
384
|
collections = {
|
|
363
385
|
...filtered,
|
|
364
386
|
[baseCollection]: {
|
|
365
387
|
...baseColl,
|
|
366
|
-
indexes: [...baseIndexes, ...
|
|
388
|
+
indexes: [...baseIndexes, ...scopedVariantIndexes]
|
|
367
389
|
}
|
|
368
390
|
};
|
|
369
391
|
} else collections = filtered;
|
|
370
|
-
} else if (
|
|
371
|
-
const
|
|
372
|
-
const
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
|
|
392
|
+
} else if (baseColl) {
|
|
393
|
+
const existingIndexes = baseColl["indexes"] ?? [];
|
|
394
|
+
const variantIndexSet = new Set(variantOwnIndexes);
|
|
395
|
+
const withoutUnscopedVariants = existingIndexes.filter((idx) => !variantIndexSet.has(idx));
|
|
396
|
+
const mergedIndexes = [...withoutUnscopedVariants];
|
|
397
|
+
for (const idx of scopedVariantIndexes) {
|
|
398
|
+
const idxKey = canonicalJson(idx);
|
|
399
|
+
if (!withoutUnscopedVariants.some((existing) => canonicalJson(existing) === idxKey)) mergedIndexes.push(idx);
|
|
400
|
+
}
|
|
401
|
+
if (mergedIndexes.length !== existingIndexes.length || mergedIndexes.some((idx, i) => idx !== existingIndexes[i])) {
|
|
402
|
+
const next = { ...baseColl };
|
|
403
|
+
if (mergedIndexes.length > 0) next["indexes"] = mergedIndexes;
|
|
404
|
+
else delete next["indexes"];
|
|
405
|
+
collections = {
|
|
406
|
+
...collections,
|
|
407
|
+
[baseCollection]: next
|
|
408
|
+
};
|
|
409
|
+
}
|
|
381
410
|
}
|
|
382
411
|
}
|
|
383
412
|
return {
|
|
@@ -387,6 +416,11 @@ function resolvePolymorphism(input) {
|
|
|
387
416
|
diagnostics
|
|
388
417
|
};
|
|
389
418
|
}
|
|
419
|
+
function canonicalJson(value) {
|
|
420
|
+
if (Array.isArray(value)) return `[${value.map(canonicalJson).join(",")}]`;
|
|
421
|
+
if (value && typeof value === "object") return `{${Object.entries(value).sort(([left], [right]) => left.localeCompare(right)).map(([key, entry]) => `${JSON.stringify(key)}:${canonicalJson(entry)}`).join(",")}}`;
|
|
422
|
+
return JSON.stringify(value);
|
|
423
|
+
}
|
|
390
424
|
function parseIndexDirection(raw) {
|
|
391
425
|
if (!raw) return 1;
|
|
392
426
|
const stripped = raw.replace(/^["']/, "").replace(/["']$/, "");
|
|
@@ -452,20 +486,27 @@ function parseProjectionList(raw, value) {
|
|
|
452
486
|
for (const f of fields) result[f] = value;
|
|
453
487
|
return result;
|
|
454
488
|
}
|
|
455
|
-
function collectIndexes(pslModel, fieldMappings, modelNames, sourceId, diagnostics) {
|
|
489
|
+
function collectIndexes(pslModel, fieldMappings, modelNames, sourceId, diagnostics, indexSpans) {
|
|
456
490
|
const indexes = [];
|
|
457
491
|
let textIndexCount = 0;
|
|
492
|
+
const indexableFieldNames = /* @__PURE__ */ new Set();
|
|
493
|
+
for (const f of pslModel.fields) {
|
|
494
|
+
if (modelNames.has(f.typeName)) continue;
|
|
495
|
+
indexableFieldNames.add(f.name);
|
|
496
|
+
}
|
|
458
497
|
for (const field of pslModel.fields) {
|
|
459
498
|
if (modelNames.has(field.typeName)) continue;
|
|
460
|
-
|
|
461
|
-
|
|
462
|
-
|
|
499
|
+
const uniqueAttr = getAttribute(field.attributes, "unique");
|
|
500
|
+
if (!uniqueAttr) continue;
|
|
501
|
+
const fieldUniqueIndex = {
|
|
463
502
|
keys: [{
|
|
464
|
-
field:
|
|
503
|
+
field: fieldMappings.pslNameToMapped.get(field.name) ?? field.name,
|
|
465
504
|
direction: 1
|
|
466
505
|
}],
|
|
467
506
|
unique: true
|
|
468
|
-
}
|
|
507
|
+
};
|
|
508
|
+
indexes.push(fieldUniqueIndex);
|
|
509
|
+
indexSpans.set(fieldUniqueIndex, uniqueAttr.span);
|
|
469
510
|
}
|
|
470
511
|
for (const attr of pslModel.attributes) {
|
|
471
512
|
const isIndex = attr.name === "index";
|
|
@@ -540,6 +581,28 @@ function collectIndexes(pslModel, fieldMappings, modelNames, sourceId, diagnosti
|
|
|
540
581
|
});
|
|
541
582
|
continue;
|
|
542
583
|
}
|
|
584
|
+
let missingField;
|
|
585
|
+
for (const pf of parsedFields) {
|
|
586
|
+
let fieldNameForLookup;
|
|
587
|
+
if (pf.isWildcard) {
|
|
588
|
+
const wildcardMatch = pf.name.match(/^(.+)\.\$\*\*$/);
|
|
589
|
+
fieldNameForLookup = wildcardMatch ? wildcardMatch[1] : void 0;
|
|
590
|
+
} else fieldNameForLookup = pf.name;
|
|
591
|
+
if (fieldNameForLookup === void 0 || fieldNameForLookup.length === 0) continue;
|
|
592
|
+
if (!indexableFieldNames.has(fieldNameForLookup)) {
|
|
593
|
+
missingField = fieldNameForLookup;
|
|
594
|
+
break;
|
|
595
|
+
}
|
|
596
|
+
}
|
|
597
|
+
if (missingField !== void 0) {
|
|
598
|
+
diagnostics.push({
|
|
599
|
+
code: "PSL_INDEX_FIELD_NOT_FOUND",
|
|
600
|
+
message: `Index on model "${pslModel.name}" references unknown field "${missingField}"`,
|
|
601
|
+
sourceId,
|
|
602
|
+
span: attr.span
|
|
603
|
+
});
|
|
604
|
+
continue;
|
|
605
|
+
}
|
|
543
606
|
const keys = parsedFields.map((pf) => {
|
|
544
607
|
return {
|
|
545
608
|
field: pf.isWildcard ? pf.name.replace(/^(.+)\.\$\*\*$/, (_, prefix) => {
|
|
@@ -614,6 +677,7 @@ function collectIndexes(pslModel, fieldMappings, modelNames, sourceId, diagnosti
|
|
|
614
677
|
...language_override != null && { language_override }
|
|
615
678
|
};
|
|
616
679
|
indexes.push(index);
|
|
680
|
+
indexSpans.set(index, attr.span);
|
|
617
681
|
}
|
|
618
682
|
return indexes;
|
|
619
683
|
}
|
|
@@ -669,6 +733,8 @@ function interpretPslDocumentToMongoContract(input) {
|
|
|
669
733
|
const collections = {};
|
|
670
734
|
const roots = {};
|
|
671
735
|
const allFkRelations = [];
|
|
736
|
+
const indexSpans = /* @__PURE__ */ new Map();
|
|
737
|
+
const modelIndexesByName = /* @__PURE__ */ new Map();
|
|
672
738
|
const backrelationCandidates = [];
|
|
673
739
|
for (const pslModel of document.ast.models) {
|
|
674
740
|
const collectionName = resolveCollectionName(pslModel);
|
|
@@ -729,7 +795,8 @@ function interpretPslDocumentToMongoContract(input) {
|
|
|
729
795
|
relations,
|
|
730
796
|
storage: { collection: collectionName }
|
|
731
797
|
};
|
|
732
|
-
const modelIndexes = collectIndexes(pslModel, fieldMappings, modelNames, sourceId, diagnostics);
|
|
798
|
+
const modelIndexes = collectIndexes(pslModel, fieldMappings, modelNames, sourceId, diagnostics, indexSpans);
|
|
799
|
+
modelIndexesByName.set(pslModel.name, modelIndexes);
|
|
733
800
|
const existingColl = collections[collectionName];
|
|
734
801
|
if (existingColl && modelIndexes.length > 0) collections[collectionName] = { indexes: [...existingColl["indexes"] ?? [], ...modelIndexes] };
|
|
735
802
|
else if (!existingColl) collections[collectionName] = modelIndexes.length > 0 ? { indexes: modelIndexes } : {};
|
|
@@ -796,6 +863,8 @@ function interpretPslDocumentToMongoContract(input) {
|
|
|
796
863
|
discriminatorDeclarations,
|
|
797
864
|
baseDeclarations,
|
|
798
865
|
modelNames,
|
|
866
|
+
indexSpans,
|
|
867
|
+
modelIndexesByName,
|
|
799
868
|
sourceId
|
|
800
869
|
});
|
|
801
870
|
if (diagnostics.length > 0 || polyResult.diagnostics.length > 0) return notOk({
|
|
@@ -848,4 +917,4 @@ function interpretPslDocumentToMongoContract(input) {
|
|
|
848
917
|
|
|
849
918
|
//#endregion
|
|
850
919
|
export { interpretPslDocumentToMongoContract as t };
|
|
851
|
-
//# sourceMappingURL=interpreter-
|
|
920
|
+
//# sourceMappingURL=interpreter-Dm_5ijze.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"interpreter-Dm_5ijze.mjs","names":["properties: Record<string, unknown>","required: string[]","result: Record<string, unknown>","oneOf: Record<string, unknown>[]","variantOnlyFields: Record<string, ContractField>","entry: Record<string, unknown>","variantProperties: Record<string, unknown>","variantRequired: string[]","parts: string[]","relationName: string | undefined","fieldsArg: PslAttributeArgument | undefined","referencesArg: PslAttributeArgument | undefined","diagnostics: ContractSourceDiagnostic[]","variants: Record<string, { readonly value: string }>","scopedVariantIndexes: MongoStorageIndex[]","next: Record<string, unknown>","collation: Record<string, unknown>","result: Record<string, 0 | 1>","indexes: MongoStorageIndex[]","fieldUniqueIndex: MongoStorageIndex","defaultDirection: MongoIndexKeyDirection","missingField: string | undefined","fieldNameForLookup: string | undefined","weights: Record<string, number> | undefined","index: MongoStorageIndex","result: ContractField","result","models: Record<string, MongoModelEntry>","collections: Record<string, Record<string, unknown>>","roots: Record<string, string>","allFkRelations: FkRelation[]","backrelationCandidates: BackrelationCandidate[]","fields: Record<string, ContractField>","relations: Record<string, ContractReferenceRelation>","valueObjects: Record<string, ContractValueObject>","capabilities: Record<string, Record<string, boolean>>"],"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 type { MongoStorageValidator } from '@prisma-next/mongo-contract';\n\nfunction resolveBsonType(\n codecId: string,\n codecLookup: CodecLookup | undefined,\n): string | undefined {\n const codec = codecLookup?.get(codecId);\n return codec?.targetTypes[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): MongoStorageValidator {\n return {\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): MongoStorageValidator {\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 {\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 type MongoIndexKeyDirection,\n type MongoStorageIndex,\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<MongoStorageIndex, PslSpan>;\n modelIndexesByName: Map<string, readonly MongoStorageIndex[]>;\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: MongoStorageIndex[] = [];\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 MongoStorageIndex[];\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 MongoStorageIndex[];\n const variantIndexSet = new Set<MongoStorageIndex>(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 MongoStorageIndex 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<MongoStorageIndex, PslSpan>,\n): MongoStorageIndex[] {\n const indexes: MongoStorageIndex[] = [];\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: MongoStorageIndex = {\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: MongoStorageIndex = {\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<MongoStorageIndex, PslSpan>();\n const modelIndexesByName = new Map<string, readonly MongoStorageIndex[]>();\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 MongoStorageIndex[];\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 storageWithoutHash = { collections: resolvedCollections };\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;AAEpB,SADc,aAAa,IAAI,QAAQ,GACzB,YAAY;;AAG5B,SAAS,kBACP,OACA,cACA,aACqC;AACrC,KAAI,MAAM,KAAK,SAAS,UAAU;EAChC,MAAM,WAAW,gBAAgB,MAAM,KAAK,SAAS,YAAY;AACjE,MAAI,CAAC,SAAU,QAAO;AAEtB,MAAI,UAAU,SAAS,MAAM,KAC3B,QAAO;GAAE,UAAU;GAAS,OAAO,EAAE,UAAU;GAAE;AAGnD,MAAI,MAAM,SACR,QAAO,EAAE,UAAU,CAAC,QAAQ,SAAS,EAAE;AAGzC,SAAO,EAAE,UAAU;;AAGrB,KAAI,MAAM,KAAK,SAAS,eAAe;EACrC,MAAM,KAAK,eAAe,MAAM,KAAK;AACrC,MAAI,CAAC,GAAI,QAAO;EAChB,MAAM,WAAW,mBAAmB,GAAG,QAAQ,cAAc,YAAY;AACzE,MAAI,UAAU,SAAS,MAAM,KAC3B,QAAO;GAAE,UAAU;GAAS,OAAO;GAAU;AAE/C,MAAI,MAAM,SACR,QAAO,EAA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,eAAwD,EAAE;AAEhE,QAAO,GAAG;EACR;EACA;EACA,OAAO,WAAW;EAClB,QAAQ,WAAW;EACnB,GAAI,OAAO,KAAK,aAAa,CAAC,SAAS,IAAI,EAAE,cAAc,GAAG,EAAE;EAChE,SAAS;GAAE,GAAG;GAAoB;GAAa;EAC/C,gBAAgB,EAAE;EAClB;EACA,aAAa,mBAAmB;GAAE;GAAQ;GAAc;GAAc,CAAC;EACvE,MAAM,EAAE;EACT,CAAC"}
|
package/package.json
CHANGED
|
@@ -1,17 +1,18 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@prisma-next/mongo-contract-psl",
|
|
3
|
-
"version": "0.5.0-dev.
|
|
3
|
+
"version": "0.5.0-dev.60",
|
|
4
|
+
"license": "Apache-2.0",
|
|
4
5
|
"type": "module",
|
|
5
6
|
"sideEffects": false,
|
|
6
7
|
"description": "PSL-to-Mongo ContractIR interpreter for Prisma Next",
|
|
7
8
|
"dependencies": {
|
|
8
9
|
"pathe": "^2.0.3",
|
|
9
|
-
"@prisma-next/config": "0.5.0-dev.
|
|
10
|
-
"@prisma-next/framework-components": "0.5.0-dev.
|
|
11
|
-
"@prisma-next/contract": "0.5.0-dev.
|
|
12
|
-
"@prisma-next/
|
|
13
|
-
"@prisma-next/
|
|
14
|
-
"@prisma-next/
|
|
10
|
+
"@prisma-next/config": "0.5.0-dev.60",
|
|
11
|
+
"@prisma-next/framework-components": "0.5.0-dev.60",
|
|
12
|
+
"@prisma-next/contract": "0.5.0-dev.60",
|
|
13
|
+
"@prisma-next/utils": "0.5.0-dev.60",
|
|
14
|
+
"@prisma-next/mongo-contract": "0.5.0-dev.60",
|
|
15
|
+
"@prisma-next/psl-parser": "0.5.0-dev.60"
|
|
15
16
|
},
|
|
16
17
|
"devDependencies": {
|
|
17
18
|
"tsdown": "0.18.4",
|
package/src/interpreter.ts
CHANGED
|
@@ -10,8 +10,12 @@ 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
|
|
14
|
-
|
|
13
|
+
import {
|
|
14
|
+
applyPolymorphicScopeToMongoIndex,
|
|
15
|
+
type MongoIndexKeyDirection,
|
|
16
|
+
type MongoStorageIndex,
|
|
17
|
+
} from '@prisma-next/mongo-contract';
|
|
18
|
+
import type { ParsePslDocumentResult, PslField, PslModel, PslSpan } from '@prisma-next/psl-parser';
|
|
15
19
|
import { notOk, ok, type Result } from '@prisma-next/utils/result';
|
|
16
20
|
import { deriveJsonSchema, derivePolymorphicJsonSchema } from './derive-json-schema';
|
|
17
21
|
import {
|
|
@@ -153,6 +157,8 @@ function resolvePolymorphism(input: {
|
|
|
153
157
|
discriminatorDeclarations: Map<string, DiscriminatorDeclaration>;
|
|
154
158
|
baseDeclarations: Map<string, BaseDeclaration>;
|
|
155
159
|
modelNames: ReadonlySet<string>;
|
|
160
|
+
indexSpans: Map<MongoStorageIndex, PslSpan>;
|
|
161
|
+
modelIndexesByName: Map<string, readonly MongoStorageIndex[]>;
|
|
156
162
|
sourceId: string;
|
|
157
163
|
}): {
|
|
158
164
|
models: Record<string, MongoModelEntry>;
|
|
@@ -160,7 +166,15 @@ function resolvePolymorphism(input: {
|
|
|
160
166
|
collections: Record<string, Record<string, unknown>>;
|
|
161
167
|
diagnostics: ContractSourceDiagnostic[];
|
|
162
168
|
} {
|
|
163
|
-
const {
|
|
169
|
+
const {
|
|
170
|
+
discriminatorDeclarations,
|
|
171
|
+
baseDeclarations,
|
|
172
|
+
modelNames,
|
|
173
|
+
sourceId,
|
|
174
|
+
document,
|
|
175
|
+
indexSpans,
|
|
176
|
+
modelIndexesByName,
|
|
177
|
+
} = input;
|
|
164
178
|
let patched = input.models;
|
|
165
179
|
let roots = input.roots;
|
|
166
180
|
let collections = input.collections;
|
|
@@ -281,39 +295,78 @@ function resolvePolymorphism(input: {
|
|
|
281
295
|
}
|
|
282
296
|
}
|
|
283
297
|
|
|
284
|
-
const
|
|
285
|
-
const variantIndexes = (variantColl?.['indexes'] ?? []) as MongoStorageIndex[];
|
|
298
|
+
const variantOwnIndexes = modelIndexesByName.get(variantName) ?? [];
|
|
286
299
|
const baseColl = collections[baseCollection];
|
|
287
300
|
|
|
301
|
+
const baseModelEntry = patched[baseDecl.baseName];
|
|
302
|
+
const discriminatorField = baseModelEntry?.discriminator?.field;
|
|
303
|
+
const scopedVariantIndexes: MongoStorageIndex[] = [];
|
|
304
|
+
if (discriminatorField) {
|
|
305
|
+
for (const idx of variantOwnIndexes) {
|
|
306
|
+
const result = applyPolymorphicScopeToMongoIndex(idx, {
|
|
307
|
+
discriminatorField,
|
|
308
|
+
discriminatorValue: baseDecl.value,
|
|
309
|
+
});
|
|
310
|
+
if (result.kind === 'conflict') {
|
|
311
|
+
const span = indexSpans.get(idx) ?? baseDecl.span;
|
|
312
|
+
diagnostics.push({
|
|
313
|
+
code: 'PSL_INVALID_INDEX',
|
|
314
|
+
message: `Variant "${variantName}" index conflicts with discriminator scope: ${result.reason}`,
|
|
315
|
+
sourceId,
|
|
316
|
+
span,
|
|
317
|
+
});
|
|
318
|
+
continue;
|
|
319
|
+
}
|
|
320
|
+
if (result.index !== idx) {
|
|
321
|
+
indexSpans.set(result.index, indexSpans.get(idx) ?? baseDecl.span);
|
|
322
|
+
}
|
|
323
|
+
scopedVariantIndexes.push(result.index);
|
|
324
|
+
}
|
|
325
|
+
} else {
|
|
326
|
+
scopedVariantIndexes.push(...variantOwnIndexes);
|
|
327
|
+
}
|
|
328
|
+
|
|
288
329
|
if (variantCollectionName !== baseCollection) {
|
|
289
330
|
const filtered = Object.fromEntries(
|
|
290
331
|
Object.entries(collections).filter(([key]) => key !== variantCollectionName),
|
|
291
332
|
);
|
|
292
|
-
if (
|
|
333
|
+
if (scopedVariantIndexes.length > 0 && baseColl) {
|
|
293
334
|
const baseIndexes = (baseColl['indexes'] ?? []) as MongoStorageIndex[];
|
|
294
335
|
collections = {
|
|
295
336
|
...filtered,
|
|
296
|
-
[baseCollection]: {
|
|
337
|
+
[baseCollection]: {
|
|
338
|
+
...baseColl,
|
|
339
|
+
indexes: [...baseIndexes, ...scopedVariantIndexes],
|
|
340
|
+
},
|
|
297
341
|
};
|
|
298
342
|
} else {
|
|
299
343
|
collections = filtered;
|
|
300
344
|
}
|
|
301
|
-
} else if (
|
|
302
|
-
const
|
|
303
|
-
const
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
345
|
+
} else if (baseColl) {
|
|
346
|
+
const existingIndexes = (baseColl['indexes'] ?? []) as MongoStorageIndex[];
|
|
347
|
+
const variantIndexSet = new Set<MongoStorageIndex>(variantOwnIndexes);
|
|
348
|
+
const withoutUnscopedVariants = existingIndexes.filter((idx) => !variantIndexSet.has(idx));
|
|
349
|
+
const mergedIndexes = [...withoutUnscopedVariants];
|
|
350
|
+
for (const idx of scopedVariantIndexes) {
|
|
351
|
+
const idxKey = canonicalJson(idx);
|
|
352
|
+
const isDuplicate = withoutUnscopedVariants.some(
|
|
353
|
+
(existing) => canonicalJson(existing) === idxKey,
|
|
307
354
|
);
|
|
308
355
|
if (!isDuplicate) {
|
|
309
356
|
mergedIndexes.push(idx);
|
|
310
357
|
}
|
|
311
358
|
}
|
|
312
|
-
if (
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
};
|
|
359
|
+
if (
|
|
360
|
+
mergedIndexes.length !== existingIndexes.length ||
|
|
361
|
+
mergedIndexes.some((idx, i) => idx !== existingIndexes[i])
|
|
362
|
+
) {
|
|
363
|
+
const next: Record<string, unknown> = { ...baseColl };
|
|
364
|
+
if (mergedIndexes.length > 0) {
|
|
365
|
+
next['indexes'] = mergedIndexes;
|
|
366
|
+
} else {
|
|
367
|
+
delete next['indexes'];
|
|
368
|
+
}
|
|
369
|
+
collections = { ...collections, [baseCollection]: next };
|
|
317
370
|
}
|
|
318
371
|
}
|
|
319
372
|
}
|
|
@@ -321,6 +374,24 @@ function resolvePolymorphism(input: {
|
|
|
321
374
|
return { models: patched, roots, collections, diagnostics };
|
|
322
375
|
}
|
|
323
376
|
|
|
377
|
+
// Property-order-stable serialization for structural equality of plain
|
|
378
|
+
// JSON-compatible values. Used for comparing MongoStorageIndex shapes in
|
|
379
|
+
// the variant-merge dedup path where a future change to the spread order
|
|
380
|
+
// would otherwise produce JSON-stringify mismatches even though the
|
|
381
|
+
// indexes are structurally identical.
|
|
382
|
+
function canonicalJson(value: unknown): string {
|
|
383
|
+
if (Array.isArray(value)) {
|
|
384
|
+
return `[${value.map(canonicalJson).join(',')}]`;
|
|
385
|
+
}
|
|
386
|
+
if (value && typeof value === 'object') {
|
|
387
|
+
return `{${Object.entries(value as Record<string, unknown>)
|
|
388
|
+
.sort(([left], [right]) => left.localeCompare(right))
|
|
389
|
+
.map(([key, entry]) => `${JSON.stringify(key)}:${canonicalJson(entry)}`)
|
|
390
|
+
.join(',')}}`;
|
|
391
|
+
}
|
|
392
|
+
return JSON.stringify(value);
|
|
393
|
+
}
|
|
394
|
+
|
|
324
395
|
function parseIndexDirection(raw: string | undefined): MongoIndexKeyDirection {
|
|
325
396
|
if (!raw) return 1;
|
|
326
397
|
const stripped = raw.replace(/^["']/, '').replace(/["']$/, '');
|
|
@@ -424,19 +495,33 @@ function collectIndexes(
|
|
|
424
495
|
modelNames: ReadonlySet<string>,
|
|
425
496
|
sourceId: string,
|
|
426
497
|
diagnostics: ContractSourceDiagnostic[],
|
|
498
|
+
indexSpans: Map<MongoStorageIndex, PslSpan>,
|
|
427
499
|
): MongoStorageIndex[] {
|
|
428
500
|
const indexes: MongoStorageIndex[] = [];
|
|
429
501
|
let textIndexCount = 0;
|
|
430
502
|
|
|
503
|
+
// Storage-indexable PSL field names — i.e. all declared fields except
|
|
504
|
+
// relation fields (which don't materialize a column on this model). The
|
|
505
|
+
// index field-existence check (PSL_INDEX_FIELD_NOT_FOUND) consults this
|
|
506
|
+
// rather than fieldMappings.pslNameToMapped because the latter contains
|
|
507
|
+
// every PSL field including relation fields.
|
|
508
|
+
const indexableFieldNames = new Set<string>();
|
|
509
|
+
for (const f of pslModel.fields) {
|
|
510
|
+
if (modelNames.has(f.typeName)) continue;
|
|
511
|
+
indexableFieldNames.add(f.name);
|
|
512
|
+
}
|
|
513
|
+
|
|
431
514
|
for (const field of pslModel.fields) {
|
|
432
515
|
if (modelNames.has(field.typeName)) continue;
|
|
433
516
|
const uniqueAttr = getAttribute(field.attributes, 'unique');
|
|
434
517
|
if (!uniqueAttr) continue;
|
|
435
518
|
const mappedName = fieldMappings.pslNameToMapped.get(field.name) ?? field.name;
|
|
436
|
-
|
|
519
|
+
const fieldUniqueIndex: MongoStorageIndex = {
|
|
437
520
|
keys: [{ field: mappedName, direction: 1 }],
|
|
438
521
|
unique: true,
|
|
439
|
-
}
|
|
522
|
+
};
|
|
523
|
+
indexes.push(fieldUniqueIndex);
|
|
524
|
+
indexSpans.set(fieldUniqueIndex, uniqueAttr.span);
|
|
440
525
|
}
|
|
441
526
|
|
|
442
527
|
for (const attr of pslModel.attributes) {
|
|
@@ -526,6 +611,31 @@ function collectIndexes(
|
|
|
526
611
|
continue;
|
|
527
612
|
}
|
|
528
613
|
|
|
614
|
+
let missingField: string | undefined;
|
|
615
|
+
for (const pf of parsedFields) {
|
|
616
|
+
let fieldNameForLookup: string | undefined;
|
|
617
|
+
if (pf.isWildcard) {
|
|
618
|
+
const wildcardMatch = pf.name.match(/^(.+)\.\$\*\*$/);
|
|
619
|
+
fieldNameForLookup = wildcardMatch ? wildcardMatch[1] : undefined;
|
|
620
|
+
} else {
|
|
621
|
+
fieldNameForLookup = pf.name;
|
|
622
|
+
}
|
|
623
|
+
if (fieldNameForLookup === undefined || fieldNameForLookup.length === 0) continue;
|
|
624
|
+
if (!indexableFieldNames.has(fieldNameForLookup)) {
|
|
625
|
+
missingField = fieldNameForLookup;
|
|
626
|
+
break;
|
|
627
|
+
}
|
|
628
|
+
}
|
|
629
|
+
if (missingField !== undefined) {
|
|
630
|
+
diagnostics.push({
|
|
631
|
+
code: 'PSL_INDEX_FIELD_NOT_FOUND',
|
|
632
|
+
message: `Index on model "${pslModel.name}" references unknown field "${missingField}"`,
|
|
633
|
+
sourceId,
|
|
634
|
+
span: attr.span,
|
|
635
|
+
});
|
|
636
|
+
continue;
|
|
637
|
+
}
|
|
638
|
+
|
|
529
639
|
const keys = parsedFields.map((pf) => {
|
|
530
640
|
const mappedName = pf.isWildcard
|
|
531
641
|
? pf.name.replace(/^(.+)\.\$\*\*$/, (_, prefix: string) => {
|
|
@@ -629,6 +739,7 @@ function collectIndexes(
|
|
|
629
739
|
};
|
|
630
740
|
|
|
631
741
|
indexes.push(index);
|
|
742
|
+
indexSpans.set(index, attr.span);
|
|
632
743
|
}
|
|
633
744
|
|
|
634
745
|
return indexes;
|
|
@@ -692,6 +803,8 @@ export function interpretPslDocumentToMongoContract(
|
|
|
692
803
|
const collections: Record<string, Record<string, unknown>> = {};
|
|
693
804
|
const roots: Record<string, string> = {};
|
|
694
805
|
const allFkRelations: FkRelation[] = [];
|
|
806
|
+
const indexSpans = new Map<MongoStorageIndex, PslSpan>();
|
|
807
|
+
const modelIndexesByName = new Map<string, readonly MongoStorageIndex[]>();
|
|
695
808
|
|
|
696
809
|
interface BackrelationCandidate {
|
|
697
810
|
readonly modelName: string;
|
|
@@ -783,7 +896,15 @@ export function interpretPslDocumentToMongoContract(
|
|
|
783
896
|
}
|
|
784
897
|
|
|
785
898
|
models[pslModel.name] = { fields, relations, storage: { collection: collectionName } };
|
|
786
|
-
const modelIndexes = collectIndexes(
|
|
899
|
+
const modelIndexes = collectIndexes(
|
|
900
|
+
pslModel,
|
|
901
|
+
fieldMappings,
|
|
902
|
+
modelNames,
|
|
903
|
+
sourceId,
|
|
904
|
+
diagnostics,
|
|
905
|
+
indexSpans,
|
|
906
|
+
);
|
|
907
|
+
modelIndexesByName.set(pslModel.name, modelIndexes);
|
|
787
908
|
const existingColl = collections[collectionName];
|
|
788
909
|
if (existingColl && modelIndexes.length > 0) {
|
|
789
910
|
const existingIndexes = (existingColl['indexes'] ?? []) as MongoStorageIndex[];
|
|
@@ -876,6 +997,8 @@ export function interpretPslDocumentToMongoContract(
|
|
|
876
997
|
discriminatorDeclarations,
|
|
877
998
|
baseDeclarations,
|
|
878
999
|
modelNames,
|
|
1000
|
+
indexSpans,
|
|
1001
|
+
modelIndexesByName,
|
|
879
1002
|
sourceId,
|
|
880
1003
|
});
|
|
881
1004
|
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"interpreter-CWOjqYYA.mjs","names":["properties: Record<string, unknown>","required: string[]","result: Record<string, unknown>","oneOf: Record<string, unknown>[]","variantOnlyFields: Record<string, ContractField>","entry: Record<string, unknown>","variantProperties: Record<string, unknown>","variantRequired: string[]","parts: string[]","relationName: string | undefined","fieldsArg: PslAttributeArgument | undefined","referencesArg: PslAttributeArgument | undefined","diagnostics: ContractSourceDiagnostic[]","variants: Record<string, { readonly value: string }>","collation: Record<string, unknown>","result: Record<string, 0 | 1>","indexes: MongoStorageIndex[]","defaultDirection: MongoIndexKeyDirection","weights: Record<string, number> | undefined","index: MongoStorageIndex","result: ContractField","result","models: Record<string, MongoModelEntry>","collections: Record<string, Record<string, unknown>>","roots: Record<string, string>","allFkRelations: FkRelation[]","backrelationCandidates: BackrelationCandidate[]","fields: Record<string, ContractField>","relations: Record<string, ContractReferenceRelation>","valueObjects: Record<string, ContractValueObject>","capabilities: Record<string, Record<string, boolean>>"],"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 type { MongoStorageValidator } from '@prisma-next/mongo-contract';\n\nfunction resolveBsonType(\n codecId: string,\n codecLookup: CodecLookup | undefined,\n): string | undefined {\n const codec = codecLookup?.get(codecId);\n return codec?.targetTypes[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): MongoStorageValidator {\n return {\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): MongoStorageValidator {\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 {\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 type { MongoIndexKeyDirection, MongoStorageIndex } from '@prisma-next/mongo-contract';\nimport type { ParsePslDocumentResult, PslField, PslModel } 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 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 { discriminatorDeclarations, baseDeclarations, modelNames, sourceId, document } = 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 variantColl = collections[variantCollectionName];\n const variantIndexes = (variantColl?.['indexes'] ?? []) as MongoStorageIndex[];\n const baseColl = collections[baseCollection];\n\n if (variantCollectionName !== baseCollection) {\n const filtered = Object.fromEntries(\n Object.entries(collections).filter(([key]) => key !== variantCollectionName),\n );\n if (variantIndexes.length > 0 && baseColl) {\n const baseIndexes = (baseColl['indexes'] ?? []) as MongoStorageIndex[];\n collections = {\n ...filtered,\n [baseCollection]: { ...baseColl, indexes: [...baseIndexes, ...variantIndexes] },\n };\n } else {\n collections = filtered;\n }\n } else if (variantIndexes.length > 0 && baseColl) {\n const baseIndexes = (baseColl['indexes'] ?? []) as MongoStorageIndex[];\n const mergedIndexes = [...baseIndexes];\n for (const idx of variantIndexes) {\n const isDuplicate = baseIndexes.some(\n (existing) => JSON.stringify(existing) === JSON.stringify(idx),\n );\n if (!isDuplicate) {\n mergedIndexes.push(idx);\n }\n }\n if (mergedIndexes.length > baseIndexes.length) {\n collections = {\n ...collections,\n [baseCollection]: { ...baseColl, indexes: mergedIndexes },\n };\n }\n }\n }\n\n return { models: patched, roots, collections, diagnostics };\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): MongoStorageIndex[] {\n const indexes: MongoStorageIndex[] = [];\n let textIndexCount = 0;\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 indexes.push({\n keys: [{ field: mappedName, direction: 1 }],\n unique: true,\n });\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 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: MongoStorageIndex = {\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 }\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\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(pslModel, fieldMappings, modelNames, sourceId, diagnostics);\n const existingColl = collections[collectionName];\n if (existingColl && modelIndexes.length > 0) {\n const existingIndexes = (existingColl['indexes'] ?? []) as MongoStorageIndex[];\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 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 storageWithoutHash = { collections: resolvedCollections };\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;AAEpB,SADc,aAAa,IAAI,QAAQ,GACzB,YAAY;;AAG5B,SAAS,kBACP,OACA,cACA,aACqC;AACrC,KAAI,MAAM,KAAK,SAAS,UAAU;EAChC,MAAM,WAAW,gBAAgB,MAAM,KAAK,SAAS,YAAY;AACjE,MAAI,CAAC,SAAU,QAAO;AAEtB,MAAI,UAAU,SAAS,MAAM,KAC3B,QAAO;GAAE,UAAU;GAAS,OAAO,EAAE,UAAU;GAAE;AAGnD,MAAI,MAAM,SACR,QAAO,EAAE,UAAU,CAAC,QAAQ,SAAS,EAAE;AAGzC,SAAO,EAAE,UAAU;;AAGrB,KAA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