Prisma Examples

repository·latest·Indexed 27 days ago

https://github.com/prisma/prisma-examples

A collection of ready-to-run example projects demonstrating various Prisma use cases, including fullstack applications, backend-only services, and database examples. The repository features multiple starters for Prisma Accelerate caching and connection pooling across frameworks such as Next.js, Astro, Nuxt.js, Remix, SolidStart, and TypeScript.

Tokens
99.9K
Snippets
402
Records
519
Agent score
91%

What's inside prisma-examples

  1. Project structure of the Hono & Prisma example

    latest

    The project is organized as follows:

    • src/index.ts: Defines the Hono server and all REST routes (/signup, /post, /feed, etc.).
    • src/lib/prisma.ts: Contains lightweight middleware that initializes a Prisma Client using @prisma/adapter-pg and exposes it through Hono's context.
    • prisma/schema.prisma: The Prisma schema defining the User and Post models.
    • prisma/seed.ts: Script used to seed the database with sample users and posts.
  2. Project Structure for Prisma Turso Example

    latest

    The Prisma with Turso example consists of the following key files:

    • prisma/schema.prisma: Defines the database schema for Users and Posts.
    • src/script.ts: A Node.js script demonstrating CRUD operations using Prisma Client.
    • .env: Stores environment variables (TURSO_DATABASE_URL and TURSO_AUTH_TOKEN) for database credentials.
  3. Project structure for Bun + Prisma example

    latest

    The project is organized as follows:

    • prisma/schema.prisma: Contains the database schema and Prisma configuration, including the prisma-client generator settings.
    • generated/: The custom output location for the generated Prisma Client.
    • db.ts: Handles the database connection using Prisma Postgres.
    • index.ts: The main server file using Bun's built-in HTTP server.
    • package.json: Project dependencies and scripts.
    • .env.example: Template for required environment variables.
  4. Explore Prisma Compute examples

    latest

    Prisma Compute examples demonstrate how to deploy Prisma ORM applications to Prisma Compute using the latest @prisma/cli. The available examples include:

    • Hono: A Hono API running on Node.js.
    • Next.js: A Next.js App Router application configured with standalone output for Prisma Compute compatibility.
    • TanStack Start: A TanStack Start application using the Nitro Vite plugin output supported by Prisma Compute.

    Each example provides a complete setup including Prisma ORM, a PostgreSQL schema, seed data, and scripts for both local development and deployment to Prisma Compute.

  5. Project structure for TypedSQL

    latest

    When using TypedSQL, the project structure includes a specific directory for your queries:

    • prisma/sql/: Contains .sql query files. These files are type-checked by Prisma and included in the generated Prisma Client.
    • prisma/schema.prisma: Defines the database schema.
    • prisma/seed.ts: Contains logic for populating the database.
    • src/index.ts: Demonstrates how to execute the generated SQL queries via the Prisma Client.
  6. Setup CockroachDB with Prisma

    latest

    This example demonstrates how to connect Prisma to a CockroachDB database, create schemas using raw SQL, populate the Prisma schema using prisma db pull, and perform CRUD operations with Prisma Client.

    Important Note: CockroachDB support is currently in Preview, and Prisma Migrate is not supported. You must create the database schema using raw SQL and then use prisma db pull to sync your Prisma schema.

  7. Setup the Prisma Accelerate SolidStart Starter

    latest

    Follow these steps to clone, configure, and run the SolidStart starter example which demonstrates Prisma Accelerate caching strategies.

    1. Clone and install dependencies:

      git clone git@github.com:prisma/prisma-examples.git --depth=1
      cd prisma-examples/accelerate/solidstart-starter
      npm install
    2. Configure environment variables: Create a .env file in the project root and set the DATABASE_URL to your Accelerate connection string (format: prisma://accelerate.prisma-data.net/?api_key=__YOUR_ACCELERATE_API_KEY__).

      touch .env

      Add the following to .env:

      DATABASE_URL="__YOUR_ACCELERATE_CONNECTION_STRING__"
      SOLID_START_PUBLIC_URL="http://localhost:3000"
    3. Initialize the database: Run migrations to create the Quotes table and seed the database with sample records:

      npx prisma migrate dev --name init
      npx prisma db seed
    4. Generate Prisma Client:

      npx prisma generate
    5. Start the application:

      npm run dev
    git clone git@github.com:prisma/prisma-examples.git --depth=1
    cd prisma-examples/accelerate/solidstart-starter
    npm install
    
    touch .env
    # Edit .env with DATABASE_URL and SOLID_START_PUBLIC_URL
    
    npx prisma migrate dev --name init
    npx prisma db seed
    npx prisma generate
    npm run dev
  8. Evolve the database schema with Prisma Migrate

    latest

    To add new features (like a Profile model) to your application, follow these steps:

    1. Update the Prisma Schema: Add the new model to prisma/schema.prisma.
      model Profile {
        id     Int     @id @default(autoincrement())
        bio    String?
        user   User    @relation(fields: [userId], references: [id])
        userId Int     @unique
      }
    2. Run a Migration: Execute the following command to create the new table in your database:
      npx prisma migrate dev --name add-profile
    3. Update Application Code: Use the newly generated types and the PrismaClient to interact with the new model in your controllers or services.
  9. Backend-only Prisma ORM Examples

    latest

    Access specialized backend-only implementations of Prisma ORM. These examples focus on server-side logic, API construction, and database interaction without a frontend layer.

    Key Categories:

    • GraphQL Servers: Implementations using Apollo Server, GraphQL Yoga, Nexus, TypeGraphQL, Pothos, and Mercurius. Covers patterns like SDL-first, code-first, subscriptions, and CRUD APIs.
    • REST APIs: Implementations using Express, Fastify, Koa, Hapi, and NestJS.
    • Specialized Protocols & Tools: gRPC APIs, PostGIS spatial queries with Express, and integration testing with Jest and Supertest.
    • Scripts: Direct usage of Prisma Client JS in TypeScript scripts.
  10. Run the Prisma REST API locally with Node.js and PostgreSQL

    latest

    If you prefer not to use Docker, you can run the application using Node.js and a local PostgreSQL instance.

    1. Create a .env file and set the DATABASE_URL to your local database connection string.
    2. Install dependencies and generate the Prisma Client.
    3. Apply migrations using npx prisma migrate dev.
    4. Start the development server with npm run dev.
    # 1. Setup environment
    cp .env.example .env
    # edit .env and set DATABASE_URL, e.g.:
    # DATABASE_URL="postgresql://prisma:prisma@localhost:5432/prisma"
    
    # 2. Install and migrate
    npm install
    npx prisma migrate dev
    
    # 3. Start server
    npm run dev