apps/docs/content/docs.v6/guides/migrate-from-sequelize.mdx
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.
Before starting this guide, make sure you have:
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:
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.
Create a new Prisma schema file:
npx prisma init --output ../generated/prisma
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: A dotenv to configure your database connection URL as an environment variableThe 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"
}
generator client {
provider = "prisma-client"
output = "./generated/prisma"
}
:::tip
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"
Create a prisma.config.ts file in the root of your project with the following content:
import "dotenv/config";
import { defineConfig, env } from "prisma/config";
export default defineConfig({
schema: "prisma/schema.prisma",
migrations: {
path: "prisma/migrations",
},
datasource: {
url: env("DATABASE_URL"),
},
});
:::note
You'll need to install the dotenv package to load environment variables. If you haven't already, install it using your package manager:
npm install dotenv
:::
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.
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: the current database state using the URL in the datasource block--script: output a SQL scriptnpx prisma migrate diff --from-empty --to-schema 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.
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
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.
// Find one
const user = await User.findOne({
where: { id: 1 },
});
// Create
const user = await User.create({
email: "[email protected]",
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: "[email protected]",
name: "Alice",
},
});
// Update
await prisma.user.update({
where: { id: 1 },
data: { name: "New name" },
});
// Delete
await prisma.user.delete({
where: { id: 1 },
});
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);
}
}
Now that you've migrated to Prisma ORM, you can:
For more information: