» npm install prisma
» npm install @prisma/client
» npm install @prisma/cli
I have a project that uses prisma and postgres for database management, this project is set up as an npm package in an effort to centralize it, as it will be used across multiple other different projects, so it is installed via `npm i <project-db>`. I am currently trying to set up a docker-compose file so that I can create the tables needed when setting up a fresh install of one of my projects that requires the database, but I'm not sure how to go about doing this since the database/prisma are all being managed by an npm package, and not directly. How would I go about setting things up so that when I run docker-compose, all the tables are created and things can just work?
In case that is a bit confusing of an explanation, I'll attempt to lay it out in a diagram to explain it better:
@username/project-db package.json, this is installed inside Project A repository (it's a dependency in Project A's package.json)
{
...
"scripts": {
"db:introspect": "dotenv -- prisma db pull",
"db:generate": "dotenv -- prisma migrate",
"postinstall": "npx prisma generate && tsc && cp src/index.d.ts dist/index.d.ts",
"test": "exit 0"
},
...
"devDependencies": {
"@types/node": "^20.10.0",
"dotenv-cli": "^7.3.0",
"prisma": "^5.6.0",
"typescript": "^5.3.2"
},
"dependencies": {
"@prisma/client": "^5.6.0"
}
}Dockerfile (Project A's repository)
ARG OWNER
FROM node:20-buster-slim
ENV NODE_ENV 'development'
ENV OWNER "username"
RUN apt-get update && apt-get install libssl-dev ca-certificates -y
# Create app directory
WORKDIR /usr/src/app
# Copy package.json and package-lock.json
COPY package*.json .
# Setup npmrc
RUN --mount=type=secret,id=TOKEN \
TOKEN=$(cat /run/secrets/TOKEN) && \
echo //npm.pkg.github.com/:_authToken=${TOKEN} >> ~/.npmrc
RUN echo @${OWNER}:registry=https://npm.pkg.github.com/ >> ~/.npmrc
# Copy src files
COPY src/ src/
# Install packages
RUN npm ci
# ^ installs "project-db as a dependency"
# remove github token from npmrc
RUN echo > ~/.npmrc
# Creates prisma client
RUN npm explore @username/project-db -- npm run postinstall
# Build the project
RUN npm run build
# Run the start script in package.json
CMD ["npm", "run", "start"]docker-compose for all services:
services:
postgres:
image: postgres:16.2
container_name: postgres
hostname: postgres
environment:
POSTGRES_USER: ${PSQL_USER}
POSTGRES_PASSWORD: ${PSQL_PASS}
POSTGRES_DB: ${PSQL_DB_NAME}
ports:
- ${PSQL_PORT}:${PSQL_PORT}
restart: 'unless-stopped'
volumes:
- 'pg-data:/var/lib/postgresql/data'
healthcheck:
test: ['CMD-SHELL', 'pg_isready -q -d $$POSTGRES_DB -U $$POSTGRES_USER']
interval: 10s
timeout: 5s
retries: 5
projectA:
build:
context: .
secrets:
- TOKEN
container_name: projectA
depends_on:
postgres:
condition: service_healthy
tty: true
env_file:
- src/.env
volumes:
pg-data:
driver: local
secrets:
TOKEN:
environment: "TOKEN"So now my question is: where do I put `npx prisma db push/migrate dev/whatever` so that my tables are actually created in the `postgres` container? I can see that the database itself is created but there are no tables in it.
» npm install @casl/prisma
» npm install @prisma/react-native
That's what I did in package.json file (It was a deploy a Next app on Versel) I just added generate command to the build script:
"scripts": {
"dev": "next dev",
"build": "prisma generate && next build",
"start": "next start",
"lint": "next lint"
},
Not sure if it is a correct way, though..
There's no need to run the prisma generate command that is executed on installation of the @prisma/client.
EDIT: https://www.prisma.io/docs/reference/tools-and-interfaces/prisma-client/generating-prisma-client