P
Prisma•7mo ago
Matusko167

make from model.prisma sql

Hello its possible to just get some method to do that too send model.prisma inside some function and get return sql? or send to models and get difference sql like alter? becouse we dont want to use prisma db push or prisma migrate and make our own flow to create tables
6 Replies
Matusko167
Matusko167OP•7mo ago
i found only this but how call it?
No description
jonfanz
jonfanz•7mo ago
the migrate dev command has a --create-only flag which I believe is what you need
Prisma CLI reference | Prisma Documentation
This page gives an overview of all available Prisma CLI commands, explains their options and shows numerous usage examples.
Matusko167
Matusko167OP•7mo ago
but can i call it only for one file like user.prisma? where is not defined datasource and generator? what i found inside documentation nothing helps me 😦 i am trying with chatgpt and geminy to create some parser but wihtout any solution import { getDMMF } from "@prisma/sdk"; import fs from "fs"; import path from "path"; async function generateSQL() { // Read your Prisma schema file const schemaPath = path.join(dirname, "schema.prisma"); const schema = fs.readFileSync(schemaPath, "utf-8"); // Get the DMMF (Data Model Meta Format) from the schema const dmmf = await getDMMF({ datamodel: schema }); // Initialize SQL string let sql = ""; // Iterate over the models and generate SQL statements for (const model of dmmf.datamodel.models) { sql += CREATE TABLE ${model.name} (\n; for (const field of model.fields) { sql += ${field.name} ${getSqlType(field)}${ field.isRequired ? " NOT NULL" : "" },\n; } // Remove the trailing comma and newline sql = sql.slice(0, -2); // Add primary key constraint const idFields = model.fields.filter((field) => field.isId); if (idFields.length) { sql += ,\n PRIMARY KEY (${idFields .map((field) => field.name) .join(", ")}); } sql += \n);\n\n; } // Write the generated SQL to a file fs.writeFileSync(path.join(dirname, "generated.sql"), sql); console.log("SQL generated successfully!"); } function getSqlType(field: any): string { switch (field.type) { case "String": return "VARCHAR(255)"; case "Int": return "INTEGER"; case "BigInt": return "BIGINT"; case "Float": return "FLOAT"; case "Decimal": return "DECIMAL"; case "Boolean": return "BOOLEAN"; case "DateTime": return "TIMESTAMP"; case "Json": return "JSON"; case "Bytes": return "BYTEA"; default: if (field.kind === "object") { return "INTEGER"; // Foreign key reference, typically an integer } return "TEXT"; } } generateSQL().catch((e) => console.error(e)); i make this code but its not good becouse i cant handle relations and many things becouse i cant find any functon inside prisma which can help me i want just send user.prisma file to some fucntion with provider and get back sql statement, or some function to send 2 files where these 2 files will be checked and return me differences in sql alter code
jonfanz
jonfanz•7mo ago
That's currently not possible
Matusko167
Matusko167OP•7mo ago
ouu 😦 and its possible it will be implements in short time or i need to find another way? 🙂
jonfanz
jonfanz•7mo ago
As of right now, it's not on our internal roadmap. If you'd like, you can open an issue at https://github.com/prisma/prisma and explain your use case and our team can comment
GitHub
GitHub - prisma/prisma: Next-generation ORM for Node.js & TypeScrip...
Next-generation ORM for Node.js & TypeScript | PostgreSQL, MySQL, MariaDB, SQL Server, SQLite, MongoDB and CockroachDB - prisma/prisma
Want results from more Discord servers?
Add your server