Nuxt (Prisma ORM v7)
A step-by-step guide to setting up and using Prisma ORM and Prisma Postgres in a Nuxt app
This guide shows you how to set up Prisma ORM in a Nuxt application with Prisma Postgres.
Quick start
Section titled “Quick start”Run one command to scaffold a Nuxt project with Prisma ORM and Prisma Postgres ready to go:
bunx create-prisma@stable --template nuxtpnpm create prisma@stable --template nuxtyarn create prisma@stable --template nuxtnpm create prisma@stable -- --template nuxtOr follow the steps below to set it up manually.
Prerequisites
Section titled “Prerequisites”- Node.js 18+
- A Prisma Postgres database (or any PostgreSQL database)
1. Create a Nuxt project
Section titled “1. Create a Nuxt project”Create a new Nuxt project:
bunx nuxi@latest init hello-prismapnpm dlx nuxi@latest init hello-prismayarn dlx nuxi@latest init hello-prismanpx nuxi@latest init hello-prismaNavigate to the project and install dependencies:
cd hello-prismabun add @prisma/client@7 @prisma/adapter-pg pgpnpm add @prisma/client@7 @prisma/adapter-pg pgyarn add @prisma/client@7 @prisma/adapter-pg pgnpm install @prisma/client@7 @prisma/adapter-pg pgbun add --dev prisma@prev @types/pg dotenv tsxpnpm add -D prisma@prev @types/pg dotenv tsxyarn add --dev prisma@prev @types/pg dotenv tsxnpm install -D prisma@prev @types/pg dotenv tsx2. Initialize Prisma
Section titled “2. Initialize Prisma”Initialize Prisma in your project:
bunx --bun prisma initpnpm prisma inityarn prisma initnpx prisma initUpdate your prisma/schema.prisma:
generator client {
provider = "prisma-client"
output = "./generated"
}
datasource db {
provider = "postgresql"
}
model User {
id Int @id @default(autoincrement())
email String @unique
name String?
posts Post[]
}
model Post {
id Int @id @default(autoincrement())
title String
content String?
published Boolean @default(false)
author User? @relation(fields: [authorId], references: [id])
authorId Int?
}Create a prisma.config.ts file in the root of your project:
import { defineConfig, env } from "prisma/config";
import "dotenv/config";
export default defineConfig({
schema: "prisma/schema.prisma",
migrations: {
path: "prisma/migrations",
seed: "tsx ./prisma/seed.ts",
},
datasource: {
url: env("DATABASE_URL"),
},
});Update your .env file with your database connection string:
DATABASE_URL="postgresql://user:password@localhost:5432/mydb"Run the migration to create your database tables:
bunx prisma migrate dev --name initpnpm prisma migrate dev --name inityarn prisma migrate dev --name initnpx prisma migrate dev --name init3. Set up Prisma Client
Section titled “3. Set up Prisma Client”Create server/utils/db.ts. Nuxt auto-imports exports from server/utils, making prisma available in all API routes:
import { PrismaPg } from "@prisma/adapter-pg";
import { PrismaClient } from "../../prisma/generated/client";
const prismaClientSingleton = () => {
const pool = new PrismaPg({ connectionString: process.env.DATABASE_URL! });
return new PrismaClient({ adapter: pool });
};
type PrismaClientSingleton = ReturnType<typeof prismaClientSingleton>;
const globalForPrisma = globalThis as unknown as {
prisma: PrismaClientSingleton | undefined;
};
export const prisma = globalForPrisma.prisma ?? prismaClientSingleton();
if (process.env.NODE_ENV !== "production") globalForPrisma.prisma = prisma;4. Create API routes
Section titled “4. Create API routes”Create an API route to fetch users. The prisma instance is auto-imported:
export default defineEventHandler(async () => {
const users = await prisma.user.findMany({
include: { posts: true },
});
return users;
});Create an API route to create a user:
export default defineEventHandler(async (event) => {
const body = await readBody<{ name: string; email: string }>(event);
const user = await prisma.user.create({
data: {
name: body.name,
email: body.email,
},
});
return user;
});5. Create a page
Section titled “5. Create a page”Update app.vue to display users:
<template>
<div>
<h1>Users</h1>
<ul v-if="users?.length">
<li v-for="user in users" :key="user.id">{{ user.name }} ({{ user.email }})</li>
</ul>
<p v-else>No users yet.</p>
</div>
</template>
<script setup>
const { data: users } = await useFetch('/api/users')
</script>6. Run the app
Section titled “6. Run the app”Start the development server:
bun run devpnpm run devyarn devnpm run devOpen http://localhost:3000 to see your app.
7. Seed your database (optional)
Section titled “7. Seed your database (optional)”Create a seed file to populate your database with sample data:
import "dotenv/config";
import { PrismaClient } from "./generated/client";
import { PrismaPg } from "@prisma/adapter-pg";
const adapter = new PrismaPg({ connectionString: process.env.DATABASE_URL! });
const prisma = new PrismaClient({ adapter });
async function main() {
const alice = await prisma.user.create({
data: {
name: "Alice",
email: "alice@prisma.io",
posts: {
create: { title: "Hello World", published: true },
},
},
});
console.log(`Created user: ${alice.name}`);
}
main()
.then(() => prisma.$disconnect())
.catch(async (e) => {
console.error(e);
await prisma.$disconnect();
process.exit(1);
});Run the seed:
bunx prisma db seedpnpm prisma db seedyarn prisma db seednpx prisma db seed8. Deploy to Vercel
Section titled “8. Deploy to Vercel”You can deploy your Nuxt application to Vercel using one of two methods:
Option A: Deploy using Vercel CLI
Section titled “Option A: Deploy using Vercel CLI”- Install the Vercel CLI (if not already installed):
bun add --global vercelpnpm add -g vercelyarn global add vercelnpm install -g vercel- Deploy your project:
bunx vercelpnpm dlx vercelyarn dlx vercelnpx vercel-
Set the
DATABASE_URLenvironment variable:- Go to your Vercel dashboard
- Select your project
- Navigate to Settings → Environment Variables
- Add
DATABASE_URLwith your database connection string
-
Redeploy your application to apply the environment variable:
bunx vercel --prodpnpm dlx vercel --prodyarn dlx vercel --prodnpx vercel --prodOption B: Deploy using Git integration
Section titled “Option B: Deploy using Git integration”-
Push your code to a Git repository (GitHub, GitLab, or Bitbucket).
-
Add
prisma generateto yourpostinstallscript inpackage.jsonto ensure Prisma Client is generated during deployment:package.json { "scripts": { "postinstall": "prisma generate", "build": "nuxt build", "dev": "nuxt dev" } } -
Import your project in Vercel:
- Go to Vercel dashboard
- Click Add New → Project
- Import your Git repository
- Vercel will automatically detect it as a Nuxt project
-
Configure environment variables:
- Before deploying, go to Environment Variables
- Add
DATABASE_URLwith your database connection string - Click Deploy
Vercel will automatically build and deploy your Nuxt application. The deployment process is the same as any other Node.js application, and Prisma Client will be generated during the build process thanks to the postinstall script.
Next steps
Section titled “Next steps”- Explore the full Nuxt + Prisma example for a complete blog application
- Learn about Prisma Client API
- Set up Prisma Postgres for a managed database