fix(data): expose database client to CLI scripts
This commit is contained in:
@@ -0,0 +1,37 @@
|
|||||||
|
import { drizzle, type PostgresJsDatabase } from "drizzle-orm/postgres-js";
|
||||||
|
import postgres from "postgres";
|
||||||
|
|
||||||
|
import * as schema from "@/db/schema";
|
||||||
|
|
||||||
|
type Database = PostgresJsDatabase<typeof schema>;
|
||||||
|
|
||||||
|
let database: Database | undefined;
|
||||||
|
let client: ReturnType<typeof postgres> | undefined;
|
||||||
|
|
||||||
|
export function getDb(): Database {
|
||||||
|
const databaseUrl = process.env.DATABASE_URL;
|
||||||
|
|
||||||
|
if (!databaseUrl) {
|
||||||
|
throw new Error(
|
||||||
|
"DATABASE_URL is required for database-backed routes. Copy .env.example to .env and configure it.",
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!database) {
|
||||||
|
client = postgres(databaseUrl, {
|
||||||
|
max: Number(process.env.DATABASE_POOL_SIZE ?? 10),
|
||||||
|
prepare: false,
|
||||||
|
});
|
||||||
|
database = drizzle(client, { schema });
|
||||||
|
}
|
||||||
|
|
||||||
|
return database;
|
||||||
|
}
|
||||||
|
|
||||||
|
export async function closeDb(): Promise<void> {
|
||||||
|
await client?.end();
|
||||||
|
client = undefined;
|
||||||
|
database = undefined;
|
||||||
|
}
|
||||||
|
|
||||||
|
export type { Database };
|
||||||
+1
-37
@@ -1,39 +1,3 @@
|
|||||||
import "server-only";
|
import "server-only";
|
||||||
|
|
||||||
import { drizzle, type PostgresJsDatabase } from "drizzle-orm/postgres-js";
|
export { closeDb, getDb, type Database } from "@/db/client";
|
||||||
import postgres from "postgres";
|
|
||||||
|
|
||||||
import * as schema from "@/db/schema";
|
|
||||||
|
|
||||||
type Database = PostgresJsDatabase<typeof schema>;
|
|
||||||
|
|
||||||
let database: Database | undefined;
|
|
||||||
let client: ReturnType<typeof postgres> | undefined;
|
|
||||||
|
|
||||||
export function getDb(): Database {
|
|
||||||
const databaseUrl = process.env.DATABASE_URL;
|
|
||||||
|
|
||||||
if (!databaseUrl) {
|
|
||||||
throw new Error(
|
|
||||||
"DATABASE_URL is required for database-backed routes. Copy .env.example to .env and configure it.",
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!database) {
|
|
||||||
client = postgres(databaseUrl, {
|
|
||||||
max: Number(process.env.DATABASE_POOL_SIZE ?? 10),
|
|
||||||
prepare: false,
|
|
||||||
});
|
|
||||||
database = drizzle(client, { schema });
|
|
||||||
}
|
|
||||||
|
|
||||||
return database;
|
|
||||||
}
|
|
||||||
|
|
||||||
export async function closeDb(): Promise<void> {
|
|
||||||
await client?.end();
|
|
||||||
client = undefined;
|
|
||||||
database = undefined;
|
|
||||||
}
|
|
||||||
|
|
||||||
export type { Database };
|
|
||||||
|
|||||||
+1
-1
@@ -1,6 +1,6 @@
|
|||||||
import { migrate } from "drizzle-orm/postgres-js/migrator";
|
import { migrate } from "drizzle-orm/postgres-js/migrator";
|
||||||
|
|
||||||
import { closeDb, getDb } from "@/db";
|
import { closeDb, getDb } from "@/db/client";
|
||||||
|
|
||||||
await migrate(getDb(), { migrationsFolder: "drizzle" });
|
await migrate(getDb(), { migrationsFolder: "drizzle" });
|
||||||
await closeDb();
|
await closeDb();
|
||||||
|
|||||||
@@ -1,4 +1,4 @@
|
|||||||
import { closeDb, getDb } from "@/db";
|
import { closeDb, getDb } from "@/db/client";
|
||||||
import { templates } from "@/db/schema";
|
import { templates } from "@/db/schema";
|
||||||
import { builtInTemplates } from "@/lib/templates";
|
import { builtInTemplates } from "@/lib/templates";
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user