# Prisma **Repository Path**: mirrors/Prisma ## Basic Information - **Project Name**: Prisma - **Description**: Prisma 是一个快速构建 GraphQL 服务、REST API、数据库服务的后端框架 已支持 MySQL, PostgreSQL, MongoDB 等数据库,其他流行数据库正 - **Primary Language**: Scala - **License**: Apache-2.0 - **Default Branch**: main - **Homepage**: https://www.oschina.net/p/Prisma - **GVP Project**: No ## Statistics - **Stars**: 5 - **Forks**: 0 - **Created**: 2019-01-08 - **Last Updated**: 2026-01-10 ## Categories & Tags **Categories**: restful **Tags**: None ## README 
## What is Prisma? Prisma ORM is a **next-generation ORM** that consists of these tools: - [**Prisma Client**](https://www.prisma.io/docs/orm/prisma-client): Auto-generated and type-safe query builder for Node.js & TypeScript - [**Prisma Migrate**](https://www.prisma.io/docs/orm/prisma-migrate): Declarative data modeling & migration system - [**Prisma Studio**](https://github.com/prisma/studio): GUI to view and edit data in your database Prisma Client can be used in _any_ Node.js or TypeScript backend application (including serverless applications and microservices). This can be a [REST API](https://www.prisma.io/docs/concepts/overview/prisma-in-your-stack/rest), a [GraphQL API](https://www.prisma.io/docs/concepts/overview/prisma-in-your-stack/graphql), a gRPC API, or anything else that needs a database. **If you need a database to use with Prisma ORM, check out [Prisma Postgres](https://www.prisma.io/docs/getting-started/quickstart-prismaPostgres?utm_source=github&utm_medium=prisma-readme) or if you are looking for our MCP Server, head [here](https://github.com/prisma/mcp).** ## Getting started ### Quickstart (5min) The fastest way to get started with Prisma is by following the quickstart guides. You can choose either of two databases: - [Prisma Postgres](https://www.prisma.io/docs/getting-started/quickstart-prismaPostgres) - [SQLite](https://www.prisma.io/docs/getting-started/quickstart-sqlite) ### Bring your own database If you already have your own database, you can follow these guides: - [Add Prisma to an existing project](https://www.prisma.io/docs/getting-started/setup-prisma/add-to-existing-project/relational-databases-typescript-postgresql) - [Set up a new project with Prisma from scratch](https://www.prisma.io/docs/getting-started/setup-prisma/start-from-scratch/relational-databases-typescript-postgresql) ## How Prisma ORM works This section provides a high-level overview of how Prisma ORM works and its most important technical components. For a more thorough introduction, visit the [Prisma documentation](https://www.prisma.io/docs/). ### The Prisma schema Every project that uses a tool from the Prisma toolkit starts with a [Prisma schema file](https://www.prisma.io/docs/orm/prisma-schema). The Prisma schema allows developers to define their _application models_ in an intuitive data modeling language and configure _generators_. ```prisma // Data source datasource db { provider = "postgresql" } // Generator generator client { provider = "prisma-client" output = "../generated" } // Data model model Post { id Int @id @default(autoincrement()) title String content String? published Boolean @default(false) author User? @relation(fields: [authorId], references: [id]) authorId Int? } model User { id Int @id @default(autoincrement()) email String @unique name String? posts Post[] } ``` In this schema, you configure three things: - **Data source**: Specifies your database type and thus defines the features and data types you can use in the schema - **Generator**: Indicates that you want to generate Prisma Client - **Data model**: Defines your application models ### `prisma.config.ts` Database connection details are defined via [`prisma.config.ts`](https://www.prisma.io/docs/orm/prisma-schema/prisma-config-reference). ```ts import { defineConfig } from 'prisma/config' export default defineConfig({ datasource: { url: 'postgres://...', }, }) ``` If you store the database connection string in `process.env`, an `env` function can help you access it in a type safe way and throw an error if it is missing at run time: ```ts import { defineConfig, env } from 'prisma/config' export default defineConfig({ datasource: { url: env('DATABASE_URL'), }, }) ``` Prisma ORM does not load the `.env` files for you automatically. If you want to populate the environment variables from a `.env` file, consider using a package such as [`dotenv`](https://www.npmjs.com/package/dotenv) or [`@dotenvx/dotenvx`](https://www.npmjs.com/package/@dotenvx/dotenvx). The configuration file may look like this in that case: ```ts import 'dotenv/config' import { defineConfig, env } from 'prisma/config' export default defineConfig({ datasource: { url: env('DATABASE_URL'), }, }) ``` To start a local PostgreSQL development server without using Docker and without any configuration, run `prisma dev`: ```sh npx prisma dev ``` Alternatively, spin up an instant Prisma Postgres® database in the cloud: ```sh npx create-db --interactive ``` --- ### The Prisma data model On this page, the focus is on the data model. You can learn more about [Data sources](https://www.prisma.io/docs/reference/tools-and-interfaces/prisma-schema/data-sources) and [Generators](https://www.prisma.io/docs/reference/tools-and-interfaces/prisma-schema/generators) on the respective docs pages. #### Functions of Prisma models The data model is a collection of [models](https://www.prisma.io/docs/orm/prisma-schema/data-model/models). A model has two major functions: - Represent a table in the underlying database - Provide the foundation for the queries in the Prisma Client API #### Getting a data model There are two major workflows for "getting" a data model into your Prisma schema: - Generate the data model from [introspecting](https://www.prisma.io/docs/orm/prisma-schema/introspection) a database - Manually writing the data model and mapping it to the database with [Prisma Migrate](https://www.prisma.io/docs/orm/prisma-migrate) Once the data model is defined, you can [generate Prisma Client](https://www.prisma.io/docs/orm/prisma-client/setup-and-configuration/generating-prisma-client) which will expose CRUD and more queries for the defined models. If you're using TypeScript, you'll get full type-safety for all queries (even when only retrieving the subsets of a model's fields). --- ### Accessing your database with Prisma Client #### Step 1: Install Prisma First, install Prisma CLI as a development dependency and Prisma Client: ``` npm install prisma --save-dev npm install @prisma/client ``` #### Step 2: Set up your Prisma schema Ensure your Prisma schema includes a `generator` block with an `output` path specified: ```prisma generator client { provider = "prisma-client" output = "../generated" } datasource db { provider = "postgresql" // mysql, sqlite, sqlserver, mongodb or cockroachdb } ``` #### Step 3: Configure Prisma Config Configure the Prisma CLI using a `prisma.config.ts` file. This file configures Prisma CLI subcommands like `migrate` and `studio`. Create a `prisma.config.ts` file in your project root: ```ts import { defineConfig, env } from 'prisma/config' type Env = { DATABASE_URL: string } export default defineConfig({ schema: 'prisma/schema.prisma', migrations: { path: 'prisma/migrations', }, datasource: { url: env