跳至主要内容

Sequelize

本页比较了 Prisma ORM 和 Sequelize API。

Sequelize 对比 Prisma ORM

虽然 Prisma ORM 和 Sequelize 解决了类似的问题,但它们的工作方式却截然不同。

Sequelize 是一个传统的 ORM,它将映射到模型类。模型类的实例在运行时为应用程序提供执行 CRUD 查询的接口。

Prisma ORM 是一种新型的 ORM,它解决了传统 ORM 的许多问题,例如臃肿的模型实例、将业务逻辑与存储逻辑混合、缺乏类型安全或因延迟加载等原因导致不可预测的查询。

它使用Prisma schema以声明式方式定义应用程序模型。然后 Prisma Migrate 可以从 Prisma schema 生成 SQL 迁移并在数据库上执行。CRUD 查询由 Prisma Client 提供,这是一个轻量级且完全类型安全的 Node.js 和 TypeScript 数据库客户端。

API 对比

获取单个对象

Prisma ORM

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

Sequelize

const user = await User.findByPk(id)

获取单个对象的选定标量字段

Prisma ORM

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

Sequelize

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

使用 raw: true 查询选项返回纯 JavaScript 对象。

获取关联关系

Prisma ORM

const posts = await prisma.user.findUnique({
where: {
id: 2,
},
include: {
post: true,
},
})

注意select 返回一个包含 post 数组的用户对象,而流畅 API 只返回一个 post 数组。

Sequelize

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

如果您使用别名定义了 UserPost 之间的关系,请使用 model: Post as "Post" - 例如:User.hasMany(Post, { as: "Post", foreignKey: "authorId" });

按具体值过滤

Prisma ORM

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

Sequelize

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

其他过滤条件

Prisma ORM

Prisma ORM 生成了许多在现代应用程序开发中常用的额外过滤器

Sequelize

Sequelize 拥有广泛的运算符集合

关联过滤器

Prisma ORM

Prisma ORM 允许您根据不仅适用于正在检索的列表模型,还适用于该模型关联关系的条件来过滤列表。

例如,以下查询返回标题中包含“Hello”的一个或多个帖子的用户

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

Sequelize

Sequelize不提供专门的关联过滤器 API。您可以通过向数据库发送原始 SQL 查询来获得类似的功能。

分页

Prisma ORM

游标式分页

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

偏移量分页

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

Sequelize

游标分页

const posts = await Post.findAll({
limit: 20,
where: {
id: {
[Op.gt]: 242,
},
},
})

注意:Sequelize 使用Sequelize 运算符执行游标分页。

偏移量分页

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

创建对象

Prisma ORM

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

Sequelize

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

更新对象

Prisma ORM

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

Sequelize

user.name = 'James'
user.email = ' alice@prisma.com'
await user.save()

删除对象

Prisma ORM

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

Sequelize

await user.destroy()

批量更新

Prisma ORM

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"
}
},
})

批量删除

Prisma ORM

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],
},
},
})

事务

Prisma ORM

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

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

保持与 Prisma 的联系

通过与 我们的活跃社区保持联系,继续您的 Prisma 之旅。随时了解最新信息,积极参与,并与其他开发者协作

我们非常珍视您的参与,并期待您成为我们社区的一员!