{"version":3,"file":"provider.mjs","names":[],"sources":["../../src/provider.ts"],"sourcesContent":["import { readFile } from 'node:fs/promises';\nimport type { ContractConfig, ContractSourceDiagnostic } from '@prisma-next/config/config-types';\nimport { buildSymbolTable, rangeToPslSpan } from '@prisma-next/psl-parser';\nimport type { PslInterpretCapable } from '@prisma-next/psl-parser/interpret';\nimport { withSeedDiagnostics } from '@prisma-next/psl-parser/interpret';\nimport type { ParseDiagnostic, SourceFile } from '@prisma-next/psl-parser/syntax';\nimport { parse } from '@prisma-next/psl-parser/syntax';\nimport { ifDefined } from '@prisma-next/utils/defined';\nimport { notOk } from '@prisma-next/utils/result';\n\nimport { interpretPslDocumentToMongoContract } from './interpreter';\n\nexport interface MongoContractOptions {\n  readonly output?: string;\n  /** The target's default codec ids for an `enum` block that omits `@@type`. */\n  readonly enumInferenceCodecs?: { readonly text: string; readonly int: string };\n}\n\nfunction mapParseDiagnostics(\n  diagnostics: readonly ParseDiagnostic[],\n  sourceFile: SourceFile,\n  sourceId: string,\n): ContractSourceDiagnostic[] {\n  return diagnostics.map((diagnostic) => ({\n    code: diagnostic.code,\n    message: diagnostic.message,\n    sourceId,\n    span: rangeToPslSpan(diagnostic.range, sourceFile),\n  }));\n}\n\nexport function mongoContract(schemaPath: string, options?: MongoContractOptions): ContractConfig {\n  const source: PslInterpretCapable = {\n    sourceFormat: 'psl',\n    inputs: [schemaPath],\n    interpret(input, context) {\n      return interpretPslDocumentToMongoContract({\n        symbolTable: input.symbolTable,\n        sourceFile: input.sourceFile,\n        sourceId: input.sourceId,\n        seedDiagnostics: [],\n        scalarTypeDescriptors: context.scalarTypeDescriptors,\n        codecLookup: context.codecLookup,\n        authoringContributions: context.authoringContributions,\n        ...ifDefined('enumInferenceCodecs', options?.enumInferenceCodecs),\n      });\n    },\n    async load(context) {\n      const [absoluteSchemaPath] = context.resolvedInputs;\n      if (absoluteSchemaPath === undefined) {\n        throw new Error(\n          'mongoContract: context.resolvedInputs is empty. The CLI config loader should populate it positional-matched with source.inputs.',\n        );\n      }\n      let schema: string;\n      try {\n        schema = await readFile(absoluteSchemaPath, 'utf-8');\n      } catch (error) {\n        const message = String(error);\n        return notOk({\n          summary: `Failed to read Prisma schema at \"${schemaPath}\"`,\n          diagnostics: [\n            {\n              code: 'PSL_SCHEMA_READ_FAILED',\n              message,\n              sourceId: schemaPath,\n            },\n          ],\n          meta: { schemaPath, absoluteSchemaPath, cause: message },\n        });\n      }\n\n      const { document, sourceFile, diagnostics: parseDiagnostics } = parse(schema);\n      const { table: symbolTable, diagnostics: symbolTableDiagnostics } = buildSymbolTable({\n        document,\n        sourceFile,\n        scalarTypes: [...context.scalarTypeDescriptors.keys()],\n        pslBlockDescriptors: context.authoringContributions.pslBlockDescriptors,\n      });\n\n      // Do not short-circuit on provider-level diagnostics; recovered CST can\n      // still produce interpreter diagnostics in the same response.\n      const seedDiagnostics = [\n        ...mapParseDiagnostics(parseDiagnostics, sourceFile, schemaPath),\n        ...mapParseDiagnostics(symbolTableDiagnostics, sourceFile, schemaPath),\n      ];\n\n      return withSeedDiagnostics(\n        this.interpret({ document, sourceFile, symbolTable, sourceId: schemaPath }, context),\n        seedDiagnostics,\n      );\n    },\n  };\n\n  return {\n    source,\n    ...ifDefined('output', options?.output),\n  };\n}\n"],"mappings":";;;;;;;;AAkBA,SAAS,oBACP,aACA,YACA,UAC4B;CAC5B,OAAO,YAAY,KAAK,gBAAgB;EACtC,MAAM,WAAW;EACjB,SAAS,WAAW;EACpB;EACA,MAAM,eAAe,WAAW,OAAO,UAAU;CACnD,EAAE;AACJ;AAEA,SAAgB,cAAc,YAAoB,SAAgD;CA+DhG,OAAO;EACL,QAAA;GA9DA,cAAc;GACd,QAAQ,CAAC,UAAU;GACnB,UAAU,OAAO,SAAS;IACxB,OAAO,oCAAoC;KACzC,aAAa,MAAM;KACnB,YAAY,MAAM;KAClB,UAAU,MAAM;KAChB,iBAAiB,CAAC;KAClB,uBAAuB,QAAQ;KAC/B,aAAa,QAAQ;KACrB,wB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