Skip to main content

Quickstart

This quickstart guide will help you get up and running with Prisma ORM in just a few minutes. You’ll set up Prisma, create a simple schema, and run your first queries.
This guide uses SQLite for simplicity. For production applications, consider using PostgreSQL, MySQL, or another production database.

Prerequisites

Before you begin, make sure you have:
  • Node.js 20.19+, 22.12+, or 24.0+ installed
  • A package manager (npm, yarn, pnpm, or bun)
  • Basic knowledge of TypeScript or JavaScript

Step 1: Install Prisma

First, create a new project directory and initialize it:
mkdir my-prisma-app
cd my-prisma-app
npm init -y
Install Prisma CLI as a development dependency and the Prisma Client:
npm install prisma --save-dev
npm install @prisma/client

Step 2: Create your Prisma schema

Create a prisma directory with a schema.prisma file:
mkdir prisma
Create prisma/schema.prisma with the following content:
prisma/schema.prisma
generator client {
  provider = "prisma-client"
  output   = "../generated/client"
}

datasource db {
  provider = "sqlite"
}

model User {
  id    String @id @default(uuid())
  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  String?
}
This schema defines two models: User and Post, with a one-to-many relationship. The @id, @default, and @relation attributes define the database structure.

Step 3: Configure database connection

Create a prisma.config.ts file in your project root:
prisma.config.ts
import { defineConfig } from 'prisma/config'

export default defineConfig({
  datasource: {
    url: 'file:./dev.db',
  },
})
For SQLite, the connection URL is a file path relative to the config file. For other databases, you’d use a connection string.
Prisma 7 uses prisma.config.ts for configuration instead of environment variables in the schema file. This provides better type safety and flexibility.

Step 4: Create the database

Run the following command to create your SQLite database and tables:
npx prisma db push
This command reads your Prisma schema and creates the corresponding tables in your database.

Step 5: Generate Prisma Client

Generate the Prisma Client code:
npx prisma generate
This creates a type-safe client in the generated/client directory (as specified in your schema’s output path).
Whenever you change your schema, run prisma generate again to update the client with your latest data model.

Step 6: Write your first query

Create an index.ts file:
index.ts
import { PrismaClient } from './generated/client'
import { PrismaSqlite } from '@prisma/adapter-better-sqlite3'
import Database from 'better-sqlite3'

async function main() {
  // Initialize the database adapter
  const db = new Database('dev.db')
  const adapter = new PrismaSqlite(db)
  const prisma = new PrismaClient({ adapter })

  // Create a new user with a post
  const user = await prisma.user.create({
    data: {
      name: 'Alice',
      email: 'alice@prisma.io',
      posts: {
        create: {
          title: 'Hello World',
          content: 'This is my first post!',
          published: true,
        },
      },
    },
  })
  console.log('Created user:', user)

  // Query all users with their posts
  const allUsers = await prisma.user.findMany({
    include: {
      posts: true,
    },
  })
  console.log('All users:', JSON.stringify(allUsers, null, 2))

  // Close the database connection
  db.close()
}

main()
  .catch((e) => {
    console.error(e)
    process.exit(1)
  })
Install the SQLite adapter:
npm install @prisma/adapter-better-sqlite3 better-sqlite3

Step 7: Run your application

Install tsx to run TypeScript files:
npm install tsx --save-dev
Run your application:
npx tsx index.ts
You should see output showing the created user and all users with their posts.

Understanding the code

Let’s break down what’s happening:
1

Import Prisma Client

import { PrismaClient } from './generated/client'
Import the generated Prisma Client from the output location specified in your schema.
2

Create adapter and client

const db = new Database('dev.db')
const adapter = new PrismaSqlite(db)
const prisma = new PrismaClient({ adapter })
Prisma uses driver adapters to connect to databases. The adapter wraps the native database driver.
3

Create data

await prisma.user.create({
  data: { /* ... */ }
})
Use the create method to insert new records. Nested creates allow you to create related records in one query.
4

Query data

await prisma.user.findMany({
  include: { posts: true }
})
Use findMany to retrieve multiple records. The include option loads related data.

Next steps

Congratulations! You’ve successfully set up Prisma and run your first queries. Here’s what to explore next:

Explore CRUD operations

Learn about create, read, update, and delete operations

Use Prisma Migrate

Create and apply database migrations for production

Add relationships

Work with complex data models and relations

Learn filtering

Master advanced queries and filtering

Common operations

Here are some common Prisma Client operations:

Find a single record

const user = await prisma.user.findUnique({
  where: { email: 'alice@prisma.io' },
})

Update a record

const updatedPost = await prisma.post.update({
  where: { id: 1 },
  data: { published: true },
})

Delete a record

const deletedUser = await prisma.user.delete({
  where: { id: 'user-id' },
})

Filter records

const publishedPosts = await prisma.post.findMany({
  where: {
    published: true,
    title: { contains: 'Prisma' },
  },
})
All Prisma Client queries return plain JavaScript objects with full TypeScript type safety.