Favicon of prisma

prisma

Open-source ORM for Node.js and TypeScript with a type-safe client, declarative migrations, and Prisma Studio for viewing and editing your database.

Prisma is an open-source ORM for Node.js and TypeScript backends. It's built for developers who want type-safe database access, a declarative migration workflow, and a visual data browser, without hand-writing SQL for every query. It works behind REST, GraphQL, or gRPC APIs, and fits anything from a monolith to serverless functions and microservices. The toolkit is made up of three parts: Prisma Client for queries, Prisma Migrate for schema changes, and Prisma Studio for browsing data.

Key features

  • Prisma Client: an auto-generated, type-safe query builder for Node.js and TypeScript, so invalid fields and typos are caught at compile time.
  • Prisma schema: a single schema file defines the data source, the generator, and the application's data model in a readable modeling language.
  • Prisma Migrate: a declarative migration system that turns schema changes into database migrations.
  • Prisma Studio: a GUI for viewing and editing the data in your database directly.
  • Driver adapters: connect Prisma Client to PostgreSQL, MySQL, SQLite, SQL Server, MongoDB, or CockroachDB through an adapter passed to the client constructor.
  • Introspection or manual modeling: generate a data model from an existing database, or write the schema by hand and apply it with Prisma Migrate.
  • prisma.config.ts: a typed configuration file for CLI subcommands like migrate and studio, with an env() helper for reading environment variables safely.
  • Local dev database: prisma dev starts a local PostgreSQL server without needing Docker.
  • Prisma Postgres: an instant, cloud-hosted Postgres database you can provision with a single command.
  • Type safety on partial queries: even when a query selects only a subset of a model's fields, the returned object is still statically typed in TypeScript.

Ideal use cases

Good fit: Node.js or TypeScript backends that want compile-time safety on every database query; teams that prefer a declarative schema file over hand-written migration scripts; projects that need a quick way to inspect and edit data during development through Prisma Studio; both new projects starting from scratch, using the Prisma Postgres or SQLite quickstart, and existing projects with a database that can be introspected into a schema; teams building REST or GraphQL APIs that want the query layer to return plain JavaScript objects with predictable types.

Less of a fit: stacks outside Node.js and TypeScript, since Prisma Client is scoped to that ecosystem; projects that want hand-tuned SQL as the primary interface rather than a generated query builder (raw queries are supported, but that isn't the main workflow); teams that want zero build step, since Prisma Client requires running prisma generate after every schema change and after a fresh install.

Installation

Prisma offers two starting points depending on whether you already have a database. For a new project, quickstart guides cover Prisma Postgres or SQLite. If you already have a database, separate guides cover adding Prisma to an existing project or starting a new relational-database project from scratch.

Install the CLI and client:

npm install prisma --save-dev
npm install @prisma/client

Add a generator and datasource block to your Prisma schema:

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

datasource db {
  provider = "postgresql"
}

Configure the database connection in prisma.config.ts:

import { defineConfig, env } from 'prisma/config'

export default defineConfig({
  schema: 'prisma/schema.prisma',
  datasource: {
    url: env('DATABASE_URL'),
  },
})

Generate the client:

npx prisma generate

Re-run npx prisma generate any time the schema changes. For a from-scratch project without an existing database, npx prisma dev starts a local PostgreSQL server with no Docker required, or npx create-db --interactive provisions an instant cloud Prisma Postgres database. Once the client is generated, instantiate it with a driver adapter, such as PrismaPg, and start querying with methods like findMany, create, and update. Environment variables from .env files aren't loaded automatically by prisma.config.ts; import a package like dotenv at the top of the config file if you need that.

Categories:

Frequently asked questions

Share:

Stars
46.8K
Forks
2.3K
Last commit
2 hours ago
Repository age
7 years
License
Apache-2.0
Self-hosted
No
Activity score
85/100
View Repository
Built with:

Similar to prisma

Favicon

 

  
  
Favicon

 

  
  
Favicon