Overview

This page compares the Prisma and Sequelize APIs.

Fetching single objects

Prisma

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

Sequelize

const user = await User.findByPk(id)

Fetching selected scalars of single objects

Prisma

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

Sequelize

const 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
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.

Sequelize

const user = await User.findByPk(id, {
include: [{
model: Post
}]
})

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

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

Sequelize

const post = await Post.findAll({
raw: true,
where: {
title: {
[Op.like]: "%Hello%",
},
},
});

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:

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

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:

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

Offset pagination:

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

Sequelize

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

Creating objects

Prisma

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

Sequelize

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

Updating objects

Prisma

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

Sequelize

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

Deleting objects

Prisma

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

Sequelize

await user.destroy()

Batch updates

Prisma

const user = await prisma.user.updateMany({
data: {
name: "Published author!",
},
where: {
email: {
contains: "prisma.io"
}
},
});

Sequelize

const updatedUsers = await User.update({
{ role: "Admin" },
where: {
email: {
[Op.like]: "%@prisma.io"
}
},
})

Batch deletes

Prisma

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

Sequelize

await User.destroy({
where: {
id: {
[Op.in]: [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" },
],
},
},
});

Sequelize

Manual
Automatic
return sequelize.$transaction(async t => {
const user = await User.create({
name: "Alice",
email: "alice@prisma,io"
}, {
transaction: t
})
const post1 = await Post.create({
title: "Join us for GraphQL Conf in 2019"
}, {
transaction: t
})
const post2 = await Post.create({
title: "Subscribe to GraphQL Weekly for GraphQL news"
}, {
transaction: t
})
await user.setPosts([post1, post2])
})
Edit this page on Github