Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion apps/cloud/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@
"react-dom": "catalog:"
},
"devDependencies": {
"@electric-sql/pglite": "^0.4.3",
"@tailwindcss/vite": "catalog:",
"@types/pg": "^8.15.4",
"@types/react": "catalog:",
Expand Down
2 changes: 0 additions & 2 deletions apps/cloud/src/env.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ const sharedShape = {

const serverShape = {
DATABASE_URL: Env.stringOr("DATABASE_URL", ""),
PGLITE_DATA_DIR: Env.stringOr("PGLITE_DATA_DIR", ".pglite"),
ENCRYPTION_KEY: Env.stringOr(
"ENCRYPTION_KEY",
"local-dev-encryption-key",
Expand All @@ -28,7 +27,6 @@ type SharedEnv = Readonly<{

type ServerEnv = SharedEnv & Readonly<{
DATABASE_URL: string;
PGLITE_DATA_DIR: string;
ENCRYPTION_KEY: string;
WORKOS_API_KEY: string;
WORKOS_CLIENT_ID: string;
Expand Down
35 changes: 9 additions & 26 deletions apps/cloud/src/services/db.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,11 @@ const resolveHyperdriveUrl = Effect.tryPromise({

const resolveConnectionString = resolveHyperdriveUrl.pipe(
Effect.map((url) => url ?? (server.DATABASE_URL || undefined)),
Effect.flatMap((url) =>
url
? Effect.succeed(url)
: Effect.fail(new Error("No database connection string available (set DATABASE_URL or configure Hyperdrive)")),
),
);

// ---------------------------------------------------------------------------
Expand All @@ -47,22 +52,6 @@ const acquirePostgres = (connectionString: string) =>
const releasePostgres = ({ pool }: { pool: { end: () => Promise<void> } }) =>
Effect.promise(() => pool.end()).pipe(Effect.orElseSucceed(() => undefined));

// ---------------------------------------------------------------------------
// PGlite — local dev fallback
// ---------------------------------------------------------------------------

const acquirePglite = Effect.tryPromise(async () => {
const { PGlite } = await import("@electric-sql/pglite");
const { drizzle } = await import("drizzle-orm/pglite");
const client = new PGlite(server.PGLITE_DATA_DIR);
return { db: drizzle(client, { schema }) as DrizzleDb, client };
});

const releasePglite = ({ client }: { client: { close?: () => Promise<void> } }) =>
Effect.promise(() => client.close?.() ?? Promise.resolve()).pipe(
Effect.orElseSucceed(() => undefined),
);

// ---------------------------------------------------------------------------
// Service
// ---------------------------------------------------------------------------
Expand All @@ -75,16 +64,10 @@ export class DbService extends Context.Tag("@executor/cloud/DbService")<
this,
Effect.gen(function* () {
const connectionString = yield* resolveConnectionString;

if (connectionString) {
const { db } = yield* Effect.acquireRelease(
acquirePostgres(connectionString),
releasePostgres,
);
return db;
}

const { db } = yield* Effect.acquireRelease(acquirePglite, releasePglite);
const { db } = yield* Effect.acquireRelease(
acquirePostgres(connectionString),
releasePostgres,
);
return db;
}),
);
Expand Down