Get Prisma ORM running with TypeScript and query your first database in under 10 minutes. This guide covers project setup, schema definition, migrations, and basic CRUD operations using SQLite.
Prerequisites
-
Node.js Version 18.0 or higher
-
Terminal Command line access (bash, zsh, PowerShell)
-
Code Editor VS Code recommended for Prisma extension support
Initialize Project
Initialize Project
~2 minCreate a new directory and initialize a TypeScript project with Prisma:
mkdir prisma-quickstart
cd prisma-quickstart
npm init -y
npm install prisma typescript ts-node @types/node --save-dev
npm install @prisma/client
Initialize TypeScript configuration:
npx tsc --init
Initialize Prisma with SQLite:
npx prisma init --datasource-provider sqlite
✔ Your Prisma schema was created at prisma/schema.prisma
You can now open it in your favorite editor.
Next steps:
- Set the DATABASE_URL in the .env file to point to your existing database.
- Run prisma db pull to turn your database schema into a Prisma schema.
- Run prisma generate to generate the Prisma Client.
This creates a prisma folder with schema.prisma and a .env file for your database URL.
Set Up Database Connection
Set Up Database Connection
~1 minSQLite is already configured. Verify your .env file contains:
DATABASE_URL="file:./dev.db"
Your prisma/schema.prisma should look like this:
generator client {
provider = "prisma-client-js"
}
datasource db {
provider = “sqlite”
url = env(“DATABASE_URL”)
}
SQLite stores data in a local file, perfect for development. For production, switch to PostgreSQL or MySQL.
Define Your Schema
Define Your Schema
~2 minAdd a User and Post model to your prisma/schema.prisma:
generator client {
provider = "prisma-client-js"
}
datasource db {
provider = “sqlite”
url = env(“DATABASE_URL”)
}
model User {
id Int @id @default(autoincrement())
email String @unique
name String?
posts Post[]
createdAt DateTime @default(now())
}
model Post {
id Int @id @default(autoincrement())
title String
content String?
published Boolean @default(false)
author User @relation(fields: [authorId], references: [id])
authorId Int
createdAt DateTime @default(now())
}
The @relation directive creates a foreign key relationship. Each Post belongs to one User, and each User can have many Posts.
Run Migrations
Run Migrations
~1 minCreate and apply your first migration:
npx prisma migrate dev --name init
Environment variables loaded from .env
Prisma schema loaded from prisma/schema.prisma
Datasource "db": SQLite database "dev.db" at "file:./dev.db"
SQLite database dev.db created at file:./dev.db
Applying migration 20260203120000_init
The following migration(s) have been created and applied from new schema changes:
migrations/
└─ 20260203120000_init/
└─ migration.sql
Your database is now in sync with your schema.
✔ Generated Prisma Client (v5.x.x) to ./node_modules/@prisma/client
Your SQLite database is ready with User and Post tables.
Add Data with Prisma Client
Add Data with Prisma Client
~2 minCreate a script to add data. Save this as script.ts:
import { PrismaClient } from '@prisma/client'
const prisma = new PrismaClient()
async function main() {
// Create a user with a post
const user = await prisma.user.create({
data: {
email: ‘[email protected]’,
name: ‘Alice’,
posts: {
create: {
title: ‘Hello Prisma’,
content: ‘This is my first post!’,
published: true,
},
},
},
include: {
posts: true,
},
})
console.log(‘Created user with post:’, user)
// Create another user
const bob = await prisma.user.create({
data: {
email: ‘[email protected]’,
name: ‘Bob’,
},
})
console.log(‘Created user:’, bob)
}
main()
.catch((e) => {
console.error(e)
process.exit(1)
})
.finally(async () => {
await prisma.$disconnect()
})
Run the script:
npx ts-node script.ts
Created user with post: {
id: 1,
email: '[email protected]',
name: 'Alice',
createdAt: 2026-02-03T12:00:00.000Z,
posts: [
{
id: 1,
title: 'Hello Prisma',
content: 'This is my first post!',
published: true,
authorId: 1,
createdAt: 2026-02-03T12:00:00.000Z
}
]
}
Created user: { id: 2, email: '[email protected]', name: 'Bob', createdAt: 2026-02-03T12:00:00.000Z }
The include option tells Prisma to return related posts along with the user.
Query Data
Query Data
~2 minCreate a new file query.ts to explore different queries:
import { PrismaClient } from '@prisma/client'
const prisma = new PrismaClient()
async function main() {
// Get all users
const allUsers = await prisma.user.findMany({
include: { posts: true },
})
console.log(‘All users:’, JSON.stringify(allUsers, null, 2))
// Find a specific user by email
const alice = await prisma.user.findUnique({
where: { email: ‘[email protected]’ },
})
console.log(’\nFound Alice:’, alice)
// Get all published posts
const publishedPosts = await prisma.post.findMany({
where: { published: true },
include: { author: true },
})
console.log(’\nPublished posts:’, publishedPosts)
// Update a post
const updatedPost = await prisma.post.update({
where: { id: 1 },
data: { title: ‘Hello Prisma (Updated)’ },
})
console.log(’\nUpdated post:’, updatedPost)
// Count users
const userCount = await prisma.user.count()
console.log(’\nTotal users:’, userCount)
}
main()
.catch((e) => {
console.error(e)
process.exit(1)
})
.finally(async () => {
await prisma.$disconnect()
})
Run the query script:
npx ts-node query.ts
All users: [
{
"id": 1,
"email": "[email protected]",
"name": "Alice",
"createdAt": "2026-02-03T12:00:00.000Z",
"posts": [
{
"id": 1,
"title": "Hello Prisma",
"content": "This is my first post!",
"published": true,
"authorId": 1,
"createdAt": "2026-02-03T12:00:00.000Z"
}
]
},
{
"id": 2,
"email": "[email protected]",
"name": "Bob",
"createdAt": "2026-02-03T12:00:00.000Z",
"posts": []
}
]
Found Alice: { id: 1, email: ‘[email protected]’, name: ‘Alice’, createdAt: 2026-02-03T12:00:00.000Z }
Published posts: [
{
id: 1,
title: ‘Hello Prisma’,
content: ‘This is my first post!’,
published: true,
authorId: 1,
createdAt: 2026-02-03T12:00:00.000Z,
author: { id: 1, email: ‘[email protected]’, name: ‘Alice’, createdAt: 2026-02-03T12:00:00.000Z }
}
]
Updated post: { id: 1, title: ‘Hello Prisma (Updated)’, content: ‘This is my first post!’, published: true, authorId: 1, createdAt: 2026-02-03T12:00:00.000Z }
Total users: 2
Explore your data visually with Prisma Studio:
npx prisma studio
You've set up a database, defined a schema, and performed CRUD operations with type-safe queries.