fix/playtest-feedback #19
6 changed files with 44 additions and 25 deletions
21
apps/server/src/db/client.ts
Normal file
21
apps/server/src/db/client.ts
Normal file
|
|
@ -0,0 +1,21 @@
|
||||||
|
// Création du client postgres-js. Deux modes :
|
||||||
|
// 1. DATABASE_URL (DSN complet) — pratique en dev.
|
||||||
|
// 2. Variables PG* (PGHOST/PGPORT/PGUSER/PGPASSWORD/PGDATABASE) — recommandé en
|
||||||
|
// prod : le mot de passe est passé tel quel (pas d'encodage d'URL à gérer).
|
||||||
|
// postgres-js lit automatiquement les PG* de l'environnement quand on ne lui
|
||||||
|
// passe pas de DSN.
|
||||||
|
|
||||||
|
import postgres from "postgres"
|
||||||
|
import { env } from "../env"
|
||||||
|
|
||||||
|
export const hasDbConfig = !!env.databaseUrl || !!process.env.PGHOST
|
||||||
|
|
||||||
|
export function createSqlClient(opts: { max?: number } = {}) {
|
||||||
|
if (env.databaseUrl) {
|
||||||
|
return postgres(env.databaseUrl, opts)
|
||||||
|
}
|
||||||
|
if (process.env.PGHOST) {
|
||||||
|
return postgres(opts)
|
||||||
|
}
|
||||||
|
return null
|
||||||
|
}
|
||||||
|
|
@ -2,17 +2,16 @@
|
||||||
// le jeu retombe sur la banque de questions en dur (dev sans infra).
|
// le jeu retombe sur la banque de questions en dur (dev sans infra).
|
||||||
|
|
||||||
import { drizzle } from "drizzle-orm/postgres-js"
|
import { drizzle } from "drizzle-orm/postgres-js"
|
||||||
import postgres from "postgres"
|
import { createSqlClient } from "./client"
|
||||||
import { env } from "../env"
|
|
||||||
import * as schema from "./schema"
|
import * as schema from "./schema"
|
||||||
|
|
||||||
export type Database = ReturnType<typeof drizzle<typeof schema>>
|
export type Database = ReturnType<typeof drizzle<typeof schema>>
|
||||||
|
|
||||||
function createDb(): Database | null {
|
function createDb(): Database | null {
|
||||||
if (!env.databaseUrl) {
|
const client = createSqlClient()
|
||||||
|
if (!client) {
|
||||||
return null
|
return null
|
||||||
}
|
}
|
||||||
const client = postgres(env.databaseUrl)
|
|
||||||
return drizzle(client, { schema })
|
return drizzle(client, { schema })
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -2,15 +2,13 @@
|
||||||
// bun run db:migrate
|
// bun run db:migrate
|
||||||
import { drizzle } from "drizzle-orm/postgres-js"
|
import { drizzle } from "drizzle-orm/postgres-js"
|
||||||
import { migrate } from "drizzle-orm/postgres-js/migrator"
|
import { migrate } from "drizzle-orm/postgres-js/migrator"
|
||||||
import postgres from "postgres"
|
import { createSqlClient } from "./client"
|
||||||
import { env } from "../env"
|
|
||||||
|
|
||||||
if (!env.databaseUrl) {
|
const client = createSqlClient({ max: 1 })
|
||||||
console.error("DATABASE_URL manquant — rien à migrer.")
|
if (!client) {
|
||||||
|
console.error("Aucune config DB (DATABASE_URL ou PG*) — rien à migrer.")
|
||||||
process.exit(1)
|
process.exit(1)
|
||||||
}
|
}
|
||||||
|
|
||||||
const client = postgres(env.databaseUrl, { max: 1 })
|
|
||||||
const db = drizzle(client)
|
const db = drizzle(client)
|
||||||
|
|
||||||
await migrate(db, { migrationsFolder: "./drizzle" })
|
await migrate(db, { migrationsFolder: "./drizzle" })
|
||||||
|
|
|
||||||
|
|
@ -6,21 +6,19 @@
|
||||||
|
|
||||||
import { eq } from "drizzle-orm"
|
import { eq } from "drizzle-orm"
|
||||||
import { drizzle } from "drizzle-orm/postgres-js"
|
import { drizzle } from "drizzle-orm/postgres-js"
|
||||||
import postgres from "postgres"
|
import { createSqlClient } from "./client"
|
||||||
import { env } from "../env"
|
|
||||||
import { quizCategory, quizQuestion, type NewQuizQuestion } from "./schema"
|
import { quizCategory, quizQuestion, type NewQuizQuestion } from "./schema"
|
||||||
|
|
||||||
if (!env.databaseUrl) {
|
|
||||||
console.error("DATABASE_URL manquant — impossible de seeder.")
|
|
||||||
process.exit(1)
|
|
||||||
}
|
|
||||||
|
|
||||||
const COUNT = Number(process.env.IMAGE_SEED_COUNT ?? 151)
|
const COUNT = Number(process.env.IMAGE_SEED_COUNT ?? 151)
|
||||||
const CATEGORY = "Pokémon"
|
const CATEGORY = "Pokémon"
|
||||||
const PROMPT = "Quel est ce Pokémon ?"
|
const PROMPT = "Quel est ce Pokémon ?"
|
||||||
const sleep = (ms: number) => new Promise((r) => setTimeout(r, ms))
|
const sleep = (ms: number) => new Promise((r) => setTimeout(r, ms))
|
||||||
|
|
||||||
const client = postgres(env.databaseUrl, { max: 1 })
|
const client = createSqlClient({ max: 1 })
|
||||||
|
if (!client) {
|
||||||
|
console.error("Aucune config DB (DATABASE_URL ou PG*) — impossible de seeder.")
|
||||||
|
process.exit(1)
|
||||||
|
}
|
||||||
const db = drizzle(client, { schema: { quizCategory, quizQuestion } })
|
const db = drizzle(client, { schema: { quizCategory, quizQuestion } })
|
||||||
|
|
||||||
async function upsertCategory(name: string): Promise<string> {
|
async function upsertCategory(name: string): Promise<string> {
|
||||||
|
|
|
||||||
|
|
@ -5,8 +5,7 @@
|
||||||
|
|
||||||
import { eq } from "drizzle-orm"
|
import { eq } from "drizzle-orm"
|
||||||
import { drizzle } from "drizzle-orm/postgres-js"
|
import { drizzle } from "drizzle-orm/postgres-js"
|
||||||
import postgres from "postgres"
|
import { createSqlClient } from "./client"
|
||||||
import { env } from "../env"
|
|
||||||
import {
|
import {
|
||||||
quizCategory,
|
quizCategory,
|
||||||
quizQuestion,
|
quizQuestion,
|
||||||
|
|
@ -14,12 +13,11 @@ import {
|
||||||
} from "./schema"
|
} from "./schema"
|
||||||
import { QUIZ_QUESTIONS } from "../game/modes/quiz/questions"
|
import { QUIZ_QUESTIONS } from "../game/modes/quiz/questions"
|
||||||
|
|
||||||
if (!env.databaseUrl) {
|
const client = createSqlClient({ max: 1 })
|
||||||
console.error("DATABASE_URL manquant — impossible de seeder.")
|
if (!client) {
|
||||||
|
console.error("Aucune config DB (DATABASE_URL ou PG*) — impossible de seeder.")
|
||||||
process.exit(1)
|
process.exit(1)
|
||||||
}
|
}
|
||||||
|
|
||||||
const client = postgres(env.databaseUrl, { max: 1 })
|
|
||||||
const db = drizzle(client, { schema: { quizCategory, quizQuestion } })
|
const db = drizzle(client, { schema: { quizCategory, quizQuestion } })
|
||||||
|
|
||||||
// Catégories Open Trivia DB pertinentes (id OpenTDB → nom affiché).
|
// Catégories Open Trivia DB pertinentes (id OpenTDB → nom affiché).
|
||||||
|
|
|
||||||
|
|
@ -34,7 +34,12 @@ services:
|
||||||
NODE_ENV: production
|
NODE_ENV: production
|
||||||
HOST: 0.0.0.0
|
HOST: 0.0.0.0
|
||||||
PORT: 3001
|
PORT: 3001
|
||||||
DATABASE_URL: postgres://${POSTGRES_USER:-nerdware}:${POSTGRES_PASSWORD:-nerdware}@postgres:5432/${POSTGRES_DB:-nerdware}
|
# Connexion via PG* (mot de passe passé tel quel, pas d'encodage d'URL).
|
||||||
|
PGHOST: postgres
|
||||||
|
PGPORT: 5432
|
||||||
|
PGUSER: ${POSTGRES_USER:-nerdware}
|
||||||
|
PGPASSWORD: ${POSTGRES_PASSWORD:-nerdware}
|
||||||
|
PGDATABASE: ${POSTGRES_DB:-nerdware}
|
||||||
ADMIN_TOKEN: ${ADMIN_TOKEN}
|
ADMIN_TOKEN: ${ADMIN_TOKEN}
|
||||||
CORS_ORIGINS: ${CORS_ORIGINS}
|
CORS_ORIGINS: ${CORS_ORIGINS}
|
||||||
UPLOADS_DIR: /data/uploads
|
UPLOADS_DIR: /data/uploads
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue