TypeORM
Learn how to migrate from TypeORM to Prisma ORM
Introduction
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.
Prerequisites
Before starting this guide, make sure you have:
- A TypeORM project you want to migrate
- Node.js installed (version 16 or higher)
- PostgreSQL or another supported database
- Basic familiarity with TypeORM and Express.js
2. Prepare for migration
2.1. Understand the migration process
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:
- Install the Prisma CLI
- Introspect your database
- Create a baseline migration
- Install Prisma Client
- Gradually replace your TypeORM 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 TypeORM for database access.
2.2. Set up Prisma configuration
Create a new Prisma schema file:
npx prisma init --output ../generated/prismaUpdate the DATABASE_URL in the .env file with your database connection string:
DATABASE_URL="postgresql://USER:PASSWORD@HOST:PORT/DATABASE"2.3. Configure Prisma
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"),
},
});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 dotenv3. Migrate the database schema
3.1. Introspect your database
Run Prisma's introspection to create the Prisma schema from your existing database:
npx prisma db pullThis will create a schema.prisma file with your database schema.
3.2. Create a baseline migration
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.sqlnpx prisma migrate resolve --applied "baseline"4. Update your application code
4.1. Install Prisma Client
Install the Prisma Client package:
npm install @prisma/clientGenerate Prisma Client:
npx prisma generate4.2. Replace TypeORM queries
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: "alice@prisma.io",
name: "Alice",
});
// Update
await userRepository.update(1, {
name: "New name",
});
// Delete
await userRepository.delete(1);4.3. Update your controllers
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);
}
}5. Test and deploy
5.1. Test your changes
Test all migrated endpoints to ensure they work as expected:
npm test5.2. Deploy your changes
- Deploy your schema changes:
npx prisma migrate deploy- Deploy your application code with the updated dependencies.
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: