Prisma
Prisma is an ORM that allows us to interact with databases in a type-safe manner.
It provides a way to define your database schema using a Prisma schema file, and then generates TypeScript types based on that schema.
Prismabox
Section titled “Prismabox”Prismabox is a library that generates TypeBox or Elysia validation models from a Prisma schema.
We can use Prismabox to convert a Prisma schema into Elysia validation models, which can then be used to ensure type validation in Elysia.
How it works:
Section titled “How it works:”- Define your database schema in Prisma Schema.
- Add a
prismaboxgenerator to generate an Elysia schema. - Use the converted Elysia validation models to ensure type validation.
- OpenAPI schema is generated from Elysia validation models.
- Add Eden Treaty to add type-safety to your frontend.
* ——————————————— * | | | -> | Documentation |* ————————— * * ———————— * OpenAPI | | || | prismabox | | ——————— | * ——————————————— *| Prisma | —————————-> | Elysia || | | | ——————— | * ——————————————— ** ————————— * * ———————— * Eden | | | | -> | Frontend Code | | | * ——————————————— *Installation
Section titled “Installation”To install Prisma, run the following command:
bun add @prisma/client prismabox prisma-adapter-bun-sqlite && \bun add -d prismaPrisma schema
Section titled “Prisma schema”Assume you already have a prisma/schema.prisma file.
The datasource has no url because Prisma 7 reads the database URL from prisma.config.ts. See Prisma’s config reference to set it up.
We can add a prismabox generator to the Prisma schema file as follows:
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}This will generate Elysia validation models in the generated/prismabox directory.
Each model will have its own file, and the models will be named based on the Prisma model names.
For example:
- The
Usermodel will be generated togenerated/prismabox/User.ts - The
Postmodel will be generated togenerated/prismabox/Post.ts
Using generated models
Section titled “Using generated models”Then we can import the generated models in our Elysia application:
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}`)This allows us to reuse the database schema in Elysia validation models.
For more information, please refer to the Prisma and Prismabox documentation.