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

在 Express 應用程式中使用 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 和 Express 如何協同運作

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

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

Prisma 和 Express 程式碼範例

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

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

REST API

REST API

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

1import express from 'express'
2import { PrismaClient } from '@prisma/client'
3
4const prisma = new PrismaClient()
5const app = express()
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
47const server = app.listen(3000)
GraphQL API
Prisma schema

REST API

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

1import express from 'express'
2import { PrismaClient } from '@prisma/client'
3
4const prisma = new PrismaClient()
5const app = express()
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
47const server = app.listen(3000)

為什麼選擇 Prisma 和 Express?

彈性架構

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

更高的生產力

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

類型安全的資料庫用戶端

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

直覺的資料塑模

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

輕鬆的資料庫遷移

從宣告式 Prisma schema 產生可預測和可自訂的 SQL 遷移。

專為建構 API 而設計

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

精選 Prisma 和 Express 範例

如何使用 Prisma 和 PostgreSQL 建構 REST API

使用 Express、Prisma 和 PostgreSQL 建構 REST API 的綜合教學課程

REST API 入門套件

適用於具有 SQLite 資料庫的 REST API 的隨時可執行範例專案

GraphQL API 入門套件

適用於具有 SQLite 資料庫的 GraphQL API 的隨時可執行範例專案