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 提供,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: 'alice@prisma.io',
},
})
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 = ' alice@prisma.com'
await user.save()
await User.update({
name: 'James',
email: 'james@prisma.io',
})
刪除物件
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])
})
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()
}
})