Easy, type-safe 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.

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 Express fit together

Prisma ORM 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, 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 monolothic 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 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