轻松、类型安全的数据库访问
Fastify 服务器中

使用 Prisma 在 Fastify 应用中查询 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 等查询构建器或 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)
在 Fastify 插件中使用 Prisma
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 示例项目