|
| 1 | +--- |
| 2 | +title: 'How to use Prisma ORM in Bun' |
| 3 | +metaTitle: 'How to use Prisma ORM and Prisma Postgres with Bun' |
| 4 | +description: 'Learn how to use Prisma ORM in a Bun application with driver adapters and Prisma Postgres' |
| 5 | +sidebar_label: 'Bun' |
| 6 | +image: '/img/guides/prisma-bun-cover-image.png' |
| 7 | +completion_time: '10 min' |
| 8 | +community_section: true |
| 9 | +--- |
| 10 | + |
| 11 | +## Introduction |
| 12 | + |
| 13 | +[Bun](https://bun.sh) is a fast JavaScript runtime that includes a bundler, test runner, and package manager. In this guide, you will set up a Bun project with Prisma ORM and a Prisma Postgres database. You will configure Prisma driver adapters, create a simple HTTP server, and build a Bun executable for deployment. |
| 14 | + |
| 15 | +## Prerequisites |
| 16 | + |
| 17 | +- [Bun](https://bun.sh/docs/installation) installed in your system |
| 18 | +- A [Prisma Postgres database](/postgres) (created during setup) |
| 19 | +- Basic knowledge of JavaScript/TypeScript |
| 20 | + |
| 21 | +## 1. Setting up your Bun project |
| 22 | + |
| 23 | +First, create a directory for your project and navigate to it: |
| 24 | + |
| 25 | +```terminal |
| 26 | +mkdir bun-prisma |
| 27 | +cd bun-prisma |
| 28 | +``` |
| 29 | + |
| 30 | +Then, initialise a new Bun project: |
| 31 | + |
| 32 | +```terminal |
| 33 | +bun init -y |
| 34 | +``` |
| 35 | + |
| 36 | +This creates a basic Bun project that includes a `package.json` file and an `index.ts` file. |
| 37 | + |
| 38 | +## 2. Installing and configuring Prisma |
| 39 | + |
| 40 | +### 2.1. Install dependencies |
| 41 | + |
| 42 | +Install the required Prisma packages and other dependencies: |
| 43 | + |
| 44 | +```terminal |
| 45 | +bun add -d prisma |
| 46 | +bun add @prisma/client @prisma/adapter-pg dotenv |
| 47 | +``` |
| 48 | + |
| 49 | +### 2.2. Initialize Prisma ORM with Prisma Postgres |
| 50 | + |
| 51 | +Initialize Prisma ORM with Prisma Postgres in your project: |
| 52 | + |
| 53 | +```terminal |
| 54 | +bun prisma init --db |
| 55 | +``` |
| 56 | + |
| 57 | +:::info |
| 58 | + |
| 59 | +You'll need to answer a few questions while setting up your Prisma Postgres database. Select the region closest to your location and a memorable name for your database like "My Bun Project" |
| 60 | + |
| 61 | +::: |
| 62 | + |
| 63 | +This command creates: |
| 64 | + |
| 65 | +- A `prisma/` directory with your `schema.prisma` file |
| 66 | +- A new Prisma Postgres database |
| 67 | +- A `.env` file with your `DATABASE_URL` |
| 68 | + |
| 69 | +### 2.3. Configure environment variables for driver adapters |
| 70 | + |
| 71 | +We are going to use the [`node-postgres` driver adapter](/orm/overview/databases/postgresql#using-the-node-postgres-driver) to perform queries to our database. |
| 72 | + |
| 73 | +When using the `node-postgres` driver adapter with Prisma Postgres, you need to add a `DIRECT_URL` environment variable. This provides a direct connection to your PostgreSQL database. |
| 74 | + |
| 75 | +To get your [direct connection string](/postgres/database/direct-connections#how-to-connect-to-prisma-postgres-via-direct-tcp): |
| 76 | + |
| 77 | +1. Navigate to your recently created Prisma Postgres project dashboard (e.g. "My Bun Project") |
| 78 | +2. Click the **API Keys** tab in the project's sidebar |
| 79 | +3. Click the **Create API key** button |
| 80 | +4. Provide a name for the API key and click **Create** |
| 81 | +5. Copy the connection string starting with `postgres://` |
| 82 | + |
| 83 | +Update your `.env` file to include both URLs: |
| 84 | + |
| 85 | +```env file=.env |
| 86 | +DATABASE_URL="your_database_url_here" |
| 87 | +//add-start |
| 88 | +DIRECT_URL="your_direct_connection_string_here" |
| 89 | +//add-end |
| 90 | +``` |
| 91 | + |
| 92 | +### 2.4. Update your Prisma schema |
| 93 | + |
| 94 | +Open `prisma/schema.prisma` and update it to use driver adapters with Bun runtime: |
| 95 | + |
| 96 | +```prisma file=prisma/schema.prisma |
| 97 | +generator client { |
| 98 | + //delete-start |
| 99 | + provider = "prisma-client-js" |
| 100 | + //delete-end |
| 101 | + //add-start |
| 102 | + provider = "prisma-client" |
| 103 | + //add-end |
| 104 | + output = "../generated/prisma" |
| 105 | + //add-start |
| 106 | + previewFeatures = ["driverAdapters", "queryCompiler"] |
| 107 | + runtime = "bun" |
| 108 | + //add-end |
| 109 | +} |
| 110 | +
|
| 111 | +datasource db { |
| 112 | + provider = "postgresql" |
| 113 | + url = env("DATABASE_URL") |
| 114 | +} |
| 115 | +
|
| 116 | +//add-start |
| 117 | +model User { |
| 118 | + id Int @id @default(autoincrement()) |
| 119 | + email String @unique |
| 120 | + name String? |
| 121 | +} |
| 122 | +//add-end |
| 123 | +``` |
| 124 | + |
| 125 | +## 3. Setting up database configuration and creating a seed script |
| 126 | + |
| 127 | +### 3.1. Create a database utility file |
| 128 | + |
| 129 | +Create a `db.ts` file in your project root to configure `PrismaClient` with the `node-postgres` adapter: |
| 130 | + |
| 131 | +```typescript file=db.ts |
| 132 | +import "dotenv/config"; |
| 133 | +import { PrismaClient } from "./generated/prisma/client"; |
| 134 | +import { PrismaPg } from "@prisma/adapter-pg"; |
| 135 | + |
| 136 | +const connectionString = `${process.env.DIRECT_URL}`; |
| 137 | + |
| 138 | +const adapter = new PrismaPg({ connectionString }); |
| 139 | + |
| 140 | +export const prisma = new PrismaClient({ adapter }); |
| 141 | +``` |
| 142 | + |
| 143 | +### 3.2. Create a seed script |
| 144 | + |
| 145 | +Create a seed script in the `prisma` folder to populate your database with sample data: |
| 146 | + |
| 147 | +```typescript file=prisma/seed.ts |
| 148 | +import { PrismaClient } from "../generated/prisma/client"; |
| 149 | + |
| 150 | +const prisma = new PrismaClient(); |
| 151 | + |
| 152 | +async function main() { |
| 153 | + // Create multiple users |
| 154 | + await prisma.user.createMany({ |
| 155 | + data: [ |
| 156 | + { email: "alice@example.com", name: "Alice" }, |
| 157 | + { email: "bob@example.com", name: "Bob" }, |
| 158 | + { email: "charlie@example.com", name: "Charlie" }, |
| 159 | + { email: "diana@example.com", name: "Diana" }, |
| 160 | + { email: "eve@example.com", name: "Eve" }, |
| 161 | + { email: "frank@example.com", name: "Frank" }, |
| 162 | + { email: "grace@example.com", name: "Grace" }, |
| 163 | + { email: "henry@example.com", name: "Henry" }, |
| 164 | + { email: "isabella@example.com", name: "Isabella" }, |
| 165 | + { email: "jack@example.com", name: "Jack" }, |
| 166 | + ], |
| 167 | + skipDuplicates: true, // prevents errors if you run the seed multiple times |
| 168 | + }); |
| 169 | + |
| 170 | + console.log("Seed data inserted!"); |
| 171 | +} |
| 172 | + |
| 173 | +main() |
| 174 | + .catch((e) => { |
| 175 | + console.error(e); |
| 176 | + process.exit(1); |
| 177 | + }) |
| 178 | + .finally(async () => { |
| 179 | + await prisma.$disconnect(); |
| 180 | + }); |
| 181 | +``` |
| 182 | + |
| 183 | +### 3.3. Create Prisma Config file to run the seed script |
| 184 | + |
| 185 | +Create a [`prisma.config.ts` file](/orm/reference/prisma-config-reference#migrationsseed) to configure Prisma's seed command: |
| 186 | + |
| 187 | +```terminal |
| 188 | +touch prisma.config.ts |
| 189 | +``` |
| 190 | + |
| 191 | +Then add the following content to the file: |
| 192 | + |
| 193 | +```typescript file=prisma.config.ts |
| 194 | +import 'dotenv/config' |
| 195 | +import { defineConfig } from 'prisma/config' |
| 196 | + |
| 197 | +export default defineConfig({ |
| 198 | + migrations: { |
| 199 | + seed: `bun run prisma/seed.ts`, |
| 200 | + }, |
| 201 | +}) |
| 202 | +``` |
| 203 | + |
| 204 | +## 4. Generate Prisma client and run migrations |
| 205 | + |
| 206 | +Generate the Prisma client and apply your schema to the database: |
| 207 | + |
| 208 | +```terminal |
| 209 | +bun prisma migrate dev --name init |
| 210 | +``` |
| 211 | + |
| 212 | +This command: |
| 213 | + |
| 214 | +- Creates the database tables based on your schema |
| 215 | +- Generates the Prisma client in the `generated/prisma` directory |
| 216 | + |
| 217 | +Because you are using the `node-postgres` driver adapter, you will need to generate the `PrismaClient` again. The client automatically produced by `migrate dev` is optimized for Prisma Postgres, but the adapter requires a client built specifically for the driver: |
| 218 | + |
| 219 | +```terminal |
| 220 | +bun prisma generate |
| 221 | +``` |
| 222 | + |
| 223 | +Run the seed script to populate your database: |
| 224 | + |
| 225 | +```terminal |
| 226 | +bun prisma db seed |
| 227 | +``` |
| 228 | + |
| 229 | +## 5. Creating your Bun server |
| 230 | + |
| 231 | +Replace the `index.ts` file contents with the following code to build a simple HTTP server that uses Prisma ORM to fetch and display users: |
| 232 | + |
| 233 | +```typescript file=index.ts |
| 234 | +import { prisma } from './db' |
| 235 | + |
| 236 | +const server = Bun.serve({ |
| 237 | + port: 3000, |
| 238 | + async fetch(req) { |
| 239 | + const { pathname } = new URL(req.url) |
| 240 | + |
| 241 | + // Skip favicon route |
| 242 | + if (pathname === '/favicon.ico') { |
| 243 | + return new Response(null, { status: 204 }) // or serve an icon if you have one |
| 244 | + } |
| 245 | + |
| 246 | + // Return all users |
| 247 | + const users = await prisma.user.findMany() |
| 248 | + |
| 249 | + // Count all users |
| 250 | + const count = await prisma.user.count() |
| 251 | + |
| 252 | + // Format the response with JSON |
| 253 | + return new Response( |
| 254 | + JSON.stringify({ |
| 255 | + users: users, |
| 256 | + totalUsers: count, |
| 257 | + }), |
| 258 | + { headers: { 'Content-Type': 'application/json' } }, |
| 259 | + ) |
| 260 | + }, |
| 261 | +}) |
| 262 | + |
| 263 | +console.log(`Listening on http://localhost:${server.port}`) |
| 264 | +``` |
| 265 | + |
| 266 | +## 6. Running your application |
| 267 | + |
| 268 | +Start your Bun server: |
| 269 | + |
| 270 | +```terminal |
| 271 | +bun run index.ts |
| 272 | +``` |
| 273 | + |
| 274 | +You should see `Listening on http://localhost:3000` in the console. When you visit `http://localhost:3000` in your browser, you'll see a JSON response with all the users in your database and the total count. |
| 275 | + |
| 276 | +## 7. Building and running a Bun executable |
| 277 | + |
| 278 | +Bun can compile your [TypeScript application into a single executable file](https://bun.com/docs/bundler/executables), which is useful for deployment and distribution. |
| 279 | + |
| 280 | +### 7.1. Build the executable |
| 281 | + |
| 282 | +Build your application into an executable: |
| 283 | + |
| 284 | +```terminal |
| 285 | +bun build --compile index.ts |
| 286 | +``` |
| 287 | + |
| 288 | +This creates an executable file named `index` (or `index.exe` on Windows) in your project directory. |
| 289 | + |
| 290 | +### 7.2. Run the executable |
| 291 | + |
| 292 | +Run the compiled executable: |
| 293 | + |
| 294 | +```terminal |
| 295 | +./index |
| 296 | +``` |
| 297 | + |
| 298 | +You should see the same `Listening on http://localhost:3000` message, and your application will work exactly the same as before. The executable includes all dependencies and can be deployed to any compatible system without requiring Bun or Node.js to be installed. |
| 299 | + |
| 300 | +:::note |
| 301 | + |
| 302 | +Bun executables are useful for: |
| 303 | + |
| 304 | +- **Deployment**: Ship a single file instead of managing dependencies |
| 305 | +- **Distribution**: Share your application without requiring users to install Bun |
| 306 | +- **Performance**: Faster startup times compared to running TypeScript files |
| 307 | +- **Security**: Your source code is compiled and not easily readable |
| 308 | + |
| 309 | +::: |
| 310 | + |
| 311 | +## Next steps |
| 312 | + |
| 313 | +You can explore the [sample app here](https://pris.ly/bun-guide-example) to see what you will build by following this guide. If you would like to add caching to your application, check out [this example](https://pris.ly/bun_ppg_example). |
| 314 | + |
| 315 | +Now that you have a Bun application connected to a Prisma Postgres database, you can continue by: |
| 316 | + |
| 317 | +- Extending your Prisma schema with additional models and relationships |
| 318 | +- Implementing authentication and authorization |
| 319 | +- Adding input validation and error handling |
| 320 | +- Exploring Bun's built-in testing tools |
| 321 | +- Deploying your executable to production servers |
| 322 | + |
| 323 | +### More info |
| 324 | + |
| 325 | +- [Bun Documentation](https://bun.sh/docs) |
| 326 | +- [Prisma Driver Adapters](/orm/overview/databases/database-drivers) |
| 327 | +- [Prisma Config File](/orm/reference/prisma-config-reference) |
| 328 | +- [Prisma Client without the Rust engine](/orm/prisma-client/setup-and-configuration/no-rust-engine) |
| 329 | +- [Prisma Postgres](/postgres) |
| 330 | + |
0 commit comments