Add container build for deployment

Standalone Next output, migration script run on start, storage volume
at /app/storage, port 4700. Database connection is created lazily so
the image builds without a database.
This commit is contained in:
Matthias Giesselmann
2026-07-31 21:52:38 +02:00
parent b90ff252d1
commit 7582c64709
6 changed files with 114 additions and 7 deletions
+35 -7
View File
@@ -2,13 +2,41 @@ import { drizzle } from 'drizzle-orm/node-postgres'
import { Pool } from 'pg'
import * as schema from './schema'
const isTest = process.env.VITEST === 'true' || process.env.NODE_ENV === 'test'
const url = isTest ? process.env.DATABASE_URL_TEST : process.env.DATABASE_URL
type Client = ReturnType<typeof drizzle<typeof schema>>
if (!url) {
throw new Error(isTest ? 'DATABASE_URL_TEST is not set' : 'DATABASE_URL is not set')
let connection: { pool: Pool, db: Client } | undefined
function connectionUrl(): string {
const isTest = process.env.VITEST === 'true' || process.env.NODE_ENV === 'test'
const url = isTest ? process.env.DATABASE_URL_TEST : process.env.DATABASE_URL
if (!url) {
throw new Error(isTest ? 'DATABASE_URL_TEST is not set' : 'DATABASE_URL is not set')
}
return url
}
export const pool = new Pool({ connectionString: url })
export const db = drizzle(pool, { schema })
export type Db = typeof db
function connect(): { pool: Pool, db: Client } {
if (!connection) {
const pool = new Pool({ connectionString: connectionUrl() })
connection = { pool, db: drizzle(pool, { schema }) }
}
return connection
}
export const pool = new Proxy({} as Pool, {
get(_target, property) {
return Reflect.get(connect().pool, property, connect().pool)
},
})
export const db = new Proxy({} as Client, {
get(_target, property) {
return Reflect.get(connect().db, property, connect().db)
},
})
export type Db = Client