Prisma ORM

Learn how to use Prisma ORM

The prisma folder contains the Prisma ORM configuration, primarily the schema.prisma file, which defines your database models, relationships, and data sources. Prisma simplifies database queries with a type-safe client, making it easier to work with your Supabase PostgreSQL instance. The DATABASE_URL environment variable connects Prisma to Supabase.

Key Features

  • Schema Definition: The schema.prisma file in the prisma folder outlines your database structure (e.g., tables like User or Post).
  • Type-Safe Queries: Prisma generates a client (PrismaClient) in node_modules/@prisma/client, allowing you to write queries with TypeScript autocompletion.
  • Integration with Supabase: Uses the DATABASE_URL to connect to your Supabase PostgreSQL instance.

Setup

Explore schema.prisma

  • Open prisma/schema.prisma to view or modify models. For example:
@/prisma/schema.prisma
generator client {
  provider = "prisma-client-js"
  output   = "../lib/generated/prisma"
}

datasource db {
  provider  = "postgresql"
  url       = env("DATABASE_URL")
  directUrl = env("DIRECT_URL")
}

model Event {
  id        String @id @default(cuid())
  type      String
  slug      String
  text      String
  timestamp BigInt
}

Generate Prisma Client

  • Run the following to generate the Prisma client after modifying schema.prisma:
npx prisma generate
  • This updates the client for use in your lib or API routes.

Use Prisma in Code

  • Import the Prisma client in your code to perform queries like fetching or creating records.
import { PrismaClient } from '@prisma/client'`

 await prisma.event.create({
      data: { text, type, slug, timestamp: incomingTs },
    });

For further details, consult the Prisma Documentation.

How is this guide ?

Last updated on