prisma2-postgres-example

prisma2-postgres-example

Stars
4
docker run -d --name postgresql -p 5432:5432 -e POSTGRESQL_USERNAME=prisma -e POSTGRESQL_PASSWORD=prisma -e POSTGRESQL_DATABASE=prisma bitnami/postgresql:latest

REST API Example

This example shows how to implement a REST API with TypeScript using Express.JS and Prisma.

How to use

1. Download example & install dependencies

Clone the repository:

git clone [email protected]:prisma/photonjs.git

Install Node dependencies:

cd examples/typescript/rest-express
npm install

2. Install the Prisma CLI

To run the example, you need the Prisma CLI. Please install it via NPM.

npm install -g prisma2

3. Set up database & deploy Prisma schema

prisma2 lift save --name 'init'
prisma2 lift up
prisma2 generate

4. Start the REST API server

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.

5. Using the REST API

GET

  • /post/:id: Fetch a single post by its id
  • /feed: Fetch all published posts
  • /filterPosts?searchString={searchString}: Filter posts by title or content

POST

  • /post: Create a new post
    • Body:
      • title: String (required): The title of the post
      • content: String (optional): The content of the post
      • authorEmail: String (required): The email of the user that creates the post
  • /user: Create a new user
    • Body:
      • email: String (required): The email address of the user
      • name: String (optional): The name of the user

PUT

  • /publish/:id: Publish a post by its id

DELETE

  • /post/:id: Delete a post by its id

Next steps

prisma2-postgres-example