chore: dockerize app and automate database initialization
This commit is contained in:
7
backend/.dockerignore
Normal file
7
backend/.dockerignore
Normal file
@@ -0,0 +1,7 @@
|
||||
node_modules
|
||||
npm-debug.log
|
||||
.env
|
||||
coverage
|
||||
.git
|
||||
.gitignore
|
||||
README.md
|
||||
15
backend/Dockerfile
Normal file
15
backend/Dockerfile
Normal 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"]
|
||||
@@ -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": "",
|
||||
|
||||
55
backend/src/scripts/initDatabase.js
Normal file
55
backend/src/scripts/initDatabase.js
Normal 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();
|
||||
});
|
||||
Reference in New Issue
Block a user