Skip to content

Commit bac9acc

Browse files
committed
Restore cloud database scripts
1 parent ab10f6b commit bac9acc

7 files changed

Lines changed: 239 additions & 8 deletions

File tree

apps/cloud/drizzle.config.ts

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import { defineConfig } from "drizzle-kit";
2+
3+
// drizzle-kit studio reads `dbCredentials.url`; everything else (generate,
4+
// migrate) ignores it. Default to the local PGlite socket started by
5+
// `bun run dev:db`; override via `DATABASE_URL` for prod studio sessions.
6+
// drizzle-kit uses node-postgres (`pg`) for studio and the `ssl` option in
7+
// dbCredentials doesn't reliably reach the pool - append `sslmode=require`
8+
// directly to the URL instead, which `pg` honours.
9+
const DEFAULT_DEV_URL = "postgresql://postgres:postgres@127.0.0.1:5433/postgres";
10+
11+
const withSslMode = (url: string): string => {
12+
if (url.includes("127.0.0.1") || url.includes("localhost")) return url;
13+
if (/[?&]sslmode=/.test(url)) return url;
14+
return url + (url.includes("?") ? "&" : "?") + "sslmode=require";
15+
};
16+
17+
export default defineConfig({
18+
schema: ["./src/services/schema.ts", "./src/services/executor-schema.ts"],
19+
out: "./drizzle",
20+
dialect: "postgresql",
21+
dbCredentials: {
22+
url: withSslMode(process.env.DATABASE_URL ?? DEFAULT_DEV_URL),
23+
},
24+
});

apps/cloud/package.json

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,12 @@
88
"dev:proxy": "portless proxy start --multiplex --shared-port --port 5394 || (portless proxy stop -p 5394 && portless proxy start --multiplex --shared-port --port 5394)",
99
"dev:db": "bun run scripts/dev-db.ts",
1010
"dev:vite": "EXECUTOR_DIRECT_DATABASE_URL=true CLOUDFLARE_INCLUDE_PROCESS_ENV=true op run --env-file=.env.op -- portless --name executor-cloud vite dev",
11+
"db:schema": "node --import jiti/register ../../packages/core/cli/src/index.ts schema generate --config ./executor.config.ts --output ./src/services/executor-schema.ts --namespace executor_cloud --adapter drizzle --provider postgresql",
12+
"db:generate": "drizzle-kit generate",
13+
"db:studio": "drizzle-kit studio",
14+
"db:studio:prod": "op run --env-file=.env.production -- bun --bun ../../node_modules/.bun/node_modules/drizzle-kit/bin.cjs studio",
15+
"db:migrate:prod": "op run --env-file=.env.production -- bun --bun ../../node_modules/.bun/node_modules/drizzle-kit/bin.cjs migrate",
16+
"db:migrate:dev": "op run --env-file=.env.op -- bun --bun ../../node_modules/.bun/node_modules/drizzle-kit/bin.cjs migrate",
1117
"build": "node scripts/build.mjs",
1218
"preview": "vite preview",
1319
"deploy": "op run --env-file=.env.production -- bash -lc 'bun run build && wrangler deploy -c dist/server/wrangler.json'",
@@ -74,6 +80,7 @@
7480
"@types/react-dom": "catalog:",
7581
"@vitejs/plugin-react": "catalog:",
7682
"concurrently": "^9.2.1",
83+
"drizzle-kit": "catalog:",
7784
"jiti": "^2.6.1",
7885
"typescript": "catalog:",
7986
"vite": "catalog:",

bun.lock

Lines changed: 112 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/core/cli/package.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
"directory": "packages/core/cli"
1414
},
1515
"bin": {
16-
"executor": "./dist/index.js"
16+
"executor-sdk": "./dist/index.js"
1717
},
1818
"files": [
1919
"dist"
@@ -40,12 +40,13 @@
4040
"typecheck": "tsc --noEmit"
4141
},
4242
"dependencies": {
43+
"@executor-js/sdk": "workspace:*",
4344
"commander": "^12.1.0",
4445
"effect": "catalog:",
46+
"fumadb": "workspace:*",
4547
"jiti": "^2.6.1"
4648
},
4749
"devDependencies": {
48-
"@executor-js/sdk": "workspace:*",
4950
"@types/node": "catalog:",
5051
"tsup": "catalog:",
5152
"typescript": "catalog:",
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
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+
);

packages/core/cli/src/index.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,15 @@
11
#!/usr/bin/env node
22

33
import { Command } from "commander";
4+
import { schema } from "./commands/schema.js";
45

56
process.on("SIGINT", () => process.exit(0));
67
process.on("SIGTERM", () => process.exit(0));
78

8-
const program = new Command("executor")
9+
const program = new Command("executor-sdk")
910
.version("0.0.1")
10-
.description("Executor CLI")
11+
.description("Executor SDK CLI")
12+
.addCommand(schema)
1113
.action(() => program.help());
1214

13-
program.parse();
15+
await program.parseAsync();

packages/core/fumadb/src/adapters/drizzle/generate.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,7 @@ export function generateSchema(
155155
if (!typeFn.isCustomType) imports.addImport(typeFn.name, importSource);
156156
col.push(`${typeFn.name}(${params.join(", ")})`);
157157

158-
if (column instanceof IdColumn) {
158+
if (column instanceof IdColumn || (column as { id?: unknown }).id === true) {
159159
col.push("primaryKey()");
160160
}
161161

0 commit comments

Comments
 (0)