跳到主要內容

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 陣列的 user 物件,而 fluent 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 之旅 我們的活躍社群。保持資訊暢通,積極參與,並與其他開發者協作

我們真誠地感謝您的參與,並期待您成為我們社群的一份子!

© . This site is unofficial and not affiliated with Prisma Data, Inc.