How to migrate from Sequelize to Prisma ORM
Introduction
This guide shows you how to migrate your application from Sequelize to Prisma ORM. We'll use an extended version of the Sequelize Express example as a sample project to demonstrate the migration steps.
This migration guide uses PostgreSQL as the example database, but it equally applies to any other relational database that's supported by Prisma ORM. You can learn how Prisma ORM compares to Sequelize on the Prisma ORM vs Sequelize page.
Prerequisites
Before starting this guide, make sure you have:
- A Sequelize project you want to migrate
- Node.js installed (version 18 or higher)
- PostgreSQL or another supported database
- Basic familiarity with Sequelize and Express.js
1. Prepare for migration
1.1. Understand the migration process
The steps for migrating from Sequelize to Prisma ORM are always the same, no matter what kind of application or API layer you're building:
- Install the Prisma CLI
- Introspect your database
- Create a baseline migration
- Install Prisma Client
- Gradually replace your Sequelize queries with Prisma Client
These steps apply whether you're building a REST API (e.g., with Express, Koa, or NestJS), a GraphQL API (e.g., with Apollo Server, TypeGraphQL, or Nexus), or any other kind of application that uses Sequelize for database access.
1.2. Set up Prisma configuration
Create a new Prisma schema file:
npx prisma init
This command created a new directory called prisma
with the following files for you:
schema.prisma
: Your Prisma schema that specifies your database connection and models.env
: Adotenv
to configure your database connection URL as an environment variable
The Prisma schema currently looks as follows:
// This is your Prisma schema file,
// learn more about it in the docs: https://pris.ly/d/prisma-schema
datasource db {
provider = "postgresql"
url = env("DATABASE_URL")
}
generator client {
provider = "prisma-client-js"
}
If you're using VS Code, be sure to install the Prisma VS Code extension for syntax highlighting, formatting, auto-completion and a lot more cool features.
Update the DATABASE_URL
in the .env
file with your database connection string:
DATABASE_URL="postgresql://USER:PASSWORD@HOST:PORT/DATABASE"
2. Migrate the database schema
2.1. Introspect your database
Run Prisma's introspection to create the Prisma schema from your existing database:
npx prisma db pull
This will create a schema.prisma
file with your database schema.
2.2. Create a baseline migration
To continue using Prisma Migrate to evolve your database schema, you will need to baseline your database.
First, create a migrations
directory and add a directory inside with your preferred name for the migration. In this example, we will use 0_init
as the migration name:
mkdir -p prisma/migrations/0_init
Next, generate the migration file with prisma migrate diff
. Use the following arguments:
--from-empty
: assumes the data model you're migrating from is empty--to-schema-datamodel
: the current database state using the URL in thedatasource
block--script
: output a SQL script
npx prisma migrate diff --from-empty --to-schema-datamodel prisma/schema.prisma --script > prisma/migrations/0_init/migration.sql
npx prisma migrate resolve --applied 0_init
The command will mark 0_init
as applied by adding it to the _prisma_migrations
table.
You now have a baseline for your current database schema. To make further changes to your database schema, you can update your Prisma schema and use prisma migrate dev
to apply the changes to your database.
3. Update your application code
3.1. Install Prisma Client
As a next step, you can install Prisma Client in your project so that you can start replacing the database queries in your project that are currently made with Sequelize:
npm install @prisma/client
After installing Prisma Client, you can generate the Prisma Client code:
npx prisma generate
3.2. Replace Sequelize queries
In this section, we'll show a few sample queries that are being migrated from Sequelize to Prisma Client based on the example routes from the sample REST API project. For a comprehensive overview of how the Prisma Client API differs from Sequelize, check out the API comparison page.
- Sequelize
- Prisma Client
// Find one
const user = await User.findOne({
where: { id: 1 }
});
// Create
const user = await User.create({
email: 'alice@prisma.io',
name: 'Alice'
});
// Update
await User.update({ name: 'New name' }, {
where: { id: 1 }
});
// Delete
await User.destroy({
where: { id: 1 }
});
// Find one
const user = await prisma.user.findUnique({
where: { id: 1 }
});
// Create
const user = await prisma.user.create({
data: {
email: 'alice@prisma.io',
name: 'Alice'
}
});
// Update
await prisma.user.update({
where: { id: 1 },
data: { name: 'New name' }
});
// Delete
await prisma.user.delete({
where: { id: 1 }
});
3.3. Update your controllers
Update your Express controllers to use Prisma Client. For example, here's how to update a user controller:
import { prisma } from '../client'
export class UserController {
async create(req: Request, res: Response) {
const { email, name } = req.body
const result = await prisma.user.create({
data: {
email,
name,
},
})
return res.json(result)
}
}
Next steps
Now that you've migrated to Prisma ORM, you can:
- Add more complex queries using Prisma's powerful query API
- Set up Prisma Studio for database management
- Implement database monitoring
- Add automated tests using Prisma's testing utilities
For more information and updates: