[Pg]: Fix json/jsonb double-parsing with node-postgres driver#5524
Closed
Sim-hu wants to merge 1 commit intodrizzle-team:mainfrom
Closed
[Pg]: Fix json/jsonb double-parsing with node-postgres driver#5524Sim-hu wants to merge 1 commit intodrizzle-team:mainfrom
Sim-hu wants to merge 1 commit intodrizzle-team:mainfrom
Conversation
node-postgres (pg) already parses json/jsonb columns via its built-in type parser, so drizzle's mapFromDriverValue would double-parse scalar string values like "0.1", silently converting them to numbers. Override the JSON (114) and JSONB (3802) type parsers to return raw strings, matching the existing pattern used for timestamps and dates. Fixes #5485
Author
|
Closing this — I missed that #5487 already addresses the same issue. Also noted AlexBlokh's comment about the upcoming codecs system handling this properly across all drivers. |
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 #5485
Problem
node-postgres(pg) already parsesjson/jsonbcolumns via its built-in type parser (OIDs 114 and 3802), returning JS objects/values. Drizzle'smapFromDriverValuethen callsJSON.parseagain on the result.For most values this is harmless — objects and arrays pass the
typeof value === 'string'guard without being re-parsed. But JSON string scalars get double-parsed:This silently converts string values to numbers/booleans, which can cause data corruption.
Fix
Override the JSON and JSONB type parsers in the node-postgres session to return raw strings, matching the existing pattern already used for timestamps, dates, and intervals. This lets
mapFromDriverValuehandle the single parse correctly.Notes
TIMESTAMPTZ/TIMESTAMP/DATE/INTERVALoverrides in the same filenode-postgres— other drivers that return raw strings are unaffectedmapToDriverValue(JSON.stringify on write) remains unchanged