Easy, type-safe database Access in Fastify servers
Query data from MySQL, PostgreSQL & SQL Server databases in Fastify apps with Prisma – a better ORM for JavaScript and TypeScript.
Why Prisma and Fastify?
Flexible architecture
Prisma fits perfectly into your stack, no matter if you're building microservices or monolithic apps.
Higher productivity
Prisma's gives you autocompletion for database queries, great developer experience and full type safety.
Type-safe database client
Prisma Client ensures fully type-safe database queries with benefits like autocompletion - even in JavaScript.
Intuitive data modeling
Prisma's declarative modeling language is simple and lets you intuitively describe your database schema.
Easy database migrations
Generate predictable and customizable SQL migrations from the declarative Prisma schema.
Designed for building APIs
Prisma Client reduces boilerplates by providing queries for common API features (e.g. pagination, filters, ...).
How Prisma and Fastify fit together
REST API
Prisma is used inside your route handlers to read and write data in your database.
import fastify from 'fastify'
import { PrismaPg } from '@prisma/adapter-pg'
import { PrismaClient } from '../generated/client'
const adapter = new PrismaPg({
connectionString: process.env.DATABASE_URL,
})
const prisma = new PrismaClient({ adapter })
const app = fastify()
app.get('/feed', async (req, res) => {
const posts = await prisma.post.findMany({
where: { published: true },
include: { author: true },
})
res.json(posts)
})
app.post('/post', async (req, res) => {
const { title, content, authorEmail } = req.body
const post = await prisma.post.create({
data: { title, content, published: false, author: { connect: { email: authorEmail } } },
})
res.json(post)
})
app.listen(3000)Featured Prisma & Fastify examples
Monitoring your GraphQL API with Fastify, Mercurius, and Prisma
Exploring some of the practices to ensure the reliable operation of a GraphQL server in addition to helping with production troubleshooting.
Fastify REST API starter kit
A ready-to-run example project for a REST API with a SQLite database.
Fastify GraphQL API starter kit
A ready-to-run example project for a GraphQL API with a PostgreSQL database.
