apps/docs/content/docs.v6/guides/migrate-from-typeorm.mdx
This guide shows you how to migrate your application from TypeORM to Prisma ORM. We'll use an extended version of the TypeORM 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 TypeORM on the Prisma ORM vs TypeORM page.
Before starting this guide, make sure you have:
The steps for migrating from TypeORM 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 TypeORM for database access.
Create a new Prisma schema file:
npx prisma init --output ../generated/prisma
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.
Create and apply a baseline migration to mark the current state of your database:
npx prisma migrate diff --from-empty --to-schema prisma/schema.prisma --script > baseline.sql
npx prisma migrate resolve --applied "baseline"
Install the Prisma Client package:
npm install @prisma/client
Generate Prisma Client:
npx prisma generate
Start replacing your TypeORM queries with Prisma Client. Here's an example of how to convert some common queries:
// Find one
const user = await userRepository.findOne({
where: { id: 1 },
});
// Create
const user = await userRepository.save({
email: "[email protected]",
name: "Alice",
});
// Update
await userRepository.update(1, {
name: "New name",
});
// Delete
await userRepository.delete(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 the CreateUserAction:
import { prisma } from "../client";
export class CreateUserAction {
async run(req: Request, res: Response) {
const { email, name } = req.body;
const result = await prisma.user.create({
data: {
email,
name,
},
});
return res.json(result);
}
}
Test all migrated endpoints to ensure they work as expected:
npm test
npx prisma migrate deploy
Now that you've migrated to Prisma ORM, you can:
For more information: