Skip to main content

Sequelize

本页比较 Prisma ORM 和 Sequelize API。

¥This page compares the Prisma ORM and Sequelize APIs.

Sequelize 与 Prisma ORM

¥Sequelize vs Prisma ORM

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

¥While Prisma ORM and Sequelize solve similar problems, they work in very different ways.

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

¥Sequelize is a traditional ORM which maps tables to model classes. Instances of the model classes then provide an interface for CRUD queries to an application at runtime.

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

¥Prisma ORM is a new kind of ORM that mitigates many problems of traditional ORMs, such as bloated model instances, mixing business with storage logic, lack of type-safety or unpredictable queries caused e.g. by lazy loading.

它使用 Prisma 架构 以声明性方式定义应用模型。然后,Prisma Migrate 允许从 Prisma 架构生成 SQL 迁移并针对数据库执行它们。CRUD 查询由 Prisma Client 提供,这是一个适用于 Node.js 和 TypeScript 的轻量级且完全类型安全的数据库客户端。

¥It uses the Prisma schema to define application models in a declarative way. Prisma Migrate then allows to generate SQL migrations from the Prisma schema and executes them against the database. CRUD queries are provided by Prisma Client, a lightweight and entirely type-safe database client for Node.js and TypeScript.

API 比较

¥API comparison

获取单个对象

¥Fetching single objects

Prisma ORM

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

Sequelize

const user = await User.findByPk(id)

获取单个对象的选定标量

¥Fetching selected scalars of single objects

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 对象。

¥Use the raw: true query option to return plain JavaScript objects.

获取关系

¥Fetching relations

Prisma ORM

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

注意:select 返回一个包含 post 数组的 user 对象,而 Fluent API 仅返回一个 post 数组。

¥Note: select returns 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,
},
],
})
提示

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

¥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 ORM

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 ORM

Prisma ORM 生成许多现代应用开发中常用的 附加过滤器

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

Sequelize

Sequelize 有 广泛的运算符集

¥Sequelize has an extensive set of operators.

关系过滤器

¥Relation filters

Prisma ORM

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

¥Prisma ORM 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 不提供关系过滤器的专用 API。你可以通过向数据库发送原始 SQL 查询来获得类似的功能。

¥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 ORM

光标式分页:

¥Cursor-style pagination:

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

偏移分页:

¥Offset pagination:

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

Sequelize

光标分页:

¥Cursor pagination:

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

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

¥Note: Sequelize use the Sequelize operators to perform cursor pagination.

偏移分页:

¥Offset pagination:

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

创建对象

¥Creating objects

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()

更新对象

¥Updating objects

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()

删除对象

¥Deleting objects

Prisma ORM

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

Sequelize

await user.destroy()

批量更新

¥Batch updates

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

批量删除

¥Batch deletes

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

事务

¥Transactions

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

Stay connected with Prisma

Continue your Prisma journey by connecting with our active community. Stay informed, get involved, and collaborate with other developers:

We genuinely value your involvement and look forward to having you as part of our community!