Skip to content

Latest commit

 

History

History

README.md

Example Nuxt App Using Prisma Nuxt Module

This example app showcases how to use the Prisma Nuxt module.

The app demonstrates:

  • How to use the usePrismaClient composable in a server-side component named FirstUser.server.vue.
  • How to customize the PrismaClient in the file lib/prisma.ts, which is generated by the @prisma/nuxt module. This customization adds an exists method to the PrismaClient instance. The extended instance of the PrismaClient with the exists method is then used in the API route located at the user-exists.ts file.

Getting started

  1. Download example and install dependencies:

    npx try-prisma@latest --template orm/nuxt-prisma-module
    
  2. Navigate into the project directory:

    cd nuxt-prisma-module
    
  3. Install dependencies:

    npm install
    
  4. Set up your Prisma Postgres database:

    npx prisma init --db
    

    This will prompt you to:

    • Select a region (e.g., ap-southeast-1)
    • Enter a project name
    • Copy the provided database URL to your .env file
  5. Create a .env file with your Prisma Postgres database URL:

    # .env
    DATABASE_URL="postgres://<username>:<password>@<host>:<port>/<database>"
  6. Apply database migrations:

    npx prisma migrate dev
    
  7. Seed the database with sample data:

    npx prisma db seed
    
  8. Start the development server:

    npm run dev
    
  9. Follow the instructions in the terminal to launch Prisma Studio integrated in the Nuxt devtools within your browser.

Prisma Postgres Setup

This example uses Prisma Postgres with the @prisma/adapter-pg driver adapter configured in lib/prisma.ts:

import { PrismaPg } from '@prisma/adapter-pg'
import { PrismaClient } from '../prisma/generated/prisma/client'

const pool = new PrismaPg({ connectionString: process.env.DATABASE_URL! })
const prisma = new PrismaClient({ adapter: pool })

Resources