prisma-next-quickstart
此技能帮助开发者从零快速上手 Prisma Next 数据库访问层,实现连接数据库、写入数据、读取首个查询的完整闭环。支持全新项目、已有数据库导入和首次接触三种路径,自动生成契约与运行入口,让开发者专注数据建模而非繁琐配置,显著缩短项目初始化时间,加速后端开发流程。
npx skills add https://github.com/prisma/prisma-next --skill prisma-next-quickstartBefore / After 效果对比
1 组开发者需要手动阅读文档、配置数据库连接、定义数据模型并编写运行时代码,过程繁琐且容易出错,首次配置通常需要数小时,且难以确定最佳实践。
此 Skill 提供清晰的向导式流程,自动生成契约文件和运行入口,开发者只需简单几步即可完成数据库连接并跑通首个查询,大幅降低上手门槛和配置时间。
Prisma Next — Quickstart (Adoption)
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
npx createprismadropped them in, they cloned a teammate's repo, or they ranprisma-next initthemselves 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. - Greenfield — new project, fresh database. User runs
prisma-next initthemselves.initseeds a starter contract with a sample model, so the path joins the first-touch orientation arc as soon as the database is initialised. - Brownfield-DB — existing database, no contract yet. Infer the contract from the database with
contract infer, sign the marker withdb sign, then write queries against one of the existing tables.
This skill does not cover migrating from another ORM (Drizzle, Prisma 6/7, Sequelize, TypeORM, Kysely, Knex, raw drivers). Those are separately-installable skills.
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
createprisma(or equivalent scaffold tool) and is asking what to do next. First-touch orientation path. - 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.
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
@prisma-next/migrate-from-<orm>-skill(separate). - User wants to wire
db.tsin a project that already has a contract →prisma-next-runtime. - User wants to integrate Prisma Next with a build tool (Vite plugin, Next.js, …) →
prisma-next-build.
Key Concepts
- Contract: the data model. Authored as
contract.prisma(PSL, the canonical surface) orcontract.ts(TypeScript builder). The framework reads it and emits two artefacts:contract.json(runtime IR) andcontract.d.ts(types). - Target: the backing store. Today:
postgresormongodb. Picked atinittime; baked into the@prisma-next/<target>façade the scaffold imports from. - Authoring mode: how you write the contract.
psl(Prisma Schema Language, default) ortypescript(programmatic builder, optionally paired with the Vite plugin for auto-emit duringvite dev— seeprisma-next-build). - Façade packages. The scaffold installs exactly one façade per target —
@prisma-next/postgres(or@prisma-next/mongo). User code imports from façade subpaths (@prisma-next/postgres/config,@prisma-next/postgres/runtime,@prisma-next/postgres/contract-builder). The façade bakes in the family / target / adapter / driver wiring; never reach past it. Seeprisma-next-contractfor the full list. db.ts: the runtime entry point. Lives next to the contract source atsrc/prisma/db.ts. Imports the contract artefacts and exports adbvalue the rest of the app uses.- Marker: a
pn_meta_markerrow in your database that records the contract hash. Lets PN detect drift between contract and live DB. Created bydb init(greenfield / first-touch orientation) ordb sign(brownfield).
Canonical on-disk layout
Every application that consumes Prisma Next uses the same shape:
<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/
├── snapshots/ ← content-addressed contract store, shared across spaces
│ └── <hex>/
│ ├── contract.json
│ └── contract.d.ts
└── app/ ← created on first `migration plan` / `db init`
├── refs/head.json
└── <timestamp>_<slug>/
├── migration.json
├── ops.json
└── migration.ts
Three things to internalise:
src/prisma/is the home for the contract — source + emitted artefacts +db.tsall colocated. The rest ofsrc/imports from./prisma/db(or../prisma/db, depending on file depth).migrations/app/— theapp/segment is the consuming application's space-id. Extensions you depend on get sibling directories undermigrations/(one per extension contract-space), but you don't write into those — only theapp/subtree is your migrations.prisma-next.config.tslives at the repo root, not undersrc/. Every command resolves paths relative to the config's directory.
Contributors building extension packages or aggregate-root monorepo packages use a different layout — src/contract.{prisma,ts} (no prisma/ subdir) + migrations/<timestamp>_<slug>/ (no app/ segment). That distinction is intentional; see prisma-next-contract for which path applies to you.
Heads up —
prisma-next initcurrently scaffolds the wrong layout. It writesprisma/contract.{prisma,ts}andprisma/db.tsat the repo root instead of undersrc/prisma/. Tracked as TML-2532. Until the fix lands, either pass--schema-path src/prisma/contract.prismatoinit, or move the scaffoldedprisma/directory intosrc/prisma/afterinitand update thecontractpath inprisma-next.config.tsto match. The canonical layout above is what the demo example uses and what the rest of the framework expects.
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 src/ (e.g. src/first-arc.ts) so the relative import resolves to one level deep:
// 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 [{ id: 1, email: 'alice@example.com' }], the project is wired end-to-end and the user has crossed from "I have a project" to "I'm building."
db.orm.<Model> is the default ORM lane — model-shaped, fully typed against the contract, lazily connects to the database on first use (it picks up DATABASE_URL from .env via the runtime's dotenv/config-loaded environment). The deeper prisma-next-queries skill covers the rest of the surface (filters, joins, transactions, the SQL builder, raw SQL, TypedSQL) when the user is ready.
Mongo target: the snippet above is SQL-target shape. On
@prisma-next/mongo,db.ormis keyed by the collection's storage name (@@map(...), or the lowercased model name if no@@map), so the same arc readsawait db.orm.users.create(...)/await db.orm.users.select('id', 'email').all()— notdb.orm.User. Full rule and rewrite recipe inprisma-next-queries§ MongoDB ORM addressing.
Prerequisites for the arc to work. All three paths leave these in place by the time you reach the arc:
prisma-next.config.tsexists at the repo root and declares the target + contract source (typicallysrc/prisma/contract.prismaorsrc/prisma/contract.ts).- The contract source exists at
src/prisma/contract.{prisma,ts}(a starter model frominit, or the inferred contract fromcontract infer, or whatever the bootstrap tool generated). src/prisma/db.tsexists and instantiates the runtime with the emitted contract.DATABASE_URLis set in.env(or wherever the runtime's config tells it to look).- The database has been initialised (
db init) or marker-signed (db sign), so the marker row exists and the schema matches the contract.
The three workflows below each describe how their path gets the user to that state. After that, the arc above is the same.
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 createprisma, by prisma-next init, by a teammate, however).
The 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.
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 <path>. 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."
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.
Step 1 — Read the project, name the contract
Before saying anything specific to the user, read:
prisma-next.config.tsat the repo root — what target (postgres/mongodb) is wired, whatcontract:path it declares, what extensions are installed.- The contract source the config declares (canonically
src/prisma/contract.prismaorsrc/prisma/contract.ts; a project that pre-dates TML-2532 may have it atprisma/contract.{prisma,ts}instead — check thecontractfield of the config) — what starter models, if any, exist. src/prisma/db.ts(next to the contract) — the runtime entry point..env/.env.example— isDATABASE_URLset, or only the example?- Optionally
pnpm prisma-next db verify— does the live DB match the contract?
Then say the contract path back to the user, with its role attached. Something like: "Your contract is at src/prisma/contract.prisma, and it currently declares a User 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.
Step 2 — Get the user's app connected and round-tripping
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.
DATABASE_URLnot set. Have the user set it in.env(not inprisma-next.config.ts— see Pitfall 5). Thenpnpm prisma-next db initto apply the current contract to that database and write the marker row. Now the app can connect.- Database is connectable but not yet aware of the contract (marker row missing;
db verifyreports drift). Runpnpm prisma-next db init. (db updateis 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.) - Contract is empty (bootstrap left the source blank). Add one model with two fields (e.g.
User { id, email }),pnpm prisma-next contract emit, thenpnpm prisma-next db init. Minimal — get the round-trip working, then extend.
The user encounters db init (and optionally db update, contract emit) here because they're the commands their current move requires. They learn what those commands are by using them.
Step 3 — Round-trip a row
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
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 →
prisma-next-contract. They'll touchcontract emitanddb update(ormigration plan+migrate) as part of that workflow. - 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.
Anti-patterns on this path
- Leading with a feature tour or capability inventory. The user asked what t
...
用户评价 (0)
发表评价
暂无评价
统计数据
用户评分
为此 Skill 评分