多阶段镜像编出 server/engine 单文件与前端静态;compose 起 pg/server/engine/caddy; 启动时自动迁移;仅用 DATABASE_URL,postgres 建库参数由 entrypoint 拆解。
20 lines
661 B
TypeScript
20 lines
661 B
TypeScript
import { drizzle } from "drizzle-orm/postgres-js";
|
|
import { migrate } from "drizzle-orm/postgres-js/migrator";
|
|
import postgres from "postgres";
|
|
import * as schema from "./schema.ts";
|
|
|
|
export function createDb(connectionString: string) {
|
|
const client = postgres(connectionString, { max: 10 });
|
|
return drizzle(client, { schema });
|
|
}
|
|
|
|
export type Db = ReturnType<typeof createDb>;
|
|
|
|
/** 启动时按 journal 应用缺失迁移(幂等);`migrationsFolder` 指向 drizzle/ 目录 */
|
|
export async function runMigrations(db: Db, migrationsFolder: string) {
|
|
await migrate(db, { migrationsFolder });
|
|
}
|
|
|
|
export * from "./schema.ts";
|
|
export * from "./repos.ts";
|