SPB Git forge
38commits 1branches 0releases
338.7 MBsize
maindefault branch
3 h agolast push
HTML 53.9% TypeScript 44.5% JavaScript 0.6% SQL 0.5%
1.3 KB · 37 lines typescript
Raw Blame History
1import { drizzle, type PostgresJsDatabase } from "drizzle-orm/postgres-js";2import postgres from "postgres";3import * as schema from "./schema.js";45export * from "./schema.js";6export { schema };7export { sql, eq, and, or, desc, asc, inArray, isNull, isNotNull, gte, lte, lt, gt, ilike, like, ne, count, sum, avg, max, min, notInArray } from "drizzle-orm";89export type Db = PostgresJsDatabase<typeof schema>;1011let _client: ReturnType<typeof postgres> | null = null;12let _db: Db | null = null;1314export function getSql(): ReturnType<typeof postgres> {15  if (!_client) {16    const url = process.env.DATABASE_URL ?? "postgres://dci:dci@127.0.0.1:5432/dci";17    _client = postgres(url, { max: Number(process.env.PG_POOL_MAX ?? 16), idle_timeout: 30, connect_timeout: 15, prepare: true, onnotice: () => {} });18  }19  return _client;20}2122export function getDb(): Db {23  if (!_db) _db = drizzle(getSql(), { schema, casing: "snake_case" });24  return _db;25}2627export async function closeDb(): Promise<void> {28  if (_client) await _client.end({ timeout: 5 });29  _client = null;30  _db = null;31}3233/** Postgres `text[]` literal helper — drizzle `sql\`${arr}\`` would spread arrays into tuples. */34export function textArray(values: string[]): string {35  return `{${values.map((v) => `"${v.replace(/(["\\])/g, "\\$1")}"`).join(",")}}`;36}37