Skip to content

Commit e40a12b

Browse files
committed
Updating with seed process
1 parent 66ce031 commit e40a12b

File tree

3 files changed

+34
-17
lines changed

3 files changed

+34
-17
lines changed

docker-compose.yml

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,31 +7,35 @@ services:
77
environment:
88
POSTGRES_USER: "${POSTGRES_USER}"
99
POSTGRES_PASSWORD: "${POSTGRES_PASSWORD}"
10-
POSTGRES_DB: "${POSTGRES_DB}"
10+
POSTGRES_DB: ${POSTGRES_DB}
1111
volumes:
1212
- ./docker_test_db:/var/lib/postgresql/data
13+
healthcheck:
14+
test: ["CMD-SHELL", "sh -c 'pg_isready -U ${POSTGRES_USER} -d ${POSTGRES_DB}'"]
15+
interval: 5s
16+
timeout: 60s
17+
retries: 5
18+
start_period: 80s
1319
server:
1420
container_name: server
1521
build:
1622
context: ./server
1723
dockerfile: Dockerfile
18-
# command: npm start
1924
ports:
2025
- "7999:8000"
26+
command: bash -c "npx prisma migrate dev && npx prisma db seed && npm start"
2127
environment:
2228
DATABASE_URL: "${DATABASE_URL}"
2329
PORT: "${PORT}"
2430
depends_on:
25-
- postgres
31+
postgres:
32+
condition: service_healthy
2633
client:
2734
container_name: client
2835
build:
2936
context: ./client
3037
dockerfile: Dockerfile
31-
# command: npm start
3238
ports:
3339
- "4174:4173"
34-
# environment:
35-
# VITE_SERVER_URL: "${VITE_SERVER_URL}"
3640
depends_on:
3741
- server

server/Dockerfile

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
FROM node:18
2+
ARG DATABASE_URL="postgresql://john_doe:john.doe@postgres:5432/docker_test_db?schema=public"
3+
ENV DATABASE_URL=$DATABASE_URL
24
WORKDIR /server
35
COPY package.json ./
46
COPY package-lock.json ./
57
COPY . .
68
RUN npm install
7-
RUN npx prisma generate
8-
CMD ["npm", "start"]
9+
RUN npx prisma generate

server/prisma/seed.js

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -408,12 +408,24 @@ async function main() {
408408
});
409409
}
410410

411-
main()
412-
.then(async () => {
413-
await prisma.$disconnect();
414-
})
415-
.catch(async (e) => {
416-
console.error(e);
417-
await prisma.$disconnect();
418-
process.exit(1);
419-
});
411+
async function init() {
412+
const users = await prisma.user.count();
413+
414+
console.log(users)
415+
416+
if (users === 0) {
417+
main()
418+
.then(async () => {
419+
await prisma.$disconnect();
420+
})
421+
.catch(async (e) => {
422+
console.error(e);
423+
await prisma.$disconnect();
424+
process.exit(1);
425+
});
426+
} else {
427+
console.log("Database seeding is not needed");
428+
}
429+
}
430+
431+
init()

0 commit comments

Comments
 (0)