Commit 464805d
Throw a clear error when anything other than an object with an collection is passed to "from" (#875)
* Improve error message when invalid source type is passed to .from()
Fixes #873
The issue was that users were getting a confusing error message when
passing a string (or other invalid type) to the .from() method instead
of an object with a collection.
For example:
```javascript
// Incorrect usage
q.from("conversations") // String instead of object
// Correct usage
q.from({ conversations: conversationsCollection })
```
When a string was passed, Object.keys("conversations") would return
an array of character indices ['0', '1', ...], which has length > 1,
triggering the "Only one source is allowed in the from clause" error.
This was misleading because the real issue was passing a string instead
of an object.
Changes:
- Added validation to check if the source is a plain object before
checking its key count
- Improved error message to explicitly state the expected format with
an example: .from({ alias: collection })
- Added comprehensive tests for various invalid input types (string,
null, array, undefined)
The new error message is:
"Invalid source for from clause: Expected an object with a single
key-value pair like { alias: collection }. For example:
.from({ todos: todosCollection }). Got: string \"conversations\""
* Refactor error validation to use positive checks
Simplified the validation logic in _createRefForSource to use positive
checks instead of negative checks:
- Check if it's a valid object (handles null/undefined via try-catch)
- Check if it's an array (arrays pass Object.keys but aren't valid)
- Check if it has exactly one key
- Check if keys look like numeric indices (indicates string was passed)
This approach is cleaner and handles all edge cases including when
callers bypass TypeScript's type checks with 'as any'.
Also added a changeset documenting the improvement.
* Move error messages to dedicated error class
Addresses code review feedback by creating InvalidSourceTypeError
class in errors.ts and using it consistently across all validation
cases instead of duplicating error message strings.
Changes:
- Added InvalidSourceTypeError class to errors.ts that takes context
and type parameters
- Updated _createRefForSource to use InvalidSourceTypeError in all
four validation cases (null/undefined, array, empty object, string)
- Updated tests to expect InvalidSourceTypeError instead of
QueryBuilderError
- This eliminates string duplication and makes error messages
consistent across .from() and .join() methods
---------
Co-authored-by: Claude <[email protected]>1 parent f15b2a7 commit 464805d
File tree
4 files changed
+99
-2
lines changed- .changeset
- packages/db
- src
- query/builder
- tests/query/builder
4 files changed
+99
-2
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
360 | 360 | | |
361 | 361 | | |
362 | 362 | | |
| 363 | + | |
| 364 | + | |
| 365 | + | |
| 366 | + | |
| 367 | + | |
| 368 | + | |
| 369 | + | |
| 370 | + | |
| 371 | + | |
363 | 372 | | |
364 | 373 | | |
365 | 374 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
10 | 10 | | |
11 | 11 | | |
12 | 12 | | |
| 13 | + | |
13 | 14 | | |
14 | 15 | | |
15 | 16 | | |
| |||
60 | 61 | | |
61 | 62 | | |
62 | 63 | | |
63 | | - | |
| 64 | + | |
| 65 | + | |
| 66 | + | |
| 67 | + | |
| 68 | + | |
| 69 | + | |
| 70 | + | |
| 71 | + | |
| 72 | + | |
| 73 | + | |
| 74 | + | |
| 75 | + | |
| 76 | + | |
| 77 | + | |
| 78 | + | |
| 79 | + | |
| 80 | + | |
| 81 | + | |
| 82 | + | |
| 83 | + | |
| 84 | + | |
| 85 | + | |
| 86 | + | |
| 87 | + | |
| 88 | + | |
64 | 89 | | |
65 | 90 | | |
66 | 91 | | |
67 | | - | |
| 92 | + | |
68 | 93 | | |
69 | 94 | | |
| 95 | + | |
70 | 96 | | |
71 | 97 | | |
72 | 98 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
3 | 3 | | |
4 | 4 | | |
5 | 5 | | |
| 6 | + | |
6 | 7 | | |
7 | 8 | | |
8 | 9 | | |
| |||
108 | 109 | | |
109 | 110 | | |
110 | 111 | | |
| 112 | + | |
| 113 | + | |
| 114 | + | |
| 115 | + | |
| 116 | + | |
| 117 | + | |
| 118 | + | |
| 119 | + | |
| 120 | + | |
| 121 | + | |
| 122 | + | |
| 123 | + | |
| 124 | + | |
| 125 | + | |
| 126 | + | |
| 127 | + | |
| 128 | + | |
| 129 | + | |
| 130 | + | |
| 131 | + | |
| 132 | + | |
| 133 | + | |
| 134 | + | |
| 135 | + | |
| 136 | + | |
| 137 | + | |
| 138 | + | |
| 139 | + | |
| 140 | + | |
| 141 | + | |
| 142 | + | |
| 143 | + | |
| 144 | + | |
| 145 | + | |
| 146 | + | |
| 147 | + | |
| 148 | + | |
| 149 | + | |
| 150 | + | |
| 151 | + | |
| 152 | + | |
| 153 | + | |
| 154 | + | |
| 155 | + | |
| 156 | + | |
| 157 | + | |
| 158 | + | |
| 159 | + | |
| 160 | + | |
| 161 | + | |
| 162 | + | |
| 163 | + | |
| 164 | + | |
| 165 | + | |
| 166 | + | |
| 167 | + | |
111 | 168 | | |
0 commit comments