@prisma-next/cli 0.7.0 → 0.8.0-dev.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (63) hide show
  1. package/dist/cli.mjs +10 -9
  2. package/dist/cli.mjs.map +1 -1
  3. package/dist/{client-BCnP7cHo.mjs → client-4d26awB-.mjs} +8 -5
  4. package/dist/client-4d26awB-.mjs.map +1 -0
  5. package/dist/commands/contract-emit.mjs +1 -1
  6. package/dist/commands/contract-infer.mjs +1 -1
  7. package/dist/commands/db-init.mjs +2 -2
  8. package/dist/commands/db-schema.mjs +1 -1
  9. package/dist/commands/db-sign.mjs +1 -1
  10. package/dist/commands/db-update.mjs +2 -2
  11. package/dist/commands/db-verify.mjs +1 -1
  12. package/dist/commands/migration-apply.mjs +1 -1
  13. package/dist/commands/migration-plan.d.mts.map +1 -1
  14. package/dist/commands/migration-plan.mjs +1 -1
  15. package/dist/commands/migration-show.mjs +1 -1
  16. package/dist/commands/migration-status.mjs +1 -1
  17. package/dist/{contract-emit-9DBda5Ou.mjs → contract-emit-BhKR-D9Y.mjs} +2 -2
  18. package/dist/{contract-emit-9DBda5Ou.mjs.map → contract-emit-BhKR-D9Y.mjs.map} +1 -1
  19. package/dist/{contract-emit-B77TsJqf.mjs → contract-emit-DLc5GYbr.mjs} +6 -2
  20. package/dist/contract-emit-DLc5GYbr.mjs.map +1 -0
  21. package/dist/{contract-infer-ByxhPjpW.mjs → contract-infer-Bnla2kuK.mjs} +2 -2
  22. package/dist/{contract-infer-ByxhPjpW.mjs.map → contract-infer-Bnla2kuK.mjs.map} +1 -1
  23. package/dist/{db-verify-Czm5T-J4.mjs → db-verify-DitNxDiE.mjs} +2 -2
  24. package/dist/{db-verify-Czm5T-J4.mjs.map → db-verify-DitNxDiE.mjs.map} +1 -1
  25. package/dist/exports/control-api.d.mts.map +1 -1
  26. package/dist/exports/control-api.mjs +2 -2
  27. package/dist/exports/index.mjs +1 -1
  28. package/dist/exports/init-output.mjs +1 -1
  29. package/dist/{init-BRKnARU6.mjs → init-CVBXQidr.mjs} +342 -207
  30. package/dist/init-CVBXQidr.mjs.map +1 -0
  31. package/dist/{inspect-live-schema-DxdBd4Er.mjs → inspect-live-schema-CyzAzPzF.mjs} +2 -2
  32. package/dist/{inspect-live-schema-DxdBd4Er.mjs.map → inspect-live-schema-CyzAzPzF.mjs.map} +1 -1
  33. package/dist/{migration-command-scaffold-BdV8JYXV.mjs → migration-command-scaffold-Jp1rosw8.mjs} +2 -2
  34. package/dist/{migration-command-scaffold-BdV8JYXV.mjs.map → migration-command-scaffold-Jp1rosw8.mjs.map} +1 -1
  35. package/dist/{migration-plan-mRu5K81L.mjs → migration-plan-q1pPoOCf.mjs} +9 -3
  36. package/dist/migration-plan-q1pPoOCf.mjs.map +1 -0
  37. package/dist/{migration-status-By9G5p2H.mjs → migration-status-Do4Ei0i_.mjs} +2 -2
  38. package/dist/{migration-status-By9G5p2H.mjs.map → migration-status-Do4Ei0i_.mjs.map} +1 -1
  39. package/dist/{output-B16Kefzx.mjs → output-nBJ6NvsE.mjs} +12 -11
  40. package/dist/{output-B16Kefzx.mjs.map → output-nBJ6NvsE.mjs.map} +1 -1
  41. package/package.json +19 -19
  42. package/src/commands/init/agent-skill-install.ts +130 -0
  43. package/src/commands/init/errors.ts +32 -0
  44. package/src/commands/init/exit-codes.ts +10 -0
  45. package/src/commands/init/index.ts +9 -1
  46. package/src/commands/init/init.ts +74 -7
  47. package/src/commands/init/inputs.ts +23 -0
  48. package/src/commands/init/output.ts +22 -17
  49. package/src/commands/init/templates/code-templates.ts +4 -1
  50. package/src/commands/migration-plan.ts +12 -1
  51. package/src/control-api/client.ts +11 -4
  52. package/src/control-api/operations/contract-emit.ts +13 -1
  53. package/src/control-api/operations/db-verify.ts +1 -1
  54. package/src/utils/contract-space-seed-phase.ts +1 -1
  55. package/dist/agent-skill-mongo.md +0 -138
  56. package/dist/agent-skill-postgres.md +0 -106
  57. package/dist/client-BCnP7cHo.mjs.map +0 -1
  58. package/dist/contract-emit-B77TsJqf.mjs.map +0 -1
  59. package/dist/init-BRKnARU6.mjs.map +0 -1
  60. package/dist/migration-plan-mRu5K81L.mjs.map +0 -1
  61. package/src/commands/init/templates/agent-skill-mongo.md +0 -138
  62. package/src/commands/init/templates/agent-skill-postgres.md +0 -106
  63. package/src/commands/init/templates/agent-skill.ts +0 -41
