Skip to content

Commit fe83215

Browse files
ADR 259: a row-changing step refuses a collection whose row was narrowed by select or variant
Signed-off-by: willbot <[email protected]> Signed-off-by: Will Madden <[email protected]>
1 parent dc33efe commit fe83215

1 file changed

Lines changed: 8 additions & 3 deletions

File tree

‎docs/architecture docs/adrs/ADR 259 - Query fragments are functions.md‎

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ A **query fragment** is a function. A fragment of a row is a function from the r
3131
1. **A collection fragment is a step, applied with `pipe`** (ADR 258). The step receives the caller's exact type, including a custom collection class, and the result is whatever the step returns.
3232
2. **The query API has no control-flow methods.** Whatever the application would write in a function body, it writes inside the step. The type stays sound because a filtered collection is a subtype of an unfiltered one, so a step that may or may not filter yields the unfiltered type (ADR 258).
3333
3. **A field type is named by its codec.** `FieldExpression<Contract, CodecId, Nullable>` is the type of any row field with that codec and nullability, with the same comparison methods and operations as the field on a model. A row fragment typed with it fits every model that has such a field.
34-
4. **A step that changes the row is defined once per model.** `rowFragment<Contract, Model>()(body)` types the body against the plain collection of that model and returns a step that takes that model's collection in any state. `CollectionRowOf` (ADR 258) names the row the step produces.
34+
4. **A step that changes the row is defined once per model.** `rowFragment<Contract, Model>()(body)` types the body against the plain collection of that model and returns a step that takes any collection of that model whose rows still have every field of the model. `CollectionRowOf` (ADR 258) names the row the step produces.
3535
5. **A sort field from a string is checked at run time.** `sortField(collection, name, direction, allowed)` returns an `orderBy` selector. The allowed list may name only fields whose codec can be ordered. A name outside it throws before the query runs.
3636

3737
## Why a fragment is a function
@@ -93,10 +93,15 @@ type PostSummary = CollectionRowOf<ReturnType<typeof summary>>;
9393
db.Post.pipe(summary);
9494
db.Post.where({ userId }).pipe(summary);
9595
db.User.include('posts', (posts) => posts.pipe(summary));
96-
db.Comment.pipe(summary); // error: not a Post collection
96+
db.Comment.pipe(summary); // error: not a Post collection
97+
db.Post.select('id').pipe(summary); // error: the rows no longer have every Post field
9798
```
9899

99-
A step that calls `select` produces a new row, so its type cannot be the caller's type. `rowFragment` types the body once, against the plain collection of the model, and returns a step that accepts a collection of that model in any state, including an include refinement and a collection after an earlier `select`. `CollectionRowOf` reads the row type off the result, so the application can name it.
100+
A step that calls `select` produces a new row, so its type cannot be the caller's type. `rowFragment` types the body once, against the plain collection of the model, and returns a step that accepts any collection of that model whose row is the model's full row or wider: a root or filtered collection, a collection after `include`, an include refinement, `this` in a custom class.
101+
102+
It refuses a collection whose row was narrowed by `select` or `variant`. The body was typed against the full row, so on a narrowed collection its result would claim fields the query does not return: `(posts) => posts.include('user')` applied after `select('id')` would be typed with every field of a post. The rule is the row subtyping of ADR 258: a narrowed row is not assignable to the full row, a widened one is. On a widened collection the result can omit relations the caller included, which refuses more and never claims more.
103+
104+
`CollectionRowOf` reads the row type off the result, so the application can name it.
100105

101106
The step's result has the default state. A filter or order applied before the step is not recorded after it, so `update` is refused after `pipe(summary)` even when a `where` came first. Row-changing steps are for reading.
102107

0 commit comments

Comments
 (0)