在 Fastify 應用程式中使用 Prisma,從 MySQL、PostgreSQL 和 SQL Server 資料庫查詢資料 – 適用於 JavaScript 和 TypeScript 的更優質 ORM。
// Creating a new recordawait prisma.user.create({firstName: “Alice”,email: “alice@prisma.io”})
id firstName email1 Bobby bobby@tables.io2 Nilufar nilu@email.com3 Jürgen jums@dums.edu4 Alice alice@prisma.io
Prisma ORM 是一種次世代 ORM,用於在 Fastify 伺服器中查詢您的資料庫。您可以將其用作編寫純 SQL 查詢、查詢建構器(如 knex.js)或傳統 ORM(如 TypeORM、MikroORM 和 Sequelize)的替代方案。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 的宣告式塑模語言簡單易用,讓您能夠直覺地描述您的資料庫結構描述。
從宣告式 Prisma 結構描述產生可預測且可自訂的 SQL 遷移。
Prisma Client 透過為常見 API 功能(例如分頁、篩選器等)提供查詢,來減少重複程式碼。
除了協助生產環境疑難排解之外,探索一些確保 GraphQL 伺服器可靠運作的實務做法。
適用於 REST API 的隨時可執行範例專案,搭配 SQLite 資料庫
適用於 GraphQL API 的隨時可執行範例專案,搭配 PosgreSQL 資料庫
我們有多個管道,您可以在其中與社群成員以及 Prisma 團隊互動。