|
| 1 | +import { existsSync } from "node:fs"; |
| 2 | +import fs from "node:fs/promises"; |
| 3 | +import path from "node:path"; |
| 4 | +import { Command } from "commander"; |
| 5 | +import { collectTables } from "@executor-js/sdk/core"; |
| 6 | +import { getConfig } from "../utils/get-config.js"; |
| 7 | + |
| 8 | +type SchemaGenerateOptions = { |
| 9 | + readonly cwd: string; |
| 10 | + readonly config?: string; |
| 11 | + readonly output?: string; |
| 12 | + readonly namespace: string; |
| 13 | + readonly adapter: string; |
| 14 | + readonly provider: string; |
| 15 | + readonly version: string; |
| 16 | +}; |
| 17 | + |
| 18 | +const schemaGenerateAction = async (opts: SchemaGenerateOptions) => { |
| 19 | + const cwd = path.resolve(opts.cwd); |
| 20 | + if (!existsSync(cwd)) { |
| 21 | + console.error(`The directory "${cwd}" does not exist.`); |
| 22 | + process.exit(1); |
| 23 | + } |
| 24 | + |
| 25 | + const config = await getConfig({ cwd, configPath: opts.config }); |
| 26 | + if (!config) { |
| 27 | + console.error( |
| 28 | + "No configuration file found. Add an `executor.config.ts` file to " + |
| 29 | + "your project or pass the path using the `--config` flag.", |
| 30 | + ); |
| 31 | + process.exit(1); |
| 32 | + } |
| 33 | + if (opts.adapter !== "drizzle") { |
| 34 | + console.error(`Unsupported schema adapter "${opts.adapter}". Supported adapters: drizzle.`); |
| 35 | + process.exit(1); |
| 36 | + } |
| 37 | + if (opts.provider !== "mysql" && opts.provider !== "postgresql" && opts.provider !== "sqlite") { |
| 38 | + console.error( |
| 39 | + `Unsupported drizzle provider "${opts.provider}". Supported providers: mysql, postgresql, sqlite.`, |
| 40 | + ); |
| 41 | + process.exit(1); |
| 42 | + } |
| 43 | + |
| 44 | + const [{ fumadb }, { drizzleAdapter }, { schema: fumaSchema }] = await Promise.all([ |
| 45 | + import("fumadb"), |
| 46 | + import("fumadb/adapters/drizzle"), |
| 47 | + import("fumadb/schema"), |
| 48 | + ]); |
| 49 | + |
| 50 | + const schema = fumaSchema({ |
| 51 | + version: opts.version, |
| 52 | + tables: collectTables(config.plugins()), |
| 53 | + }); |
| 54 | + const factory = fumadb({ |
| 55 | + namespace: opts.namespace, |
| 56 | + schemas: [schema], |
| 57 | + }); |
| 58 | + const generated = factory |
| 59 | + .client( |
| 60 | + drizzleAdapter({ |
| 61 | + db: {}, |
| 62 | + provider: opts.provider, |
| 63 | + }), |
| 64 | + ) |
| 65 | + .generateSchema("latest", opts.namespace); |
| 66 | + |
| 67 | + const output = opts.output ?? generated.path; |
| 68 | + const outPath = path.resolve(cwd, output); |
| 69 | + await fs.mkdir(path.dirname(outPath), { recursive: true }); |
| 70 | + await fs.writeFile(outPath, generated.code); |
| 71 | + console.log(`Schema generated: ${path.relative(cwd, outPath)}`); |
| 72 | +}; |
| 73 | + |
| 74 | +export const schema = new Command("schema") |
| 75 | + .description("Database schema utilities") |
| 76 | + .addCommand( |
| 77 | + new Command("generate") |
| 78 | + .description("Generate an ORM schema file from the executor config") |
| 79 | + .option("-c, --cwd <cwd>", "the working directory", process.cwd()) |
| 80 | + .option("--config <config>", "path to the executor config file") |
| 81 | + .option("--output <output>", "output file path for the generated schema") |
| 82 | + .option("--namespace <namespace>", "FumaDB namespace", "executor") |
| 83 | + .option("--adapter <adapter>", "FumaDB adapter", "drizzle") |
| 84 | + .option("--provider <provider>", "database provider", "postgresql") |
| 85 | + .option("--version <version>", "FumaDB schema version", "1.0.0") |
| 86 | + .action(schemaGenerateAction), |
| 87 | + ); |
0 commit comments