forked from git.gladyson/openmonetis
- Replace ESLint with Biome for linting and formatting - Configure Biome with tabs, double quotes, and organized imports - Move all SQL/Drizzle queries from page.tsx files to data.ts files - Create new data.ts files for: ajustes, dashboard, relatorios/categorias - Update existing data.ts files: extrato, fatura (add lancamentos queries) - Remove all drizzle-orm imports from page.tsx files - Update README.md with new tooling info Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
40 lines
871 B
TypeScript
40 lines
871 B
TypeScript
import { drizzle, type PgDatabase } from "drizzle-orm/node-postgres";
|
|
import { Pool } from "pg";
|
|
import * as schema from "@/db/schema";
|
|
|
|
const globalForDb = globalThis as unknown as {
|
|
db?: PgDatabase<typeof schema>;
|
|
pool?: Pool;
|
|
};
|
|
|
|
let _db: PgDatabase<typeof schema> | undefined;
|
|
let _pool: Pool | undefined;
|
|
|
|
function getDb() {
|
|
if (_db) return _db;
|
|
|
|
const { DATABASE_URL } = process.env;
|
|
|
|
if (!DATABASE_URL) {
|
|
throw new Error("DATABASE_URL env variable is not set");
|
|
}
|
|
|
|
_pool = globalForDb.pool ?? new Pool({ connectionString: DATABASE_URL });
|
|
_db = globalForDb.db ?? drizzle(_pool, { schema });
|
|
|
|
if (process.env.NODE_ENV !== "production") {
|
|
globalForDb.pool = _pool;
|
|
globalForDb.db = _db;
|
|
}
|
|
|
|
return _db;
|
|
}
|
|
|
|
export const db = new Proxy({} as PgDatabase<typeof schema>, {
|
|
get(_, prop) {
|
|
return Reflect.get(getDb(), prop);
|
|
},
|
|
});
|
|
|
|
export { schema };
|