Skip to content

Commit

Permalink
Add the Prisma schema generator
Browse files Browse the repository at this point in the history
Add the `prisma` command to the list of valid @edgedb/generate commands.
It requires a `--file` and some way of connecting the client (DSN or
whatever other method). The generated Prisma schema will be output as
specified by `--file`.
  • Loading branch information
vpetrovykh committed Dec 19, 2024
1 parent d153164 commit be7976b
Show file tree
Hide file tree
Showing 2 changed files with 757 additions and 4 deletions.
29 changes: 25 additions & 4 deletions packages/generate/src/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,15 @@ import { generateQueryBuilder } from "./edgeql-js";
import { runInterfacesGenerator } from "./interfaces";
import { type Target, exitWithError } from "./genutil";
import { generateQueryFiles } from "./queries";
import { runGelPrismaGenerator } from "./gel-prisma";

const { path, readFileUtf8, exists } = adapter;

enum Generator {
QueryBuilder = "edgeql-js",
Queries = "queries",
Interfaces = "interfaces",
GelPrisma = "prisma",
}

const availableGeneratorsHelp = `
Expand Down Expand Up @@ -65,6 +67,8 @@ const run = async () => {
case Generator.Interfaces:
options.target = "ts";
break;
case Generator.GelPrisma:
break;
}

let projectRoot: string | null = null;
Expand Down Expand Up @@ -190,7 +194,10 @@ const run = async () => {
options.useHttpClient = true;
break;
case "--target": {
if (generator === Generator.Interfaces) {
if (
generator === Generator.Interfaces ||
generator === Generator.GelPrisma
) {
exitWithError(
`--target is not supported for generator "${generator}"`,
);
Expand Down Expand Up @@ -220,7 +227,10 @@ const run = async () => {
options.out = getVal();
break;
case "--file":
if (generator === Generator.Interfaces) {
if (
generator === Generator.Interfaces ||
generator === Generator.GelPrisma
) {
options.file = getVal();
} else if (generator === Generator.Queries) {
if (args.length > 0 && args[0][0] !== "-") {
Expand Down Expand Up @@ -290,9 +300,13 @@ const run = async () => {
case Generator.Interfaces:
console.log(`Generating TS interfaces from schema...`);
break;
case Generator.GelPrisma:
console.log(`Generating Prisma schema from database...`);
break;
}

if (!options.target) {
// don't need to do any of that for the prisma schema generator
if (!options.target && generator !== Generator.GelPrisma) {
if (!projectRoot) {
throw new Error(
`Failed to detect project root.
Expand Down Expand Up @@ -411,6 +425,12 @@ Run this command inside an EdgeDB project directory or specify the desired targe
schemaDir,
});
break;
case Generator.GelPrisma:
await runGelPrismaGenerator({
options,
client,
});
break;
}
} catch (e) {
exitWithError((e as Error).message);
Expand All @@ -432,6 +452,7 @@ COMMANDS:
queries Generate typed functions from .edgeql files
edgeql-js Generate query builder
interfaces Generate TS interfaces for schema types
prisma Generate a Prisma schema for an existing database instance
CONNECTION OPTIONS:
Expand Down Expand Up @@ -460,7 +481,7 @@ OPTIONS:
Change the output directory the querybuilder files are generated into
(Only valid for 'edgeql-js' generator)
--file <path>
Change the output filepath of the 'queries' and 'interfaces' generators
Change the output filepath of the 'queries', 'interfaces', and 'prisma' generators
When used with the 'queries' generator, also changes output to single-file mode
--force-overwrite
Overwrite <path> contents without confirmation
Expand Down
Loading

0 comments on commit be7976b

Please sign in to comment.