Posted on: 12/06/2026(updated)
Prisma is a typesafe, schema-driven ORM for Node.js and TypeScript that generates a tailored client based on your structural definitions.
Configure a Node.js workspace running native ES Modules (ESM).
# Initialize workspace
mkdir hello-prisma && cd hello-prisma
npm init -y
# Install TypeScript toolchain
npm install typescript tsx @types/node --save-dev
npx tsc --init
# Install Prisma engine and PostgreSQL adapters
npm install prisma @types/pg --save-dev
npm install @prisma/client @prisma/adapter-pg pg dotenv
Update your configuration files to support target environments
// tsconfig.json
{
"compilerOptions": {
"module": "ESNext",
"moduleResolution": "node",
"target": "ES2023",
"strict": true,
"esModuleInterop": true
}
}
// package.json
{
"type": "module"
}
Generate the configuration layer and schema framework:
npx prisma init
Scaffolding Prisma
// prisma.config.ts
import { defineConfig, env } from 'prisma/config';
export default defineConfig({
schema: 'prisma/schema.prisma',
migrations: {
path: 'prisma/migrations',
},
datasource: {
url: env('DATABASE_URL'),
},
});
Configure your local or remote target instance within your secure configuration file:
# .env
DATABASE_URL="postgresql://username:password@localhost:5432/mydb?schema=public"
Define application models
// prisma/schema.prisma
generator client {
provider = "prisma-client-js"
output = "../generated/prisma"
}
datasource db {
provider = "postgresql"
url = env("DATABASE_URL")
}
model User {
id Int @id @default(autoincrement())
email String @unique
name String?
posts Post[]
}
model Post {
id Int @id @default(autoincrement())
title String
content String?
published Boolean @default(false)
author User @relation(fields: [authorId], references: [id])
authorId Int
}
Synchronize your target database with the declaration models. This maps the .prisma file
npx prisma migrate dev --name init
To optimize application connection pooling
// lib/prisma.ts
import pg from 'pg';
import { PrismaPg } from '@prisma/adapter-pg';
import { PrismaClient } from '../generated/prisma/client/index.js';
const connectionString = `${process.env.DATABASE_URL}`;
// Setup physical driver layer
const pool = new pg.Pool({ connectionString });
const adapter = new PrismaPg(pool);
// Instantiate global client
export const prisma = new PrismaClient({ adapter });
Verify transaction lifecycles and relation joins natively using type-safe logic execution:
// script.ts
import { prisma } from './lib/prisma.js';
async function main() {
// Write operation with nested relation creation
const newUser = await prisma.user.create({
data: {
name: 'Alice',
email: 'alice@prisma.io',
posts: {
create: {
title: 'Hello World',
content: 'This is my first post!',
published: true,
},
},
},
include: {
posts: true,
},
});
console.log('User created safely:', newUser);
// Read operation with relation hydration
const allUsers = await prisma.user.findMany({
include: { posts: true },
});
console.dir(allUsers, { depth: null });
}
main()
.then(async () => {
await prisma.$disconnect();
})
.catch(async (e) => {
console.error(e);
await prisma.$disconnect();
process.exit(1);
});