@prisma/orm-mongo 8.0.0-rc.9-dev.12 → 8.0.1-dev.1

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 (151) hide show
  1. package/README.md +48 -8
  2. package/dist/components-CWskH7lF-Dkc9ysZz.mjs +18 -0
  3. package/dist/components-CWskH7lF-Dkc9ysZz.mjs.map +1 -0
  4. package/dist/config.d.mts +14 -5
  5. package/dist/config.d.mts.map +1 -1
  6. package/dist/config.mjs +47 -25
  7. package/dist/config.mjs.map +1 -1
  8. package/dist/contract-builder.d.mts +33 -91
  9. package/dist/contract-builder.d.mts.map +1 -1
  10. package/dist/contract-builder.mjs +23 -7
  11. package/dist/contract-builder.mjs.map +1 -1
  12. package/dist/contract-types.d.mts +3 -0
  13. package/dist/contract-types.mjs +1 -0
  14. package/dist/contract__format-version.d.mts +1 -0
  15. package/dist/contract__format-version.mjs +2 -0
  16. package/dist/contract__internal__format-version.d.mts +1 -0
  17. package/dist/contract__internal__format-version.mjs +2 -0
  18. package/dist/contract__validate-once.d.mts +1 -0
  19. package/dist/contract__validate-once.mjs +2 -0
  20. package/dist/contract__validate-storage.d.mts +1 -0
  21. package/dist/contract__validate-storage.mjs +2 -0
  22. package/dist/control.d.mts +1 -1
  23. package/dist/control.d.mts.map +1 -1
  24. package/dist/control.mjs +3 -9
  25. package/dist/control.mjs.map +1 -1
  26. package/dist/driver__runtime.d.mts +3 -0
  27. package/dist/driver__runtime.mjs +3 -0
  28. package/dist/family-contract__data-type.d.mts +1 -0
  29. package/dist/family-contract__data-type.mjs +2 -0
  30. package/dist/init-quick-reference.md +116 -0
  31. package/dist/init-readme.md +35 -0
  32. package/dist/init.d.mts +7 -0
  33. package/dist/init.d.mts.map +1 -0
  34. package/dist/init.mjs +245 -0
  35. package/dist/init.mjs.map +1 -0
  36. package/dist/migration.d.mts +1 -0
  37. package/dist/migration.mjs +2 -0
  38. package/dist/module-base-B70DHaBE-k_c3Klu4.mjs +6 -0
  39. package/dist/module-base-B70DHaBE-k_c3Klu4.mjs.map +1 -0
  40. package/dist/mongo-static-CfvV1-qX-DdckMRxc.mjs +74 -0
  41. package/dist/mongo-static-CfvV1-qX-DdckMRxc.mjs.map +1 -0
  42. package/dist/{mongo-static-BdSGXYgQ-DSA-2DiK.d.mts → mongo-static-DabeBtmV-DILuhcbE.d.mts} +18 -5
  43. package/dist/mongo-static-DabeBtmV-DILuhcbE.d.mts.map +1 -0
  44. package/dist/runtime.d.mts +49 -44
  45. package/dist/runtime.d.mts.map +1 -1
  46. package/dist/runtime.mjs +104 -42
  47. package/dist/runtime.mjs.map +1 -1
  48. package/dist/static.d.mts +24 -2
  49. package/dist/static.d.mts.map +1 -0
  50. package/dist/static.mjs +2 -2
  51. package/dist/target__codec-ids.d.mts +1 -0
  52. package/dist/target__codec-ids.mjs +2 -0
  53. package/dist/target__codecs.d.mts +1 -0
  54. package/dist/target__codecs.mjs +2 -0
  55. package/dist/target__data-types.d.mts +1 -0
  56. package/dist/target__data-types.mjs +2 -0
  57. package/dist/target__server-version.d.mts +1 -0
  58. package/dist/target__server-version.mjs +2 -0
  59. package/dist/utils__guards.d.mts +1 -0
  60. package/dist/utils__guards.mjs +2 -0
  61. package/dist/utils__text.d.mts +1 -0
  62. package/dist/utils__text.mjs +2 -0
  63. package/dist/utils__upgrade-guides.d.mts +1 -0
  64. package/dist/utils__upgrade-guides.mjs +2 -0
  65. package/package.json +37 -13
  66. package/skills/prisma-8/SKILL.md +24 -19
  67. package/skills/prisma-8/references/build-a-target-package.md +117 -0
  68. package/skills/prisma-8/references/build.md +12 -16
  69. package/skills/prisma-8/references/contract.md +181 -75
  70. package/skills/prisma-8/references/debug.md +38 -27
  71. package/skills/prisma-8/references/feedback.md +8 -8
  72. package/skills/prisma-8/references/migration-model.md +28 -18
  73. package/skills/prisma-8/references/migration-review.md +24 -53
  74. package/skills/prisma-8/references/migrations.md +298 -84
  75. package/skills/prisma-8/references/queries-mongo.md +53 -17
  76. package/skills/prisma-8/references/queries-postgres.md +345 -34
  77. package/skills/prisma-8/references/queries.md +44 -25
  78. package/skills/prisma-8/references/quickstart.md +28 -25
  79. package/skills/prisma-8/references/runtime.md +172 -72
  80. package/skills/prisma-8/references/supabase.md +22 -19
  81. package/skills/prisma-8/references/upgrade-app.md +57 -22
  82. package/skills/prisma-8/references/upgrade-extension.md +72 -23
  83. package/skills/prisma-8/upgrading/app/upgrades/0.11-to-0.12/instructions.md +1 -1
  84. package/skills/prisma-8/upgrading/app/upgrades/8.0.0-rc.10-to-8.0.0-rc.11/instructions.md +34 -0
  85. package/skills/prisma-8/upgrading/app/upgrades/8.0.0-rc.11-to-8.0.0-rc.12/instructions.md +639 -0
  86. package/skills/prisma-8/upgrading/app/upgrades/8.0.0-rc.11-to-8.0.0-rc.12/scripts/multifile-psl/add-use-prisma-8-directive.mjs +60 -0
  87. package/skills/prisma-8/upgrading/app/upgrades/8.0.0-rc.11-to-8.0.0-rc.12/scripts/psl-verbatim-table-names/add-model-map.mjs +256 -0
  88. package/skills/prisma-8/upgrading/app/upgrades/8.0.0-rc.12-to-8.0.0-rc.13/instructions.md +332 -0
  89. package/skills/prisma-8/upgrading/app/upgrades/8.0.0-rc.12-to-8.0.0-rc.13/scripts/execution-ref-entry-field/rename-execution-ref-keys.ts +193 -0
  90. package/skills/prisma-8/upgrading/app/upgrades/8.0.0-rc.13-to-8.0.0-rc.14/instructions.md +489 -0
  91. package/skills/prisma-8/upgrading/app/upgrades/8.0.0-rc.14-to-8.0.0-rc.15/instructions.md +1368 -0
  92. package/skills/prisma-8/upgrading/app/upgrades/8.0.0-rc.14-to-8.0.0-rc.15/scripts/data-type-in-contract/data-type-in-contract.ts +1492 -0
  93. package/skills/prisma-8/upgrading/app/upgrades/8.0.0-rc.15-to-8.0.0-rc.16/instructions.md +108 -0
  94. package/skills/prisma-8/upgrading/app/upgrades/8.0.0-rc.16-to-8.0.0-rc.17/instructions.md +755 -0
  95. package/skills/prisma-8/upgrading/app/upgrades/8.0.0-rc.17-to-8.0.0-rc.18/instructions.md +973 -0
  96. package/skills/prisma-8/upgrading/app/upgrades/8.0.0-rc.17-to-8.0.0-rc.18/scripts/sql-expression-literals-psl/rewrite-sql-strings.mjs +271 -0
  97. package/skills/prisma-8/upgrading/app/upgrades/8.0.0-rc.18-to-8.0.0-rc.19/instructions.md +1862 -0
  98. package/skills/prisma-8/upgrading/app/upgrades/8.0.0-rc.18-to-8.0.0-rc.19/scripts/client-takes-contract-value/contract-from-json.ts +597 -0
  99. package/skills/prisma-8/upgrading/app/upgrades/8.0.0-rc.18-to-8.0.0-rc.19/scripts/data-type-values/data-type-values.ts +1120 -0
  100. package/skills/prisma-8/upgrading/app/upgrades/8.0.0-rc.18-to-8.0.0-rc.19/scripts/default-generators-to-on-create/rewrite-default-generators.mjs +86 -0
  101. package/skills/prisma-8/upgrading/app/upgrades/8.0.0-rc.18-to-8.0.0-rc.19/scripts/mongo-field-storage-names/mongo-field-storage-names.ts +304 -0
  102. package/skills/prisma-8/upgrading/app/upgrades/8.0.0-rc.18-to-8.0.0-rc.19/scripts/remove-nocheck/remove-nocheck.ts +2462 -0
  103. package/skills/prisma-8/upgrading/app/upgrades/8.0.0-rc.19-to-8.0.0-rc.20/instructions.md +513 -0
  104. package/skills/prisma-8/upgrading/app/upgrades/8.0.0-rc.19-to-8.0.0-rc.20/scripts/expand-type-aliases/expand-type-aliases.ts +1206 -0
  105. package/skills/prisma-8/upgrading/app/upgrades/8.0.0-rc.20-to-8.0.0-rc.21/instructions.md +803 -0
  106. package/skills/prisma-8/upgrading/app/upgrades/8.0.0-rc.21-to-8.0.0-rc.22/instructions.md +586 -0
  107. package/skills/prisma-8/upgrading/app/upgrades/8.0.0-rc.21-to-8.0.0-rc.22/scripts/value-objects-in-storage/value-objects-in-storage.ts +1184 -0
  108. package/skills/prisma-8/upgrading/app/upgrades/8.0.0-rc.22-to-8.0.0-rc.23/instructions.md +480 -0
  109. package/skills/prisma-8/upgrading/app/upgrades/8.0.0-rc.22-to-8.0.0-rc.23/scripts/mongo-index-names/mongo-index-names.ts +317 -0
  110. package/skills/prisma-8/upgrading/app/upgrades/8.0.0-rc.23-to-8.0.0-rc.24/instructions.md +336 -0
  111. package/skills/prisma-8/upgrading/app/upgrades/8.0.0-rc.23-to-8.0.0-rc.24/scripts/mongo-convenience-date-time-types/rewrite-mongo-date-types.ts +220 -0
  112. package/skills/prisma-8/upgrading/app/upgrades/8.0.0-rc.23-to-8.0.0-rc.24/scripts/mongo-type-helpers/rewrite-mongo-field-types.ts +621 -0
  113. package/skills/prisma-8/upgrading/app/upgrades/8.0.0-rc.24-to-8.0.0-rc.25/instructions.md +1561 -0
  114. package/skills/prisma-8/upgrading/app/upgrades/8.0.0-rc.9-to-8.0.0-rc.10/instructions.md +13 -0
  115. package/skills/prisma-8/upgrading/extension/upgrades/0.10-to-0.11/instructions.md +1 -1
  116. package/skills/prisma-8/upgrading/extension/upgrades/0.11-to-0.12/instructions.md +1 -1
  117. package/skills/prisma-8/upgrading/extension/upgrades/8.0.0-rc.10-to-8.0.0-rc.11/instructions.md +34 -0
  118. package/skills/prisma-8/upgrading/extension/upgrades/8.0.0-rc.11-to-8.0.0-rc.12/instructions.md +857 -0
  119. package/skills/prisma-8/upgrading/extension/upgrades/8.0.0-rc.11-to-8.0.0-rc.12/scripts/psl-verbatim-table-names/add-model-map.mjs +256 -0
  120. package/skills/prisma-8/upgrading/extension/upgrades/8.0.0-rc.12-to-8.0.0-rc.13/instructions.md +504 -0
  121. package/skills/prisma-8/upgrading/extension/upgrades/8.0.0-rc.12-to-8.0.0-rc.13/scripts/execution-ref-entry-field/rename-execution-ref-keys.ts +193 -0
  122. package/skills/prisma-8/upgrading/extension/upgrades/8.0.0-rc.13-to-8.0.0-rc.14/instructions.md +143 -0
  123. package/skills/prisma-8/upgrading/extension/upgrades/8.0.0-rc.14-to-8.0.0-rc.15/instructions.md +2168 -0
  124. package/skills/prisma-8/upgrading/extension/upgrades/8.0.0-rc.14-to-8.0.0-rc.15/scripts/data-type-in-contract/data-type-in-contract.ts +1492 -0
  125. package/skills/prisma-8/upgrading/extension/upgrades/8.0.0-rc.15-to-8.0.0-rc.16/instructions.md +223 -0
  126. package/skills/prisma-8/upgrading/extension/upgrades/8.0.0-rc.16-to-8.0.0-rc.17/instructions.md +583 -0
  127. package/skills/prisma-8/upgrading/extension/upgrades/8.0.0-rc.17-to-8.0.0-rc.18/instructions.md +1008 -0
  128. package/skills/prisma-8/upgrading/extension/upgrades/8.0.0-rc.18-to-8.0.0-rc.19/instructions.md +2186 -0
  129. package/skills/prisma-8/upgrading/extension/upgrades/8.0.0-rc.18-to-8.0.0-rc.19/scripts/client-takes-contract-value/contract-from-json.ts +597 -0
  130. package/skills/prisma-8/upgrading/extension/upgrades/8.0.0-rc.18-to-8.0.0-rc.19/scripts/data-type-values/data-type-values.ts +1120 -0
  131. package/skills/prisma-8/upgrading/extension/upgrades/8.0.0-rc.18-to-8.0.0-rc.19/scripts/default-generators-to-on-create/rewrite-default-generators.mjs +86 -0
  132. package/skills/prisma-8/upgrading/extension/upgrades/8.0.0-rc.18-to-8.0.0-rc.19/scripts/mongo-field-storage-names/mongo-field-storage-names.ts +304 -0
  133. package/skills/prisma-8/upgrading/extension/upgrades/8.0.0-rc.18-to-8.0.0-rc.19/scripts/remove-nocheck/remove-nocheck.ts +2462 -0
  134. package/skills/prisma-8/upgrading/extension/upgrades/8.0.0-rc.19-to-8.0.0-rc.20/instructions.md +381 -0
  135. package/skills/prisma-8/upgrading/extension/upgrades/8.0.0-rc.19-to-8.0.0-rc.20/scripts/expand-type-aliases/expand-type-aliases.ts +1206 -0
  136. package/skills/prisma-8/upgrading/extension/upgrades/8.0.0-rc.20-to-8.0.0-rc.21/instructions.md +570 -0
  137. package/skills/prisma-8/upgrading/extension/upgrades/8.0.0-rc.21-to-8.0.0-rc.22/instructions.md +464 -0
  138. package/skills/prisma-8/upgrading/extension/upgrades/8.0.0-rc.22-to-8.0.0-rc.23/instructions.md +410 -0
  139. package/skills/prisma-8/upgrading/extension/upgrades/8.0.0-rc.23-to-8.0.0-rc.24/instructions.md +303 -0
  140. package/skills/prisma-8/upgrading/extension/upgrades/8.0.0-rc.23-to-8.0.0-rc.24/scripts/mongo-convenience-date-time-types/rewrite-mongo-date-types.ts +220 -0
  141. package/skills/prisma-8/upgrading/extension/upgrades/8.0.0-rc.23-to-8.0.0-rc.24/scripts/mongo-type-helpers/rewrite-mongo-field-types.ts +621 -0
  142. package/skills/prisma-8/upgrading/extension/upgrades/8.0.0-rc.24-to-8.0.0-rc.25/instructions.md +1593 -0
  143. package/dist/adapter__codec-ids.d.mts +0 -1
  144. package/dist/adapter__codec-ids.mjs +0 -2
  145. package/dist/adapter__codec-types.d.mts +0 -1
  146. package/dist/adapter__codec-types.mjs +0 -2
  147. package/dist/adapter__codecs.d.mts +0 -1
  148. package/dist/adapter__codecs.mjs +0 -2
  149. package/dist/mongo-static-BdSGXYgQ-DSA-2DiK.d.mts.map +0 -1
  150. package/dist/mongo-static-_wy-fC4P-mXlhePy0.mjs +0 -38
  151. package/dist/mongo-static-_wy-fC4P-mXlhePy0.mjs.map +0 -1
