Files
samreshu_backend/src/plugins/database.ts
Anton a7394c4d9d feat: add Drizzle enums for schema
Made-with: Cursor
2026-03-04 13:37:55 +03:00

34 lines
821 B
TypeScript

import { FastifyInstance, FastifyPluginAsync } from 'fastify';
import { drizzle } from 'drizzle-orm/node-postgres';
import pg from 'pg';
import fp from 'fastify-plugin';
import { env } from '../config/env.js';
import * as schema from '../db/schema/index.js';
const { Pool } = pg;
declare module 'fastify' {
interface FastifyInstance {
db: ReturnType<typeof drizzle<typeof schema>>;
}
}
const databasePlugin: FastifyPluginAsync = async (app: FastifyInstance) => {
const pool = new Pool({
connectionString: env.DATABASE_URL,
max: 10,
idleTimeoutMillis: 30000,
connectionTimeoutMillis: 5000,
});
const db = drizzle(pool, { schema });
app.decorate('db', db);
app.addHook('onClose', async () => {
await pool.end();
});
};
export default fp(databasePlugin, { name: 'database' });