Prisma
Prisma คือ ORM ที่ช่วยให้เราทำงานกับฐานข้อมูลได้แบบ type-safe
Prisma ช่วยให้เรากำหนด schema ของฐานข้อมูลไว้ในไฟล์ Prisma schema แล้วสร้าง TypeScript type ตาม schema ให้เลย
Prismabox
หัวข้อที่มีชื่อว่า “Prismabox”Prismabox เป็นไลบรารีที่สร้างโมเดล validation ของ TypeBox หรือ Elysia จาก Prisma schema
เราสามารถใช้ Prismabox เพื่อเปลี่ยน Prisma schema ให้เป็นโมเดล validation ของ Elysia เอาไปช่วยตรวจ type ใน Elysia ได้
ทำงานยังไง
หัวข้อที่มีชื่อว่า “ทำงานยังไง”- กำหนด schema ของฐานข้อมูลใน Prisma Schema
- เพิ่ม
prismaboxgenerator เพื่อสร้าง Elysia schema - ใช้โมเดล validation ของ Elysia ที่แปลงมาแล้ว เพื่อตรวจสอบ type
- Elysia สร้าง OpenAPI schema จากโมเดล validation ให้เอง
- เพิ่ม Eden Treaty เพื่อเพิ่ม type-safety ให้ฝั่ง frontend
* ——————————————— * | | | -> | Documentation |* ————————— * * ———————— * OpenAPI | | || | prismabox | | ——————— | * ——————————————— *| Prisma | —————————-> | Elysia || | | | ——————— | * ——————————————— ** ————————— * * ———————— * Eden | | | | -> | Frontend Code | | | * ——————————————— *การติดตั้ง
หัวข้อที่มีชื่อว่า “การติดตั้ง”รันคำสั่งนี้เพื่อติดตั้ง Prisma:
bun add @prisma/client prismabox prisma-adapter-bun-sqlite && \bun add -d prismaPrisma schema
หัวข้อที่มีชื่อว่า “Prisma schema”สมมุติว่าเรามีไฟล์ prisma/schema.prisma แล้ว
ในส่วน datasource ไม่มี url เพราะ Prisma 7 อ่าน URL ของฐานข้อมูลจาก prisma.config.ts ดูวิธีการตั้งค่า Prisma ได้จาก เอกสารอ้างอิง
เราสามารถเพิ่ม prismabox generator ไปที่ไฟล์ Prisma schema ตามตัวอย่างนี้:
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 ได้:
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