Overview

This page compares the Prisma and Sequelize APIs.

Fetching single objects

Prisma

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

Sequelize

1const user = await User.findByPk(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});

Sequelize

1const user = await User.findByPk(1, { attributes: ["name"], raw: true });

Tip Use the raw: true query option to return plain Javascript objects.

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.

Sequelize

1const user = await User.findByPk(id, {
2 include: [{
3 model: Post
4 }]
5})

Tip: Use model: Post as "Post" if you used an alias to define the relationship between User and Post - for example: User.hasMany(Post, { as: "Post", foreignKey: "authorId" });

Filtering for concrete values

Prisma

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

Sequelize

1const post = await Post.findAll({
2 raw: true,
3 where: {
4 title: {
5 [Op.like]: "%Hello%",
6 },
7 },
8});

Other filter criteria

Prisma

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

Sequelize

Sequelize has an extensive set of operators.

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});

Sequelize

Sequelize doesn't offer a dedicated API for relation filters. You can get similar functionality by sending a raw SQL query to the database.

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});

Sequelize

1const posts = await Post.findAll({
2 offset: 5,
3 limit: 10
4})

Creating objects

Prisma

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

Sequelize

Using `save`
Using `create`
1const user = User.build({
2 name: "Alice",
3 email: "alice@prisma,io"
4})
5await user.save()

Updating objects

Prisma

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

Sequelize

Using `save`
Using `update`
1user.name = "James"
2user.email =" alice@prisma.com"
3await user.save()

Deleting objects

Prisma

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

Sequelize

1await user.destroy()

Batch updates

Prisma

1const user = await prisma.user.updateMany({
2 data: {
3 name: "Published author!",
4 },
5 where: {
6 email: {
7 contains: "prisma.io"
8 }
9 },
10});

Sequelize

1const updatedUsers = await User.update({
2 { role: "Admin" },
3 where: {
4 email: {
5 [Op.like]: "%@prisma.io"
6 }
7 },
8})

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});

Sequelize

1await User.destroy({
2 where: {
3 id: {
4 [Op.in]: [id1, id2, id3]
5 }
6 }
7})

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});

Sequelize

Manual
Automatic
1return sequelize.transaction(async t => {
2 const user = await User.create({
3 name: "Alice",
4 email: "alice@prisma,io"
5 }, {
6 transaction: t
7 })
8 const post1 = await Post.create({
9 title: "Join us for GraphQL Conf in 2019"
10 }, {
11 transaction: t
12 })
13 const post2 = await Post.create({
14 title: "Subscribe to GraphQL Weekly for GraphQL news"
15 }, {
16 transaction: t
17 })
18 await user.setPosts([post1, post2])
19})
Edit this page on Github