---
title: "Prisma"
description: "We may use Prisma to create end-to-end type safety from database to validation to frontend with prismabox"
url: "https://next.elysiajs.com/integrations/prisma/"
---

# Prisma

[Prisma](https://prisma.io) 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.

Generator compatibility

Configure Prismabox to import `t` from `elysia`, as shown below, and check that your generator version supports Elysia v2’s TypeBox 1.x schemas. The Prisma and Prismabox packages are separate integrations; these examples do not establish their runtime compatibility with every version.

### Prismabox

[Prismabox](https://github.com/m1212e/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:

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](/eden) to add type-safety to your frontend.

```text
                                                    * ——————————————— *
                                                    |                 |
                                               | -> |  Documentation  |
* ————————— *             * ———————— * OpenAPI |    |                 |
|           |  prismabox  |          | ——————— |    * ——————————————— *
|  Prisma   | —————————-> |  Elysia  |
|           |             |          | ——————— |    * ——————————————— *
* ————————— *             * ———————— *   Eden  |    |                 |
                                               | -> |  Frontend Code  |
                            |                 |
                            * ——————————————— *
```

## Installation

To install Prisma, run the following command:

Terminal window

```bash
bun add @prisma/client prismabox prisma-adapter-bun-sqlite && \
bun add -d prisma
```

## 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](https://www.prisma.io/docs/orm/reference/prisma-config-reference) to set it up.

We can add a `prismabox` generator to the Prisma schema file as follows:

prisma/schema.prisma

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

## Using generated models

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

src/index.ts

```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](https://prisma.io) and [Prismabox](https://github.com/m1212e/prismabox) documentation.
