nest-prisma-crud
nest-prisma-crud copied to clipboard
Example on how to create a CRUD Service for Prisma
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 itsid/feed: Fetch all published posts/filterPosts?searchString={searchString}: Filter posts bytitleorcontent
POST
/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:
PUT
/publish/:id: Publish a post by itsid
DELETE
/post/:id: Delete a post by itsid
Next steps
- Check out the Prisma docs
- Share your feedback in the
prisma2channel on the Prisma Slack - Create issues and ask questions on GitHub