Query data from MySQL, PostgreSQL & SQL Server databases in Fastify apps with Prisma – a better ORM for JavaScript and TypeScript.
Prisma is an open-source ORM that drastically simplifies data modeling, migrations, and data access for SQL databases in Node.js and TypeScript.
// Creating a new recordawait prisma.user.create({ firstName: “Alice”, email: “alice@prisma.io”})
id firstName email 1 Bobby bobby@tables.io2 Nilufar nilu@email.com3 Jürgen jums@dums.edu4 Alice alice@prisma.io
Prisma 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, to using query builders like knex.js or to traditional ORMs like TypeORM, MikroORM and Sequelize.
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.
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.body17 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.params30 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.params39 const user = await prisma.user.delete({40 where: {41 id,42 },43 })44 res.json(user)45})46
47app.listen(3000)
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.body17 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.params30 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.params39 const user = await prisma.user.delete({40 where: {41 id,42 },43 })44 res.json(user)45})46
47app.listen(3000)
Prisma fits perfectly into your stack, no matter if you're building microservices or monolithic apps.
Prisma's gives you autocompletion for database queries, great developer experience and full type safety.
Prisma Client ensures fully type-safe database queries with benefits like autocompletion - even in JavaScript.
Prisma's declarative modeling language is simple and lets you intuitively describe your database schema.
Generate predictible and customizable SQL migrations from the declarative Prisma schema.
Prisma Client reduces boilerplates by providing queries for common API features (e.g. pagination, filters, ...).
Exploring some of the practices to ensure the reliable operation of a GraphQL server in addition to helping with production troubleshooting.
A ready-to-run example project for a REST API with a SQLite database
A ready-to-run example project for a GraphQL API with a PosgreSQL database
Get Involved
Get help and engage with thousands of Fastify and Prisma developers on Slack
We’d love to see what you’re building with Fastify and Prisma
Show your Fastify + Prisma love with custom free stickers