Skip to content

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 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.

  1. Define your database schema in Prisma Schema.
  2. Add a prismabox generator to generate an Elysia schema.
  3. Use the converted Elysia validation models to ensure type validation.
  4. OpenAPI schema is generated from Elysia validation models.
  5. Add Eden Treaty to add type-safety to your frontend.
* ——————————————— *
| |
| -> | Documentation |
* ————————— * * ———————— * OpenAPI | | |
| | prismabox | | ——————— | * ——————————————— *
| Prisma | —————————-> | Elysia |
| | | | ——————— | * ——————————————— *
* ————————— * * ———————— * Eden | | |
| -> | Frontend Code |
| |
* ——————————————— *

To install Prisma, run the following command:

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

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:

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
}

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 User model will be generated to generated/prismabox/User.ts
  • The Post model will be generated to generated/prismabox/Post.ts

Then we can import the generated models in our Elysia application:

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}`
)

This allows us to reuse the database schema in Elysia validation models.


For more information, please refer to the Prisma and Prismabox documentation.

Type to search the documentation.

    Up arrowDown arrow to navigateEnter to selectEscape to close