Skip to content

johannesschobel/nest-prisma-crud

Repository files navigation

REST API Example

This example shows how to implement a REST API using NestJS and Prisma Client. It uses a SQLite database file with some initial dummy data which you can find at ./prisma/dev.db. The example was bootstrapped using the NestJS CLI command nest new rest-nestjs.

How to use

1. Download example & install dependencies

Download this example:

curl https://codeload.github.com/prisma/prisma-examples/tar.gz/latest | tar -xz --strip=2 prisma-examples-latest/typescript/rest-nestjs

Install npm dependencies:

cd rest-nestjs
npm install

Note that this also generates Prisma Client JS into node_modules/@prisma/client via a postinstall hook of the @prisma/client package from your package.json.

Alternative: Clone the entire repo

Clone this repository:

git clone [email protected]:prisma/prisma-examples.git --depth=1

Install npm dependencies:

cd prisma-examples/typescript/rest-nestjs
npm install

2. Start the REST API server

npm run dev

The server is now running on http://localhost:3000. You can now the API requests, e.g. http://localhost:3000/feed.

Using the REST API

You can access the REST API of the server using the following endpoints:

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

About

Example on how to create a CRUD Service for Prisma

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published