fix(sqlite-proxy): handle undefined/empty rows in get() method#5537
Open
claygeo wants to merge 1 commit intodrizzle-team:mainfrom
Open
fix(sqlite-proxy): handle undefined/empty rows in get() method#5537claygeo wants to merge 1 commit intodrizzle-team:mainfrom
claygeo wants to merge 1 commit intodrizzle-team:mainfrom
Conversation
When using sqlite-proxy, the `get` method should return `undefined`
when no matching row is found. Previously, the `AsyncRemoteCallback`
type required `rows: any[]` for all methods, which led proxy
implementers to return `{ rows: [] }` for empty results. The empty
array is truthy, so drizzle treated it as a valid row and returned
`{ id: undefined }` instead of `undefined`.
Changes:
- Update `AsyncRemoteCallback` return type to `{ rows: any[] | undefined }`
so proxy implementers can return `undefined` for get with no results
- Add runtime guard in `get()` to normalize empty arrays to undefined,
matching the behavior of native drivers (better-sqlite3, bun:sqlite)
- Guard `extractRawGetValueFromBatchResult` against undefined rows
- Add unit tests covering both undefined and empty array cases
Fixes drizzle-team#5461
Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
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.
Bug
Fixes #5461
When using
drizzle-orm/sqlite-proxy, thegetmethod returns{ id: undefined }instead ofundefinedwhen no matching row exists. This happens because:AsyncRemoteCallbacktype declaresrows: any[]for all methods, so proxy implementers naturally return{ rows: [] }for empty results[]is truthy, somapGetResult()treats it as a valid row and maps it to an object with all fields set toundefinedNative SQLite drivers (better-sqlite3, bun:sqlite) return
undefinedfromstmt.get()when no row matches. The sqlite-proxy driver should match this behavior.Fix
AsyncRemoteCallbackandAsyncBatchRemoteCallbackreturn types from{ rows: any[] }to{ rows: any[] | undefined }, allowing proxy implementers to returnundefinedfor get with no resultsget()to treat empty arrays asundefined, so both{ rows: undefined }and{ rows: [] }correctly produceundefinedextractRawGetValueFromBatchResultto use optional chaining (rows?.[0]) instead of non-null assertion (rows![0]) to handle undefined rows safelyVerification
All 565 existing tests pass. TypeScript compilation clean. New tests:
get()returnsundefinedwhen callback returns{ rows: undefined }✓get()returnsundefinedwhen callback returns{ rows: [] }✓get()returns mapped row when callback returns valid result ✓