ข้ามไปยังเนื้อหา

Prisma

Prisma คือ ORM ที่ช่วยให้เราทำงานกับฐานข้อมูลได้แบบ type-safe

Prisma ช่วยให้เรากำหนด schema ของฐานข้อมูลไว้ในไฟล์ Prisma schema แล้วสร้าง TypeScript type ตาม schema ให้เลย

Prismabox เป็นไลบรารีที่สร้างโมเดล validation ของ TypeBox หรือ Elysia จาก Prisma schema

เราสามารถใช้ Prismabox เพื่อเปลี่ยน Prisma schema ให้เป็นโมเดล validation ของ Elysia เอาไปช่วยตรวจ type ใน Elysia ได้

  1. กำหนด schema ของฐานข้อมูลใน Prisma Schema
  2. เพิ่ม prismabox generator เพื่อสร้าง Elysia schema
  3. ใช้โมเดล validation ของ Elysia ที่แปลงมาแล้ว เพื่อตรวจสอบ type
  4. Elysia สร้าง OpenAPI schema จากโมเดล validation ให้เอง
  5. เพิ่ม Eden Treaty เพื่อเพิ่ม type-safety ให้ฝั่ง frontend
* ——————————————— *
| |
| -> | Documentation |
* ————————— * * ———————— * OpenAPI | | |
| | prismabox | | ——————— | * ——————————————— *
| Prisma | —————————-> | Elysia |
| | | | ——————— | * ——————————————— *
* ————————— * * ———————— * Eden | | |
| -> | Frontend Code |
| |
* ——————————————— *

รันคำสั่งนี้เพื่อติดตั้ง Prisma:

Terminal window
bun add @prisma/client prismabox prisma-adapter-bun-sqlite && \
bun add -d prisma

สมมุติว่าเรามีไฟล์ prisma/schema.prisma แล้ว

ในส่วน datasource ไม่มี url เพราะ Prisma 7 อ่าน URL ของฐานข้อมูลจาก prisma.config.ts ดูวิธีการตั้งค่า Prisma ได้จาก เอกสารอ้างอิง

เราสามารถเพิ่ม prismabox generator ไปที่ไฟล์ Prisma schema ตามตัวอย่างนี้:

prisma/schema.prisma
generator client {
provider = "prisma-client"
output = "../generated/prisma"
}
datasource db {
provider = "sqlite"
// Note: In Prisma 7+, datasource URL is configured in prisma.config.ts (for Prisma CLI),
// and when using driver adapters you also pass the runtime URL in adapter setup.
}
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
}

Prismabox จะสร้างโมเดล validation ของ Elysia ไว้ในโฟลเดอร์ generated/prismabox

แต่ละโมเดลจะมีไฟล์ของตัวเอง และจะตั้งชื่อตามชื่อของโมเดล Prisma

ยกตัวอย่าง:

  • โมเดล User จะถูกสร้างเป็นไฟล์ generated/prismabox/User.ts
  • โมเดล Post จะถูกสร้างเป็นไฟล์ generated/prismabox/Post.ts

ตอนนี้เราสามารถ import โมเดลที่สร้างขึ้น มาใช้ในแอป Elysia ได้:

src/index.ts
import { Elysia, t } from 'elysia'
import { PrismaBunSqlite } from 'prisma-adapter-bun-sqlite';
import { PrismaClient } from '../generated/prisma/client'
import { UserPlain, UserPlainInputCreate } from '../generated/prismabox/User'
const adapter = new PrismaBunSqlite({ url: process.env.DATABASE_URL });
export const prisma = new PrismaClient({ adapter });
const app = new Elysia()
.put(
'/',
{
body: UserPlainInputCreate,
response: UserPlain
}, async ({ body }) =>
prisma.user.create({
data: body
})
)
.get(
'/id/:id',
{
response: {
200: UserPlain,
404: t.String()
}
}, async ({ params: { id }, status }) => {
const user = await prisma.user.findUnique({
where: { id }
})
if (!user) return status(404, 'User not found')
return user
}
)
.listen(3000)
console.log(
`🦊 Elysia is running at ${app.server?.hostname}:${app.server?.port}`
)

เราก็จะใช้ schema ของฐานข้อมูลที่เขียนไว้ มาใช้ซ้ำเป็นโมเดล validation ของ Elysia ได้แล้ว


สำหรับข้อมูลเพิ่มเติม สามารถดูได้ที่เอกสาร Prisma และ Prismabox

แก้ไขหน้า

อัพเดทล่าสุด:

Type to search the documentation.

    Up arrowDown arrow to navigateEnter to selectEscape to close