輕鬆、類型安全的資料庫存取
Fastify 伺服器中

Fastify 應用程式中使用 Prisma,從 MySQL、PostgreSQL 和 SQL Server 資料庫查詢資料 – 適用於 JavaScript 和 TypeScript 的更優質 ORM。

tech

什麼是 Prisma?

Prisma 讓資料處理變得輕鬆!它提供類型安全的 Node.js 和 TypeScript ORM、全域資料庫快取、連線池和即時資料庫事件。

查詢
// Creating a new record
await prisma.user.create({
firstName: “Alice”,
email: “alice@prisma.io”
})
表格
id firstName email
1 Bobby bobby@tables.io
2 Nilufar nilu@email.com
3 Jürgen jums@dums.edu
4 Alice alice@prisma.io

Prisma 和 Fastify 如何協同運作

Prisma ORM 是一種次世代 ORM,用於在 Fastify 伺服器中查詢您的資料庫。您可以將其用作編寫純 SQL 查詢、查詢建構器(如 knex.js)或傳統 ORM(如 TypeORM、MikroORM 和 Sequelize)的替代方案。Prisma ORM 可用於建構 RESTGraphQL API,並與微服務和單體式架構順暢整合。

您還可以透過我們的其他工具,進一步強化 Prisma ORM 的使用。
Prisma Accelerate 是一種全域資料庫快取和可擴展的連線池,可加速您的資料庫查詢。
Prisma Pulse 使您能夠以類型安全的方式建構反應式即時應用程式。

Prisma 和 Fastify 程式碼範例

Prisma 提供了一個方便的資料庫存取層,可與 Fastify 完美整合。

以下程式碼示範了在使用 Fastify 建構 API 伺服器時,Prisma 的各種用途。

REST API

REST API

Prisma 在您的路由處理常式內部使用,以讀取和寫入資料庫中的資料。

1import fastify from 'fastify'
2import { PrismaClient } from '@prisma/client'
3
4const prisma = new PrismaClient()
5const app = fastify()
6
7app.get('/feed', async (req, res) => {
8 const posts = await prisma.post.findMany({
9 where: { published: true },
10 include: { author: true },
11 })
12 res.json(posts)
13})
14
15app.post('/post', async (req, res) => {
16 const { title, content, authorEmail } = req.body
17 const post = await prisma.post.create({
18 data: {
19 title,
20 content,
21 published: false,
22 author: { connect: { email: authorEmail } },
23 },
24 })
25 res.json(post)
26})
27
28app.put('/publish/:id', async (req, res) => {
29 const { id } = req.params
30 const post = await prisma.post.update({
31 where: { id },
32 data: { published: true },
33 })
34 res.json(post)
35})
36
37app.delete('/user/:id', async (req, res) => {
38 const { id } = req.params
39 const user = await prisma.user.delete({
40 where: {
41 id,
42 },
43 })
44 res.json(user)
45})
46
47app.listen(3000)
Fastify 外掛程式中的 Prisma
GraphQL API
Prisma 結構描述

REST API

Prisma 在您的路由處理常式內部使用,以讀取和寫入資料庫中的資料。

1import fastify from 'fastify'
2import { PrismaClient } from '@prisma/client'
3
4const prisma = new PrismaClient()
5const app = fastify()
6
7app.get('/feed', async (req, res) => {
8 const posts = await prisma.post.findMany({
9 where: { published: true },
10 include: { author: true },
11 })
12 res.json(posts)
13})
14
15app.post('/post', async (req, res) => {
16 const { title, content, authorEmail } = req.body
17 const post = await prisma.post.create({
18 data: {
19 title,
20 content,
21 published: false,
22 author: { connect: { email: authorEmail } },
23 },
24 })
25 res.json(post)
26})
27
28app.put('/publish/:id', async (req, res) => {
29 const { id } = req.params
30 const post = await prisma.post.update({
31 where: { id },
32 data: { published: true },
33 })
34 res.json(post)
35})
36
37app.delete('/user/:id', async (req, res) => {
38 const { id } = req.params
39 const user = await prisma.user.delete({
40 where: {
41 id,
42 },
43 })
44 res.json(user)
45})
46
47app.listen(3000)

為何選擇 Prisma 和 Fastify?

彈性架構

無論您建構的是微服務還是單體式應用程式,Prisma 都能完美融入您的堆疊。

更高的生產力

Prisma 提供資料庫查詢的自動完成、絕佳的開發人員體驗和完整的類型安全。

類型安全的資料庫用戶端

Prisma Client 確保完全類型安全的資料庫查詢,並具有自動完成等優點 – 即使在 JavaScript 中也是如此。

直覺式資料塑模

Prisma 的宣告式塑模語言簡單易用,讓您能夠直覺地描述您的資料庫結構描述。

輕鬆的資料庫遷移

從宣告式 Prisma 結構描述產生可預測且可自訂的 SQL 遷移。

專為建構 API 而設計

Prisma Client 透過為常見 API 功能(例如分頁、篩選器等)提供查詢,來減少重複程式碼。

精選 Prisma 和 Fastify 範例

使用 Fastify、Mercurius 和 Prisma 監控您的 GraphQL API

除了協助生產環境疑難排解之外,探索一些確保 GraphQL 伺服器可靠運作的實務做法。

Fastify REST API 入門套件

適用於 REST API 的隨時可執行範例專案,搭配 SQLite 資料庫

Fastify GraphQL API 入門套件

適用於 GraphQL API 的隨時可執行範例專案,搭配 PosgreSQL 資料庫