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.

tech

What is Prisma?

Prisma makes working with data easy! It offers a type-safe Node.js & TypeScript ORM, global database caching, connection pooling, and real-time database events.

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 ORM is a next-generation ORM that's used to query your database in a Fastify server. You can use it as an alternative to writing plain SQL queries, query builders like knex.js or traditional ORMs like TypeORM, MikroORM and Sequelize. Prisma ORM can be used to build REST and GraphQL APIs and integrates smoothly with both microservices and monolithic architectures.

You can also supercharge usage of Prisma ORM with our additional tools:
Prisma Accelerate is a global database cache and scalable connection pool that speeds up your database queries.
Prisma Pulse enables you to build reactive, real-time applications in a type-safe manner.

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