@@ -1,12 +1,12 @@
1
1
  # Prisma 8 — Queries (Mongo)
2
2
 
3
- > Load this guide when `db.ts` imports from `@internal/mongo/runtime`.
3
+ > Load this guide when `db.ts` imports from `@prisma/orm-mongo/runtime`.
4
4
 
5
5
  Shared concepts (result consumption, script teardown, cross-target pitfalls, capability gaps) live in [`queries.md`](./queries.md).
6
6
 
7
7
  ## Key Concepts
8
8
 
9
- **Mongo** (`mongo<Contract>(...)` from `@internal/mongo/runtime`):
9
+ **Mongo** (`mongo({ contract, ... })` from `@prisma/orm-mongo/runtime`):
10
10
 
11
11
  - **`db.orm.<root>`** — ORM, lowercased plural contract root (`db.orm.users`, `db.orm.posts`). Same fluent chaining; `.where({ field: value })` object equality is the idiomatic filter form.
12
12
  - **`db.query`** — typed aggregation-pipeline builder. Start with `db.query.from('<root>')`, chain `.match(...)` / `.project(...)` / `.group(...)` / `.lookup(...)`, terminal with `.build()`. Run via `(await db.runtime()).query(plan)` for anything that returns documents; `execute(plan)` is only for a write you want an affected count from, and it throws `RUNTIME.MONGO_STATISTICS_UNSUPPORTED` on a find or aggregate.
