Throw a clear error when anything other than an object with an collection is passed to "from"#875
Merged
KyleAMathews merged 3 commits intomainfrom Nov 21, 2025
Conversation
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\""
🦋 Changeset detectedLatest commit: 28444e1 The changes in this PR will be included in the next version bump. This PR includes changesets to release 11 packages
Not sure what this means? Click here to learn what changesets are. Click here if you're a maintainer who wants to add another changeset to this PR |
More templates
@tanstack/angular-db
@tanstack/db
@tanstack/db-ivm
@tanstack/electric-db-collection
@tanstack/offline-transactions
@tanstack/powersync-db-collection
@tanstack/query-db-collection
@tanstack/react-db
@tanstack/rxdb-db-collection
@tanstack/solid-db
@tanstack/svelte-db
@tanstack/trailbase-db-collection
@tanstack/vue-db
commit: |
Contributor
|
Size Change: +214 B (+0.25%) Total Size: 86.1 kB
ℹ️ View Unchanged
|
Contributor
|
Size Change: 0 B Total Size: 3.34 kB ℹ️ View Unchanged
|
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.
samwillis
approved these changes
Nov 21, 2025
Collaborator
samwillis
left a comment
There was a problem hiding this comment.
Once small change, but other than that great addition. Approving for once the errors have been combined and moved
Comment on lines
+70
to
+74
| throw new QueryBuilderError( | ||
| `Invalid source for ${context}: Expected an object with a single key-value pair like { alias: collection }. ` + | ||
| // eslint-disable-next-line @typescript-eslint/no-unnecessary-condition | ||
| `For example: .from({ todos: todosCollection }). Got: ${source === null ? `null` : `undefined`}` | ||
| ) |
Collaborator
There was a problem hiding this comment.
Can we move this error into the error.ts and then use it in all four places rather than duplicate the strings?
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
Merged
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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:
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:
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""
🎯 Changes
✅ Checklist
pnpm test:pr.🚀 Release Impact