Sequelize
本页比较了 Prisma ORM 和 Sequelize API。
Sequelize vs Prisma ORM
虽然 Prisma ORM 和 Sequelize 解决了类似的问题,但它们的工作方式截然不同。
Sequelize 是一个传统的 ORM,它将表映射到模型类。然后,模型类的实例为运行时应用程序的 CRUD 查询提供接口。
Prisma ORM 是一种新型 ORM,它缓解了传统 ORM 的许多问题,例如臃肿的模型实例、混合业务与存储逻辑、缺乏类型安全或由惰性加载等引起的不可预测的查询。
它使用 Prisma schema 以声明方式定义应用程序模型。然后,Prisma Migrate 允许从 Prisma schema 生成 SQL 迁移,并针对数据库执行这些迁移。CRUD 查询由 Prisma Client 提供,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
- 使用 `include`
- Fluent API
const posts = await prisma.user.findUnique({
where: {
id: 2,
},
include: {
post: true,
},
})
const posts = await prisma.user
.findUnique({
where: {
id: 2,
},
})
.post()
注意:
select
返回一个包含post
数组的user
对象,而 fluent API 仅返回一个post
数组。
Sequelize
const user = await User.findByPk(id, {
include: [
{
model: Post,
},
],
})
如果您使用别名来定义 User
和 Post
之间的关系,请使用 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: '[email protected]',
},
})
Sequelize
- 使用 `save`
- 使用 `create`
const user = User.build({
name: 'Alice',
email: 'alice@prisma,io',
})
await user.save()
const user = await User.create({
name: 'Alice',
email: 'alice@prisma,io',
})
更新对象
Prisma ORM
const user = await prisma.user.update({
data: {
name: 'Alicia',
},
where: {
id: 2,
},
})
Sequelize
- 使用 `save`
- 使用 `update`
user.name = 'James'
user.email = ' [email protected]'
await user.save()
await User.update({
name: 'James',
email: '[email protected]',
})
删除对象
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: '[email protected]',
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])
})
return sequelize.$transaction(async (transaction) => {
try {
const user = await User.create({
name: 'Alice',
email: 'alice@prisma,io',
})
const post1 = await Post.create({
title: 'Join us for GraphQL Conf in 2019',
})
const post2 = await Post.create({
title: 'Subscribe to GraphQL Weekly for GraphQL news',
})
await user.setPosts([post1, post2])
} catch (e) {
return transaction.rollback()
}
})