Phase 1: Monorepo scaffold, database, and dev environment
Turborepo monorepo with @forte/shared and @forte/backend workspaces. Docker Compose dev env with PostgreSQL 16 + Valkey 8. Fastify server with Pino JSON logging, request ID tracing, and health endpoint. Drizzle ORM with company + location tables. Includes: - Root config (turbo, tsconfig, eslint, prettier) - @forte/shared: types, schemas, currency/date utils - @forte/backend: Fastify entry, plugins (database, redis, cors, error-handler, dev-auth), health route, Drizzle schema + migration - Dev auth bypass via X-Dev-Company/Location/User headers - Vitest integration test with clean DB per test (forte_test) - Seed script for dev company + location
This commit is contained in:
28
packages/backend/src/plugins/database.ts
Normal file
28
packages/backend/src/plugins/database.ts
Normal file
@@ -0,0 +1,28 @@
|
||||
import fp from 'fastify-plugin'
|
||||
import { drizzle } from 'drizzle-orm/postgres-js'
|
||||
import postgres from 'postgres'
|
||||
import * as schema from '../db/schema/stores.js'
|
||||
|
||||
declare module 'fastify' {
|
||||
interface FastifyInstance {
|
||||
db: ReturnType<typeof drizzle<typeof schema>>
|
||||
sql: ReturnType<typeof postgres>
|
||||
}
|
||||
}
|
||||
|
||||
export const databasePlugin = fp(async (app) => {
|
||||
const connectionString = process.env.DATABASE_URL
|
||||
if (!connectionString) {
|
||||
throw new Error('DATABASE_URL environment variable is required')
|
||||
}
|
||||
|
||||
const sql = postgres(connectionString)
|
||||
const db = drizzle(sql, { schema })
|
||||
|
||||
app.decorate('db', db)
|
||||
app.decorate('sql', sql)
|
||||
|
||||
app.addHook('onClose', async () => {
|
||||
await sql.end()
|
||||
})
|
||||
})
|
||||
Reference in New Issue
Block a user