22 lines
526 B
TypeScript
22 lines
526 B
TypeScript
import fp from 'fastify-plugin';
|
|
import type { FastifyInstance } from 'fastify';
|
|
import { createDb, type Database } from '@source/database';
|
|
|
|
declare module 'fastify' {
|
|
interface FastifyInstance {
|
|
db: Database;
|
|
}
|
|
}
|
|
|
|
export default fp(async (app: FastifyInstance) => {
|
|
const databaseUrl = process.env.DATABASE_URL;
|
|
if (!databaseUrl) {
|
|
throw new Error('DATABASE_URL environment variable is required');
|
|
}
|
|
|
|
const db = createDb(databaseUrl);
|
|
app.decorate('db', db);
|
|
|
|
app.log.info('Database connected');
|
|
});
|