@@ -19,7 +19,7 @@ Reach for the ORM first; drop to `db.query` when the ORM can't express the shape
19
19
  |---|---|---|
20
20
  | Standard CRUD with reference relations | **ORM (`db.orm.<root>`)** | Collection-shaped; object `.where({ ... })`; `.create` / `.update` / `.delete` / `.upsert`. |
21
21
  | Eager-load a reference relation | **ORM `.include('<relation>')`** | Lowers to `$lookup`; composes with `.where` / `.select` / `.orderBy` / `.limit`. |
22
- | Polymorphic root (discriminated variants) | **ORM `.variant('<VariantName>')`** | Narrows to one variant and injects the discriminator filter. |
22
+ | Polymorphic root (discriminated variants) | **ORM `.variant('<discriminator value>')`** | Narrows to the variant declaring that value and injects the discriminator filter. |
23
23
  | Field-level Mongo updates (`$push`, `$inc`, dot-path `$set`) | **ORM `.update((f) => [f.field.inc(1)])`** | Field-accessor callback; plain-object `.update({ ... })` for whole-field replacement. |
24
24
  | Aggregation pipeline (group, facet, `$lookup` with reshaping) | **Query builder (`db.query.from(...)`)** | Full pipeline surface; typed row shape through `.build()`. |
