diff --git a/.gitignore b/.gitignore index 6cd1410a81..d974a2a963 100644 --- a/.gitignore +++ b/.gitignore @@ -40,3 +40,4 @@ next-env.d.ts # opensrc - source code for packages opensrc .env +prisma-next/ diff --git a/apps/docs/content/docs/(index)/meta.json b/apps/docs/content/docs/(index)/meta.json index 878f912fea..1737611716 100644 --- a/apps/docs/content/docs/(index)/meta.json +++ b/apps/docs/content/docs/(index)/meta.json @@ -5,6 +5,7 @@ "---Getting Started---", "index", "getting-started", + "next", "---Prisma ORM---", "...prisma-orm", "---Prisma Postgres---", diff --git a/apps/docs/content/docs/(index)/next/add-to-existing-project/meta.json b/apps/docs/content/docs/(index)/next/add-to-existing-project/meta.json new file mode 100644 index 0000000000..84c814653c --- /dev/null +++ b/apps/docs/content/docs/(index)/next/add-to-existing-project/meta.json @@ -0,0 +1,4 @@ +{ + "title": "Add to Existing Project", + "pages": ["postgresql", "mongodb"] +} diff --git a/apps/docs/content/docs/(index)/next/add-to-existing-project/mongodb.mdx b/apps/docs/content/docs/(index)/next/add-to-existing-project/mongodb.mdx new file mode 100644 index 0000000000..15dd0684c0 --- /dev/null +++ b/apps/docs/content/docs/(index)/next/add-to-existing-project/mongodb.mdx @@ -0,0 +1,186 @@ +--- +title: MongoDB +description: Add Prisma Next to an existing MongoDB project. +url: /next/add-to-existing-project/mongodb +metaTitle: Add Prisma Next to an existing MongoDB project +metaDescription: Add Prisma Next to an existing MongoDB project. +--- + +This guide shows how to add Prisma Next to a project that already uses MongoDB. You will initialize Prisma Next, describe the collections you want to work with, emit the generated artifacts, and run a couple of queries. + +:::warning[Prisma Next is still in development] + +Prisma Next is not ready for production yet. Prisma ORM 7 is still the recommended choice for production applications today. + +::: + +## Prerequisites + +Before you start, make sure you can already reach the MongoDB deployment this project uses today. + +- Node.js 24 or newer +- npm +- An existing MongoDB 6.0 or newer deployment + +For local development, use a replica set. MongoDB Atlas already gives you that. + +## 1. Make sure you can run the example script + +If your project already runs TypeScript scripts, you can skip this step. + +Otherwise, install the script tooling: + +```npm +npm install --save-dev tsx typescript +``` + +Later, `prisma-next init` will also add the Node.js types it needs and make sure the generated Prisma Next files can run as ES modules. If your project already declares `"type": "commonjs"`, Prisma Next leaves that choice alone and prints a warning so you can decide how to wire the generated helper into your app. + +## 2. Initialize Prisma Next + +From the root of your existing project, run: + +```npm +npx prisma-next init --write-env +``` + +When Prisma Next asks a few setup questions: + +- choose `MongoDB` +- choose `PSL` +- keep the default schema path, `prisma/contract.prisma` + +## 3. Set your database connection string + +Update `.env` with the connection string for the MongoDB deployment your app already uses: + +```text title=".env" +DATABASE_URL="mongodb://127.0.0.1:27017/my-app?replicaSet=rs0" +``` + +## 4. Describe the collections you want Prisma Next to know about + +This is the key adoption step for MongoDB, because you decide which part of the existing database Prisma Next should model first. + +PostgreSQL has `contract infer`. MongoDB does not, so this step is manual. + +Open `prisma/contract.prisma` and make it match the collections you want Prisma Next to query first. If your existing database already has `users` and `posts` collections with `email`, `name`, `title`, and `authorId`, the starter contract is already a useful first draft: + +```prisma title="prisma/contract.prisma" +// use prisma-next + +model User { + id ObjectId @id @map("_id") + email String @unique + name String? + posts Post[] + @@map("users") +} + +model Post { + id ObjectId @id @map("_id") + title String + content String? + author User @relation(fields: [authorId], references: [id]) + authorId ObjectId + @@map("posts") +} +``` + +You do not need to model every collection on day one. Start with the part of the database you want to read and write first. + +## 5. Emit the generated artifacts + +Once the contract looks right, this step turns it into the generated files the runtime and query APIs use. + +Run: + +```npm +npx prisma-next contract emit +``` + +This refreshes `prisma/contract.json` and `prisma/contract.d.ts` so the runtime and query APIs are aligned with the contract you just reviewed. + +## 6. Run a simple high-level query + +With the emitted artifacts in place, you can test the higher-level API first and confirm Prisma Next can read the existing collections. + +Create a `script.ts` file: + +```typescript title="script.ts" +import "dotenv/config"; +import { db } from "./prisma/db"; + +async function main() { + const user = await db.orm.users.where({ email: "existing@example.com" }).first(); + console.log(user); + + await db.close(); +} + +main().catch((error) => { + console.error(error); + process.exit(1); +}); +``` + +Run it: + +```npm +npx tsx script.ts +``` + +## 7. Run a simple low-level query + +After the ORM example, this step shows the lower-level MongoDB pipeline builder against the same existing collections. + +Replace `script.ts` with this version: + +```typescript title="script.ts" +import "dotenv/config"; +import { db } from "./prisma/db"; + +async function main() { + const runtime = await db.runtime(); + const plan = db.query + .from("users") + .match((fields) => fields.email.eq("existing@example.com")) + .project("email", "name") + .build(); + + const rows = await runtime.execute(plan); + console.log(rows); + + await db.close(); +} + +main().catch((error) => { + console.error(error); + process.exit(1); +}); +``` + +Run it again: + +```npm +npx tsx script.ts +``` + +## 8. Commands you will use next + +Once Prisma Next is in the project, these are the MongoDB commands to remember: + +```npm +npx prisma-next contract emit +``` + +Run this after you change `prisma/contract.prisma`. + +```npm +npx prisma-next migration plan --name add-user-bio +npx prisma-next migration apply +``` + +Use these when you want Prisma Next to manage a schema change for the collections in your contract. + +You do not need to create a migration just to start reading collections that already exist. Create a migration when you are ready for Prisma Next to own a change. diff --git a/apps/docs/content/docs/(index)/next/add-to-existing-project/postgresql.mdx b/apps/docs/content/docs/(index)/next/add-to-existing-project/postgresql.mdx new file mode 100644 index 0000000000..6db183be5e --- /dev/null +++ b/apps/docs/content/docs/(index)/next/add-to-existing-project/postgresql.mdx @@ -0,0 +1,190 @@ +--- +title: Postgres +description: Add Prisma Next to an existing PostgreSQL project. +url: /next/add-to-existing-project/postgresql +metaTitle: Add Prisma Next to an existing Postgres project +metaDescription: Add Prisma Next to an existing PostgreSQL project. +--- + +This guide shows how to add Prisma Next to a project that already uses PostgreSQL. You will initialize Prisma Next, infer a contract from the live schema, sign the database, and run a couple of queries. + +:::warning[Prisma Next is still in development] + +Prisma Next is not ready for production yet. Prisma ORM 7 is still the recommended choice for production applications today. + +::: + +## Prerequisites + +Before you start, make sure you can already reach the PostgreSQL database this project uses today. + +- Node.js 24 or newer +- npm +- An existing PostgreSQL database + +## 1. Make sure you can run the example script + +If your project already runs TypeScript scripts, you can skip this step. + +Otherwise, install the script tooling: + +```npm +npm install --save-dev tsx typescript +``` + +Later, `prisma-next init` will also add the Node.js types it needs and make sure the generated Prisma Next files can run as ES modules. If your project already declares `"type": "commonjs"`, Prisma Next leaves that choice alone and prints a warning so you can decide how to wire the generated helper into your app. + +## 2. Initialize Prisma Next + +From the root of your existing project, run: + +```npm +npx prisma-next init --write-env +``` + +When Prisma Next asks a few setup questions: + +- choose `PostgreSQL` +- choose `PSL` +- keep the default schema path, `prisma/contract.prisma` + +## 3. Set your database connection string + +Update `.env` with the connection string for the database your app already uses: + +```text title=".env" +DATABASE_URL="postgres://username:password@host:5432/database?sslmode=require" +``` + +## 4. Infer a starter contract from the live database + +This step gives you a starting contract by reading the schema that already exists in PostgreSQL. + +Run: + +```npm +npx prisma-next contract infer --output ./prisma/contract.prisma +``` + +This reads the live PostgreSQL schema and writes a first draft of `prisma/contract.prisma`. + +Open that file and review it before you go on. This is the moment to clean up model names, keep only the tables you want Prisma Next to know about first, and make the file easier to read. + +## 5. Emit the generated artifacts + +Once the contract looks right, this step turns it into the generated files the runtime and CLI use. + +After you are happy with the contract, run: + +```npm +npx prisma-next contract emit +``` + +This refreshes `prisma/contract.json` and `prisma/contract.d.ts` so the runtime and query APIs are aligned with the contract you just reviewed. + +## 6. Sign the database + +Record that the live database matches the emitted contract: + +```npm +npx prisma-next db sign +``` + +This step matters in two common cases: + +- the database has never been signed by Prisma Next before +- the database was signed earlier, but under an older contract hash + +## 7. Run a simple high-level query + +With the database signed, you can test the higher-level API first and confirm Prisma Next is reading the existing schema correctly. + +Create a `script.ts` file: + +```typescript title="script.ts" +import "dotenv/config"; +import { db } from "./prisma/db"; + +async function main() { + const runtime = await db.connect({ url: process.env.DATABASE_URL! }); + + const users = await db.orm.User + .select("id", "email", "name") + .take(2) + .all(); + + console.log(users); + + await runtime.close(); +} + +main().catch((error) => { + console.error(error); + process.exit(1); +}); +``` + +Run it: + +```npm +npx tsx script.ts +``` + +## 8. Run a simple low-level query + +After the ORM example, this step shows the lower-level SQL builder against the same existing schema. + +Replace `script.ts` with this version: + +```typescript title="script.ts" +import "dotenv/config"; +import { db } from "./prisma/db"; + +async function main() { + const runtime = await db.connect({ url: process.env.DATABASE_URL! }); + + const plan = db.sql.user + .select("id", "email", "name") + .limit(2) + .build(); + + const rows = await db.runtime().execute(plan); + console.log(rows); + + await runtime.close(); +} + +main().catch((error) => { + console.error(error); + process.exit(1); +}); +``` + +Run it again: + +```npm +npx tsx script.ts +``` + +## 9. Commands you will use next + +Once Prisma Next is in the project, these are the PostgreSQL commands to remember: + +```npm +npx prisma-next contract emit +``` + +Run this after you change `prisma/contract.prisma`. + +```npm +npx prisma-next db update +``` + +Use this when you want Prisma Next to update the live database directly so it matches the latest contract. + +```npm +npx prisma-next migration plan --name add-user-bio +npx prisma-next migration apply +``` + +Use these when you want checked-in migration packages instead of a direct database update. diff --git a/apps/docs/content/docs/(index)/next/create-prisma.mdx b/apps/docs/content/docs/(index)/next/create-prisma.mdx new file mode 100644 index 0000000000..903e92d836 --- /dev/null +++ b/apps/docs/content/docs/(index)/next/create-prisma.mdx @@ -0,0 +1,80 @@ +--- +title: create-prisma@next +description: Scaffold a new Prisma Next app with create-prisma@next. +url: /next/create-prisma +metaTitle: Scaffold a Prisma Next app with create-prisma@next +metaDescription: Use create-prisma@next to create a new Prisma Next app with templates, database scripts, and runtime helpers. +--- + +`create-prisma@next` creates a new Prisma Next project from an app template. It installs the Prisma Next packages and adds the files and scripts needed to start developing. + +:::warning[Prisma Next is still in development] + +Prisma Next is not ready for production yet. Prisma ORM 7 is still the recommended choice for production applications today. + +::: + +Use this when you want to start from a working app template. If you already have an app, use [Add Prisma Next to an existing PostgreSQL project](/next/add-to-existing-project/postgresql) or [Add Prisma Next to an existing MongoDB project](/next/add-to-existing-project/mongodb). + +## Create a project + +Run: + +```npm +npx create-prisma@next +``` + +Then choose a project name, app template, database provider, contract authoring style, and package manager. + +Generated Node.js projects expect Node.js 24 LTS or newer. + +## Skip the prompts + +Pass flags when you already know the project shape: + +```npm +npx create-prisma@next --name my-app --template next --provider postgres +``` + +Use `--name .` to create the project in the current directory. + +Useful flags: + +| Flag | What it does | +| --- | --- | +| `--template ` | Chooses the app template. | +| `--provider postgres\|mongodb` | Chooses the database provider. | +| `--authoring psl\|typescript` | Chooses the contract authoring style. | +| `--package-manager ` | Chooses `npm`, `pnpm`, `yarn`, `bun`, or `deno`. | +| `--database-url ` | Writes `DATABASE_URL` during setup. | +| `--no-install` | Scaffolds files without installing dependencies. | +| `--no-emit` | Skips `prisma-next contract emit`. | +| `--force` | Allows scaffolding into a non-empty directory. | + +## What gets created + +The generated project includes Prisma Next dependencies, a contract file, Prisma Next configuration, environment files, and package scripts for local development. + +Supported templates include Hono, Elysia, NestJS, Next.js, SvelteKit, Astro, Nuxt, TanStack Start, and Turborepo. + +## Start the app + +Review `DATABASE_URL` in `.env`, then initialize the database. + +For PostgreSQL: + +```npm +npm run db:init +npm run dev +``` + +For MongoDB: + +```npm +npm run db:up +npm run migration:plan -- --name init +npm run migration:apply +npm run dev +``` + +To add Prisma Next to an app you already have, use the [PostgreSQL existing-project guide](/next/add-to-existing-project/postgresql) or [MongoDB existing-project guide](/next/add-to-existing-project/mongodb). diff --git a/apps/docs/content/docs/(index)/next/index.mdx b/apps/docs/content/docs/(index)/next/index.mdx new file mode 100644 index 0000000000..54e4e477d5 --- /dev/null +++ b/apps/docs/content/docs/(index)/next/index.mdx @@ -0,0 +1,29 @@ +--- +title: Get started with Prisma Next +description: Start a new Prisma Next app or add Prisma Next to an existing project. +url: /next +metaTitle: Get started with Prisma Next +metaDescription: Start a Prisma Next project with create-prisma@next or add Prisma Next to an existing project. +badge: early-access +--- + +Prisma Next is the next foundation for Prisma ORM. Start here if you want to try the Prisma Next workflow. + +:::warning[Prisma Next is still in development] + +Prisma Next is not ready for production yet. Prisma ORM 7 is still the recommended choice for production applications today. + +::: + +## Start a new project + +- [Scaffold a Prisma Next app with create-prisma@next](/next/create-prisma) + +## Add Prisma Next to an existing project + +- [Add Prisma Next to an existing PostgreSQL project](/next/add-to-existing-project/postgresql) +- [Add Prisma Next to an existing MongoDB project](/next/add-to-existing-project/mongodb) + +## Learn the ORM workflow + +Read the [Prisma Next overview](/orm/next) to learn about contracts, emitted artifacts, runtime clients, query styles, and database change commands. diff --git a/apps/docs/content/docs/(index)/next/meta.json b/apps/docs/content/docs/(index)/next/meta.json new file mode 100644 index 0000000000..022d2ebb44 --- /dev/null +++ b/apps/docs/content/docs/(index)/next/meta.json @@ -0,0 +1,11 @@ +{ + "title": "Next", + "defaultOpen": true, + "root": true, + "pages": [ + "---Getting Started---", + "index", + "create-prisma", + "add-to-existing-project" + ] +} diff --git a/apps/docs/content/docs/cli/meta.json b/apps/docs/content/docs/cli/meta.json index 648b580a5d..e95c2953ad 100644 --- a/apps/docs/content/docs/cli/meta.json +++ b/apps/docs/content/docs/cli/meta.json @@ -6,6 +6,7 @@ "pages": [ "---Introduction---", "index", + "next", "---Standalone commands---", "init", diff --git a/apps/docs/content/docs/cli/next/configuration.mdx b/apps/docs/content/docs/cli/next/configuration.mdx new file mode 100644 index 0000000000..2559fb493f --- /dev/null +++ b/apps/docs/content/docs/cli/next/configuration.mdx @@ -0,0 +1,91 @@ +--- +title: CLI configuration +description: Configure Prisma Next CLI commands with prisma-next.config.ts and global flags. +url: /cli/next/configuration +metaTitle: Prisma Next CLI configuration +metaDescription: Learn how Prisma Next CLI commands find config, read database URLs, and format output. +--- + +Prisma Next CLI commands use `prisma-next.config.ts` as the project entrypoint for contract emission, database operations, and migrations. + +## Config file + +Commands that need project context read the Prisma Next config from your project. For PostgreSQL projects, use the Postgres config helper: + +```typescript title="prisma-next.config.ts" +import "dotenv/config"; +import { defineConfig } from "@prisma-next/postgres/config"; + +export default defineConfig({ + contract: "./prisma/contract.prisma", + db: { + connection: process.env["DATABASE_URL"]!, + }, +}); +``` + +For MongoDB projects, import `defineConfig` from `@prisma-next/mongo/config` instead. + +Pass `--config` when your config file is not in the default project location: + +```bash +prisma-next contract emit --config ./config/prisma-next.config.ts +``` + +## Emit-only config + +`contract emit` does not connect to a database, so the config can omit `db.connection`: + +```typescript title="prisma-next.config.ts" +import { defineConfig } from "@prisma-next/postgres/config"; + +export default defineConfig({ + contract: "./prisma/contract.prisma", +}); +``` + +Add `db.connection` before running commands such as `db verify`, `db sign`, `db init`, `db update`, `db schema`, `contract infer`, or `migration apply`. + +## Extension packs + +Add extension control descriptors to the config when your contract uses extension-provided types: + +```typescript title="prisma-next.config.ts" +import { defineConfig } from "@prisma-next/postgres/config"; +import pgvector from "@prisma-next/extension-pgvector/control"; + +export default defineConfig({ + contract: "./prisma/contract.prisma", + extensions: [pgvector], + db: { + connection: process.env["DATABASE_URL"]!, + }, +}); +``` + +Re-run `prisma-next contract emit` after changing extension packs, then update the matching runtime client. + +## Database URLs + +Database commands accept `--db `. If you omit it, Prisma Next can use the database connection from `prisma-next.config.ts`. + +```bash +prisma-next db verify --db "$DATABASE_URL" +``` + +## Environment variables + +| Variable | What it does | +| --- | --- | +| `DATABASE_URL` | Common place to store the database connection string used by config files and scripts. | +| `NO_COLOR=1` | Disables colored terminal output. | + +## Output modes + +Use the default text output when running commands locally. Use `--json` in CI or automation: + +```bash +prisma-next db verify --db "$DATABASE_URL" --json +``` + +Use `--no-interactive` for scripts that must never pause for user input. diff --git a/apps/docs/content/docs/cli/next/contract-emit.mdx b/apps/docs/content/docs/cli/next/contract-emit.mdx new file mode 100644 index 0000000000..7991b19c5c --- /dev/null +++ b/apps/docs/content/docs/cli/next/contract-emit.mdx @@ -0,0 +1,52 @@ +--- +title: prisma-next contract emit +description: Emit Prisma Next contract artifacts. +url: /cli/next/contract-emit +metaTitle: prisma-next contract emit +metaDescription: Learn how to emit contract.json and contract.d.ts for Prisma Next. +--- + +`prisma-next contract emit` reads your contract source and writes the generated artifacts used by the runtime, verification, and migration tooling. + +The command is offline. It does not need a database connection. + +## Usage + +```bash +prisma-next contract emit +``` + +## Options + +| Option | What it does | +| --- | --- | +| `--config ` | Reads a specific `prisma-next.config.ts` file. | +| `--json` | Prints a machine-readable result. | +| `-q`, `--quiet` | Suppresses nonessential output. | +| `-v`, `--verbose` | Prints more detail. | + +## What it creates + +The command emits: + +- `contract.json`, the canonical machine-readable contract +- `contract.d.ts`, the generated TypeScript contract declarations + +Do not edit these files by hand. Re-run `contract emit` after changing the contract source or extension pack list. + +## Examples + +```bash +prisma-next contract emit +prisma-next contract emit --config ./custom-config.ts +prisma-next contract emit --json +``` + +## Next steps + +After emitting, choose the database workflow: + +- use [`prisma-next db init`](/cli/next/db-init) for first-time bootstrap +- use [`prisma-next db update`](/cli/next/db-update) for direct reconciliation +- use [`prisma-next migration plan`](/cli/next/migration-plan) for checked-in migrations +- use [`prisma-next db verify`](/cli/next/db-verify) to check drift diff --git a/apps/docs/content/docs/cli/next/contract-infer.mdx b/apps/docs/content/docs/cli/next/contract-infer.mdx new file mode 100644 index 0000000000..c4e80b446a --- /dev/null +++ b/apps/docs/content/docs/cli/next/contract-infer.mdx @@ -0,0 +1,54 @@ +--- +title: prisma-next contract infer +description: Infer a starter contract from an existing database. +url: /cli/next/contract-infer +metaTitle: prisma-next contract infer +metaDescription: Learn how to infer a Prisma Next PSL contract from a live database schema. +--- + +`prisma-next contract infer` inspects a live database and writes a starter PSL contract. + +Use it when you are adding Prisma Next to an existing database and want an initial contract to review and edit. + +## Usage + +```bash +prisma-next contract infer --db "$DATABASE_URL" +``` + +## Options + +| Option | What it does | +| --- | --- | +| `--db ` | Connects to the database. | +| `--config ` | Reads a specific `prisma-next.config.ts` file. | +| `--output ` | Writes the inferred PSL contract to a specific path. | +| `--json` | Prints a machine-readable result. | + +## Examples + +```bash +prisma-next contract infer --db "$DATABASE_URL" +prisma-next contract infer --db "$DATABASE_URL" --output ./prisma/contract.prisma +prisma-next contract infer --db "$DATABASE_URL" --json +``` + +## What to review + +Inference gives you a starting point, not a finished design. Review: + +- model and field names +- relation names +- mapped database names +- defaults, indexes, and constraints +- extension-backed column types + +Then run: + +```bash +prisma-next contract emit +prisma-next db sign --db "$DATABASE_URL" +prisma-next db verify --db "$DATABASE_URL" +``` + +`db sign` is the handoff point where you record that the existing database matches the reviewed contract. diff --git a/apps/docs/content/docs/cli/next/db-init.mdx b/apps/docs/content/docs/cli/next/db-init.mdx new file mode 100644 index 0000000000..0129b726fa --- /dev/null +++ b/apps/docs/content/docs/cli/next/db-init.mdx @@ -0,0 +1,52 @@ +--- +title: prisma-next db init +description: Initialize a database from the current Prisma Next contract. +url: /cli/next/db-init +metaTitle: prisma-next db init +metaDescription: Learn how to create missing database structures from a Prisma Next contract and sign the database. +--- + +`prisma-next db init` creates missing database structures from the current emitted contract and signs the database. + +Use it for first setup of a database that should match your current contract. + +## Usage + +```bash +prisma-next db init --db "$DATABASE_URL" +``` + +## Options + +| Option | What it does | +| --- | --- | +| `--db ` | Connects to the database. | +| `--config ` | Reads a specific `prisma-next.config.ts` file. | +| `--dry-run` | Shows planned operations without applying them. | +| `--json` | Prints a machine-readable result. | + +## Behavior + +`db init` is intended for bootstrap work. It creates missing structures needed by the contract and writes the contract marker after the database matches. + +Run a dry run first when you are not working with a disposable local database: + +```bash +prisma-next db init --db "$DATABASE_URL" --dry-run +``` + +## Examples + +```bash +prisma-next contract emit +prisma-next db init --db "$DATABASE_URL" +prisma-next db verify --db "$DATABASE_URL" +``` + +```bash +prisma-next db init --db "$DATABASE_URL" --dry-run --json +``` + +## When to use db update instead + +Use [`prisma-next db update`](/cli/next/db-update) when the database already exists and you want Prisma Next to reconcile it with a changed contract. Use [`prisma-next migration plan`](/cli/next/migration-plan) when you want a reviewable migration package in version control. diff --git a/apps/docs/content/docs/cli/next/db-schema.mdx b/apps/docs/content/docs/cli/next/db-schema.mdx new file mode 100644 index 0000000000..668f5fc4cd --- /dev/null +++ b/apps/docs/content/docs/cli/next/db-schema.mdx @@ -0,0 +1,36 @@ +--- +title: prisma-next db schema +description: Inspect a live database schema. +url: /cli/next/db-schema +metaTitle: prisma-next db schema +metaDescription: Learn how to inspect a live database schema with Prisma Next. +--- + +`prisma-next db schema` reads the live database schema and prints it. The command is read-only. + +Use it when you need to inspect what Prisma Next sees in the database before inferring, signing, updating, or debugging drift. + +## Usage + +```bash +prisma-next db schema --db "$DATABASE_URL" +``` + +## Options + +| Option | What it does | +| --- | --- | +| `--db ` | Connects to the database. | +| `--config ` | Reads a specific `prisma-next.config.ts` file. | +| `--json` | Prints machine-readable schema output. | + +## Examples + +```bash +prisma-next db schema --db "$DATABASE_URL" +prisma-next db schema --db "$DATABASE_URL" --json > schema.json +``` + +## Related commands + +Use [`prisma-next contract infer`](/cli/next/contract-infer) when you want to turn a live schema into a starter PSL contract. Use [`prisma-next db verify`](/cli/next/db-verify) when you want to compare the live schema with the emitted contract. diff --git a/apps/docs/content/docs/cli/next/db-sign.mdx b/apps/docs/content/docs/cli/next/db-sign.mdx new file mode 100644 index 0000000000..7e265dcdc2 --- /dev/null +++ b/apps/docs/content/docs/cli/next/db-sign.mdx @@ -0,0 +1,43 @@ +--- +title: prisma-next db sign +description: Sign a database with the current Prisma Next contract. +url: /cli/next/db-sign +metaTitle: prisma-next db sign +metaDescription: Learn how to sign a database once it matches the current Prisma Next contract. +--- + +`prisma-next db sign` verifies that the live database satisfies the emitted contract and writes the database signature. + +Use it after importing or inferring an existing schema, or after a deployment flow that already applied the required database changes. + +## Usage + +```bash +prisma-next db sign --db "$DATABASE_URL" +``` + +## Options + +| Option | What it does | +| --- | --- | +| `--db ` | Connects to the database. | +| `--config ` | Reads a specific `prisma-next.config.ts` file. | +| `--json` | Prints a machine-readable result. | + +## Example + +```bash +prisma-next contract emit +prisma-next db sign --db "$DATABASE_URL" +prisma-next db verify --db "$DATABASE_URL" +``` + +## When to use it + +Use `db sign` only after you believe the live database already matches the emitted contract. It is common after: + +- `contract infer` for a brownfield database +- a manually reviewed migration flow +- a database restore that you need to mark as matching the current contract + +Do not use `db sign` to hide drift. If verification fails, fix the contract or database first. diff --git a/apps/docs/content/docs/cli/next/db-update.mdx b/apps/docs/content/docs/cli/next/db-update.mdx new file mode 100644 index 0000000000..68d622f3f6 --- /dev/null +++ b/apps/docs/content/docs/cli/next/db-update.mdx @@ -0,0 +1,44 @@ +--- +title: prisma-next db update +description: Update a database to match the current Prisma Next contract. +url: /cli/next/db-update +metaTitle: prisma-next db update +metaDescription: Learn how to reconcile a database with the current Prisma Next contract. +--- + +`prisma-next db update` compares the live database with the emitted contract and applies the required changes. + +Use it for direct reconciliation when you do not need a checked-in migration package. + +## Usage + +```bash +prisma-next db update --db "$DATABASE_URL" +``` + +## Options + +| Option | What it does | +| --- | --- | +| `--db ` | Connects to the database. | +| `--config ` | Reads a specific `prisma-next.config.ts` file. | +| `--dry-run` | Shows planned operations without applying them. | +| `-y`, `--yes` | Confirms prompts where the command supports confirmation. | +| `--json` | Prints a machine-readable result. | + +## Recommended flow + +```bash +prisma-next contract emit +prisma-next db update --db "$DATABASE_URL" --dry-run +prisma-next db update --db "$DATABASE_URL" +prisma-next db verify --db "$DATABASE_URL" +``` + +Use `--dry-run` before applying changes in shared environments. + +## When to use migrations instead + +For reviewable database changes in version control, use [`prisma-next migration plan`](/cli/next/migration-plan) and [`prisma-next migration apply`](/cli/next/migration-apply). + +Use `db update` for local development, preview environments, and workflows where direct reconciliation is acceptable. diff --git a/apps/docs/content/docs/cli/next/db-verify.mdx b/apps/docs/content/docs/cli/next/db-verify.mdx new file mode 100644 index 0000000000..dca6a1d1ed --- /dev/null +++ b/apps/docs/content/docs/cli/next/db-verify.mdx @@ -0,0 +1,54 @@ +--- +title: prisma-next db verify +description: Verify a database against the current Prisma Next contract. +url: /cli/next/db-verify +metaTitle: prisma-next db verify +metaDescription: Learn how to verify that a database marker and live schema match the Prisma Next contract. +--- + +`prisma-next db verify` checks whether the database marker and live schema match your emitted contract. + +Use it in CI and deployment checks before application code that depends on a contract reaches users. + +## Usage + +```bash +prisma-next db verify --db "$DATABASE_URL" +``` + +## Options + +| Option | What it does | +| --- | --- | +| `--db ` | Connects to the database. | +| `--config ` | Reads a specific `prisma-next.config.ts` file. | +| `--marker-only` | Checks only the database marker. | +| `--schema-only` | Checks only whether the live schema satisfies the contract. | +| `--strict` | Fails if the database includes schema elements not present in the contract. | +| `--json` | Prints machine-readable output. | + +## Examples + +```bash +prisma-next db verify --db "$DATABASE_URL" +prisma-next db verify --db "$DATABASE_URL" --strict +prisma-next db verify --db "$DATABASE_URL" --schema-only +prisma-next db verify --db "$DATABASE_URL" --marker-only +``` + +Use JSON output in automation: + +```bash +prisma-next db verify --db "$DATABASE_URL" --json +``` + +## What failures mean + +| Failure | Meaning | +| --- | --- | +| Marker mismatch | The database was not signed for the emitted contract, or the contract changed after signing. | +| Schema mismatch | The live database does not satisfy the emitted contract. | +| Strict mismatch | The database has extra schema elements not present in the contract. | +| Extension mismatch | The contract requires an extension that is not wired in the config. | + +Fix the database or contract, emit again if needed, then verify again. diff --git a/apps/docs/content/docs/cli/next/index.mdx b/apps/docs/content/docs/cli/next/index.mdx new file mode 100644 index 0000000000..bcd24d33db --- /dev/null +++ b/apps/docs/content/docs/cli/next/index.mdx @@ -0,0 +1,97 @@ +--- +title: Prisma Next CLI +description: Reference for the Prisma Next command line interface. +url: /cli/next +metaTitle: Prisma Next CLI reference +metaDescription: Learn the Prisma Next CLI commands for contracts, databases, and migrations. +badge: early-access +--- + +The Prisma Next CLI is exposed as `prisma-next`. It reads your `prisma-next.config.ts`, emits contract artifacts, verifies databases, and manages Prisma Next migrations. + +:::warning[Prisma Next is still in development] + +Prisma Next is not ready for production yet. Prisma ORM 7 is still the recommended choice for production applications today. + +::: + +Install the CLI package in a Prisma Next project: + +```bash +npm install -D prisma-next +``` + +Then run commands with your package manager: + +```bash +npx prisma-next help +npx prisma-next contract emit +``` + +For a full app scaffold, use [`create-prisma@next`](/next/create-prisma). That path creates the project files and package scripts for you. This CLI reference is for the lower-level commands those scripts call. + +## Common workflows + +Start an existing project: + +```bash +prisma-next init --target postgres --authoring psl +prisma-next contract emit +prisma-next db init --db "$DATABASE_URL" +``` + +Adopt an existing database: + +```bash +prisma-next contract infer --db "$DATABASE_URL" --output ./prisma/contract.prisma +prisma-next contract emit +prisma-next db sign --db "$DATABASE_URL" +prisma-next db verify --db "$DATABASE_URL" +``` + +Use checked-in migrations: + +```bash +prisma-next contract emit +prisma-next migration plan --name add-users +prisma-next migration status --db "$DATABASE_URL" +prisma-next migration apply --db "$DATABASE_URL" +prisma-next db verify --db "$DATABASE_URL" +``` + +## Command groups + +| Command | Purpose | +| --- | --- | +| [`prisma-next init`](/cli/next/init) | Add Prisma Next files to a project. | +| [`prisma-next contract emit`](/cli/next/contract-emit) | Emit `contract.json` and `contract.d.ts` from your contract source. | +| [`prisma-next contract infer`](/cli/next/contract-infer) | Infer a starter PSL contract from an existing database. | +| [`prisma-next db init`](/cli/next/db-init) | Create missing database structures from the current contract and sign the database. | +| [`prisma-next db update`](/cli/next/db-update) | Reconcile an existing database with the current contract. | +| [`prisma-next db schema`](/cli/next/db-schema) | Inspect the live database schema. | +| [`prisma-next db sign`](/cli/next/db-sign) | Record that a database matches the current contract. | +| [`prisma-next db verify`](/cli/next/db-verify) | Check that a database still matches the current contract. | +| [`prisma-next migration plan`](/cli/next/migration-plan) | Create an on-disk migration package from contract changes. | +| [`prisma-next migration new`](/cli/next/migration-new) | Scaffold a migration package for manual authoring. | +| [`prisma-next migration apply`](/cli/next/migration-apply) | Apply pending on-disk migrations. | +| [`prisma-next migration status`](/cli/next/migration-status) | Show migration history and applied state. | +| [`prisma-next migration show`](/cli/next/migration-show) | Inspect a migration package. | +| [`prisma-next migration ref`](/cli/next/migration-ref) | Manage named migration refs. | + +## Global flags + +Most Prisma Next commands accept these flags. + +| Flag | What it does | +| --- | --- | +| `--json` | Print machine-readable output. Use this in CI and scripts. | +| `-q`, `--quiet` | Suppress nonessential output. | +| `-v`, `--verbose` | Print more detail. | +| `--trace` | Include stack traces for failures. | +| `--color` | Force colored output. | +| `--no-color` | Disable colored output. | +| `--interactive` | Allow prompts. | +| `--no-interactive` | Disable prompts. | +| `-y`, `--yes` | Accept prompts where the command supports confirmation. | + +Use `prisma-next --help` when you need the exact command help from the installed preview version. diff --git a/apps/docs/content/docs/cli/next/init.mdx b/apps/docs/content/docs/cli/next/init.mdx new file mode 100644 index 0000000000..07f76eb742 --- /dev/null +++ b/apps/docs/content/docs/cli/next/init.mdx @@ -0,0 +1,69 @@ +--- +title: prisma-next init +description: Initialize Prisma Next files in a project. +url: /cli/next/init +metaTitle: prisma-next init +metaDescription: Learn how to initialize Prisma Next files in an existing project. +--- + +`prisma-next init` scaffolds the Prisma Next config, contract source, and runtime files inside an existing project. + +Use [`create-prisma@next`](/next/create-prisma) when you want a complete new application template. Use `prisma-next init` when you already have a project and want to add the lower-level Prisma Next files. + +## Usage + +```bash +prisma-next init +``` + +Run it non-interactively: + +```bash +prisma-next init --yes --target postgres --authoring psl +``` + +## Options + +| Option | What it does | +| --- | --- | +| `--target ` | Sets the database target. Use `postgres` or `mongodb`. | +| `--authoring