@@ -1,138 +0,0 @@
1
- # Prisma Next — project skill
2
-
3
- This project uses **Prisma Next** with **MongoDB** via `@prisma-next/mongo`. Prisma Next lets the user define data models in a contract file and query them with a fully typed ORM.
4
-
5
- ## Files
6
-
7
- - **Contract**: `{{schemaPath}}` ({{authoringLabel}} authoring) — the user's data models. Edit this to add or change models.
8
- - **Config**: `prisma-next.config.ts` — tells the CLI where the contract is and how to connect to the database. Loads `.env` via `dotenv/config`.
9
- - **Database client**: `{{schemaDir}}/db.ts` — `import { db } from '{{dbImportPath}}'`. This is the entry point for all queries.
10
- - **Generated files** (do not edit by hand):
11
- - `{{schemaDir}}/contract.json` — compiled contract, used at runtime
12
- - `{{schemaDir}}/contract.d.ts` — TypeScript types for the contract, used for autocomplete and type checking
13
-
14
- ## Commands
15
-
16
- - `{{pkgRun}} contract emit` — regenerate `contract.json` and `contract.d.ts` after changing the contract
17
- - `{{pkgRun}} db init` — bootstrap a database to match the contract (creates collections, indexes, constraints). Additive only — won't drop existing structures.
18
- - `{{pkgRun}} db update` — update the database to match the current contract. Prompts for confirmation on destructive changes. Use `--dry-run` to preview.
19
- - `{{pkgRun}} migration plan` — create a new migration from contract changes (offline, no database needed). Use `--name <slug>` to name it.
20
- - `{{pkgRun}} migration apply` — apply pending migrations to the database
21
- - `{{pkgRun}} migration status` — show which migrations are applied and which are pending
22
- - `{{pkgRun}} migration show <name>` — show details of a specific migration
23
-
24
- ## How to write queries
25
-
26
- Use the ORM (`db.orm`). Each root accessor is the lowercased plural form emitted by `prisma-next contract emit` (typically the `@@map`-ped collection name) — for `model User { @@map("users") }` use `db.orm.users`, for `model Post { @@map("posts") }` use `db.orm.posts`. The Mongo facade has no raw-SQL surface. Two escape hatches exist for cases the ORM can't express; both are covered under "Escape hatches" below.
27
-
28
- ```typescript
29
- import { db } from '{{dbImportPath}}';
30
-
31
- // Find one record
32
- const user = await db.orm.users
33
- .where({ email: 'alice@example.com' })
34
- .first();
35
- // Returns { _id: ObjectId; email: string; ... } | null
36
-
37
- // Find multiple records — `.all()` returns an AsyncIterableResult, consume
38
- // either as an async iterable (below) or by `await`ing it to materialise an Array.
39
- for await (const user of db.orm.users
40
- .select('_id', 'email')
41
- .take(10)
42
- .all()) {
43
- // Each `user` is { _id: ObjectId; email: string }
44
- }
45
- // Returns AsyncIterableResult<{ _id: ObjectId; email: string }>
46
-
47
- // Filter, order, limit
48
- const recentPosts = await db.orm.posts
49
- .where({ authorId: userId })
50
- .orderBy({ createdAt: -1 })
51
- .select('_id', 'title', 'createdAt')
52
- .take(50)
53
- .all();
54
-
55
- // Include relations (reference relations only — embedded relations come back automatically)
56
- const usersWithPosts = await db.orm.users
57
- .select('_id', 'email')
58
- .include('posts')
59
- .take(10)
60
- .all();
61
- ```
62
-
63
- ### Key ORM methods
64
-
65
- - `.where({ field: value, ... })` — filter records by an equality object. Pass a raw filter expression for `$gt`/`$in`/`$regex` etc.
66
- - `.select('field1', 'field2', ...)` — pick which fields to return
67
- - `.orderBy({ field: 1 | -1 })` — sort results (1 = ascending, -1 = descending)
68
- - `.take(n)` / `.skip(n)` — limit and offset
69
- - `.all()` — execute and return all matching records as an `AsyncIterableResult`
70
- - `.first()` — execute with limit 1 and return the first matching row, or `null`
71
- - `.include('relationName')` — eager-load a reference relation (`$lookup`); embedded relations are already part of the row
72
- - `.variant('VariantName')` — narrow a polymorphic collection to a discriminator value
73
-
74
- ## Escape hatches
75
-
76
- The ORM covers the common cases. When you genuinely need something it can't express, prefer these — in order — over reaching for `db.runtime()` (which is an internal executor surface, not a `mongodb`-driver handle):
77
-
78
- 1. **Typed raw aggregations — `db.query`.** The facade exposes a `db.query` builder that runs aggregation pipelines through the same runtime + middleware + codec stack as `db.orm`, so results stay typed against the contract. Use this for `$lookup`/`$facet`/`$graphLookup`/window-function pipelines that the ORM doesn't surface.
79
-
80
- 2. **Direct `mongodb` driver control — `mongoClient` binding.** If you need a raw `MongoClient` (e.g. for transactions, change streams, sessions, or a driver feature Prisma Next doesn't expose), construct one yourself and pass it to `mongo({ mongoClient, dbName, contractJson })`. Your code keeps the `MongoClient` reference and uses it directly, while the same `db` object still gives you the typed ORM surface:
81
-
82
- ```typescript
83
- import { MongoClient } from 'mongodb';
84
- import mongo from '@prisma-next/mongo/runtime';
85
- import type { Contract } from './contract.d';
86
- import contractJson from './contract.json' with { type: 'json' };
87
-
88
- const client = new MongoClient(process.env['DATABASE_URL']!);
89
- await client.connect();
90
-
91
- export const db = mongo<Contract>({ contractJson, mongoClient: client, dbName: 'mydb' });
92
-
93
- const session = client.startSession();
94
- try {
95
- await session.withTransaction(async () => {
96
- await db.orm.users.createAll([{ /* ... */ }]);
97
- });
98
- } finally {
99
- await session.endSession();
100
- }
101
- ```
102
-
103
- ## Rules
104
-
105
- - **Never hand-edit** `contract.json` or `contract.d.ts`. Always regenerate them with `contract emit`.
106
- - **Always emit after contract changes.** When you modify `{{schemaPath}}`, run `{{pkgRun}} contract emit` before writing any code that depends on the new or changed models.
107
- - **Don't restructure `db.ts`.** It's scaffolded by init and works as-is. `db` connects lazily on the first query — there is no `db.connect(...)` step.
108
- - **Root accessors are emitter-driven.** Use the lowercased plural collection name (e.g. `db.orm.users`, `db.orm.posts`) — not the PascalCase model name. Re-run `{{pkgRun}} contract emit` if a new model's accessor isn't appearing on `db.orm`.
109
- - **Connection string** is `DATABASE_URL` in `.env`. If the user reports connection errors, check this value and the `.env` file.
110
- - **Transactions and change streams** require a MongoDB **replica set**. The Mongo facade does not yet expose `db.transaction(...)` — for now, use the `mongoClient` escape hatch above to drive transactions/sessions directly. See the quick reference for dev-environment options; the typed transaction API is tracked under [TML-2313](https://linear.app/prisma-company/issue/TML-2313/mongo-dev-replica-set-story-is-missing-transactions-change-streams).
111
- - **Don't reach for `db.runtime()`** as an escape hatch. It returns the internal executor (`MongoRuntime`), not a `mongodb` `MongoClient` or `Db`. Use `db.query` for raw aggregations and the `mongoClient` binding for direct driver control.
112
-
113
- ## Workflow for common tasks
114
-
115
- **User wants to add a new model or field:**
116
- 1. Edit `{{schemaPath}}`
117
- 2. Run `{{pkgRun}} contract emit`
118
- 3. Write query code using `db.orm.<collection>` (lowercased plural, see Rules above)
119
-
120
- **User wants to query data:**
121
- 1. Import `db` from `{{dbImportPath}}`
122
- 2. Use `db.orm.<collection>` with `.where()`, `.select()`, `.all()`, `.first()`, etc.
123
-
124
- **User wants to set up or change the database connection:**
125
- 1. Edit `DATABASE_URL` in `.env`
126
- 2. The config file (`prisma-next.config.ts`) reads it automatically via `dotenv/config`
127
-
128
- **User wants to set up the database for the first time:**
129
- 1. Run `{{pkgRun}} db init`
130
-
131
- **User wants to update the database after changing the contract:**
132
- 1. Quick path: `{{pkgRun}} db update` — compares the database to the contract and applies changes directly
133
- 2. Migration path (for production workflows):
134
- - `{{pkgRun}} migration plan --name describe-the-change` — creates a migration
135
- - `{{pkgRun}} migration apply` — applies pending migrations
136
-
137
- **User wants to check what migrations need to be applied:**
138
- 1. Run `{{pkgRun}} migration status`
@@ -1,106 +0,0 @@
1
- # Prisma Next — project skill
2
-
3
- This project uses **Prisma Next** with **PostgreSQL** via `@prisma-next/postgres`. Prisma Next lets the user define data models in a contract file and query them with a fully typed ORM.
4
-
5
- ## Files
6
-
7
- - **Contract**: `{{schemaPath}}` ({{authoringLabel}} authoring) — the user's data models. Edit this to add or change models.
8
- - **Config**: `prisma-next.config.ts` — tells the CLI where the contract is and how to connect to the database. Loads `.env` via `dotenv/config`.
9
- - **Database client**: `{{schemaDir}}/db.ts` — `import { db } from '{{dbImportPath}}'`. This is the entry point for all queries.
10
- - **Generated files** (do not edit by hand):
11
- - `{{schemaDir}}/contract.json` — compiled contract, used at runtime
12
- - `{{schemaDir}}/contract.d.ts` — TypeScript types for the contract, used for autocomplete and type checking
13
-
14
- ## Commands
15
-
16
- - `{{pkgRun}} contract emit` — regenerate `contract.json` and `contract.d.ts` after changing the contract
17
- - `{{pkgRun}} db init` — bootstrap a database to match the contract (creates tables, indexes, constraints). Additive only — won't drop existing structures.
18
- - `{{pkgRun}} db update` — update the database to match the current contract. Prompts for confirmation on destructive changes. Use `--dry-run` to preview.
19
- - `{{pkgRun}} migration plan` — create a new migration from contract changes (offline, no database needed). Use `--name <slug>` to name it.
20
- - `{{pkgRun}} migration apply` — apply pending migrations to the database
21
- - `{{pkgRun}} migration status` — show which migrations are applied and which are pending
22
- - `{{pkgRun}} migration show <name>` — show details of a specific migration
23
-
24
- ## How to write queries
25
-
26
- Always use the ORM (`db.orm`). Only fall back to `db.sql` if the user explicitly asks for raw SQL or the ORM doesn't support the operation.
27
-
28
- ```typescript
29
- import { db } from '{{dbImportPath}}';
30
-
31
- // Find one record
32
- const user = await db.orm.User
33
- .where(user => user.email.eq('alice@example.com'))
34
- .first();
35
- // Returns { id: number; email: string; ... } | null
36
-
37
- // Find multiple records
38
- const users = await db.orm.User
39
- .select('id', 'email')
40
- .take(10)
41
- .all();
42
- // Returns Array<{ id: number; email: string }>
43
-
44
- // Filter, order, limit
45
- const recentPosts = await db.orm.Post
46
- .where(post => post.authorId.eq(userId))
47
- .orderBy(post => post.createdAt.desc())
48
- .select('id', 'title', 'createdAt')
49
- .take(50)
50
- .all();
51
-
52
- // Include relations
53
- const usersWithPosts = await db.orm.User
54
- .select('id', 'email')
55
- .include('posts', post =>
56
- post.select('id', 'title').orderBy(p => p.createdAt.desc()).take(5)
57
- )
58
- .take(10)
59
- .all();
60
- ```
61
-
62
- ### Key ORM methods
63
-
64
- - `.where(predicate)` — filter records. Predicate receives a model accessor with `.eq()`, `.neq()`, `.ilike()`, `.lt()`, `.gt()`, etc.
65
- - `.select('field1', 'field2', ...)` — pick which fields to return
66
- - `.orderBy(accessor => accessor.field.asc()` or `.desc())` — sort results
67
- - `.take(n)` — limit number of results
68
- - `.all()` — execute and return all matching records as an array
69
- - `.first()` — execute and return the first matching record, or `null`
70
- - `.first({ id: value })` — find a single record by primary key, or `null`
71
- - `.include('relation', builder => ...)` — eager-load a relation
72
-
73
- ## Rules
74
-
75
- - **Never hand-edit** `contract.json` or `contract.d.ts`. Always regenerate them with `contract emit`.
76
- - **Always emit after contract changes.** When you modify `{{schemaPath}}`, run `{{pkgRun}} contract emit` before writing any code that depends on the new or changed models.
77
- - **Don't restructure `db.ts`.** It's scaffolded by init and works as-is.
78
- - **Use `db.orm` for queries**, not `db.sql`. The ORM is the primary query surface.
79
- - **Connection string** is `DATABASE_URL` in `.env`. If the user reports connection errors, check this value and the `.env` file.
80
-
81
- ## Workflow for common tasks
82
-
83
- **User wants to add a new model or field:**
84
- 1. Edit `{{schemaPath}}`
85
- 2. Run `{{pkgRun}} contract emit`
86
- 3. Write query code using `db.orm.ModelName`
87
-
88
- **User wants to query data:**
89
- 1. Import `db` from `{{dbImportPath}}`
90
- 2. Use `db.orm.ModelName` with `.where()`, `.select()`, `.all()`, `.first()`, etc.
91
-
92
- **User wants to set up or change the database connection:**
93
- 1. Edit `DATABASE_URL` in `.env`
94
- 2. The config file (`prisma-next.config.ts`) reads it automatically via `dotenv/config`
95
-
96
- **User wants to set up the database for the first time:**
97
- 1. Run `{{pkgRun}} db init`
98
-
99
- **User wants to update the database after changing the contract:**
100
- 1. Quick path: `{{pkgRun}} db update` — compares the database to the contract and applies changes directly
101
- 2. Migration path (for production workflows):
102
- - `{{pkgRun}} migration plan --name describe-the-change` — creates a migration
103
- - `{{pkgRun}} migration apply` — applies pending migrations
104
-
105
- **User wants to check what migrations need to be applied:**
106
- 1. Run `{{pkgRun}} migration status`
@@ -1,41 +0,0 @@
1
- import { dirname } from 'pathe';
2
- import type { AuthoringId, TargetId } from './code-templates';
3
- import { renderTemplate } from './render';
4
-
5
- export const variables = [
6
- 'schemaPath',
7
- 'schemaDir',
8
- 'dbImportPath',
9
- 'pkgRun',
10
- 'authoringLabel',
11
- ] as const;
12
-
13
- type TemplateVars = Record<(typeof variables)[number], string>;
14
-
15
- /**
16
- * Renders the per-project agent skill (FR5.2). The skill template is
17
- * target-specific (Postgres vs Mongo query syntax differs); the authoring
18
- * style enters via:
19
- *
20
- * - `schemaPath` — already routed through {@link agentSkillMd}'s caller
21
- * (the AC says a TS-authoring scaffold must reference `prisma/contract.ts`).
22
- * - `authoringLabel` — a short human-readable note (`PSL` / `TypeScript`)
23
- * the skill template uses when describing the contract file.
24
- */
25
- export function agentSkillMd(
26
- target: TargetId,
27
- authoring: AuthoringId,
28
- schemaPath: string,
29
- pkgRun: string,
30
- ): string {
31
- const schemaDir = dirname(schemaPath);
32
- const vars: TemplateVars = {
33
- schemaPath,
34
- schemaDir,
35
- dbImportPath: `./${schemaDir}/db`,
36
- pkgRun,
37
- authoringLabel: authoring === 'typescript' ? 'TypeScript' : 'PSL',
38
- };
39
- const templateFile = `agent-skill-${target}.md`;
40
- return renderTemplate(templateFile, variables, vars);
41
- }