Created
February 25, 2026 14:31
-
-
Save imderek/f78769005e43d8e5b906c4ed139e14c4 to your computer and use it in GitHub Desktop.
Install and Initialize Prisma with SQLite (Postgres-ready)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # Initialize Prisma and Database | |
| ## What This Command Does | |
| - Installs Prisma dependencies | |
| - Initializes Prisma with SQLite for development | |
| - Configures `.env` with database URL | |
| - Sets up schema with production-ready Postgres support | |
| - Creates initial migration | |
| ## Instructions for the Agent | |
| You are setting up Prisma for a Next.js app. SQLite for dev, Postgres for prod. | |
| ### Step 1: Install dependencies | |
| ```bash | |
| npm install prisma --save-dev | |
| npm install @prisma/client | |
| ``` | |
| ### Step 2: Initialize Prisma | |
| ```bash | |
| npx prisma init --datasource-provider sqlite | |
| ``` | |
| ### Step 3: Update `prisma/schema.prisma` | |
| Replace the datasource block to support both SQLite (dev) and Postgres (prod): | |
| ```prisma | |
| datasource db { | |
| provider = "sqlite" | |
| url = env("DATABASE_URL") | |
| } | |
| generator client { | |
| provider = "prisma-client-js" | |
| } | |
| ``` | |
| ### Step 4: Configure environment | |
| In `.env`: | |
| ``` | |
| DATABASE_URL="file:./dev.db" | |
| ``` | |
| Create `.env.example` with: | |
| ``` | |
| # Development (SQLite) | |
| DATABASE_URL="file:./dev.db" | |
| # Production (Postgres) - uncomment and update when deploying | |
| # DATABASE_URL="postgresql://user:password@host:5432/dbname?sslmode=require" | |
| ``` | |
| ### Step 5: Add to `.gitignore` | |
| Ensure these are in `.gitignore`: | |
| ``` | |
| *.db | |
| *.db-journal | |
| ``` | |
| ### Step 6: Create Prisma client helper | |
| Create `lib/prisma.ts`: | |
| ```typescript | |
| import { PrismaClient } from "@prisma/client" | |
| const globalForPrisma = globalThis as unknown as { | |
| prisma: PrismaClient | undefined | |
| } | |
| export const prisma = globalForPrisma.prisma ?? new PrismaClient() | |
| if (process.env.NODE_ENV !== "production") globalForPrisma.prisma = prisma | |
| ``` | |
| ### Step 7: Add convenience script | |
| Add to `package.json` scripts: | |
| ```json | |
| "studio": "prisma studio" | |
| ``` | |
| ### Step 8: Summary for user | |
| After completing, tell the user: | |
| - Prisma is configured with SQLite for development | |
| - Run `npx prisma migrate dev --name init` after adding your first model | |
| - Use `lib/prisma.ts` to import the Prisma client | |
| - Run `npm run studio` to view/edit your database in a GUI | |
| - When ready for production, switch to Postgres in schema and update DATABASE_URL |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment