Easy 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.

What is Prisma?

Prisma is an open-source ORM that drastically simplifies data modeling, migrations, and data access for SQL databases in Node.js and TypeScript.

Query
// Creating a new record
await prisma.user.create({
firstName: “Alice”,
email: “alice@prisma.io”
})
Table
id firstName email
1 Bobby bobby@tables.io
2 Nilufar nilu@email.com
3 Jürgen jums@dums.edu
4 Alice alice@prisma.io

How Prisma and Fastify fit together

Prisma is a next-generation ORM that's used to query your database in an Fastify server. You can use it as an alternative to writing plain SQL queries, to using query builders like knex.js or to traditional ORMs like TypeORM, MikroORM and Sequelize.

While Prisma works great with Fastify, you can use it with any other web framework like koa.js, Express or FeathersJS as well. Prisma can be used to build REST and GraphQL APIs and integrates smoothly with both microservices and monolothic architectures.

Diagram showing Prisma fitting in the Server part of the Client-server-database stack
clientfastify serverdatabase

Prisma and Fastify Code Examples

Prisma provides a convenient database access layer that integrates perfectly with Fastify.

The code below demonstrates various uses of Prisma when using Fastify for building an API server.

REST API

REST API

Prisma is used inside your route handlers to read and write data in your database.

1import fastify from 'fastify'
2import { PrismaClient } from '@prisma/client'
3
4const prisma = new PrismaClient()
5const app = fastify()
6
7app.get('/feed', async (req, res) => {
8 const posts = await prisma.post.findMany({
9 where: { published: true },
10 include: { author: true },
11 })
12 res.json(posts)
13})
14
15app.post('/post', async (req, res) => {
16 const { title, content, authorEmail } = req.body
17 const post = await prisma.post.create({
18 data: {
19 title,
20 content,
21 published: false,
22 author: { connect: { email: authorEmail } },
23 },
24 })
25 res.json(post)
26})
27
28app.put('/publish/:id', async (req, res) => {
29 const { id } = req.params
30 const post = await prisma.post.update({
31 where: { id },
32 data: { published: true },
33 })
34 res.json(post)
35})
36
37app.delete('/user/:id', async (req, res) => {
38 const { id } = req.params
39 const user = await prisma.user.delete({
40 where: {
41 id,
42 },
43 })
44 res.json(user)
45})
46
47app.listen(3000)
Prisma in a Fastify Plugin
GraphQL API
Prisma schema

REST API

Prisma is used inside your route handlers to read and write data in your database.

1import fastify from 'fastify'
2import { PrismaClient } from '@prisma/client'
3
4const prisma = new PrismaClient()
5const app = fastify()
6
7app.get('/feed', async (req, res) => {
8 const posts = await prisma.post.findMany({
9 where: { published: true },
10 include: { author: true },
11 })
12 res.json(posts)
13})
14
15app.post('/post', async (req, res) => {
16 const { title, content, authorEmail } = req.body
17 const post = await prisma.post.create({
18 data: {
19 title,
20 content,
21 published: false,
22 author: { connect: { email: authorEmail } },
23 },
24 })
25 res.json(post)
26})
27
28app.put('/publish/:id', async (req, res) => {
29 const { id } = req.params
30 const post = await prisma.post.update({
31 where: { id },
32 data: { published: true },
33 })
34 res.json(post)
35})
36
37app.delete('/user/:id', async (req, res) => {
38 const { id } = req.params
39 const user = await prisma.user.delete({
40 where: {
41 id,
42 },
43 })
44 res.json(user)
45})
46
47app.listen(3000)

Why Prisma and Fastify?

Flexible architecture

Prisma fits perfectly into your stack, no matter if you're building microservices or monolothic 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 predictible 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, ...).

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 PosgreSQL database

Get Involved

Join the Fastify + Prisma Community

Join us on Slack

Get help and engage with thousands of Fastify and Prisma developers on Slack

Join Prisma Slack

Showcase on Twitter

We’d love to see what you’re building with Fastify and Prisma

Tweet #prisma+fastify

Get Stickers

Show your Fastify + Prisma love with custom free stickers

Order stickers