25
25
  | Typed cross-collection join in a pipeline | **Query builder `.lookup((from) => from('users').on(...).as('author'))`** | `$lookup` with compile-time foreign-root checking. |
@@ -47,20 +47,22 @@ const recent = await db.orm.posts
47
47
  .all();
48
48
  ```
49
49
 
50
+ **Field names, not stored names.** The ORM names a field by its name in the contract, never by the key its documents store it under. A field declared `id ObjectId @id @map("_id")` is `id` in every ORM call and row, and documents store it as `_id`; passing `_id` to the ORM fails with `ORM.FIELD_UNKNOWN`, whose message names the field to use. The pipeline builder (`db.query`) works on documents, so it names stored names: `_id` there. A raw filter expression passed to the ORM's `.where(...)`, such as `MongoFieldFilter.eq('displayName', 'Ada')` or a `MongoExprFilter`, names fields too and is translated; one built for `db.query` names stored names, so do not pass it to the ORM.
51
+
50
52
  **`.where(...)`** accepts a plain object whose keys are model field names and values are compared with equality (codec-aware — `ObjectId` fields accept string ids from the contract). Chain multiple `.where({ ... })` calls to AND-compose filters.
51
53
 
52
- For operators the object form doesn't cover (`.in([...])`, range comparisons, nested logic), pass a `MongoFilterExpr` — today that means importing filter helpers from `@prisma/orm-mongo/query-ast/execution` (a façade-completeness gap; see *What Prisma 8 doesn't do yet* in [`queries.md`](./queries.md)). Prefer the object form whenever equality suffices.
54
+ For operators the object form doesn't cover (`.in([...])`, range comparisons, nested logic), pass a `MongoFilterExpr` — today that means importing filter helpers from `@prisma/orm-mongo/query-ast/execution` (a gap in the database package; see *What Prisma 8 doesn't do yet* in [`queries.md`](./queries.md)). Prefer the object form whenever equality suffices.
53
55
 
54
- **Polymorphic roots.** When the contract declares variants on a model, narrow before querying:
56
+ **Polymorphic roots.** When the contract declares variants on a model, narrow before querying. `.variant()` takes the discriminator value a variant declares (`"article"` from `@@base(Post, "article")`), not the variant's model name. Call it once, on the base collection; a second `.variant()` on a variant collection is refused:
55
57
 
56
58
  ```typescript
