@prisma-next/mongo-query-builder 0.5.0-dev.9 → 0.5.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.
- package/README.md +28 -0
- package/dist/index.d.mts +398 -192
- package/dist/index.d.mts.map +1 -1
- package/dist/index.mjs +189 -54
- package/dist/index.mjs.map +1 -1
- package/package.json +11 -9
- package/src/builder.ts +56 -31
- package/src/exports/index.ts +13 -0
- package/src/lookup-builder.ts +272 -0
- package/src/pipeline-result-shape.ts +15 -0
- package/src/query.ts +0 -1
- package/src/resolve-path.ts +54 -0
- package/src/result-shape.ts +63 -0
- package/src/state-classes.ts +12 -3
- package/src/types.ts +110 -9
package/README.md
CHANGED
|
@@ -83,6 +83,34 @@ The general-purpose aggregation chain. Reached after calling any pipeline stage
|
|
|
83
83
|
| `findOneAndUpdate(fn)` | Deconstructs `$match`/`$sort`/`$skip` into command slots |
|
|
84
84
|
| `findOneAndDelete()` | Same deconstruction |
|
|
85
85
|
|
|
86
|
+
## Typed `lookup`
|
|
87
|
+
|
|
88
|
+
`$lookup` is expressed as a single chained callback so the foreign-root literal is grounded into the inner builder before the `on(...)` callback's accessors are typed:
|
|
89
|
+
|
|
90
|
+
```typescript
|
|
91
|
+
q.from('orders')
|
|
92
|
+
.lookup((from) =>
|
|
93
|
+
from('users')
|
|
94
|
+
.on((local, foreign) => ({
|
|
95
|
+
local: local.customerId,
|
|
96
|
+
foreign: foreign._id,
|
|
97
|
+
}))
|
|
98
|
+
.as('customer'),
|
|
99
|
+
)
|
|
100
|
+
.build();
|
|
101
|
+
// row: { _id, status, amount, customerId, notes, tags, customer: Array<UserRow> }
|
|
102
|
+
```
|
|
103
|
+
|
|
104
|
+
The chain has three steps:
|
|
105
|
+
|
|
106
|
+
- `from(name)` selects the foreign root. `name` must be a literal in `keyof TContract['roots']`. The returned `LookupBuilder` carries the foreign model's `FieldAccessor` so `foreign.<field>` is checked against the foreign model's storage-name fields.
|
|
107
|
+
- `on((local, foreign) => ({ local, foreign }))` captures the equality fields. `local` is the current pipeline's `FieldAccessor`; `foreign` is the foreign root's `FieldAccessor`. Each side must be a `LeafExpression` (a property access like `local._id`); aggregation expressions like `fn.toUpper(local._id)` are a compile-time error.
|
|
108
|
+
- `.as(name)` finalises with the user-chosen output field name.
|
|
109
|
+
|
|
110
|
+
The result-row encoding uses a `ModelArrayField<ModelName>` marker so `ResolveRow` produces `Array<ForeignRow>` — the field is **not** `unknown[]`. Marker behaviour is unchanged from the legacy shape: clears `UpdateEnabled` and `FindAndModifyEnabled`, sets `LeadingMatch` to `'past-leading'`, preserves the nested-shape parameter `N`.
|
|
111
|
+
|
|
112
|
+
**Limitation.** Typoed root names (`from('usexxxrs')`) are currently accepted at compile time and rejected at runtime. The constraint is a pre-existing limitation of `Contract.roots: Record<string, string>` (shared with `mongoQuery.from('badname')`); compile-time rejection is tracked under [TML-2400](https://linear.app/prisma-company/issue/TML-2400).
|
|
113
|
+
|
|
86
114
|
## Field accessor
|
|
87
115
|
|
|
88
116
|
Stage callbacks receive a `FieldAccessor<Shape, Nested>` (typically named `f`):
|