prisma-next-quickstart
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChinesePrisma Next — Quickstart (Adoption)
Prisma Next — 快速入门(上手指南)
Edit your data contract. Prisma handles the rest.
This skill takes the user from zero (or near-zero) to a first working query against Prisma Next. Three paths — and they all converge on the same first arc: connect → write → read. Schema editing comes after the first arc, not before.
- First-touch orientation — the user has arrived at a Prisma Next project for the first time (a scaffold tool like dropped them in, they cloned a teammate's repo, or they ran
npx createprismathemselves and now want to make their first move) and they're asking "what can I do with Prisma Next?", "where do I start?", or "what's next?". The goal is to anchor them on the contract, get them connected to a database, round-trip one row, and let further commands surface organically.prisma-next init - Greenfield — new project, fresh database. User runs themselves.
prisma-next initseeds a starter contract with a sample model, so the path joins the first-touch orientation arc as soon as the database is initialised.init - Brownfield-DB — existing database, no contract yet. Infer the contract from the database with , sign the marker with
contract infer, then write queries against one of the existing tables.db sign
This skill does not cover migrating from another ORM (Drizzle, Prisma 6/7, Sequelize, TypeORM, Kysely, Knex, raw drivers). Those are separately-installable skills.
编辑你的数据契约,其余工作交给Prisma处理。
本指南将帮助用户从零(或接近零)开始,完成针对Prisma Next的首次有效查询。共有三种路径,最终都会汇聚到同一个核心流程:连接 → 写入 → 读取。编辑Schema是在完成核心流程之后,而非之前。
- 首次上手引导 — 用户首次接触Prisma Next项目(通过等脚手架工具生成、克隆队友的仓库,或是自行运行
npx createprisma后,想要进行第一步操作),并提出“我能用Prisma Next做什么?”、“我该从哪里开始?”或“接下来该怎么做?”等问题。目标是让用户熟悉契约概念,连接到数据库,完成一次数据往返,并让后续命令根据需求自然呈现。prisma-next init - 全新项目(Greenfield) — 新项目、全新数据库。用户自行运行。
prisma-next init会用示例模型初始化一个基础契约,因此在数据库初始化完成后,该路径会与首次上手引导流程汇合。init - 现有数据库(Brownfield-DB) — 已有数据库,但尚未配置Prisma Next契约。通过从数据库推导契约,使用
contract infer标记数据库,然后针对现有表编写查询。db sign
本指南不涵盖从其他ORM(Drizzle、Prisma 6/7、Sequelize、TypeORM、Kysely、Knex、原生驱动)迁移的内容。这些属于可单独安装的技能。
When to Use
使用场景
- User asks "what can I do with Prisma Next?", "what can I do next with Prisma?", "where do I start?", "what should I do first?" — and a PN project already exists on disk. First-touch orientation path below.
- User just ran (or equivalent scaffold tool) and is asking what to do next. First-touch orientation path.
createprisma - User is starting a new project and wants to use Prisma Next. Greenfield path.
- User has an existing database (no PN contract) and wants to introduce PN. Brownfield-DB path.
- User typed "prisma-next init", "get started with PN", "set up PN", "how do I scaffold a project". Greenfield path.
- User says "I have an existing Postgres/Mongo, how do I start using PN?". Brownfield-DB path.
- 用户提出“我能用Prisma Next做什么?”、“我接下来能用Prisma做什么?”、“我该从哪里开始?”、“我首先应该做什么?”等问题,且本地已存在PN项目。请遵循下方的首次上手引导路径。
- 用户刚运行(或类似脚手架工具),询问下一步操作。请遵循首次上手引导路径。
createprisma - 用户想要启动新项目并使用Prisma Next。请遵循全新项目路径。
- 用户已有数据库(无PN契约),想要引入Prisma Next。请遵循现有数据库路径。
- 用户输入了“prisma-next init”、“get started with PN”、“set up PN”、“how do I scaffold a project”等内容。请遵循全新项目路径。
- 用户表示“我已有Postgres/Mongo数据库,如何开始使用PN?”。请遵循现有数据库路径。
When Not to Use
不适用场景
- User already has a PN project and wants to add a model → .
prisma-next-contract - User wants to migrate FROM a specific ORM → install (separate).
@prisma-next/migrate-from-<orm>-skill - User wants to wire in a project that already has a contract →
db.ts.prisma-next-runtime - User wants to integrate Prisma Next with a build tool (Vite plugin, Next.js, …) → .
prisma-next-build
- 用户已有PN项目,想要添加模型 → 请使用。
prisma-next-contract - 用户想要从特定ORM迁移 → 请安装(单独安装)。
@prisma-next/migrate-from-<orm>-skill - 用户想要在已有契约的项目中配置→ 请使用
db.ts。prisma-next-runtime - 用户想要将Prisma Next与构建工具(Vite插件、Next.js等)集成 → 请使用。
prisma-next-build
Key Concepts
核心概念
- Contract: the data model. Authored as (PSL, the canonical surface) or
contract.prisma(TypeScript builder). The framework reads it and emits two artefacts:contract.ts(runtime IR) andcontract.json(types).contract.d.ts - Target: the backing store. Today: or
postgres. Picked atmongodbtime; baked into theinitfaçade the scaffold imports from.@prisma-next/<target> - Authoring mode: how you write the contract. (Prisma Schema Language, default) or
psl(programmatic builder, optionally paired with the Vite plugin for auto-emit duringtypescript— seevite dev).prisma-next-build - Façade packages. The scaffold installs exactly one façade per target — (or
@prisma-next/postgres). User code imports from façade subpaths (@prisma-next/mongo,@prisma-next/postgres/config,@prisma-next/postgres/runtime). The façade bakes in the family / target / adapter / driver wiring; never reach past it. See@prisma-next/postgres/contract-builderfor the full list.prisma-next-contract - : the runtime entry point. Lives next to the contract source at
db.ts. Imports the contract artefacts and exports asrc/prisma/db.tsvalue the rest of the app uses.db - Marker: a row in your database that records the contract hash. Lets PN detect drift between contract and live DB. Created by
pn_meta_marker(greenfield / first-touch orientation) ordb init(brownfield).db sign
- 契约(Contract):数据模型。以(PSL,标准格式)或
contract.prisma(TypeScript构建器)编写。框架会读取契约并生成两个产物:contract.ts(运行时IR)和contract.json(类型定义)。contract.d.ts - 目标(Target):后端存储。目前支持:或
postgres。在mongodb时选择;会嵌入到脚手架导入的initfacade包中。@prisma-next/<target> - 编写模式(Authoring mode):契约的编写方式。(Prisma Schema Language,默认)或
psl(程序化构建器,可搭配Vite插件在typescript期间自动生成产物 — 详见vite dev)。prisma-next-build - Facade包:脚手架会为每个目标安装一个对应的facade包 — (或
@prisma-next/postgres)。用户代码从facade的子路径导入(@prisma-next/mongo、@prisma-next/postgres/config、@prisma-next/postgres/runtime)。Facade包整合了系列/目标/适配器/驱动的配置;请勿直接绕过它。完整列表请查看@prisma-next/postgres/contract-builder。prisma-next-contract - :运行时入口文件。与契约源文件同目录,位于
db.ts。导入契约产物并导出src/prisma/db.ts对象供应用其他部分使用。db - 标记(Marker):数据库中的行,记录契约哈希值。让PN能够检测契约与实际数据库之间的差异。由
pn_meta_marker(全新项目/首次上手引导)或db init(现有数据库)创建。db sign
Canonical on-disk layout
标准磁盘结构
Every application that consumes Prisma Next uses the same shape:
text
<app-root>/
├── prisma-next.config.ts ← project config at repo root
├── src/
│ └── prisma/
│ ├── contract.prisma ← (or contract.ts) — schema source you author
│ ├── contract.json ← emitted by `contract emit` — do not edit
│ ├── contract.d.ts ← emitted by `contract emit` — do not edit
│ └── db.ts ← runtime entry; the rest of `src/` imports from here
└── migrations/
└── app/ ← created on first `migration plan` / `db init`
├── refs/head.json
└── <timestamp>_<slug>/
├── migration.json
├── ops.json
├── end-contract.json
├── end-contract.d.ts
└── migration.tsThree things to internalise:
- is the home for the contract — source + emitted artefacts +
src/prisma/all colocated. The rest ofdb.tsimports fromsrc/(or./prisma/db, depending on file depth).../prisma/db - — the
migrations/app/segment is the consuming application's space-id. Extensions you depend on get sibling directories underapp/(one per extension contract-space), but you don't write into those — only themigrations/subtree is your migrations.app/ - lives at the repo root, not under
prisma-next.config.ts. Every command resolves paths relative to the config's directory.src/
Contributors building extension packages or aggregate-root monorepo packages use a different layout — (no subdir) + (no segment). That distinction is intentional; see for which path applies to you.
src/contract.{prisma,ts}prisma/migrations/<timestamp>_<slug>/app/prisma-next-contractHeads up —currently scaffolds the wrong layout. It writesprisma-next initandprisma/contract.{prisma,ts}at the repo root instead of underprisma/db.ts. Tracked as TML-2532. Until the fix lands, either passsrc/prisma/to--schema-path src/prisma/contract.prisma, or move the scaffoldedinitdirectory intoprisma/aftersrc/prisma/and update theinitpath incontractto match. The canonical layout above is what the demo example uses and what the rest of the framework expects.prisma-next.config.ts
所有使用Prisma Next的应用都采用相同的结构:
text
<app-root>/
├── prisma-next.config.ts ← 项目配置文件,位于仓库根目录
├── src/
│ └── prisma/
│ ├── contract.prisma ← (或contract.ts)—— 你编写的Schema源文件
│ ├── contract.json ← 由`contract emit`生成 — 请勿编辑
│ ├── contract.d.ts ← 由`contract emit`生成 — 请勿编辑
│ └── db.ts ← 运行时入口;应用其他部分从这里导入
└── migrations/
└── app/ ← 首次执行`migration plan` / `db init`时创建
├── refs/head.json
└── <timestamp>_<slug>/
├── migration.json
├── ops.json
├── end-contract.json
├── end-contract.d.ts
└── migration.ts需要牢记三点:
- 是契约的存放目录 — 源文件、生成产物、
src/prisma/都放在这里。应用其他部分从db.ts(或./prisma/db,取决于文件深度)导入。../prisma/db - —
migrations/app/是当前应用的空间ID。你依赖的扩展会在app/下创建同级目录(每个扩展契约空间对应一个目录),但你无需修改这些目录 — 只有migrations/子目录是你的迁移文件存放位置。app/ - 位于仓库根目录,而非
prisma-next.config.ts下。所有命令都会相对于配置文件所在目录解析路径。src/
构建扩展包或聚合根单体仓库包的贡献者使用不同的结构 — (无子目录) + (无段)。这种区分是有意设计的;请查看了解适用的结构。
src/contract.{prisma,ts}prisma/migrations/<timestamp>_<slug>/app/prisma-next-contract注意 — 当前生成的结构有误。它会在仓库根目录创建prisma-next init和prisma/contract.{prisma,ts},而非prisma/db.ts下。该问题已记录为TML-2532。在修复完成前,你可以在src/prisma/时传入init,或者在--schema-path src/prisma/contract.prisma完成后将生成的init目录移动到prisma/下,并更新src/prisma/中的prisma-next.config.ts路径以匹配。上述标准结构是演示示例使用的结构,也是框架期望的结构。contract
Your first arc — connect, write, read
你的第一个核心流程 — 连接、写入、读取
All three paths in this skill converge here. Once the project is scaffolded and the database is reachable, the first move is always the same: connect, write a row, read it back, against whatever model the contract already declares. Don't touch the contract source on this first move — extend it later, after the round-trip works.
Write the snippet in a fresh file directly under (e.g. ) so the relative import resolves to one level deep:
src/src/first-arc.tstypescript
// src/first-arc.ts
import 'dotenv/config';
import { db } from './prisma/db';
// Write a row against the starter model. Adapt the field names to whatever
// model your contract source actually declares — read it first.
await db.orm.User.create({ email: 'alice@example.com' });
// Read it back.
const users = await db.orm.User.select('id', 'email').all();
console.log(users);If that prints , the project is wired end-to-end and the user has crossed from "I have a project" to "I'm building."
[{ id: 1, email: 'alice@example.com' }]db.orm.<Model>DATABASE_URL.envdotenv/configprisma-next-queriesPrerequisites for the arc to work. All three paths leave these in place by the time you reach the arc:
- exists at the repo root and declares the target + contract source (typically
prisma-next.config.tsorsrc/prisma/contract.prisma).src/prisma/contract.ts - The contract source exists at (a starter model from
src/prisma/contract.{prisma,ts}, or the inferred contract frominit, or whatever the bootstrap tool generated).contract infer - exists and instantiates the runtime with the emitted contract.
src/prisma/db.ts - is set in
DATABASE_URL(or wherever the runtime's config tells it to look)..env - The database has been initialised () or marker-signed (
db init), so the marker row exists and the schema matches the contract.db sign
The three workflows below each describe how their path gets the user to that state. After that, the arc above is the same.
本指南中的三种路径最终都会汇聚到这里。一旦项目搭建完成且数据库可访问,第一步操作始终是相同的:连接数据库,写入一行数据,再读取回来,针对契约中已有的任意模型进行操作。在完成首次数据往返前,请勿修改契约源文件 — 等往返成功后再扩展契约。
在下新建一个文件(例如),写入以下代码,确保相对导入路径正确:
src/src/first-arc.tstypescript
// src/first-arc.ts
import 'dotenv/config';
import { db } from './prisma/db';
// 针对基础模型写入一行数据。请根据契约源文件中实际定义的模型调整字段名 — 先查看契约内容。
await db.orm.User.create({ email: 'alice@example.com' });
// 读取数据
const users = await db.orm.User.select('id', 'email').all();
console.log(users);如果控制台输出,说明项目已完成端到端配置,用户已从“我有一个项目”过渡到“我正在构建应用”。
[{ id: 1, email: 'alice@example.com' }]db.orm.<Model>dotenv/config.envDATABASE_URLprisma-next-queries核心流程的前提条件。三种路径在到达核心流程时都会满足以下条件:
- 仓库根目录存在,并声明了目标(
prisma-next.config.ts/postgres)和契约源文件路径(通常为mongodb或src/prisma/contract.prisma)。src/prisma/contract.ts - 契约源文件存在于(由
src/prisma/contract.{prisma,ts}生成的基础模型、由init推导的契约,或由脚手架工具生成的内容)。contract infer - 存在,并使用生成的契约实例化运行时。
src/prisma/db.ts - 中已设置
.env(或运行时配置指定的其他位置)。DATABASE_URL - 数据库已初始化()或已标记签署(
db init),因此标记行存在且Schema与契约匹配。db sign
以下三个工作流分别描述了不同路径如何让用户满足上述条件。之后,核心流程都是相同的。
Workflow — First-touch orientation
工作流 — 首次上手引导
Triggers: "what can I do with Prisma Next?", "what can I do next with Prisma?", "where do I start?", "I just ran createprisma", "what's next?", or any close variant — paired with a PN project already on disk (scaffolded by , by , by a teammate, however).
createprismaprisma-next initThe user's high-level intent is "I want to be running an application against my database, against this thing called Prisma Next." The job of this workflow is to anchor them on the contract, get one round-trip working, and let further commands surface organically as their next move requires them. It is orientation, not a tour, not a feature inventory, not a syllabus.
触发场景:用户提出“我能用Prisma Next做什么?”、“我接下来能用Prisma做什么?”、“我该从哪里开始?”、“我刚运行了createprisma”、“接下来该怎么做?”等类似问题,且本地已存在PN项目(由、、队友创建等方式生成)。
createprismaprisma-next init用户的核心需求是“我想要让我的应用运行在数据库上,使用Prisma Next”。本工作流的任务是让用户熟悉契约概念,完成一次数据往返,并让后续命令根据用户的下一步需求自然呈现。这是引导,不是功能介绍、不是命令清单、不是教程大纲。
Concept — what to communicate first
概念 — 首先要传达的内容
Prisma Next is contract-first. Everything the framework does — query types, migrations, runtime types, drift detection — flows from a single source of truth: the contract. The contract describes the user's application's data model. The framework reads it; the framework derives the rest. Lead with this.
The first response to "what can I do with Prisma Next?" names the contract path, frames its role in one sentence, and then steers toward getting the user's application running. Don't open with a feature inventory. Don't open with a list of commands. Open with: "Your contract is at . It describes your application — your query types, migrations, and runtime types all flow from it. Let's get you connected to a database so your app can actually run against it."
<path>The first arc — once oriented — is connect → write → read. Not edit-the-contract-first, not plan-a-migration-first. The user's win is I have application code running against my database.
Prisma Next是契约优先的框架。框架的所有功能 — 查询类型、迁移、运行时类型、差异检测 — 都源于单一的事实来源:契约。契约描述了应用的数据模型。框架读取契约,然后衍生出其他所有内容。首先要强调这一点。
对于“我能用Prisma Next做什么?”的第一个回答,要指出契约的路径,用一句话说明其作用,然后引导用户连接数据库让应用运行起来。不要一开始就罗列功能,不要一开始就列出命令。可以这样开头:“你的契约位于,它描述了你的应用 — 框架提供的所有查询类型、迁移和运行时类型都源于这个文件。接下来让我们连接到数据库,让你的应用能够真正运行起来。”具体措辞可以调整,但关键是让用户在第一次回复中知道契约在哪里以及它是事实来源。
<path>完成引导后的第一个核心流程是连接 → 写入 → 读取。不是先编辑契约,不是先规划迁移。用户的目标是“我的应用运行在数据库上”。
Step 1 — Read the project, name the contract
步骤1 — 了解项目,明确契约位置
Before saying anything specific to the user, read:
- at the repo root — what target (
prisma-next.config.ts/postgres) is wired, whatmongodbpath it declares, what extensions are installed.contract: - The contract source the config declares (canonically or
src/prisma/contract.prisma; a project that pre-dates TML-2532 may have it atsrc/prisma/contract.tsinstead — check theprisma/contract.{prisma,ts}field of the config) — what starter models, if any, exist.contract - (next to the contract) — the runtime entry point.
src/prisma/db.ts - /
.env— is.env.exampleset, or only the example?DATABASE_URL - Optionally — does the live DB match the contract?
pnpm prisma-next db verify
Then say the contract path back to the user, with its role attached. Something like: "Your contract is at , and it currently declares a model. The contract describes your app — every query type, migration, and runtime type the framework gives you flows from this file. Let's get your app connected to a database next." The exact wording is up to the agent; what matters is that the user leaves the first response knowing where the contract is and that it is the source of truth.
src/prisma/contract.prismaUser在向用户提出具体建议前,先查看:
- 仓库根目录的— 配置了什么目标(
prisma-next.config.ts/postgres),声明了什么mongodb路径,安装了哪些扩展。contract: - 配置中声明的契约源文件(标准位置为或
src/prisma/contract.prisma;早于TML-2532的项目可能位于src/prisma/contract.ts— 请查看配置中的prisma/contract.{prisma,ts}字段) — 存在哪些基础模型(如果有的话)。contract - 契约文件同目录下的— 运行时入口文件。
src/prisma/db.ts - /
.env— 是否已设置.env.example,还是只有示例?DATABASE_URL - 可选:运行— 实际数据库是否与契约匹配?
pnpm prisma-next db verify
然后向用户说明契约的位置及其作用。例如:“你的契约位于,当前声明了一个模型。契约描述了你的应用 — 框架提供的所有查询类型、迁移和运行时类型都源于这个文件。接下来让我们连接到数据库。”具体措辞可以调整,但关键是让用户知道契约的位置和它是事实来源。
src/prisma/contract.prismaUserStep 2 — Get the user's app connected and round-tripping
步骤2 — 让用户的应用连接到数据库并完成数据往返
The motivation is "so your app can actually run against your database", not "so the prerequisite checklist passes". The mechanics depend on what's already in place from Step 1:
- Everything already wired. Go straight to writing and reading a row (see Your first arc — connect, write, read above). Adapt the snippet to whatever model the contract declares.
- not set. Have the user set it in
DATABASE_URL(not in.env— see Pitfall 5). Thenprisma-next.config.tsto apply the current contract to that database and write the marker row. Now the app can connect.pnpm prisma-next db init - Database is connectable but not yet aware of the contract (marker row missing; reports drift). Run
db verify. (pnpm prisma-next db initis the alternative for quick dev cycles — it's looser, doesn't write a migration history, and is what users reach for when they want to iterate on the schema fast. Mention it if the user asks how to make schema changes flow to the DB; don't pre-explain it.)db update - Contract is empty (bootstrap left the source blank). Add one model with two fields (e.g. ),
User { id, email }, thenpnpm prisma-next contract emit. Minimal — get the round-trip working, then extend.pnpm prisma-next db init
The user encounters (and optionally , ) here because they're the commands their current move requires. They learn what those commands are by using them.
db initdb updatecontract emit目的是“让你的应用能够真正运行在数据库上”,而不是“满足前提条件清单”。具体操作取决于步骤1中已有的配置:
- 所有配置已完成。直接进行写入和读取操作(详见上方“你的第一个核心流程 — 连接、写入、读取”)。根据契约中声明的模型调整代码片段。
- 未设置。让用户在
DATABASE_URL中设置该值(不要在.env中设置 — 详见陷阱5)。然后运行prisma-next.config.ts将当前契约应用到数据库并写入标记行。现在应用可以连接到数据库了。pnpm prisma-next db init - 数据库可连接但尚未关联契约(缺少标记行;报告差异)。运行
db verify。(pnpm prisma-next db init是快速开发周期的替代方案 — 它更灵活,不会保留迁移历史,适合用户想要快速迭代Schema的场景。如果用户询问如何将Schema变更同步到数据库,可以提及它;不要提前解释。)db update - 契约为空(脚手架工具生成的源文件为空)。添加一个模型,包含两个字段(例如),运行
User { id, email },然后运行pnpm prisma-next contract emit。保持最简 — 先让数据往返成功,然后再扩展。pnpm prisma-next db init
用户会在这里接触到(以及可选的、),因为这些是当前操作必需的命令。用户通过使用这些命令来了解它们的作用。
db initdb updatecontract emitStep 3 — Round-trip a row
步骤3 — 完成数据往返
Run the snippet from Your first arc — connect, write, read above against whatever model the contract declares. When it prints the row back, the user has crossed from "I have a project" to "my app runs against my database". That's the win.
运行上方“你的第一个核心流程 — 连接、写入、读取”中的代码片段,针对契约中声明的任意模型进行操作。当控制台输出数据行时,用户已从“我有一个项目”过渡到“我的应用运行在数据库上”。这就是目标。
Step 4 — Hand off to the next move
步骤4 — 引导到下一步操作
Now ask the user what they want to build. Route to the skill that owns that move:
- More queries (filters, joins, transactions, raw SQL, TypedSQL) → .
prisma-next-queries - Add a model, change a field, add a relation → . They'll touch
prisma-next-contractandcontract emit(ordb update+migration plan) as part of that workflow.migrate - Middleware, environment config, multiple targets → .
prisma-next-runtime - Vite / Next.js / dev-server integration → .
prisma-next-build - They want a fuller toolbelt overview at this point — Commands you'll use day-to-day below is the one-glance summary.
现在询问用户想要构建什么,引导到对应的技能:
- 更多查询功能(过滤、关联、事务、原生SQL、TypedSQL) → 。
prisma-next-queries - 添加模型、修改字段、添加关联 → 。用户在该工作流中会用到
prisma-next-contract和contract emit(或db update+migration plan)。migrate - 中间件、环境配置、多目标 → 。
prisma-next-runtime - Vite / Next.js / 开发服务器集成 → 。
prisma-next-build - 用户此时想要了解更多常用命令 — 下方的“日常使用命令”是一目了然的总结。
Anti-patterns on this path
该路径的反模式
- Leading with a feature tour or capability inventory. The user asked what they can do. Get them doing it.
- Listing commands before any have been used. Commands belong to specific moves; surface them when the move requires them.
- Diving into migration concepts before one query has run. Migrations exist; their value lands later.
- Adding several models in one go. Add one, get one query green, then iterate.
- Walking the user through keys. The scaffold's defaults are correct; revisit when the user needs to change something.
prisma-next.config.ts - Skipping the contract framing. Even one line — "your contract is at , it's the source of truth" — anchors the user; without it, the rest of the workflow lands as disconnected ceremony.
<path>
- 一开始就进行功能介绍或罗列功能。用户问的是他们能做什么,让他们动手操作。
- 在使用命令前先罗列所有命令。命令属于特定操作;在需要时再呈现它们。
- 在完成首次查询前深入讲解迁移概念。迁移确实存在,但它的价值在后续体现。
- 一次性添加多个模型。先添加一个,让一次查询成功,然后再迭代。
- 引导用户查看的所有配置项。脚手架的默认配置是正确的;当用户需要修改时再回顾。
prisma-next.config.ts - 跳过契约的概念说明。即使只用一句话 — “你的契约位于,它是事实来源” — 也能让用户建立认知;否则,后续工作流会显得杂乱无章。
<path>
Workflow — Greenfield
工作流 — 全新项目(Greenfield)
The concept: is one CLI command that scaffolds config, schema, runtime, dependencies, and the contract emit step. It operates on the current working directory — there is no positional project-name argument. Make the directory, in, then run init.
prisma-next initcdbash
mkdir my-app && cd my-app
pnpm init # if no package.json yet
pnpm dlx prisma-next init # interactive核心概念:是一个CLI命令,用于生成配置、Schema、运行时、依赖项和契约生成步骤。它在当前工作目录下操作 — 不需要指定项目名称参数。创建目录,进入目录,然后运行init命令。
prisma-next initbash
mkdir my-app && cd my-app
pnpm init # 如果还没有package.json
pnpm dlx prisma-next init # 交互式or non-interactive (CI / agent runs):
或非交互式(CI/自动化运行):
pnpm dlx prisma-next init --yes --target postgres --authoring psl
The flags `init` accepts (run `prisma-next init --help` for the source of truth):
- `--target <db>` — `postgres` or `mongodb`.
- `--authoring <style>` — `psl` or `typescript`.
- `--schema-path <path>` — defaults to `prisma/contract.prisma` (or `prisma/contract.ts`). **Pass `--schema-path src/prisma/contract.prisma` (or `.../contract.ts`)** to scaffold into the canonical `src/prisma/` location directly — `init`'s default is wrong today, see [TML-2532](https://linear.app/prisma-company/issue/TML-2532).
- `--force` — overwrite an existing scaffold without prompting (re-running init in a scaffolded directory triggers the reinit flow — `--force` skips the confirmation).
- `--write-env` — also write `.env` (default writes only `.env.example`; `.env` stays under your control).
- `--probe-db` — connect to `DATABASE_URL` once and check the server version against the target's minimum.
- `--strict-probe` — fail init if the probe fails (no-op without `--probe-db`).
- `--no-install` — skip dependency install + initial contract emit.
- `--no-skill` — skip Prisma Next skills installation (air-gapped / restricted environments). The skill cluster is always installed at the project level — never globally — so its version stays locked to the project's Prisma Next version.
`init` writes (when it runs cleanly):
- `prisma-next.config.ts` at the project root.
- The contract source at `--schema-path` — `src/prisma/contract.prisma` if you passed the canonical override, `prisma/contract.prisma` if you accepted the (currently-wrong) default.
- `db.ts` in the same directory as the contract source.
- `prisma-next.md` — a human quick-reference.
- `.env.example` (and `.env` if `--write-env`).
- Updates `package.json` (deps + scripts) and `tsconfig.json` (required compiler options).
- Installs deps and runs `prisma-next contract emit` once.
- Registers Prisma Next skills with the local agent runtime.
**If you took `init`'s default and ended up with a top-level `prisma/` directory** (TML-2532), the cleanup is one move + one config edit:
```bash
mkdir -p src && mv prisma src/prismapnpm dlx prisma-next init --yes --target postgres --authoring psl
`init`支持的参数(运行`prisma-next init --help`查看权威说明):
- `--target <db>` — `postgres`或`mongodb`。
- `--authoring <style>` — `psl`或`typescript`。
- `--schema-path <path>` — 默认值为`prisma/contract.prisma`(或`prisma/contract.ts`)。**请传入`--schema-path src/prisma/contract.prisma`(或`.../contract.ts`)**,直接生成到标准的`src/prisma/`位置 — 当前`init`的默认路径有误,详见[TML-2532](https://linear.app/prisma-company/issue/TML-2532)。
- `--force` — 覆盖现有脚手架文件,无需提示(在已搭建的目录中重新运行init会触发重新初始化流程 — `--force`会跳过确认)。
- `--write-env` — 同时写入`.env`(默认只写入`.env.example`;`.env`由用户自行管理)。
- `--probe-db` — 连接到`DATABASE_URL`一次,检查服务器版本是否符合目标数据库的最低要求。
- `--strict-probe` — 如果探测失败,则终止init(没有`--probe-db`时无效)。
- `--no-install` — 跳过依赖安装和首次契约生成。
- `--no-skill` — 跳过Prisma Next技能安装(适用于离线/受限环境)。技能集群始终安装在项目级别 — 不会全局安装,因此其版本会与项目的Prisma Next版本保持一致。
当`init`成功运行时,会生成以下文件:
- 项目根目录下的`prisma-next.config.ts`。
- 契约源文件,位于`--schema-path`指定的位置 — 如果传入了标准路径,则为`src/prisma/contract.prisma`;如果接受了当前(错误的)默认值,则为`prisma/contract.prisma`。
- 与契约源文件同目录的`db.ts`。
- `prisma-next.md` — 人类可读的快速参考文档。
- `.env.example`(如果使用`--write-env`,则同时生成`.env`)。
- 更新`package.json`(依赖项 + 脚本)和`tsconfig.json`(必需的编译选项)。
- 安装依赖项并运行一次`prisma-next contract emit`。
- 在本地代理运行时中注册Prisma Next技能。
**如果你接受了`init`的默认值,生成了顶级的`prisma/`目录**(TML-2532),只需一步操作+一处配置修改即可修复:
```bash
mkdir -p src && mv prisma src/prismaThen update prisma-next.config.ts so contract
reads
contract然后更新prisma-next.config.ts中的contract
路径
contract'src/prisma/contract.prisma' (or .ts) instead of 'prisma/contract.prisma'.
从'prisma/contract.prisma'(或.ts)改为'src/prisma/contract.prisma'(或.ts)。
pnpm prisma-next contract emit # re-emits contract.json + contract.d.ts under src/prisma/
Do this before running `db init` — once the marker row is written, restructuring is harder.
After init succeeds, the path converges on *Your first arc — connect, write, read* above. `init` has already seeded a starter contract with `User` and `Post` models (with a relation between them) and run `contract emit` once; the only remaining prerequisites are setting `DATABASE_URL` and initialising the database. Two commands:
1. Set `DATABASE_URL` in `.env` (copy from `.env.example`).
2. Initialise the database: `pnpm prisma-next db init`. Creates tables, indexes, constraints, and writes the marker row — using the starter contract `init` generated.
Then run the snippet from *Your first arc* above against the `User` model. When the user is ready to extend the contract — add more models, change fields, add relations — chain to `prisma-next-contract`. For more queries, chain to `prisma-next-queries`.
**Why this is queries-first, not schema-editing-first.** `init` ships with `User` and `Post` on purpose: the user shouldn't have to design a schema to prove their setup works. Extending the contract is the next move *after* the first arc lands, not part of getting there. If the user asks you to skip straight to *"add a Comment model"* — sure, do that — but get one query green against `User` or `Post` first if there's any doubt the project is wired correctly.pnpm prisma-next contract emit # 重新生成src/prisma/下的contract.json + contract.d.ts
请在运行`db init`前完成此操作 — 一旦写入标记行,调整结构会更困难。
init成功后,路径会汇聚到上方的“你的第一个核心流程 — 连接、写入、读取”。`init`已经生成了包含`User`和`Post`模型(两者有关联)的基础契约,并运行了一次`contract emit`;剩下的前提条件只有设置`DATABASE_URL`和初始化数据库。只需两个命令:
1. 在`.env`中设置`DATABASE_URL`(从`.env.example`复制)。
2. 初始化数据库:`pnpm prisma-next db init`。创建表、索引、约束,并写入标记行 — 使用`init`生成的基础契约。
然后针对`User`模型运行上方核心流程中的代码片段。当用户准备扩展契约 — 添加更多模型、修改字段、添加关联 — 时,引导到`prisma-next-contract`。如需更多查询功能,引导到`prisma-next-queries`。
**为什么先做查询,而不是先编辑Schema**。`init`默认生成`User`和`Post`模型是有目的的:用户无需设计Schema即可验证设置是否成功。扩展契约是完成核心流程后的下一步操作,而非设置过程的一部分。如果用户要求直接“添加Comment模型” — 可以,但如果对项目配置是否正确有疑问,请先确保针对`User`或`Post`的一次查询成功。Workflow — Brownfield-DB (existing database, no contract)
工作流 — 现有数据库(Brownfield-DB,已有数据库,无契约)
The concept: against an existing database with no PN contract, walks the live schema (tables, columns, indexes, constraints) and writes a PSL contract that describes it. The result is a starting point, not the final contract — review and clean it up, then to record the current contract hash as the marker (instead of letting try to recreate the schema from scratch).
contract inferdb signdb initbash
mkdir my-app && cd my-app
pnpm init
pnpm dlx prisma-next init --yes --target postgres --authoring psl核心概念:针对没有PN契约的现有数据库,会遍历实际Schema(表、列、索引、约束),生成描述该数据库的PSL契约。结果是一个起点,而非最终契约 — 请先检查并清理,然后使用记录当前契约哈希值作为标记(而非让尝试从头重建Schema)。
contract inferdb signdb initbash
mkdir my-app && cd my-app
pnpm init
pnpm dlx prisma-next init --yes --target postgres --authoring pslscaffold lands; you'll overwrite the starter schema below
脚手架生成完成;接下来会覆盖基础Schema
Then, with `DATABASE_URL` set in `.env`:
```bash
pnpm prisma-next contract infer --db "$DATABASE_URL" --output src/prisma/contract.prisma(Note: the flag is , not . Run for the full surface.)
--output--outprisma-next contract infer --helpThe agent should pause here and read the inferred PSL. Symptoms a re-author pass is needed:
- Tables PN couldn't categorise (e.g. legacy linking tables you could express as relations).
- Columns where PN's type guess is wrong (e.g. where you want an extension type like
String).pgvector.Vector(length: 1536) - Missing /
@uniquehints PN couldn't see.@index - Field names you'd prefer to alias.
Then re-emit and sign:
bash
pnpm prisma-next contract emit
pnpm prisma-next db sign
pnpm prisma-next db verify # confirms the DB matches the contract; reports drift if notThen run the snippet from Your first arc — connect, write, read above, using one of your existing tables in place of the starter model. The arc is the same; only the path that got you there differs.
然后,在`.env`中设置`DATABASE_URL`后:
```bash
pnpm prisma-next contract infer --db "$DATABASE_URL" --output src/prisma/contract.prisma(注意:参数是,不是。运行查看完整参数。)
--output--outprisma-next contract infer --help此时应该暂停并检查生成的PSL。出现以下情况时需要重新编辑:
- PN无法分类的表(例如可以表示为关联的遗留关联表)。
- PN类型猜测错误的列(例如应该使用等扩展类型,而不是
pgvector.Vector(length: 1536))。String - PN无法识别的缺失/
@unique提示。@index - 你想要别名的字段名。
然后重新生成契约并标记数据库:
bash
pnpm prisma-next contract emit
pnpm prisma-next db sign
pnpm prisma-next db verify # 确认数据库与契约匹配;如果不匹配则报告差异然后运行上方“你的第一个核心流程 — 连接、写入、读取”中的代码片段,用现有表替换基础模型。核心流程是相同的,只有到达该流程的路径不同。
Commands you'll use day-to-day
日常使用命令
A reference table — not a script to recite at the user. Commands surface in the workflow above as the user's next move requires them; this table is here for the moment the user asks for a wider view (typically after the first round-trip), and as a one-glance summary anyone newly oriented to Prisma Next can scan. For flag-level detail, run ; the help output is the source of truth.
<command> --help| What you want to do | Command | Deeper skill |
|---|---|---|
| Apply the current contract to the DB the first time | | this skill |
Re-emit | | |
| Quick dev-only schema sync (no migration history kept) | | |
| Plan a migration from a contract diff | | |
| Apply pending migrations | | |
| Inspect the live database | | |
| Confirm the DB matches the contract (drift check) | | |
| Bring an existing DB into a PN contract | | this skill (brownfield) |
| Decode a structured error envelope | (read the | |
| Report a bug or request a feature | (file via the feedback skill) | |
这是一个参考表 — 不是要向用户背诵的脚本。命令会在上述工作流中根据用户的下一步需求自然呈现;当用户想要了解更多(通常在首次数据往返后)时,这个表可以作为一目了然的总结,也适合刚接触Prisma Next的用户快速浏览。如需参数细节,请运行;帮助输出是权威来源。
<command> --help| 操作目标 | 命令 | 深入技能 |
|---|---|---|
| 首次将当前契约应用到数据库 | | 本技能 |
编辑契约源文件后重新生成 | | |
| 快速开发环境下的Schema同步(不保留迁移历史) | | |
| 根据契约差异规划迁移 | | |
| 应用待处理的迁移 | | |
| 查看实际数据库的Schema | | |
| 确认数据库与契约匹配(差异检查) | | |
| 将现有数据库引入PN契约 | | 本技能(现有数据库场景) |
| 解析结构化错误信息 | (查看 | |
| 报告Bug或请求功能 | (通过反馈技能提交) | |
Decision — PSL vs TypeScript authoring
选择 — PSL vs TypeScript编写模式
- PSL () — the default. Concise, declarative, familiar to anyone who has used Prisma. Recommended for most projects.
contract.prisma - TypeScript () — a programmatic builder. Use when the contract is genuinely computed (multi-tenant per-tenant variants), when you reuse contract fragments across files, or when an extension requires constructs PSL doesn't yet express (e.g. pgvector's parameterised storage-type registration). Pairs with the Vite plugin from
contract.tsfor auto-emit on save.prisma-next-build
Switch authoring later by re-running in the same directory. The init flow detects the existing scaffold and prompts to reinit (use to skip the prompt in non-interactive runs). Existing contract content is not automatically translated — you'll re-author by hand in the target language.
prisma-next init--force- PSL() — 默认选项。简洁、声明式,适合任何使用过Prisma的用户。推荐大多数项目使用。
contract.prisma - TypeScript() — 程序化构建器。当契约需要动态生成(多租户的租户专属变体)、需要在多个文件中复用契约片段,或扩展需要PSL尚未支持的结构(例如pgvector的参数化存储类型注册)时使用。搭配
contract.ts中的Vite插件可实现保存时自动生成产物。prisma-next-build
之后可以通过在同一目录下重新运行切换编写模式。init流程会检测到现有脚手架并提示重新初始化(非交互式运行时使用跳过提示)。现有契约内容不会自动转换 — 需要手动用目标语言重新编写。
prisma-next init--forceCommon Pitfalls
常见陷阱
- Running with a positional argument.
prisma-next init <project-name>operates on the current working directory; there is no positional project-name argument.init.mkdir foo && cd foo && pnpm dlx prisma-next init - doesn't connect to your database. It only scaffolds files and installs dependencies (and runs the initial
init). You connect withcontract emit/db init/db update. Ifmigratesucceeds and queries fail, the issue isinit, notDATABASE_URL.init - Treating inferred PSL as the final contract. produces a starting point. Don't
contract inferagainst a contract you haven't read.db sign - Forgetting to emit after editing the contract. The contract artefacts (,
contract.json) are stale until you runcontract.d.ts. If the type-checker says a model "doesn't exist", you skipped emit.contract emit - Setting in
DATABASE_URLinstead ofprisma-next.config.ts. The config reads.envautomatically via.env. Hardcoding the URL leaks credentials and bypasses per-environment overrides. Seedotenv/config.prisma-next-runtime - Hand-editing or
contract.json. They're emitted artefacts; the nextcontract.d.tsoverwrites your changes. Edit the source instead.contract emit - Using for
--out. The flag iscontract infer.--output
- 运行时传入项目名称参数。
prisma-next init <project-name>在当前工作目录下操作;没有项目名称参数。正确做法:init。mkdir foo && cd foo && pnpm dlx prisma-next init - 不会连接到你的数据库。它只会生成文件并安装依赖项(以及运行首次
init)。你需要通过contract emit/db init/db update连接数据库。如果migrate成功但查询失败,问题出在init,而非DATABASE_URL。init - 将推导的PSL视为最终契约。生成的是起点。不要在未检查的情况下运行
contract infer。db sign - 编辑契约源文件后忘记生成产物。在运行前,契约产物(
contract emit、contract.json)是过时的。如果类型检查器提示模型“不存在”,说明你跳过了生成步骤。contract.d.ts - 在中设置
prisma-next.config.ts,而非DATABASE_URL。配置文件会通过.env自动读取dotenv/config。硬编码URL会泄露凭据,并绕过环境特定的覆盖。详见.env。prisma-next-runtime - 手动编辑或
contract.json。它们是生成的产物;下一次contract.d.ts会覆盖你的修改。请编辑源文件。contract emit - 为使用
contract infer参数。正确参数是--out。--output
What Prisma Next doesn't do yet
Prisma Next目前不支持的功能
- Migration from another ORM. Prisma Next doesn't migrate your schema from Drizzle / Prisma 6/7 / Sequelize / TypeORM / Kysely / Knex / a raw driver. Workaround: install the matching if one exists for your source, or treat the source as a brownfield database and
@prisma-next/migrate-from-<orm>-skillfrom it. If you need a guided migration flow built-in, file a feature request via thecontract inferskill.prisma-next-feedback - -style production sync.
prisma db pushis the quick development path; for production, use migrations (db update+migration plan). PN deliberately does not offer a "push-to-prod-without-a-migration" surface — seemigrate.prisma-next-migrations - Studio / GUI database browser. Use for a CLI tree-style summary of the live DB. If you need an interactive UI, file a feature request via the
prisma-next db schemaskill.prisma-next-feedback
- 从其他ORM迁移。Prisma Next不支持从Drizzle / Prisma 6/7 / Sequelize / TypeORM / Kysely / Knex / 原生驱动迁移Schema。解决方法:如果有对应的,请安装;或者将源数据库视为现有数据库,使用
@prisma-next/migrate-from-<orm>-skill推导契约。如果需要内置的引导迁移流程,请通过contract infer技能提交功能请求。prisma-next-feedback - 类似的生产环境同步。
prisma db push是快速开发路径;生产环境请使用迁移(db update+migration plan)。PN故意不提供“无需迁移即可推送到生产环境”的功能 — 详见migrate。prisma-next-migrations - Studio / GUI数据库浏览器。使用查看CLI树形结构的实际数据库摘要。如果需要交互式UI,请通过
prisma-next db schema技能提交功能请求。prisma-next-feedback
Reference Files
参考文件
This skill is intentionally body-only; , , and are the authoritative surfaces for flag-level detail. When in doubt, run and read the actual command's description rather than guessing from this skill.
prisma-next init --helpcontract infer --helpdb sign --help--help本指南仅包含核心内容;、和是参数细节的权威来源。如有疑问,请运行并查看命令的实际描述,而非根据本指南猜测。
prisma-next init --helpcontract infer --helpdb sign --help--helpChecklist
检查清单
- Confirmed which path applies (first-touch orientation / greenfield / brownfield) before proposing commands.
- First-touch orientation: named the contract path back to the user and framed its role (source of truth from which query types, migrations, and runtime types flow) before proposing any commands.
- All paths: brought the project to the Your first arc prerequisites (config, contract source, ,
db.ts, marker row) before writing application code.DATABASE_URL - All paths: ran the first arc — one + one
createagainst the starter (or inferred) model — and got the round-trip working green.select - All paths: did not edit the contract source as part of the first arc. Schema extension is the next move, not the first.
- All paths: did not lead with a feature tour, capability inventory, or recital of CLI commands. Commands surfaced as the user's current move required them.
- Confirmed the user's target (/
postgres) and authoring mode (mongodb/psl).typescript - First-touch orientation: read , the contract source,
prisma-next.config.ts, anddb.tsbefore proposing anything — didn't assume what the scaffold tool / teammate left in place..env - Greenfield path: ran from the project directory — no positional project-name argument.
prisma-next init - All paths: the project ended up in the canonical +
src/prisma/contract.{prisma,ts}+src/prisma/db.tslayout — including moving the scaffolded directory out of a top-levelmigrations/app/ifprisma/produced one (TML-2532).init - Brownfield path: ran , reviewed the result, then
contract infer --db "$DATABASE_URL" --output src/prisma/contract.prisma+contract emit.db sign - Set in
DATABASE_URLand confirmed the value is reachable..env - Initialised the DB (greenfield / first-touch orientation) or signed the marker (
db initbrownfield).db sign - Did NOT hand-edit or
contract.json.contract.d.ts - Did NOT set in
DATABASE_URL.prisma-next.config.ts - Confirmed the user understands what the next skill is for their workflow (typically for more queries, then
prisma-next-querieswhen they're ready to extend the schema).prisma-next-contract
- 在建议命令前确认适用的路径(首次上手引导 / 全新项目 / 现有数据库)。
- 首次上手引导:在建议任何命令前,向用户说明契约的位置并解释其作用(查询类型、迁移和运行时类型都源于此事实来源)。
- 所有路径:在编写应用代码前,确保项目满足“你的第一个核心流程”的前提条件(配置、契约源文件、、
db.ts、标记行)。DATABASE_URL - 所有路径:完成第一个核心流程 — 针对基础(或推导的)模型执行一次+ 一次
create,并确保数据往返成功。select - 所有路径:在第一个核心流程中不编辑契约源文件。扩展Schema是下一步操作,而非第一步。
- 所有路径:不一开始就进行功能介绍、罗列功能或背诵CLI命令。命令根据用户当前操作的需求呈现。
- 确认用户的目标(/
postgres)和编写模式(mongodb/psl)。typescript - 首次上手引导:在建议任何操作前,查看、契约源文件、
prisma-next.config.ts和db.ts— 不要假设脚手架工具/队友留下的配置。.env - 全新项目路径:在项目目录下运行— 不传入项目名称参数。
prisma-next init - 所有路径:项目最终采用标准结构+
src/prisma/contract.{prisma,ts}+src/prisma/db.ts— 如果migrations/app/生成了顶级的init目录,需将其移动到prisma/下(TML-2532)。src/prisma/ - 现有数据库路径:运行,检查结果,然后运行
contract infer --db "$DATABASE_URL" --output src/prisma/contract.prisma+contract emit。db sign - 在中设置
.env并确认该值可访问。DATABASE_URL - 初始化数据库(全新项目/首次上手引导使用)或标记数据库(现有数据库使用
db init)。db sign - 不手动编辑或
contract.json。contract.d.ts - 不在中设置
prisma-next.config.ts。DATABASE_URL - 确认用户了解其工作流的下一个技能(通常是用于更多查询功能,然后是
prisma-next-queries用于扩展Schema)。prisma-next-contract