How to compile or get client generation result in memory?

const schema = await fs.promises.readFile("schema.prisma", "utf8");
const document = await getDMMF({ datamodel: schema });
const result = await compile(dmmf);

if (result.success === true)
console.log(result.files); // maybe Record<string, string>
else
console.log(result.diagnostics); // compilation errors
const schema = await fs.promises.readFile("schema.prisma", "utf8");
const document = await getDMMF({ datamodel: schema });
const result = await compile(dmmf);

if (result.success === true)
console.log(result.files); // maybe Record<string, string>
else
console.log(result.diagnostics); // compilation errors
I'm making an AI chatbot that automating backend development from conversation context, and the AI chatbot is generating prisma based TS backend server. To accomplish the mission, I need to compile prisma schema file in memory, so that get list of generated TS files as string values, or get compilation error messages. If AI makes invalid prisma schema file, the compilation error messages would make the AI agent to correct at the next trial, and otherwise suceeded to compile so that generates client TS files, it would be utilized for the next step (backend TS code generation). So, can you help me to compile prisma schema content only in the memory?
1 Reply
Prisma AI Help
Prisma AI Help•2d ago
You opted for speed from this bot. Let's conjure up an answer right now! Prisma does not officially support generating the Prisma Client entirely in memory (i.e., without writing files to disk) using a public API. The standard workflow requires running prisma generate, which writes the generated client files to a specified output directory on disk, not returning them as in-memory strings or objects. There is no documented or supported API for compiling the Prisma schema and retrieving the generated TypeScript files as strings in memory, nor for getting compilation errors in the format you described. However, you can programmatically access the DMMF (Data Model Meta Format) using getDmmf from @prisma/internals, which gives you the parsed schema metadata in memory. This is useful for introspection or custom generators, but it does not generate the full Prisma Client code as TypeScript files in memory. The DMMF is an internal API and not officially documented, so it may change without notice. Example:
import { getDmmf } from '@prisma/internals';
const schema = await fs.promises.readFile('schema.prisma', 'utf8');
const dmmf = await getDmmf({ datamodel: schema });
import { getDmmf } from '@prisma/internals';
const schema = await fs.promises.readFile('schema.prisma', 'utf8');
const dmmf = await getDmmf({ datamodel: schema });
Runtime schema introspection? For actual client code generation, Prisma's generators expect to write files to disk. There is no supported compile function that returns generated files as a Record<string, string> in memory. If you need this, you would have to fork or deeply integrate with Prisma's internal generator logic, which is not recommended or stable. In summary:
- You can get the DMMF in memory, but not the generated client TS files. - Compilation errors are surfaced via CLI or generator output, not as structured objects in memory. - There is no official API for fully in-memory client code generation. If you need more advanced in-memory workflows, you may need to open a feature request or explore custom generator development, but this is not currently supported out of the box. 🤔 Alright, thanks for letting us know that there's still some help needed. A team member will reply to you shortly. In the meantime, please let us know any additional info, questions, or which parts of the answer may need clarification.

Did you find this page helpful?