# Prisma — Next-Generation ORM for Node.js & TypeScript > Prisma is a next-generation ORM for Node.js and TypeScript supporting PostgreSQL, MySQL, MariaDB, SQL Server, SQLite, MongoDB, and CockroachDB. Type-safe database access with auto-generated client and intuitive schema language. ## Install Save as a script file and run: ## Quick Use ```bash npm i -D prisma npm i @prisma/client npx prisma init ``` Define schema: ```prisma // prisma/schema.prisma generator client { provider = "prisma-client-js" } datasource db { provider = "postgresql" url = env("DATABASE_URL") } model User { id Int @id @default(autoincrement()) email String @unique posts Post[] } model Post { id Int @id @default(autoincrement()) title String authorId Int author User @relation(fields: [authorId], references: [id]) } ``` Generate & migrate: ```bash npx prisma migrate dev --name init npx prisma generate ``` Query: ```ts import { PrismaClient } from "@prisma/client"; const prisma = new PrismaClient(); const users = await prisma.user.findMany({ include: { posts: true }, }); ``` ## Intro Prisma is a next-generation ORM for Node.js & TypeScript. Fully type-safe database access with auto-generated client from the Prisma schema. Supports PostgreSQL, MySQL, MariaDB, SQL Server, SQLite, MongoDB, and CockroachDB. - **Repo**: https://github.com/prisma/prisma - **Stars**: 45K+ - **Language**: TypeScript - **License**: Apache 2.0 ## What Prisma Does - **Prisma Schema** — single source of truth for data model - **Prisma Client** — auto-generated, type-safe query builder - **Prisma Migrate** — declarative migrations from schema changes - **Prisma Studio** — GUI browser for database - **Relation queries** — `include` and `select` for nested data - **Transactions** — interactive and batch ## Architecture Schema → Prisma Engine (Rust) → Database. The TypeScript client wraps a Rust binary (or WASM in edge runtimes) that handles query compilation, connection pooling, and protocol. Types are generated from the schema on `prisma generate`. ## Self-Hosting Prisma is a library, not a service. Ships with your app. For connection pooling in serverless, use Prisma Accelerate (hosted) or PgBouncer/Supavisor self-hosted. ```bash # Production npx prisma migrate deploy node dist/server.js ``` ## Key Features - Fully type-safe queries - Declarative schema (.prisma file) - Auto-migrations - Multi-database support - Prisma Studio (GUI) - Edge runtime support (Cloudflare, Vercel) - Interactive transactions - Full-text search (PostgreSQL) ## Comparison | ORM | Type Safety | Migrations | Performance | Query Style | |---|---|---|---|---| | Prisma | Excellent | Auto | Good | Builder | | Drizzle | Excellent | Manual | Fastest | SQL-like | | TypeORM | Decent | Auto | OK | ActiveRecord/DataMapper | | Sequelize | Weak | Auto | OK | Legacy | | Kysely | Excellent | Manual | Fast | SQL builder | ## 常见问题 FAQ **Q: Prisma vs Drizzle?** A: Prisma 抽象更高(Schema DSL,自动迁移),开发体验好;Drizzle 更接近 SQL、bundle 更小、边缘运行时更快。 **Q: 冷启动慢?** A: Prisma 5+ 提供 `driverAdapters`,去掉 Rust 引擎,冷启动大幅改善。 **Q: 能写原生 SQL 吗?** A: 能。`prisma.$queryRaw` 和 `$executeRaw` 支持模板字符串参数化查询。 ## 来源与致谢 Sources - Docs: https://www.prisma.io/docs - GitHub: https://github.com/prisma/prisma - License: Apache 2.0 --- Source: https://tokrepo.com/en/workflows/2f0e03dd-3567-11f1-9bc6-00163e2b0d79 Author: Script Depot