-
Notifications
You must be signed in to change notification settings - Fork 124
Throw a clear error when anything other than an object with an collection is passed to "from" #875
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Throw a clear error when anything other than an object with an collection is passed to "from" #875
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: |
|
Size Change: +214 B (+0.25%) Total Size: 86.1 kB
ℹ️ View Unchanged
|
|
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
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Once small change, but other than that great addition. Approving for once the errors have been combined and moved
| 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`}` | ||
| ) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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
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