postgres icon indicating copy to clipboard operation
postgres copied to clipboard

Prevent transform to be applied to the content of an jsonb column

Open clarknova opened this issue 1 year ago • 4 comments

I am using the transform feature to transform column names from snake case to camel case. This works great. Unfortunately it also transforms the keys inside a jsonb field which i don't want. Looking through the code this seems intended behavior. Is there a way to opt out of this behavior. The only solution i came up with is to manually typecast jsonb fields to text but this has many undesirable side effects.

clarknova avatar Nov 13 '24 09:11 clarknova

This tripped me up too. I'm stashing JSON objects from an external system in my DB and then need to read them back and pass them back to that system later. I was getting errors telling me my objects were invalid. Went down a rabbit hole debugging it and eventually figured out that postgres.js was modifying keys when reading from my jsonb columns.

Love the library, completely understand the rationale of the snake-to-camel case system for columns, but I'm kind of shocked that it's messing with the values of JSON columns.

Is this behaviour documented anywhere?

jnewman314 avatar Mar 06 '25 03:03 jnewman314

Afaict it's not documented, but there is a test asserting the behavior is desired:

https://github.com/porsager/postgres/blob/master/tests/index.js#L643

Thankfully I'm not using the transform feature, b/c yeah this is very surprising.

stephenh avatar Mar 22 '25 15:03 stephenh

Also experimenting this and based on your answer @stephenh can we guess (fear?) that this is an intended behavior?

I'm just wondering is this may be fixed or if we have to find a workaround.

LeRiton avatar Apr 11 '25 08:04 LeRiton

I looked at the code and found this workaround:

 types: {
    ...pg.camel,
    value: { from: (x) => x },
    undefined: null,
  }

Edweis avatar Jun 06 '25 09:06 Edweis