輕鬆、型別安全的資料庫訪問
Fastify 伺服器中

使用 Prisma 在 Fastify 應用程式中查詢 MySQL、PostgreSQL 和 SQL Server 資料庫——JavaScript 和 TypeScript 的更好 ORM。

tech

Prisma 是什麼?

Prisma 提供資料庫工具,是構建高效能 Next.js 應用程式並提供出色開發體驗的完美伴侶。

ORM

Prisma ORM 是最受歡迎的 TypeScript ORM。它帶有可讀性強的 Schema、自動化遷移以及直觀、完全型別安全的查詢 API。

瞭解更多關於 Prisma ORM 的資訊

Postgres

Prisma Postgres 是第一個沒有冷啟動的無伺服器資料庫。它基於單核作業系統(unikernels),在裸機上執行,並內建快取、高效能查詢和無縫擴充套件——所有這些都提供了出色的開發體驗。

瞭解更多關於 Prisma Postgres 的資訊

Prisma 和 Fastify 如何協同工作

Prisma ORM 是下一代 ORM,用於在 Fastify 伺服器中查詢您的資料庫。您可以將其作為編寫純 SQL 查詢、像 knex.js 這樣的查詢構建器或像 TypeORM、MikroORM 和 Sequelize 這樣的傳統 ORM 的替代方案。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)
Prisma 在 Fastify 外掛中
GraphQL API
Prisma Schema

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 的宣告式建模語言簡單易懂,讓您直觀地描述資料庫 Schema。

輕鬆的資料庫遷移

從宣告式 Prisma Schema 生成可預測且可定製的 SQL 遷移。

專為構建 API 設計

Prisma Client 透過為常見 API 功能(例如分頁、過濾器等)提供查詢,減少了樣板程式碼。

特色 Prisma & Fastify 示例

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

探索一些實踐,以確保 GraphQL 伺服器的可靠執行,並幫助進行生產故障排除。

Fastify REST API 入門套件

一個帶有 SQLite 資料庫的 REST API 的即用型示例專案

Fastify GraphQL API 入門套件

一個帶有 PostgreSQL 資料庫的 GraphQL API 的即用型示例專案

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