Skip to content

Instantly share code, notes, and snippets.

@Gaurav8757
Last active April 5, 2026 05:51
Show Gist options
  • Select an option

  • Save Gaurav8757/8f1bda8832195dcdf008dd2667b7dcff to your computer and use it in GitHub Desktop.

Select an option

Save Gaurav8757/8f1bda8832195dcdf008dd2667b7dcff to your computer and use it in GitHub Desktop.
All Installations setup here..

See Examples: -url: https://www.prisma.io/docs/prisma-orm/quickstart/postgresql

DIRECT_URL to create table (Migration)

DIRECT_URL="postgres://USER:PASSWORD@db.prisma.io:5432/postgres?sslmode=require" 

DATABASE_URL to query with db (Queries)!

DATABASE_URL="postgres://USER:PASSWORD@pooled.db.prisma.io:5432/postgres?sslmode=require"

Initialize a TypeScript project:

npm init
npm install typescript tsx @types/node --save-dev
npx tsc --init 

Install the packages needed for this quickstart:

npm install prisma @types/pg --save-dev
npm install @prisma/client @prisma/adapter-pg pg dotenv
npm i -D prisma-erd-generator @mermaid-js/mermaid-cli puppeteer
npm install -D prisma-dbml-generator

Update tsconfig.json for ESM compatibility:

{
  "compilerOptions": {
    "module": "ESNext",
    "moduleResolution": "bundler",
    "target": "ES2023",
    "strict": true,
    "esModuleInterop": true,
    "ignoreDeprecations": "6.0"
  }
}

In package.json file

{
  "type": "module"
}

You can now invoke the Prisma CLI by prefixing it with npx:

npx prisma 
npx prisma init --datasource-provider postgresql --output ../generated/prisma

The generated prisma.config.ts file looks like this:

import "dotenv/config";
import { defineConfig, env } from "prisma/config";

export default defineConfig({
  schema: "prisma/schema.prisma",
  migrations: {
    path: "prisma/migrations",
  },
  datasource: {
    url: env("DIRECT_URL"), 
    // DIRECT_URL="postgres://USER:PASSWORD@db.prisma.io:5432/postgres?sslmode=require"
    // Here use DIRECT_URL because it helps to create table (Migration)
    // इसे prisma.config.ts (CLI) में डालते हैं। यह Database से सीधा जुड़ता है। 
    // यह Migrations (टेबल बनाने/बदलने) के लिए ज़रूरी है क्योंकि Pooler माइग्रेशन को रोक सकता है।
  },
});

The generated schema uses the ESM-first prisma-client generator with a custom output path( prisma/schema.prisma):

generator client {
  provider = "prisma-client"
  output   = "../generated/prisma"
}

datasource db {
  provider = "postgresql"
}

// Models defines here
model User { 
  id    Int     @id @default(autoincrement()) 
  email String  @unique
  name  String?
  posts Post[]
} 

Update your .env file with your PostgreSQL connection string:

DATABASE_URL="postgresql://username:password@localhost:5432/mydb?schema=public"

Create your first migration to set up the database tables:

This command creates the database tables based on your schema.
npx prisma migrate dev --name init

To generate the Prisma Client:

npx prisma generate

Instantiate Prisma Client. You need to pass an instance of the Prisma ORM driver adapter

adapter to the PrismaClient constructor:

1. filename >> lib/db.ts

import "dotenv/config";
import { PrismaPg } from "@prisma/adapter-pg";
import { PrismaClient } from "../generated/prisma/client";

const connectionString = `${process.env.DATABASE_URL}`;

const adapter = new PrismaPg({ connectionString });
const prisma = new PrismaClient({ adapter });

export { prisma };

2. The "Singleton" Pattern (globalThis)

  • In a development environment (like Next.js with Hot Reloading), every time you save a file, the server restarts.
  • Without this code, every "save" creates a new PrismaClient instance.
  • Each instance takes up database connections.
  • Eventually, you get the error: "Too many clients already".
  • The Fix: Storing the client in globalThis ensures that even when the code reloads,
  • it checks if a client already exists before creating a new one. (stackoverflow.com)
filename >> src/db.ts
import { PrismaPg } from "@prisma/adapter-pg";
import { PrismaClient } from "./generated/prisma/client";

const globalForPrisma = globalThis as unknown as {
  prisma?: PrismaClient;
};

export const prisma =
  globalForPrisma.prisma ??
  new PrismaClient({
    adapter: new PrismaPg({
      connectionString: process.env.DATABASE_URL, 
      // DATABASE_URL: इसे src/db.ts (App Code) में डालते हैं। यह Connection Pooler के ज़रिए जुड़ता है। 
      // यह हज़ारों यूज़र्स के लोड को मैनेज करने के लिए इस्तेमाल होता है ताकि डेटाबेस क्रैश न हो।
    }),
  });

if (process.env.NODE_ENV !== "production") {
  globalForPrisma.prisma = prisma;
}

Prisma Studio is a visual editor for your database. Launch it with:

npx prisma studio
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment