使用 Prisma 在 Fastify 應用程式中查詢 MySQL、PostgreSQL 和 SQL Server 資料庫——JavaScript 和 TypeScript 的更好 ORM。
Prisma 提供資料庫工具,是構建高效能 Next.js 應用程式並提供出色開發體驗的完美伴侶。
Prisma Postgres 是第一個沒有冷啟動的無伺服器資料庫。它基於單核作業系統(unikernels),在裸機上執行,並內建快取、高效能查詢和無縫擴充套件——所有這些都提供了出色的開發體驗。
瞭解更多關於 Prisma Postgres 的資訊Prisma ORM 是下一代 ORM,用於在 Fastify 伺服器中查詢您的資料庫。您可以將其作為編寫純 SQL 查詢、像 knex.js 這樣的查詢構建器或像 TypeORM、MikroORM 和 Sequelize 這樣的傳統 ORM 的替代方案。Prisma ORM 可用於構建 REST 和 GraphQL API,並且與微服務和單體架構都能流暢整合。
您還可以使用我們的其他工具來增強 Prisma ORM 的使用
• Prisma Accelerate 是一個全球資料庫快取和可擴充套件連線池,可加快您的資料庫查詢。
• Prisma Pulse 使您能夠以型別安全的方式構建響應式、即時應用程式。
Prisma 提供了一個方便的資料庫訪問層,與 Fastify 完美整合。
下面的程式碼演示了在使用 Fastify 構建 API 伺服器時 Prisma 的各種用法。
Prisma 在您的路由處理程式內部使用,用於在資料庫中讀寫資料。
1import fastify from 'fastify'2import { PrismaClient } from '@prisma/client'34const prisma = new PrismaClient()5const app = fastify()67app.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})1415app.post('/post', async (req, res) => {16 const { title, content, authorEmail } = req.body17 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})2728app.put('/publish/:id', async (req, res) => {29 const { id } = req.params30 const post = await prisma.post.update({31 where: { id },32 data: { published: true },33 })34 res.json(post)35})3637app.delete('/user/:id', async (req, res) => {38 const { id } = req.params39 const user = await prisma.user.delete({40 where: {41 id,42 },43 })44 res.json(user)45})4647app.listen(3000)
Prisma 在您的路由處理程式內部使用,用於在資料庫中讀寫資料。
1import fastify from 'fastify'2import { PrismaClient } from '@prisma/client'34const prisma = new PrismaClient()5const app = fastify()67app.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})1415app.post('/post', async (req, res) => {16 const { title, content, authorEmail } = req.body17 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})2728app.put('/publish/:id', async (req, res) => {29 const { id } = req.params30 const post = await prisma.post.update({31 where: { id },32 data: { published: true },33 })34 res.json(post)35})3637app.delete('/user/:id', async (req, res) => {38 const { id } = req.params39 const user = await prisma.user.delete({40 where: {41 id,42 },43 })44 res.json(user)45})4647app.listen(3000)
無論您是構建微服務還是單體應用程式,Prisma 都能完美融入您的技術棧。
Prisma 為您的資料庫查詢提供自動補全功能、出色的開發者體驗和完全的型別安全。
Prisma Client 確保完全型別安全的資料庫查詢,並提供自動補全等優點——即使在 JavaScript 中也是如此。
Prisma 的宣告式建模語言簡單易懂,讓您直觀地描述資料庫 Schema。
從宣告式 Prisma Schema 生成可預測且可定製的 SQL 遷移。
Prisma Client 透過為常見 API 功能(例如分頁、過濾器等)提供查詢,減少了樣板程式碼。
探索一些實踐,以確保 GraphQL 伺服器的可靠執行,並幫助進行生產故障排除。
一個帶有 SQLite 資料庫的 REST API 的即用型示例專案
一個帶有 PostgreSQL 資料庫的 GraphQL API 的即用型示例專案
我們有多個渠道,您可以與社群成員以及 Prisma 團隊進行互動。