Simple Boilerplate Express.js, Prisma ORM, Docker Compose, PostgreSQL Database
- Clone this repository
git clone https://github.com/armandwipangestu/boilerplate-express-prisma && cd boilerplate-express-prisma
- Running Docker Compose
Note: This command will install the docker compose service such as
db
(PostgreSQL) andadminer
Here are some information
Service Port Mapping Database User Password db
5432
:5432
testDB
postgres
postgres
adminer
8080
:8080
- - -
docker compose up
- Install dependency
yarn
- Copy
.env.example
to.env
file
cp .env.example .env
- Change
DATABASE_URL
value with your database own database configuration, for example
DATABASE_URL="postgresql://username:password@localhost:5432/database_name?schema=public"
- Migrate the database
npx prisma migrate dev
- Seeding the tables
npx prisma db seed
- Running the app
npm run dev
In this boilerplate, i'm just create a data model like above, here the detail about the relationship
Table A | Table B | Relationship | Description |
---|---|---|---|
user |
profile |
One-to-One |
- |
user |
post |
One-to-Many |
- |
post |
category |
Many-to-Many |
This explicit many-to-many relationship need intermediate model/tabel, so i'm use _CategorysOnPost as the intermediate |
post |
_CategorysOnPost |
One-to-Many |
- |
category |
_CategorysOnPost |
One-to-Many |
- |
- Initialize
Run this command if you first setup with the prisma, prisma will generate the .env
file and generate prisma/schema.prisma
file for the data model
npx prisma init
- Introspection
Run this command if you have already database and inside the database already defined the tables, so prisma introspection will create the data model based on the tables inside your database. So the database will be the single source of tools.
npx prisma db pull
- Migration
Run this command if you don't have a database, but with this migration you must have a data model first by creating manually then you run the migration and your tables inside your database will be created based on data models in the prisma/schema.prisma
file
Note: Mostly if you run the migration first time, the migration name use
init
npx prisma migrate dev --name <migration_name>
- Prisma Studio
Run this command if you want to open a model via web
npx prisma studio
- Seeding
Note: Make sure you check the
package.json
file that have the custom command like this"prisma": { "seed": "node prisma/seed.js" },
npx prisma db seed
- Build the app into container
docker build -t boilerplate-express-prisma .
- Running the container
docker run -it -p 5000:5000 boilerplate-express-prisma
- Running with Compose
Note: Add this
boilerplate-express-prisma
service intodocker-compose.yaml
file
version: "3"
services:
db: ...
adminer: ...
boilerplate-express-prisma:
build:
context: .
environment:
DATABASE_URL: "postgresql://postgres:postgres@localhost:5432/testDB?schema=public"
API_VERSION: "/api/v1"
depends_on:
- db
ports:
- "5000:5000"