Overview

This page compares Prisma and TypeORM.

API comparison

Fetching single objects

Prisma

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

TypeORM

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

Fetching selected scalars of single objects

Prisma

1const user = await prisma.user.findOne({
2 where: {
3 id: 1,
4 },
5 select: {
6 name: true,
7 },
8});

TypeORM

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

Fetching relations

Prisma

Using include
Fluent API
1const posts = await prisma.user.findOne({
2 where: {
3 id: 2,
4 },
5 include: {
6 post: true,
7 },
8});

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
1const userRepository = getRepository(User)
2const user = await userRepository.findOne(id, {
3 relations: ["posts"]
4})

Filtering for concrete values

Prisma

1const posts = await prisma.post.findMany({
2 where: {
3 title: {
4 contains: "Hello",
5 },
6 },
7});

TypeORM

1const userRepository = getRepository(User)
2const users = await userRepository.find({
3 where: {
4 name: "Alice"
5 }
6})

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:

1const posts = await prisma.user.findMany({
2 where: {
3 Post: {
4 some: {
5 title: {
6 contains: "Hello",
7 },
8 },
9 },
10 }
11});

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:

1const page = prisma.post.findMany({
2 before: {
3 id: 242,
4 },
5 last: 20,
6 });

Offset pagination:

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

TypeORM

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

Creating objects

Prisma

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

TypeORM

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

Updating objects

Prisma

1const user = await prisma.user.update({
2 data: {
3 name: "Alicia",
4 },
5 where: {
6 id: 2
7 }
8});

TypeORM

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

Deleting objects

Prisma

1const user = prisma.user.delete({
2 where: {
3 id: 10,
4 },
5});

TypeORM

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

Batch updates

Prisma

1const user = await prisma.user.updateMany({
2 data: {
3 name: "Published author!",
4 },
5 where: {
6 Post: {
7 some: {
8 published: true,
9 },
10 },
11 },
12});

TypeORM

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

Batch deletes

Prisma

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

TypeORM

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

Transactions

Prisma

1const user = await prisma.user.create({
2 data: {
3 email: "bob.rufus@prisma.io",
4 name: "Bob Rufus",
5 Post: {
6 create: [
7 { title: "Working at Prisma" },
8 { title: "All about databases" },
9 ],
10 },
11 },
12});

TypeORM

1await getConnection().transaction(async transactionalEntityManager => {
2 const user = getRepository(User).create({
3 name: "Bob",
4 email: "bob@prisma.io"
5 })
6 const post1 = getRepository(Post).create({
7 title: "Join us for GraphQL Conf in 2019",
8 })
9 const post2 = getRepository(Post).create({
10 title: "Subscribe to GraphQL Weekly for GraphQL news",
11 })
12 user.posts = [post1, post2]
13 await transactionalEntityManager.save(post1)
14 await transactionalEntityManager.save(post2)
15 await transactionalEntityManager.save(user)
16 })
17
18export const _frontmatter = {"title":"Comparing Prisma and TypeORM","metaTitle":"Comparing Prisma and TypeORM","metaDescription":"Compare Prisma and TypeORM."}
Edit this page on Github