57
- const articles = await db.orm.posts.variant('Article').all();
58
- const tutorials = await db.orm.posts.variant('Tutorial').where({ authorId }).all();
59
+ const articles = await db.orm.posts.variant('article').all();
60
+ const tutorials = await db.orm.posts.variant('tutorial').where({ authorId }).all();
59
61
  ```
60
62
 
61
63
  **Sorting and pagination.** `.orderBy({ field: 1 | -1 })` (Mongo sort directions). `.limit(n)` maps to `$limit`; `.offset(n)` maps to `$skip`.
62
64
 
63
- **`.first()` vs `.all()`.** `.first()` issues a limit-1 read; `.all()` returns every matching document. There is no `.first({ pk })` shorthand on Mongo — filter on `_id` explicitly: `.where({ _id: id }).first()`.
65
+ **`.first()` vs `.all()`.** `.first()` issues a limit-1 read; `.all()` returns every matching document. There is no `.first({ pk })` shorthand on Mongo — filter on the id field explicitly: `.where({ id }).first()`.
64
66
 
65
67
  Mongo `.all()` returns the same `AsyncIterableResult` shape as Postgres — `await db.orm.users.all()` yields an array; see *Consuming the result* in [`queries.md`](./queries.md).
66
68
 
@@ -76,14 +78,14 @@ const posts = await db.orm.posts
76
78
  // → Array<{ title, authorId, createdAt, author: { name, email, ... } }>
77
79
  ```
