This example shows how to implement a REST API using Express.JS and Prisma.
Clone the repository:
git clone git@github.com:prisma/prisma-examples.git
Install Node dependencies:
cd prisma-examples/node/rest-express
npm install
To run the example, you need the Prisma CLI. Please install it via NPM or using another method:
npm install -g prisma
For this example, you'll use a free demo database (AWS Aurora) hosted in Prisma Cloud. To set up your database, run:
prisma deploy
Then, follow these steps in the interactive CLI wizard:
- Select Demo server
- Authenticate with Prisma Cloud in your browser (if necessary)
- Back in your terminal, confirm all suggested values
Alternative: Run Prisma locally via Docker
- Ensure you have Docker installed on your machine. If not, you can get it from here.
- Create
docker-compose.yml
for MySQL (see here for Postgres):version: '3' services: prisma: image: prismagraphql/prisma:1.22 restart: always ports: - "4466:4466" environment: PRISMA_CONFIG: | port: 4466 databases: default: connector: mysql host: mysql port: 3306 user: root password: prisma migrations: true mysql: image: mysql:5.7 restart: always environment: MYSQL_ROOT_PASSWORD: prisma volumes: - mysql:/var/lib/mysql volumes: mysql:
- Run
docker-compose up -d
- Run
prisma deploy
npm run start
The server is now running on http://localhost:3000
. You can send the API requests implemented in index.js
, e.g. http://localhost:3000/feed
.
/post/:id
: Fetch a single post by itsid
/feed
: Fetch all published posts/filterPosts?searchString={searchString}
: Filter posts bytitle
orcontent
/post
: Create a new post- Body:
title: String
(required): The title of the postcontent: String
(optional): The content of the postauthorEmail: String
(required): The email of the user that creates the post
- Body:
/user
: Create a new user- Body:
email: String
(required): The email address of the username: String
(optional): The name of the user
- Body:
/publish/:id
: Publish a post by itsid
/post/:id
: Delete a post by itsid