This example shows how to implement a REST API using Express
and Prisma Client
. It uses a SQLite database file with some initial dummy data which you can find at ./prisma/dev.db
.
Clone this repository:
git clone https://github.com/kevin-sg/rest-express-prisma.git
Install pnpm dependencies:
cd rest-express-prisma
pnpm install
Database environment variables .env
:
# default SQLite
DATABASE_URL = file:./dev.db
The seed file in prisma/seed.ts
will be executed and your database will be populated with the sample data.
Execute seed command:
pnpm prisma:seed
Optional: Seed the database with endpoint
This request will reset the database and populate the database with data:
curl http://localhost:8080/seed
pnpm dev
The server is now running on http://localhost:8080
.
Prisma comes with a built-in GUI to view and edit the data in your database. You can open it using the following command:
pnpm prisma:studio
- Drop the database
- Create a new database
- Apply migrations
- Seed the database with data
pnpm prisma:reset
Create a new .env
:
# default PORT
PORT = 8080
# prefix
GLOBAL_PREFIX = /api
# db connection
DATABASE_URL = file:./dev.db
# JWT
JWT_PRIVATE_KEY = secretOrPrivateKey
JWT_TIME_EXPIRATION = 24h
# cookie
COOKIE_NAME = access_token
COOKIE_PRIVATE_KEY = cookieOrPrivateKey
You can access the REST API of the server using the following endpoints:
Global prefix of environment:
GLOBAL_PREFIX = /api
-
GET
/auth/revalidate
Request and revalidate newtoken
/auth/logout
Close session andcookie
cleaning
-
POST
:/auth/login
Login and user authentication- Body:
email: String
(required): The email of userpassword: String
(required): The password of user
- Body:
-
GET
/user
Fetch all active user and requires authentication/user/:id
Fetch a single user byid
and requires authentication
-
POST
:/user
Fetch a create to user- Body:
name: String
(required): The name of userlastName: String
(optional): The last name of useremail: String
(required): The email of userpassword: String
(required): The password of user
- Body:
-
PUT
:/user
Update user and requires authentication- Body:
name: String
(optional): The name of userlastName: String
(optional): The last name of useremail: String
(optional): The email of userpassword: String
(optional): The password of user
- Body:
-
DELETE
:/user
Disable a user and requires authentication
-
GET
/post
Fetch all published post and requires authentication/post/:id
Fetch a single post byid
, returns all posts and requires authentication
-
POST
:/post
Create a new post and requires authentication- Body:
title: String
(required): The unique name of the postcontent: String
(optional): The content of postpublished: Boolean
(optional): The status of the post and the default istrue
userId: String
(optional): TheuserId
is provided automatically by the user
- Body:
-
PUT
:/post/:id
Update post byid
and requires authentication- Body:
title: String
(optional): The unique name of the postcontent: String
(optional): The content of postpublished: Boolean
(optional): The status of the post and the default istrue
userId: String
(optional): TheuserId
is provided automatically by the user
- Body:
-
DELETE
:/post
Delete active post and require authentication
When executing the seed command, two demo accounts are generated:
# demo 1
email: tom@prisma.io
password: @Demo123
# demo 2
email: bob@express.com
password: @Demo123
If you want to try this example with another database than SQLite, you can adjust the the database connection in prisma/schema.prisma
by reconfiguring the datasource
block.
Learn more about the different connection configurations in the docs.
Expand for an overview of example configurations with different databases
For PostgreSQL, the connection URL has the following structure:
datasource db {
provider = "postgresql"
url = "postgresql://USER:PASSWORD@HOST:PORT/DATABASE?schema=SCHEMA"
}
Here is an example connection string with a local MongoDB database:
datasource db {
provider = "mongodb"
url = "mongodb://USERNAME:PASSWORD@HOST/DATABASE?authSource=admin&retryWrites=true&w=majority"
}
rest-express-prisma
├── README.md
├── package.json
├── pnpm-lock.yaml
├── prisma
│ ├── dev.db
│ ├── migrations
│ │ ├── 20230522152146_initial
│ │ │ └── migration.sql
│ │ ├── 20230522172659_second
│ │ │ └── migration.sql
│ │ ├── 20230522203222_third
│ │ │ └── migration.sql
│ │ ├── 20230523224611_fourth
│ │ │ └── migration.sql
│ │ ├── 20230524163001_fifth
│ │ │ └── migration.sql
│ │ └── migration_lock.toml
│ ├── prisma-client.ts
│ ├── schema.prisma
│ └── seed.ts
├── src
│ ├── controllers
│ │ ├── abs.controller.ts
│ │ ├── auth.controller.ts
│ │ ├── index.ts
│ │ ├── post.controller.ts
│ │ ├── seed.controller.ts
│ │ └── user.controller.ts
│ ├── dto
│ │ ├── index.ts
│ │ ├── post
│ │ │ ├── create-post.dto.ts
│ │ │ ├── index.ts
│ │ │ └── update-post.dto.ts
│ │ └── user
│ │ ├── create-user.dto.ts
│ │ ├── index.ts
│ │ ├── login-user.dto.ts
│ │ └── update-user.dto.ts
│ ├── index.ts
│ ├── middleware
│ │ ├── authenticate.middleware.ts
│ │ ├── index.ts
│ │ └── validation.middleware.ts
│ ├── models
│ │ ├── http-exception.model.ts
│ │ ├── index.ts
│ │ ├── post.model.ts
│ │ └── user.model.ts
│ ├── routers
│ │ ├── auth.router.ts
│ │ ├── constant.ts
│ │ ├── index.ts
│ │ ├── post.router.ts
│ │ ├── seed.router.ts
│ │ └── user.router.ts
│ ├── services
│ │ ├── auth.service.ts
│ │ ├── index.ts
│ │ ├── post.service.ts
│ │ ├── seed.service.ts
│ │ └── user.service.ts
│ └── utils
│ ├── cookie.util.ts
│ ├── error-handler.util.ts
│ ├── http-status.util.ts
│ ├── index.ts
│ ├── jwt.util.ts
│ └── logger.util.ts
├── tsconfig.json
└── types
├── environment.d.ts
├── express.d.ts
└── jwt.d.ts
- Check out the Prisma docs
- Guide for new user of pnpm docs
- Create issues and ask questions on Kevin S.
Author: Kevin S.