Skip to content

Commit f3cb52e

Browse files
committed
Move DB setup out of core SDK
1 parent cb7382c commit f3cb52e

34 files changed

Lines changed: 933 additions & 457 deletions

apps/cloud/package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,8 @@
6565
"@cloudflare/workers-types": "^4.20250620.0",
6666
"@effect/platform-node": "catalog:",
6767
"@effect/vitest": "catalog:",
68+
"@electric-sql/pglite": "^0.4.4",
69+
"@electric-sql/pglite-socket": "^0.1.4",
6870
"@executor-js/cli": "workspace:*",
6971
"@rhyssul/portless": "^0.13.0",
7072
"@tailwindcss/vite": "catalog:",

apps/cloud/scripts/dev-db.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,9 @@ import { setTimeout as sleep } from "node:timers/promises";
1111
import { resolve, dirname } from "node:path";
1212
import { fileURLToPath } from "node:url";
1313
import { collectTables } from "@executor-js/sdk";
14-
import { createPgliteFumaDb } from "@executor-js/sdk/pglite";
1514
import executorConfig from "../executor.config";
1615
import { ensureCloudSchema } from "../src/services/schema-init";
16+
import { createPgliteFumaDb } from "../src/services/pglite";
1717

1818
const __dirname = dirname(fileURLToPath(import.meta.url));
1919
const PORT = 5433;

apps/cloud/scripts/test-globalsetup.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,9 @@
55
// ---------------------------------------------------------------------------
66

77
import { collectTables } from "@executor-js/sdk";
8-
import { createPgliteFumaDb, type PgliteFumaDb } from "@executor-js/sdk/pglite";
98
import executorConfig from "../executor.config";
109
import { ensureCloudSchema } from "../src/services/schema-init";
10+
import { createPgliteFumaDb, type PgliteFumaDb } from "../src/services/pglite";
1111

1212
const PORT = 5434;
1313

apps/cloud/src/mcp-session.e2e.node.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,11 +33,11 @@ import {
3333
createExecutor,
3434
definePlugin,
3535
} from "@executor-js/sdk";
36-
import { createDrizzleFumaDb } from "@executor-js/sdk/drizzle";
3736
import { FetchHttpClient } from "effect/unstable/http";
3837
import { makeTestWorkOSVaultClient } from "@executor-js/plugin-workos-vault/testing";
3938
import executorConfig from "../executor.config";
4039
import { DbService } from "./services/db";
40+
import { createDrizzleFumaDb } from "./services/fuma";
4141

4242
// ---------------------------------------------------------------------------
4343
// Test-only plugin: exposes one in-memory tool that elicits once. Lets the

