infoalloggi-monorepo/apps/infoalloggi/src/server/db.ts
Marco Pedone 699579b432 feat: update environment variable handling and dependencies
- Added @t3-oss/env-nextjs for improved environment variable management.
- Updated zod to version 4.1.12 for enhanced validation capabilities.
- Upgraded jiti to version 2.6.1 for better module loading.
- Refactored environment variable imports from env.mjs to env.ts.
- Removed deprecated env.mjs file and replaced it with a new env.ts file using @t3-oss/env-nextjs.
- Adjusted various components and API routes to utilize the new environment variable structure.
- Updated next.config.js to support the new environment variable management.
- Modified Docker configuration to align with new BASE_URL handling.
2025-10-20 16:22:20 +02:00

73 lines
1.7 KiB
TypeScript

import {
DeduplicateJoinsPlugin,
HandleEmptyInListsPlugin,
Kysely,
type LogEvent,
PostgresDialect,
replaceWithNoncontingentExpression,
type Transaction,
} from "kysely";
import { Pool } from "pg";
import { env } from "~/env";
import type Database from "~/schemas/Database";
import type PublicSchema from "~/schemas/public/PublicSchema";
/**
* The database connection pool.
*/
const pool = new Pool({
//connectionTimeoutMillis: 2000,
database: env.POSTGRES_DB,
host: env.PGHOST,
//idleTimeoutMillis: 30000,
max: 10,
password: env.POSTGRES_PASSWORD,
port: Number.parseInt(env.PGPORT),
user: env.POSTGRES_USER,
});
type Db = typeof db;
type Trx = Transaction<PublicSchema>;
export type Querier = Db | Trx;
/**
* The database instance.
*/
export const db = new Kysely<Database>({
dialect: new PostgresDialect({
pool: pool,
}),
log: (_event) => {
//DbLogger(event);
},
//plugins: [new SerializePlugin()]
//plugins: [new CamelCasePlugin()],
plugins: [
new DeduplicateJoinsPlugin(),
new HandleEmptyInListsPlugin({
strategy: replaceWithNoncontingentExpression,
}),
],
});
const _DbLogger = (event: LogEvent) => {
if (
event.level === "query" &&
event.query.sql !== "begin" &&
event.query.sql !== "commit"
) {
console.log();
console.log("\x1b[33mquery:\x1b[0m", event.query.sql);
console.log("\x1b[36mparams:\x1b[0m", event.query.parameters);
//console.log("\x1b[32mtaken:\x1b[0m", event.queryDurationMillis, "ms");
}
if (event.level === "error") {
console.error();
console.error("\x1b[33mquery:\x1b[0m", event.query.sql);
console.error("\x1b[36mparams:\x1b[0m", event.query.parameters);
//console.error("\x1b[31merror:\x1b[0m", event.error);
}
};