apps/docs/content/docs/guides/switch-to-prisma-orm/from-sql-orms.mdx
This guide shows you how to migrate your application from Sequelize or TypeORM to Prisma ORM.
You can learn how Prisma ORM compares to these ORMs on the comparison pages:
Before starting this guide, make sure you have:
The steps for migrating from Sequelize or TypeORM to Prisma ORM are always the same:
Prisma ORM supports incremental adoption, so you can migrate your project step-by-step rather than all at once.
npm install prisma --save-dev
npm install @prisma/client
Run the following command to create a basic Prisma setup:
npx prisma init
This creates a prisma directory with a schema.prisma file and a .env file. Update the DATABASE_URL in .env with your connection string:
DATABASE_URL="postgresql://user:password@localhost:5432/mydb?schema=public"
Then introspect your database to generate Prisma models:
npx prisma db pull
To create a baseline migration, run:
mkdir -p prisma/migrations/0_init
npx prisma migrate diff --from-empty --to-schema prisma/schema.prisma --script > prisma/migrations/0_init/migration.sql
npx prisma migrate resolve --applied 0_init
npx prisma generate
Create a file to instantiate Prisma Client (e.g., db/prisma.ts):
import { PrismaClient } from "@prisma/client";
export const prisma = new PrismaClient();
// Find records
const user = await User.findOne({ where: { id: 1 } });
const users = await User.findAll({
where: { active: true },
limit: 10,
order: [['createdAt', 'DESC']]
});
// Create
const user = await User.create({
name: 'Alice',
email: '[email protected]'
});
// Update
await User.update(
{ name: 'Alicia' },
{ where: { id: 1 } }
);
// Delete
await User.destroy({ where: { id: 1 } });
// With relations
const posts = await Post.findAll({
include: [{ model: User }],
limit: 10
});
// Transaction
const result = await sequelize.transaction(async (t) => {
const user = await User.create({ name: 'Alice' }, { transaction: t });
const post = await Post.create({ title: 'Hello', userId: user.id }, { transaction: t });
return { user, post };
});
// Find records
const user = await userRepository.findOne({ where: { id: 1 } });
const users = await userRepository.find({
where: { active: true },
take: 10,
order: { createdAt: 'DESC' }
});
// Create
const user = userRepository.create({
name: 'Alice',
email: '[email protected]'
});
await userRepository.save(user);
// Update
await userRepository.update(1, { name: 'Alicia' });
// Delete
await userRepository.delete(1);
// With relations
const posts = await postRepository.find({
relations: ['author'],
take: 10
});
// Transaction
await connection.transaction(async (manager) => {
const user = manager.create(User, { name: 'Alice' });
await manager.save(user);
const post = manager.create(Post, { title: 'Hello', author: user });
await manager.save(post);
});
// Find records
const user = await prisma.user.findUnique({ where: { id: 1 } });
const users = await prisma.user.findMany({
where: { active: true },
take: 10,
orderBy: { createdAt: 'desc' }
});
// Create
const user = await prisma.user.create({
data: {
name: 'Alice',
email: '[email protected]'
}
});
// Update
await prisma.user.update({
where: { id: 1 },
data: { name: 'Alicia' }
});
// Delete
await prisma.user.delete({ where: { id: 1 } });
// With relations
const posts = await prisma.post.findMany({
include: { author: true },
take: 10
});
// Transaction
const result = await prisma.$transaction(async (tx) => {
const user = await tx.user.create({
data: {
name: 'Alice',
posts: { create: { title: 'Hello' } }
},
include: { posts: true }
});
return user;
});
@map and @@map to map Prisma model names to existing table/column namesnpx prisma generate after any schema changesselect to fetch only needed fields and avoid N+1 issues with include