SPB Git

Repository Public

db.ts 3.1 KB 80 lines typescript

1/**2 * =============================================================================3 *                  SP BOUCHER | SOFTWARE & RESEARCH4 *             ARTIFICIAL INTELLIGENCE • FINANCE • ENGINEERING5 * =============================================================================6 *7 * Project          : UQO Éval — Le Grand Défi Immobilier8 * File             : apps/api/src/db.ts9 * Module           : api10 * Repository       : https://git.spboucher.ai/uqo-eval-jeux11 * Project Website  : https://jeux.uqo-eval.app12 * Created          : 2026-11-2513 * Last Modified    : 2026-11-2514 *15 * Author           : Simon-Pierre Boucher16 * Position         : Professor of Finance17 * Institution      : Université du Québec en Outaouais (UQO)18 * Organization     : SP Boucher19 * Website          : https://spboucher.ai20 * Git              : https://git.spboucher.ai21 *22 * Preferred Contact: contact@spboucher.ai23 * Development      : dev@spboucher.ai24 * Support          : support@spboucher.ai25 * Security/Admin   : admin@spboucher.ai26 *27 * Copyright (c) 202628 * Simon-Pierre Boucher29 * All rights reserved where applicable. This file is governed by the30 * applicable project license, copyright notices and relevant law.31 * Existing third-party rights and notices remain unaffected.32 *33 * License          : None specified (no license file in this repository)34 *35 * Do not embed credentials, private keys, access tokens,36 * certificates or confidential personal information.37 *38 * =============================================================================39 * END OF SP BOUCHER SOURCE HEADER40 * =============================================================================41 */4243import fs from "node:fs";44import path from "node:path";45import postgres from "postgres";46import { config } from "./config.ts";4748export const sql = postgres(config.databaseUrl, {49  max: Number(process.env.DATABASE_POOL ?? 20),50  idle_timeout: 30,51  onnotice: () => {},52  types: {53    // Les dates restent des chaînes ISO : pas de surprise de fuseau.54    date: { to: 1082, from: [1082], serialize: (v: string) => v, parse: (v: string) => v },55  },56});5758export type Sql = typeof sql;59export type Tx = postgres.TransactionSql;6061/** Applique, dans l'ordre, les migrations SQL qui ne l'ont pas encore été. */62export async function migrate(log: (message: string) => void = () => {}): Promise<string[]> {63  await sql`create table if not exists schema_migrations (name text primary key, applied_at timestamptz not null default now())`;64  const dir = path.join(config.root, "database/migrations");65  const files = fs.readdirSync(dir).filter((f) => f.endsWith(".sql")).sort();66  const done = new Set((await sql<{ name: string }[]>`select name from schema_migrations`).map((r) => r.name));67  const applied: string[] = [];68  for (const file of files) {69    if (done.has(file)) continue;70    const text = fs.readFileSync(path.join(dir, file), "utf8");71    await sql.begin(async (tx) => {72      await tx.unsafe(text);73      await tx`insert into schema_migrations (name) values (${file})`;74    });75    log(`migration appliquée : ${file}`);76    applied.push(file);77  }78  return applied;79}80
Files main
Menu

Theme