Easy Database Access
in Express servers

Query data from MySQL, PostgreSQL & SQL Server databases in Express 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 Express fit together

Prisma is a next-generation ORM that's used to query your database in an Express 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 Express, you can use it with any other web framework like koa.js, Fastify 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
clientexpress serverdatabase

Prisma and Express Code Examples

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

The code below demonstrates various uses of Prisma when using Express 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 express from 'express'
2import { PrismaClient } from '@prisma/client'
3
4const prisma = new PrismaClient()
5const app = express()
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
47const server = app.listen(3000)
GraphQL API
Prisma schema

REST API

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

1import express from 'express'
2import { PrismaClient } from '@prisma/client'
3
4const prisma = new PrismaClient()
5const app = express()
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
47const server = app.listen(3000)

Why Prisma and Express?

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 & Express Examples

How to build a REST API with Prisma and PostgreSQL

A comprehensive tutorial for building REST APIs with Express, Prisma and PostgreSQL

REST API Starter Kit

A ready-to-run example project for a REST API with a SQLite database

GraphQL API Starter Kit

A ready-to-run example project for a GraphQL API with a SQLite database

Get Involved

Join the Express + Prisma Community

Join us on Slack

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

Join Prisma Slack

Showcase on Twitter

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

Tweet #prisma+express

Get Stickers

Show your Express + Prisma love with custom free stickers

Order stickers