Overview
This page compares Prisma with Sequelize. Here is a high-level overview:
Prisma | Sequelize | |
---|---|---|
Auto-generated DB client | ||
Type-safe | ||
Declarative data modelling & migrations | ||
Connection pooling | ||
Supported DB types | Document & Relational | Relational |
Supported ORM patterns | DataMapper | ActiveRecord |
Fluent API for relations | ||
Relation filters | ||
Raw database access | ||
Transactional nested writes | ||
Manual transactions |
In the following, you find a detailled comparison of the Sequelize and Prisma APIs.
Fetching single objects
Prisma
const user = await prisma.user({ id })
Sequelize
const user = await User.findByPk(id)
Fetching selected scalars of single objects
Prisma
const userFragment = await prisma.user({ id }).$fragment(`
fragment NameAndEmail on User { id email }`
`)
Sequelize
const user = await User.findByPk(id, attributes: {
["name", "email"]
})
Fetching relations
Prisma
const postsByUser = await prisma.user({ id }).posts()
While the
$fragment
and the$graphql
APIs each return auser
object that includes aposts
array, the fluent API returns just theposts
array and no data about theuser
.
See the following GitHub issues to learn more about planned iterations of the Prisma client relations API:
Sequelize
const user = await User.findByPk(id, {
include: [
{
model: Post,
},
],
})
Filtering for concrete values
Prisma
const users = await prisma.users({
where: {
name: 'Alice',
},
})
Sequelize
const user = User.findAll({
where: {
name: 'Alice',
},
})
Other filter criteria
Prisma
Prisma generates many additional filters that are commonly used in modern application development:
<field>_ends_with
&<field>_starts_with
<field>_not_ends_with
&<field>_not_starts_with
<field>_gt
&<field>_gte
<field>_lt
&<field>_lte
<field>_contains
&<field>_not_contains
<field>_in
&<field>_not_in
Sequelize
Sequelize has an extensive set of operators to be found here.
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, you want to fetch only those users that wrote a post with the title "Hello World":
query {
user(where: {
posts_some: {
title: "Hello World"
}
}) {
id
}
}
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
const posts = await prisma.posts({
skip: 5,
first: 10,
})
In addition to skip
and first
, the Prisma API also offers:
last
before
&after
for cursor based pagination- Relay-style pagination
Sequelize
const posts = await Post.findAll({
offset: 5,
limit: 10,
})
Creating objects
Prisma
const user = await prisma.createUser({
name: 'Alice',
email: 'alice@prisma.io',
})
Sequelize
const user = User.build({
name: 'Alice',
email: 'alice@prisma,io',
})
await user.save()
Updating objects
Prisma
const updatedUser = await prisma.updateUser({
where: { id },
data: {
name: 'James',
email: 'james@prisma.io',
},
})
Sequelize
user.name = 'James'
user.email = ' alice@prisma.com'
await user.save()
Deleting objects
Prisma
const deletedUser = await prisma.deleteUser({ id })
Sequelize
await user.destroy()
Batch updates
Prisma
const updatedUsers = await prisma.updateManyUsers({
data: { role: 'ADMIN' },
where: {
email_ends_with: '@prisma.io',
},
})
Sequelize
const updatedUsers = await User.update({
{ role: "Admin" },
where: {
email: {
[Op.like]: "%@prisma.io"
}
},
})
Batch deletes
Prisma
await prisma.deleteManyUsers({
id_in: [id1, id2, id3],
})
Sequelize
await User.destroy({
where: {
id: {
[Op.in]: [id1, id2, id3],
},
},
})
Transactions
Prisma
const newUser = await prisma.createUser({
name: 'Bob',
email: 'bob@prisma.io',
posts: {
create: [
{ title: 'Join us for GraphQL Conf in 2019' },
{ title: 'Subscribe to GraphQL Weekly for GraphQL news' },
],
},
})
Sequelize
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])
})