apps/docs/content/docs/orm/core-concepts/api-patterns.mdx
Prisma Client can be used to query your database from any server-side JavaScript or TypeScript application. This page covers common patterns for REST APIs, GraphQL servers, and fullstack frameworks.
When building REST APIs, use Prisma Client inside your route controllers to execute database queries.
// GET /feed - fetch published posts
app.get("/feed", async (req, res) => {
const posts = await prisma.post.findMany({
where: { published: true },
include: { author: true },
});
res.json(posts);
});
// POST /post - create a post
app.post("/post", async (req, res) => {
const { title, content, authorEmail } = req.body;
const result = await prisma.post.create({
data: {
title,
content,
author: { connect: { email: authorEmail } },
},
});
res.json(result);
});
// PUT /publish/:id - publish a post
app.put("/publish/:id", async (req, res) => {
const post = await prisma.post.update({
where: { id: Number(req.params.id) },
data: { published: true },
});
res.json(post);
});
// DELETE /post/:id - delete a post
app.delete("/post/:id", async (req, res) => {
const post = await prisma.post.delete({
where: { id: Number(req.params.id) },
});
res.json(post);
});
Prisma ORM works with any GraphQL library. Use Prisma Client inside your resolvers to read and write data.
| Library | Purpose |
|---|---|
graphql-yoga | HTTP server |
apollo-server | HTTP server |
pothos | Schema builder |
nexus | Schema builder |
type-graphql | Schema builder |
Prisma ORM is used inside GraphQL resolvers the same way you'd use any other ORM:
Modern fullstack frameworks blur server/client boundaries. Use Prisma Client in the server-side portion of your application.
// In getServerSideProps or API routes
export const getServerSideProps = async () => {
const feed = await prisma.post.findMany({
where: { published: true },
});
return { props: { feed } };
};
Find ready-to-run examples in the prisma-examples repository:
| Example | Type | Description |
|---|---|---|
| Next.js | Fullstack | Next.js 15 app |
| Express | REST | Express REST API |
| Fastify | REST | Fastify REST API |
| GraphQL Yoga | GraphQL | GraphQL server with Pothos |
| NestJS | REST | NestJS REST API |
| Remix | Fullstack | Remix with actions and loaders |
| SvelteKit | Fullstack | SvelteKit app |