78
80
 
79
- Relation names match the contract's `@relation` field names. Nested includes follow the same chaining rules as the parent collection.
81
+ Relation names match the contract's `@relation` field names. `.include(...)` takes only the relation name: there is no refinement callback, so the included documents cannot be filtered, ordered or limited, and their own relations are not loaded. Only a reference relation on a single field can be included; an embedded relation is already part of the document, and naming one in `.include(...)` is a type error.
80
82
 
81
83
  ## Workflow — ORM writes
82
84
 
83
85
  Mongo mutations require a preceding `.where(...)` filter (except `.create` / `.createAll`). Updates accept either a partial document or a field-accessor callback for Mongo operators:
84
86
 
85
87
  ```typescript
86
- // Create — returns the row with server-assigned `_id`.
88
+ // Create — returns the row with the server-assigned `id` (stored as `_id`).
87
89
  const user = await db.orm.users.create({
88
90
  name: 'Alice',
89
91
  email: 'alice@example.com',
@@ -92,11 +94,11 @@ const user = await db.orm.users.create({
92
94
  });
93
95
 
94
96
  // Update one — plain object replaces top-level fields.
95
- await db.orm.users.where({ _id: user._id }).update({ bio: 'Writer' });
97
+ await db.orm.users.where({ id: user.id }).update({ bio: 'Writer' });
96
98
 
97
99
  // Update one — field operations ($push, $inc, dot-path $set).
98
100
  await db.orm.users
99
- .where({ _id: user._id })
101
+ .where({ id: user.id })
100
102
  .update((u) => [u.tags.push('admin'), u.loginCount.inc(1)]);
101
103
 
102
104
  // Update many / delete many — iterate or count.
@@ -105,7 +107,7 @@ const updated = await db.orm.users
105
107
  .updateAll({ bio: 'filled' });
106
108
  for await (const row of updated) { /* each modified doc */ }
107
109
 
108
- await db.orm.users.where({ _id: user._id }).delete();
110
+ await db.orm.users.where({ id: user.id }).delete();
109
111
 
110
112
  // Upsert — filter via .where(), split create vs update branches.
111
113
  await db.orm.users.where({ email: 'alice@example.com' }).upsert({
@@ -144,7 +146,7 @@ Import `acc` and expression helpers (`fn`) from `@prisma/orm-mongo/query-builder
144
146
 
145
147
  ## Workflow — Query builder (`db.query`)
146
148
 
147
- The concept: `db.query.from('<root>')` starts a typed aggregation-pipeline chain. Terminal methods produce a `MongoQueryPlan`; run it through the runtime with `query(plan)` (an `AsyncIterableResult` of documents — `await` it for an array):
149
+ The concept: `db.query.from('<root>')` starts a typed aggregation-pipeline chain over documents, so it names each field by its stored name (`_id` for a field declared `id … @map("_id")`). Terminal methods produce a `MongoQueryPlan`; run it through the runtime with `query(plan)` (an `AsyncIterableResult` of documents — `await` it for an array):
148
150
 
