主题
Prisma
Prisma 是一个 ORM,允许我们以类型安全的方式与数据库交互。
¥Prisma is an ORM that allows us to interact with databases in a type-safe manner.
它提供了一种使用 Prisma 模式文件定义数据库模式的方法,然后基于该模式生成 TypeScript 类型。
¥It provides a way to define your database schema using a Prisma schema file, and then generates TypeScript types based on that schema.
Prismabox
Prismabox 是一个库,可以从 Prisma 模式生成 TypeBox 或 Elysia 验证模型。
¥Prismabox is a library that generate TypeBox or Elysia validation models from Prisma schema.
我们可以使用 Prismabox 将 Prisma 模式转换为 Elysia 验证模型,然后可以使用这些模型来确保 Elysia 中的类型验证。
¥We can use Prismabox to convert Prisma schema into Elysia validation models, which can then be used to ensure type validation in Elysia.
工作原理如下:
¥Here's how it works:
- 在 Prisma Schema 中定义数据库模式。
- 添加
prismabox
生成器,用于生成 Elysia 模式。 - 使用转换后的 Elysia 验证模型来确保类型验证。
- OpenAPI 模式由 Elysia 验证模型生成。
- 添加 Eden 条约,为前端添加类型安全。
* ——————————————— *
| |
| -> | Documentation |
* ————————— * * ———————— * OpenAPI | | |
| | prismabox | | ——————— | * ——————————————— *
| Prisma | —————————-> | Elysia |
| | | | ——————— | * ——————————————— *
* ————————— * * ———————— * Eden | | |
| -> | Frontend Code |
| |
* ——————————————— *
安装
¥Installation
要安装 Prisma,请运行以下命令命令:
¥To install Prisma, run the following command:
bash
bun add @prisma/client prismabox && \
bun add -d prisma
Prisma 模式
¥Prisma schema
假设你已经有一个 prisma/schema.prisma
。
¥Assuming you already have a prisma/schema.prisma
.
我们可以将 prismabox
生成器添加到 Prisma 模式文件,如下所示:
¥We can add a prismabox
generator to the Prisma schema file as follows:
ts
generator client {
provider = "prisma-client-js"
output = "../generated/prisma"
}
datasource db {
provider = "sqlite"
url = env("DATABASE_URL")
}
generator prismabox {
provider = "prismabox"
typeboxImportDependencyName = "elysia"
typeboxImportVariableName = "t"
inputModel = true
output = "../generated/prismabox"
}
model User {
id String @id @default(cuid())
email String @unique
name String?
posts Post[]
}
model Post {
id String @id @default(cuid())
title String
content String?
published Boolean @default(false)
author User @relation(fields: [authorId], references: [id])
authorId String
}
这将在 generated/prismabox
目录中生成 Elysia 验证模型。
¥This will generate Elysia validation models in the generated/prismabox
directory.
每个模型都有自己的文件,并且模型将根据 Prisma 模型名称命名。
¥Each model will have its own file, and the models will be named based on the Prisma model names.
例如:
¥For example:
User
模型将生成为generated/prismabox/User.ts
Post
模型将生成为generated/prismabox/Post.ts
使用生成的模型
¥Using generated models
然后我们可以将生成的模型导入到 Elysia 应用中:
¥Then we can import the generated models in our Elysia application:
ts
import { Elysia, t } from 'elysia'
import { PrismaClient } from '../generated/prisma'
import { UserPlain, UserPlainInputCreate } from '../generated/prismabox/User'
const prisma = new PrismaClient()
const app = new Elysia()
.put(
'/',
async ({ body }) =>
prisma.user.create({
data: body
}),
{
body: UserPlainInputCreate,
response: UserPlain
}
)
.get(
'/id/:id',
async ({ params: { id }, status }) => {
const user = await prisma.user.findUnique({
where: { id }
})
if (!user) return status(404, 'User not found')
return user
},
{
response: {
200: UserPlain,
404: t.String()
}
}
)
.listen(3000)
console.log(
`🦊 Elysia is running at ${app.server?.hostname}:${app.server?.port}`
)
这使我们能够在 Elysia 验证模型中重用数据库模式。
¥This allows us to reuse the database schema in Elysia validation models.
更多信息,请参阅 Prisma 和 Prismabox 文档。
¥For more information, please refer to the Prisma, and Prismabox documentation.