Understand Prisma / API comparisons

Comparing Prisma and TypeORM


Overview

This page compares Prisma and TypeORM.

API comparison

Fetching single objects

Prisma

const user = await prisma.user.findOne({
where: {
id: 1,
},
});

TypeORM

const userRepository = getRepository(User)
const user = await userRepository.findOne(id)

Fetching selected scalars of single objects

Prisma

const user = await prisma.user.findOne({
where: {
id: 1,
},
select: {
name: true,
},
});

TypeORM

const userRepository = getRepository(User)
const user = await userRepository.findOne(id, {
select: ["id", "email"]
})

Fetching relations

Prisma

Using include
Fluent API
const posts = await prisma.user.findOne({
where: {
id: 2,
},
include: {
post: true,
},
});

Note: select return a user object that includes a post array, whereas the fluent API only returns a post array.

TypeORM

Using `relations`
Using JOIN
Eager relations
const userRepository = getRepository(User)
const user = await userRepository.findOne(id, {
relations: ["posts"]
})

Filtering for concrete values

Prisma

const posts = await prisma.post.findMany({
where: {
title: {
contains: "Hello",
},
},
});

TypeORM

const userRepository = getRepository(User)
const users = await userRepository.find({
where: {
name: "Alice"
}
})

Other filter criteria

Prisma

Prisma generates many additional filters that are commonly used in modern application development.

TypeORM

TypeORM provides built-in operators that can be used to create more complex comparisons

Relation filters

Prisma

Prisma lets you filter a list based on a criteria that applies not only to the models of the list being retrieved, but to a relation of that model.

For example, the following query returns users with one or more posts with "Hello" in the title:

const posts = await prisma.user.findMany({
where: {
Post: {
some: {
title: {
contains: "Hello",
},
},
},
}
});

TypeORM

TypeORM doesn't offer a dedicated API for relation filters. You can get similar functionality by using the QueryBuilder or writing the queries by hand.

Pagination

Prisma

Cursor-style pagination:

const page = prisma.post.findMany({
before: {
id: 242,
},
last: 20,
});

Offset pagination:

const cc = prisma.post.findMany({
skip: 200,
first: 20,
});

TypeORM

const postRepository = getRepository(Post)
const posts = await postRepository.find({
skip: 5,
take: 10
})

Creating objects

Prisma

const user = await prisma.user.create({
data: {
email: "alice@prisma.io",
},
});

TypeORM

Using `save`
Using `create`
Using `insert`
const user = new User()
user.name = "Alice"
user.email = "alice@prisma.io"
await user.save()

Updating objects

Prisma

const user = await prisma.user.update({
data: {
name: "Alicia",
},
where: {
id: 2
}
});

TypeORM

const userRepository = getRepository(User)
const updatedUser = await userRepository.update(id, {
name: "James",
email: "james@prisma.io"
})

Deleting objects

Prisma

const user = prisma.user.delete({
where: {
id: 10,
},
});

TypeORM

Using `delete`
Using `remove`
const userRepository = getRepository(User)
await userRepository.delete(id)

Batch updates

Prisma

const user = await prisma.user.updateMany({
data: {
name: "Published author!",
},
where: {
Post: {
some: {
published: true,
},
},
},
});

TypeORM

You can use the query builder to update entities in your database.

Batch deletes

Prisma

const users = await prisma.user.deleteMany({
where: {
id: {
in: [1, 2, 6, 6, 22, 21, 25],
},
},
});

TypeORM

Using `delete`
Using `remove`
const userRepository = getRepository(User)
await userRepository.delete([id1, id2, id3])

Transactions

Prisma

const user = await prisma.user.create({
data: {
email: "bob.rufus@prisma.io",
name: "Bob Rufus",
Post: {
create: [
{ title: "Working at Prisma" },
{ title: "All about databases" },
],
},
},
});

TypeORM

await getConnection().$transaction(async transactionalEntityManager => {
const user = getRepository(User).create({
name: "Bob",
email: "bob@prisma.io"
})
const post1 = getRepository(Post).create({
title: "Join us for GraphQL Conf in 2019",
})
const post2 = getRepository(Post).create({
title: "Subscribe to GraphQL Weekly for GraphQL news",
})
user.posts = [post1, post2]
await transactionalEntityManager.save(post1)
await transactionalEntityManager.save(post2)
await transactionalEntityManager.save(user)
})
export const _frontmatter = {"title":"Comparing Prisma and TypeORM","metaTitle":"Comparing Prisma and TypeORM","metaDescription":"Compare Prisma and TypeORM."}
Edit this page on Github