149
151
  ```typescript
150
152
  // src/queries/analytics.ts
@@ -206,21 +208,54 @@ Update callbacks return arrays of field operations (`.set`, `.inc`, `.push`, `.p
206
208
 
207
209
  **Plans vs ORM.** The ORM's `.create` / `.update` / `.all` issue queries directly. Don't pass ORM collections to `runtime.query` / `runtime.execute` — those entry points are for `db.query` plans (and migration/runtime internals).
208
210
 
211
+ ## Workflow — Prepared statements (`db.prepare`)
212
+
213
+ The concept: `db.prepare(declaration, (p) => plan)` builds a query-builder plan or an ORM read once and runs it many times with different values. The declaration names each parameter's codec (`{ email: 'mongo/string@1' }`, or `{ codecId: 'mongo/string@1', nullable: true }` to allow `null`); unknown codec ids fail to compile. The callback receives one placeholder per name and puts it where a value goes: filter operators (`eq`, `in`, …), update operators (`set`, `push`, …), insert documents, `fn.literal(p.x)`, and `limit` / `skip` (with a codec whose input is a number, such as `mongo/int32@1`). Each run encodes the value with the declared codec. Preparing opens no connection; run the handle against `await db.runtime()`:
214
+
215
+ ```typescript
216
+ const byAuthor = await db.prepare(
217
+ { authorId: 'mongo/objectId@1', take: 'mongo/int32@1' },
218
+ (p) =>
219
+ db.query
220
+ .from('posts')
221
+ .match((f) => f.authorId.eq(p.authorId))
222
+ .sort({ createdAt: -1 })
223
+ .limit(p.take)
224
+ .build(),
225
+ );
226
+
227
+ const runtime = await db.runtime();
228
+ const recent = await byAuthor.query(runtime, { authorId: someHexId, take: 10 }).toArray();
229
+ ```
230
+
231
+ An `updateOne`, `updateMany`, `deleteOne` or `deleteMany` plan gives a handle with `execute(runtime, params)` as well as `query`, resolving `{ affectedRows }`. `runtime.prepare(...)` on a `MongoRuntime` takes query-builder and raw plans only, not ORM reads. A declared name the plan never uses throws `RUNTIME.PREPARE_UNUSED_PARAM`; a placeholder in a `db.raw` command throws `RUNTIME.PREPARE_RAW_PLACEHOLDER`; a placeholder whose name, codec id or nullability the declaration does not give throws `RUNTIME.PREPARE_UNDECLARED_PARAM`; a missing value at run time throws `RUNTIME.PREPARE_MISSING_PARAM`. A `db.raw` command with no placeholders can be prepared and is sent as written on each run.
232
+
233
+ ORM reads are prepared through `.prepared.all()`, `.prepared.first()` and `.prepared.firstOrThrow()`. The handle's `query(runtime, params)` returns what the ad-hoc read returns: a row stream, `Row | null`, or `Row` (rejecting with `RUNTIME.NO_ROWS` when nothing matches). It has no `execute`.
234
+
235
+ ```typescript
236
+ const byEmail = await db.prepare({ email: 'mongo/string@1', take: 'mongo/int32@1' }, (p) =>
237
+ db.orm.users.where({ email: p.email }).include('posts').limit(p.take).prepared.all(),
238
+ );
239
+ const users = await byEmail.query(await db.runtime(), { email: 'alice@example.com', take: 5 }).toArray();
240
+ ```
241
+
242
+ In an ORM read, a placeholder goes in `limit`, `offset`, a scalar field's value in an object-form `where`, or the operand of a comparison (`$eq`, `$ne`, `$gt`, …) or an item of `$in` / `$nin` on a scalar field in a filter-expression `where`. Its codec must be the field's codec (the element codec on a list field, where it matches one item), and it may be nullable only on a nullable field that is not a list. Anything else (another operator, `$expr`, a value object, a whole list) throws `ORM.ARGUMENT_INVALID` when the `where` is built. ORM writes cannot be prepared; a write on a collection whose `where` holds a placeholder throws `ORM.ARGUMENT_INVALID`. `first()` and `firstOrThrow()` set the limit to 1, so a declared `limit` placeholder there throws `RUNTIME.PREPARE_UNUSED_PARAM`. Design: [ADR 318](../../../docs/architecture%20docs/adrs/ADR%20318%20-%20MongoDB%20prepared%20statements.md).
243
+
209
244
  ## Common Pitfalls (Mongo)
