SPB Git forge

spb/polyllm

Public
15commits 1branches 0releases
2.2 MBsize
maindefault branch
13 days agolast push
TypeScript 97.4% SQL 1% JavaScript 0.9% CSS 0.6%
1.4 KB · 52 lines typescript
Raw Blame History
1import { drizzle, type NodePgDatabase } from "drizzle-orm/node-postgres";2import { Pool } from "pg";3import * as core from "./schema";4import * as workspace from "./schema-workspace";5const schema = { ...core, ...workspace };67export type Db = NodePgDatabase<typeof schema>;89declare global {10  var __polyllmPool: Pool | undefined;11  var __polyllmDb: Db | undefined;12}1314function createPool(): Pool {15  const connectionString = process.env.DATABASE_URL ?? "postgres://localhost:5432/polyllm";16  return new Pool({17    connectionString,18    max: Number(process.env.DB_POOL_MAX ?? 10),19    idleTimeoutMillis: 30_000,20    connectionTimeoutMillis: 10_000,21    application_name: "polyllm",22  });23}2425/** Process-wide singleton (survives Next.js HMR in dev). */26export function getDb(): Db {27  if (!globalThis.__polyllmDb) {28    globalThis.__polyllmPool = globalThis.__polyllmPool ?? createPool();29    globalThis.__polyllmDb = drizzle(globalThis.__polyllmPool, { schema });30  }31  return globalThis.__polyllmDb;32}3334export async function closeDb() {35  await globalThis.__polyllmPool?.end();36  globalThis.__polyllmPool = undefined;37  globalThis.__polyllmDb = undefined;38}3940export async function pingDb(): Promise<boolean> {41  try {42    const pool = globalThis.__polyllmPool ?? (getDb(), globalThis.__polyllmPool!);43    await pool.query("select 1");44    return true;45  } catch {46    return false;47  }48}4950export * from "./schema";51export * from "./schema-workspace";52