Skip to content

Commit 992afae

Browse files
committed
chore: format investigation documentation
1 parent 5613b5a commit 992afae

File tree

1 file changed

+12
-6
lines changed

1 file changed

+12
-6
lines changed

FINDONE_JOINS_BUG_INVESTIGATION.md

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,12 @@ const query = useLiveQuery(
1212
q
1313
.from({ todo: todoCollection })
1414
.where(({ todo }) => eq(todo.id, id))
15-
.leftJoin({ todoOptions: todoOptionsCollection }, ({ todo, todoOptions }) =>
16-
eq(todo.id, todoOptions.todoId)
15+
.leftJoin(
16+
{ todoOptions: todoOptionsCollection },
17+
({ todo, todoOptions }) => eq(todo.id, todoOptions.todoId)
1718
)
1819
.findOne() // ❌ Causes type of query.data to become never
19-
);
20+
)
2021
```
2122

2223
### Workaround
@@ -29,11 +30,12 @@ const query = useLiveQuery(
2930
q
3031
.from({ todo: todoCollection })
3132
.where(({ todo }) => eq(todo.id, id))
32-
.leftJoin({ todoOptions: todoOptionsCollection }, ({ todo, todoOptions }) =>
33-
eq(todo.id, todoOptions.todoId)
33+
.leftJoin(
34+
{ todoOptions: todoOptionsCollection },
35+
({ todo, todoOptions }) => eq(todo.id, todoOptions.todoId)
3436
)
3537
.limit(1) // ✅ Works correctly
36-
);
38+
)
3739
```
3840

3941
## Root Cause Analysis
@@ -62,6 +64,7 @@ const query = useLiveQuery(
6264
The issue was in the `MergeContextWithJoinType` type definition, which was forcing `singleResult` to be explicitly `false` instead of preserving its original value.
6365

6466
**Before (Buggy)**:
67+
6568
```typescript
6669
export type MergeContextWithJoinType<
6770
TContext extends Context,
@@ -87,6 +90,7 @@ export type MergeContextWithJoinType<
8790
Changed line 577 in `/packages/db/src/query/builder/types.ts` to preserve the `singleResult` value as-is:
8891

8992
**After (Fixed)**:
93+
9094
```typescript
9195
export type MergeContextWithJoinType<
9296
TContext extends Context,
@@ -110,6 +114,7 @@ export type MergeContextWithJoinType<
110114
### Why This Works
111115

112116
By preserving `singleResult` as-is:
117+
113118
- If `findOne()` is called **before** join: `singleResult` is `true` and stays `true`
114119
- If `findOne()` is called **after** join: `singleResult` is `undefined` and the intersection `undefined & { singleResult: true }` properly resolves to `{ singleResult: true }`
115120
- No type conflict occurs
@@ -130,6 +135,7 @@ Added comprehensive type tests in `/packages/db/tests/query/join.test-d.ts`:
130135
## Impact
131136

132137
This fix ensures that:
138+
133139
-`findOne()` works correctly with all join types (left, right, inner, full)
134140
-Type inference works correctly for `query.data` in `useLiveQuery`
135141
-No breaking changes to existing code

0 commit comments

Comments
 (0)