Skip to content

Commit

Permalink
feat: seed data via docker compose (revertinc#333)
Browse files Browse the repository at this point in the history
  • Loading branch information
jatinsandilya authored and hrutik7 committed Nov 9, 2023
1 parent 7b445d4 commit 7d42b5f
Show file tree
Hide file tree
Showing 7 changed files with 65 additions and 8 deletions.
4 changes: 3 additions & 1 deletion .env.example
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
SERVER_PORT=4001
PGSQL_URL=postgresql://postgres:password@localhost:5432/postgres
PGSQL_URL=postgresql://postgres:password@localhost:5432/postgres
REDIS_PASSWORD=password
REDIS_USER=default
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,13 @@ cp .env.example .env
# Update this .env file with your own secrets

# Then In the root directory run

# When running for the first time to seed the database.
docker-compose run db-seed

# For subsequent runs
docker-compose up -d

```

The UI is now available at http://localhost:3000 and the backend is available at http://localhost:4001.
Expand Down
25 changes: 23 additions & 2 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ services:
db:
image: postgres:15
ports:
- '5432:5432'
- 5432:5432
volumes:
- pgdata:/var/lib/postgresql/data
restart: on-failure
Expand All @@ -16,6 +16,23 @@ services:
interval: 10s
timeout: 5s
retries: 5
cache:
image: redis:6.2-alpine
restart: always
ports:
- '6379:6379'
command: redis-server --save 20 1 --loglevel warning --requirepass ${REDIS_PASSWORD}
volumes:
- cache:/data
db-seed:
build:
dockerfile: packages/backend/db-seed.Dockerfile
context: "./"
args:
PGSQL_URL: postgresql://postgres:${POSTGRES_PASSWORD}@db:5432/${POSTGRES_DATABASE}
depends_on:
- db
profiles: ["tools"]
ui:
build:
dockerfile: packages/client/Dockerfile
Expand All @@ -31,13 +48,17 @@ services:
args:
PGSQL_URL: postgresql://postgres:${POSTGRES_PASSWORD}@db:5432/${POSTGRES_DATABASE}
SERVER_PORT: ${SERVER_PORT}
REDIS_SERVER_URL: redis://${REDIS_USER}:${REDIS_PASSWORD}@cache:6379
env_file:
- packages/backend/.env
ports:
- 4001:4001
depends_on:
db:
condition: service_healthy # Wait for db service to be healthy
cache:
condition: service_started

volumes:
pgdata:
pgdata:
cache:
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,4 @@
"seed": "ts-node --transpile-only ./packages/backend/prisma/seed.ts"
},
"packageManager": "yarn@3.2.2"
}
}
19 changes: 19 additions & 0 deletions packages/backend/db-seed.Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
FROM node:alpine AS BUILD_IMAGE
WORKDIR /app/

COPY ./packages/backend /app/packages/backend
COPY ./packages/backend/tsconfig.json /app/packages/backend/tsconfig.json
COPY ./yarn.lock /app/yarn.lock
COPY ./package.json /app/package.json

ARG PGSQL_URL

ENV PGSQL_URL $PGSQL_URL

RUN echo "PGSQL_URL=$PGSQL_URL" >> .env

RUN yarn install --check-cache

WORKDIR /app/packages/backend

CMD npm run db-deploy && npm run db-seed:docker
1 change: 1 addition & 0 deletions packages/backend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
"dev": "NODE_ENV=development ts-node-dev --inspect --exit-child --ignore-watch node_modules --respawn index.ts",
"db-deploy": "prisma migrate deploy",
"db-seed": "prisma db seed",
"db-seed:docker": "ts-node prisma/seed.ts",
"db-pull": "prisma db pull",
"migration:resolve": "prisma migrate resolve --applied $MIGRATION_ID",
"migration:dev": "prisma migrate dev",
Expand Down
16 changes: 12 additions & 4 deletions packages/backend/prisma/seed.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,12 @@ async function main() {
});
await Promise.all(
Object.keys(TP_ID).map(async (tp) => {
const localRevertApp = await prisma.apps.create({
data: {
const localRevertApp = await prisma.apps.upsert({
where: {
id: `${tp}_${localAccount.id}`,
},
update: {},
create: {
id: `${tp}_${localAccount.id}`,
tp_id: tp as TP_ID,
scope: [],
Expand Down Expand Up @@ -752,8 +756,12 @@ async function main() {
object: obj as StandardObjects,
};
});
await prisma.schema_mapping.create({
data: {
await prisma.schema_mapping.upsert({
where: {
id: rootSchemaMappingId,
},
update: {},
create: {
id: rootSchemaMappingId,
object_schemas: {
createMany: {
Expand Down

0 comments on commit 7d42b5f

Please sign in to comment.