使用 Prisma(一种更好的 JavaScript 和 TypeScript ORM)在 Fastify 应用程序中查询 MySQL、PostgreSQL 和 SQL Server 数据库中的数据。
Prisma 提供了数据库工具,这些工具是使用出色的 DX 构建高性能 Next.js 应用程序的完美伴侣。
Prisma Postgres 是第一个没有冷启动的无服务器数据库。它基于unikernels,在裸机上运行,并带有一个内置缓存、高性能查询和无缝扩展——所有这些都具有出色的 DX。
了解有关 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 的声明式建模语言很简单,让你能够直观地描述数据库模式。
从声明式 Prisma 模式生成可预测且可定制的 SQL 迁移。
Prisma Client 通过为常见的 API 功能(例如分页、筛选器等)提供查询来减少样板代码。
探索一些实践,以确保 GraphQL 服务器的可靠运行,并帮助进行生产故障排除。
一个包含 SQLite 数据库的 REST API 的即用型示例项目
一个包含 PostgreSQL 数据库的 GraphQL API 的即用型示例项目
我们有多个渠道,您可以与我们的社区成员以及 Prisma 团队互动。