apps/cloud/src/services/__test-harness__/api-harness.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@ import {
2727
import { createExecutionEngine } from "@executor-js/execution";
2828
import { makeQuickJsExecutor } from "@executor-js/runtime-quickjs";
2929
import { Scope, ScopeId, collectTables, createExecutor } from "@executor-js/sdk";
30-
import { createDrizzleFumaDb } from "@executor-js/sdk/drizzle";
3130
import { makeTestWorkOSVaultClient } from "@executor-js/plugin-workos-vault/testing";
3231

3332
import executorConfig from "../../../executor.config";
@@ -38,6 +37,7 @@ import {
3837
RouterConfig,
3938
} from "../../api/protected-layers";
4039
import { DbService } from "../db";
40+
import { createDrizzleFumaDb } from "../fuma";
4141

4242
export const TEST_BASE_URL = "http://test.local";
4343
export const TEST_ORG_HEADER = "x-test-org-id";

apps/cloud/src/services/db.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ const getDirectPgliteRuntime = async (): Promise<DirectPgliteRuntime> => {
8787
{ ensureCloudSchema },
8888
] = await Promise.all([
8989
import("@executor-js/sdk"),
90-
import("@executor-js/sdk/pglite"),
90+
import("./pglite"),
9191
import("../../executor.config"),
9292
import("./schema-init"),
9393
]);

apps/cloud/src/services/executor.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,11 @@ import {
1717
createExecutor,
1818
makeHostedHttpClientLayer,
1919
} from "@executor-js/sdk";
20-
import { createDrizzleFumaDb } from "@executor-js/sdk/drizzle";
2120

2221
import { env } from "cloudflare:workers";
2322
import executorConfig from "../../executor.config";
2423
import { DbService } from "./db";
24+
import { createDrizzleFumaDb } from "./fuma";
2525

2626
// ---------------------------------------------------------------------------
2727
// Plugin list lives in `executor.config.ts` — that file is the single source
Lines changed: 5 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,7 @@ import { fumadb, type FumaDB } from "fumadb";
22
import { drizzleAdapter, type DrizzleConfig } from "fumadb/adapters/drizzle";
33
import { schema as fumaSchema } from "fumadb/schema";
44

5-
import type { FumaDb, FumaTables } from "./fuma-runtime";
6-
7-
export {
8-
createPostgresDrizzleSchema,
9-
ensurePostgresFumaSchema,
10-
type ExecutableDrizzleDb,
11-
type PostgresDrizzleSchema,
12-
type PostgresDrizzleSchemaOptions,
13-
} from "./drizzle-schema";
5+
import type { FumaDb, FumaTables } from "@executor-js/sdk";
146

157
export interface DrizzleFumaDb {
168
readonly db: FumaDb;
@@ -20,9 +12,9 @@ export interface DrizzleFumaDb {
2012
export interface CreateDrizzleFumaDbOptions<TTables extends FumaTables = FumaTables> {
2113
readonly db: DrizzleConfig["db"];
2214
readonly tables: TTables;
23-
readonly namespace?: string;
15+
readonly namespace: string;
2416
readonly version?: string;
25-
readonly provider?: DrizzleConfig["provider"];
17+
readonly provider: DrizzleConfig["provider"];
2618
}
2719

2820
const asFumaDb = (db: unknown): FumaDb => db as FumaDb;
@@ -37,13 +29,13 @@ export const createDrizzleFumaDb = <const TTables extends FumaTables>(
3729
tables: options.tables,
3830
});
3931
const factory = fumadb({
40-
namespace: options.namespace ?? "executor",
32+
namespace: options.namespace,
4133
schemas: [latestSchema],
4234
});
4335
const fuma = factory.client(
4436
drizzleAdapter({
4537
db: options.db,
46-
provider: options.provider ?? "postgresql",
38+
provider: options.provider,
4739
}),
4840
);
4941

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,14 @@ import {
55
} from "@electric-sql/pglite-socket";
66
import { drizzle, type PgliteDatabase } from "drizzle-orm/pglite";
77
import type { FumaDB } from "fumadb";
8+
import {
9+
createDrizzleRuntimeSchemaFromTables,
10+
ensureDrizzleRuntimeSchemaFromTables,
11+
} from "fumadb/adapters/drizzle";
12+
13+
import type { FumaDb, FumaTables } from "@executor-js/sdk";
814

9-
import type { FumaDb, FumaTables } from "./fuma-runtime";
10-
import { createDrizzleFumaDb } from "./drizzle";
11-
import { createPostgresDrizzleSchema, ensurePostgresFumaSchema } from "./drizzle-schema";
15+
import { createDrizzleFumaDb } from "./fuma";
1216

1317
export interface PgliteFumaDb {
1418
readonly db: FumaDb;
@@ -22,7 +26,7 @@ export interface PgliteFumaDb {
2226

2327
export interface CreatePgliteFumaDbOptions<TTables extends FumaTables = FumaTables> {
2428
readonly tables: TTables;
25-
readonly namespace?: string;
29+
readonly namespace: string;
2630
readonly version?: string;
2731
readonly dataDir?: string;
2832
readonly host?: string;
@@ -33,26 +37,29 @@ export const createPgliteFumaDb = async <const TTables extends FumaTables>(
3337
options: CreatePgliteFumaDbOptions<TTables>,
3438
): Promise<PgliteFumaDb> => {
3539
const version = options.version ?? "1.0.0";
36-
const namespace = options.namespace ?? "executor";
3740
const pglite = await PGlite.create(options.dataDir ?? "memory://");
38-
const drizzleSchema = createPostgresDrizzleSchema({
41+
const schema = createDrizzleRuntimeSchemaFromTables({
3942
tables: options.tables,
40-
namespace,
43+
namespace: options.namespace,
4144
version,
45+
provider: "postgresql",
4246
});
4347
const drizzleDb = drizzle({
4448
client: pglite,
45-
schema: drizzleSchema,
49+
schema,
4650
});
47-
await ensurePostgresFumaSchema(drizzleDb, {
51+
52+
await ensureDrizzleRuntimeSchemaFromTables(drizzleDb, {
4853
tables: options.tables,
49-
namespace,
54+
namespace: options.namespace,
5055
version,
56+
provider: "postgresql",
5157
});
58+
5259
const fuma = createDrizzleFumaDb({
5360
db: drizzleDb,
5461
tables: options.tables,
55-
namespace,
62+
namespace: options.namespace,
5663
version,
5764
provider: "postgresql",
5865
});

apps/cloud/src/services/schema-init.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
import { sql } from "drizzle-orm";
2-
import type { ExecutableDrizzleDb } from "@executor-js/sdk/drizzle";
2+
3+
interface ExecutableDrizzleDb {
4+
readonly execute: (query: ReturnType<typeof sql.raw>) => Promise<unknown>;
5+
}
36

47
const statements = [
58
`CREATE TABLE IF NOT EXISTS "accounts" (

0 commit comments

Comments
 (0)