查詢資料庫
使用 Prisma Client 撰寫您的第一個查詢
現在您已經產生了 Prisma Client,您可以開始撰寫查詢,以讀取和寫入資料庫中的資料。為了本指南的目的,您將使用一個純 Node.js 腳本來探索 Prisma Client 的一些基本功能。
如果您正在建構 REST API,您可以在您的路由處理程序中使用 Prisma Client,根據傳入的 HTTP 請求來讀取和寫入資料庫中的資料。如果您正在建構 GraphQL API,您可以在您的解析器中使用 Prisma Client,根據傳入的查詢和變更來讀取和寫入資料庫中的資料。
然而,為了本指南的目的,您將僅建立一個純 Node.js 腳本,以學習如何使用 Prisma Client 將查詢傳送至您的資料庫。一旦您了解 API 的運作方式,您就可以開始將其整合到您的實際應用程式碼中 (例如 REST 路由處理程序或 GraphQL 解析器)。
建立一個名為 index.js
的新檔案,並將以下程式碼新增至其中
const { PrismaClient } = require('@prisma/client')
const prisma = new PrismaClient()
async function main() {
// ... you will write your Prisma Client queries here
}
main()
.then(async () => {
await prisma.$disconnect()
})
.catch(async (e) => {
console.error(e)
await prisma.$disconnect()
process.exit(1)
})
以下是程式碼片段不同部分的快速概觀
- 從
@prisma/client
node 模組匯入PrismaClient
建構子 - 實例化
PrismaClient
- 定義一個名為
main
的async
函數,以將查詢傳送至資料庫 - 連接到資料庫
- 呼叫
main
函數 - 在腳本終止時關閉資料庫連線
在 main
函數內部,新增以下查詢以從資料庫讀取所有 User
記錄並印出結果
async function main() {
- // ... you will write your Prisma Client queries here
+ const allUsers = await prisma.user.findMany()
+ console.log(allUsers)
}
現在使用此命令執行程式碼
node index.js
如果您內省了一個具有記錄的現有資料庫,則查詢應傳回 JavaScript 物件的陣列。
將資料寫入資料庫
您在前一節中使用的 findMany
查詢僅讀取資料庫中的資料 (儘管它仍然是空的)。在本節中,您將學習如何撰寫查詢,以將新記錄寫入到 Post
、User
和 Comment
表格中。
調整 main
函數以將 create
查詢傳送至資料庫
async function main() {
await prisma.user.create({
data: {
name: 'Rich',
email: 'hello@prisma.com',
posts: {
create: {
title: 'My first post',
body: 'Lots of really interesting stuff',
slug: 'my-first-post',
},
},
},
})
const allUsers = await prisma.user.findMany({
include: {
posts: true,
},
})
console.dir(allUsers, { depth: null })
}
此程式碼使用巢狀寫入查詢建立新的 User
記錄以及新的 Post
。User
記錄分別通過 Post.author
↔ User.posts
關聯欄位連接到另一個記錄。
請注意,您正在將 include
選項傳遞給 findMany
,這告訴 Prisma Client 在傳回的 User
物件上包含 posts
關聯。
使用此命令執行程式碼
node index.js
輸出應如下所示
[
{
id: '60cc9b0e001e3bfd00a6eddf',
email: 'hello@prisma.com',
name: 'Rich',
posts: [
{
id: '60cc9bad005059d6007f45dd',
slug: 'my-first-post',
title: 'My first post',
body: 'Lots of really interesting stuff',
userId: '60cc9b0e001e3bfd00a6eddf',
},
],
},
]
查詢將新記錄新增到 User
和 Post
集合中
Prisma schema 中的 id
欄位對應到底層 MongoDB 資料庫中的 _id
。
User 集合
_id | name | |
---|---|---|
60cc9b0e001e3bfd00a6eddf | "hello@prisma.com" | "Rich" |
Post 集合
_id | createdAt | title | content | published | authorId |
---|---|---|---|---|---|
60cc9bad005059d6007f45dd | 2020-03-21T16:45:01.246Z | "My first post" | Lots of really interesting stuff | false | 60cc9b0e001e3bfd00a6eddf |
注意:
Post
上authorId
文件欄位中的唯一識別碼參考User
集合中的_id
文件欄位,表示_id
值60cc9b0e001e3bfd00a6eddf
欄因此參考資料庫中的第一個 (也是唯一的)User
記錄。
在繼續下一節之前,您將使用 update
查詢將一些註解新增到您剛建立的 Post
記錄。如下調整 main
函數
async function main() {
await prisma.post.update({
where: {
slug: 'my-first-post',
},
data: {
comments: {
createMany: {
data: [
{ comment: 'Great post!' },
{ comment: "Can't wait to read more!" },
],
},
},
},
})
const posts = await prisma.post.findMany({
include: {
comments: true,
},
})
console.dir(posts, { depth: Infinity })
}
現在使用與之前相同的命令執行程式碼
node index.js
您將看到以下輸出
[
{
id: '60cc9bad005059d6007f45dd',
slug: 'my-first-post',
title: 'My first post',
body: 'Lots of really interesting stuff',
userId: '60cc9b0e001e3bfd00a6eddf',
comments: [
{
id: '60cca420008a21d800578793',
postId: '60cca40300af8bf000f6ca99',
comment: 'Great post!',
},
{
id: '60cca420008a21d800578794',
postId: '60cca40300af8bf000f6ca99',
comment: "Can't wait to try this!",
},
],
},
]
太棒了,您剛剛第一次使用 Prisma Client 將新資料寫入您的資料庫 🚀