210
245
 
211
246
  1. **Reaching for the lower-level lane when the ORM would have done.** Default to the ORM; drop to `db.query` only for shapes the ORM can't express.
212
247
  2. **Using `.all()` when you wanted one row.** Use `.where({ ... }).first()` — not `.all()`.
213
248
  3. **Calling `.update()` / `.delete()` without `.where()`.** Mutations other than `.create` / `.createAll` require a filter — the compiler enforces this at the type level where possible.
214
249
  4. **Using PascalCase model names on ORM.** Roots are lowercased plurals from the contract (`db.orm.users`, not `db.orm.User`).
215
- 5. **Expecting Postgres-style lambda `.where((u) => u.email.eq(...))` on ORM.** Prefer object equality `.where({ email: '...' })`; richer operators need `MongoFilterExpr` helpers (façade gap today).
216
- 6. **Expecting `db.transaction(...)`.** The Mongo façade does not expose it today. Multi-document atomicity requires MongoDB transactions on a replica set via the driver — not yet wrapped in the Prisma 8 façade. Route to *What Prisma 8 doesn't do yet* / `references/feedback.md` if the user needs this.
250
+ 5. **Expecting Postgres-style lambda `.where((u) => u.email.eq(...))` on ORM.** Prefer object equality `.where({ email: '...' })`; richer operators need `MongoFilterExpr` helpers (a gap in the database package today).
251
+ 6. **Expecting `db.transaction(...)`.** The Mongo package does not expose it today. Multi-document atomicity requires MongoDB transactions on a replica set via the driver — not yet wrapped in the Prisma 8 database package. Route to *What Prisma 8 doesn't do yet* / `references/feedback.md` if the user needs this.
217
252
  7. **Trying to use `db.sql`.** There is no `db.sql` on Mongo.
218
253
  8. **Trying to `db.execute(plan)` directly, or reading documents with `execute`.** Run query-builder plans via `(await db.runtime()).query(plan)`. `execute(plan)` resolves statistics only and throws `RUNTIME.MONGO_STATISTICS_UNSUPPORTED` for a find or aggregate.
219
254
  9. **Expecting ORM `.aggregate(...)` / `.groupBy(...)`.** Use `db.query.from(...).group(...).build()` instead.
220
255
 
221
256
  ## Reference Files
222
257
 
223
- - Example queries under [`examples/mongo-demo/src/server.ts`](examples/mongo-demo/src/server.ts) — ORM reads, `.include`, `.variant`, and pipeline DSL via `db.query`.
258
+ - Example queries under [`examples/mongo-demo/src/server.ts`](https://github.com/prisma/orm/blob/main/examples/mongo-demo/src/server.ts) — ORM reads, `.include`, `.variant`, and pipeline DSL via `db.query`.
224
259
  - Integration tests under `examples/mongo-demo/test/` (`blog.test.ts`, `crud-lifecycle.test.ts`, `query-builder-writes.test.ts`).
225
260
  - Query builder README under `packages/2-mongo-family/5-query-builders/query-builder/README.md`.
226
261
  - ORM collection surface under `packages/2-mongo-family/5-query-builders/orm/src/collection.ts`.
@@ -231,6 +266,7 @@ Update callbacks return arrays of field operations (`.set`, `.inc`, `.push`, `.p
231
266
  - [ ] Chose the right lane (ORM by default; `db.query` for shapes the ORM doesn't express).
232
267
  - [ ] Used `.where({ ... }).first()` for single-row reads — not `.all()`.
233
268
  - [ ] Ran query-builder plans via `(await db.runtime()).query(plan)`; used `execute(plan)` only for an affected count on a write.
269
+ - [ ] For a query run many times with different values, used `db.prepare(...)` and ran the handle against `await db.runtime()`.
234
270
  - [ ] For aggregations, used `db.query.from(...).group(...)` rather than a non-existent ORM `.aggregate(...)`.
235
271
  - [ ] Did NOT confabulate `db.transaction`, `db.sql`, or ORM `.aggregate(...)` — routed to *What Prisma 8 doesn't do yet* / `references/feedback.md` instead.
236
272
  - [ ] Did NOT use the lower-level builder for something the ORM cleanly expresses.