chore: dockerize app and automate database initialization

This commit is contained in:
andil
2026-08-04 11:45:04 +00:00
parent 8e00161a75
commit d6ceefdeda
9 changed files with 229 additions and 14 deletions

7
backend/.dockerignore Normal file
View File

@@ -0,0 +1,7 @@
node_modules
npm-debug.log
.env
coverage
.git
.gitignore
README.md

15
backend/Dockerfile Normal file
View File

@@ -0,0 +1,15 @@
FROM node:22-alpine
WORKDIR /app
COPY package*.json ./
RUN npm ci
COPY prisma ./prisma
RUN npx prisma generate
COPY . .
EXPOSE 3000
CMD ["npm", "start"]

View File

@@ -8,7 +8,8 @@
"generate:tickets": "node src/scripts/generateTickets.js",
"test": "jest --runInBand",
"test:watch": "jest --watch",
"test:coverage": "jest --coverage"
"test:coverage": "jest --coverage",
"init:db": "node src/scripts/initDatabase.js"
},
"keywords": [],
"author": "",

View File

@@ -0,0 +1,55 @@
const { execSync } = require("child_process");
const prisma = require("../config/prisma");
async function main() {
let lotCount = await prisma.lot.count();
let ticketCount = await prisma.ticket.count();
console.log(`Lots présents : ${lotCount}`);
console.log(`Tickets présents : ${ticketCount}`);
if (lotCount === 0) {
console.log("Aucun lot trouvé : exécution du seed...");
execSync("npx prisma db seed", {
stdio: "inherit",
env: process.env,
});
lotCount = await prisma.lot.count();
console.log(`Lots présents après seed : ${lotCount}`);
} else {
console.log("Les lots existent déjà : seed ignoré.");
}
if (lotCount !== 5) {
throw new Error(
`Initialisation interrompue : 5 lots attendus, ${lotCount} trouvé(s).`
);
}
ticketCount = await prisma.ticket.count();
if (ticketCount === 0) {
console.log("Aucun ticket trouvé : génération des tickets...");
execSync("npm run generate:tickets", {
stdio: "inherit",
env: process.env,
});
ticketCount = await prisma.ticket.count();
console.log(`Tickets présents après génération : ${ticketCount}`);
} else {
console.log("Les tickets existent déjà : génération ignorée.");
}
console.log("Initialisation de la base terminée.");
}
main()
.catch((error) => {
console.error("Erreur pendant l'initialisation de la base :", error);
process.exitCode = 1;
})
.finally(async () => {
await prisma.$disconnect();
});