Skip to content
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

Add Support for deeper update using $set #12

Merged
merged 1 commit into from
Feb 16, 2021

Conversation

romayalon
Copy link
Contributor

Signed-off-by: Romy romy2232@gmail.com

This PR handles an issue I observed when executing updates using $set op with more than 1 '.' .

For example:

When calling:
convertUpdate('data', { $set: { 'a.b.c': 2 } }

Actual Postgres query:
jsonb_set(jsonb_set(jsonb_set(data,'{a}',COALESCE(data->'a', '{}'::jsonb)),'{b}',COALESCE(data->'a'->'b', '{}'::jsonb)),'{a,b,c}','2'::jsonb)

This query indeed updates a.b.c as wanted but it also "hangs" the old value of a.b on a.

Expected Postgres query:
jsonb_set(jsonb_set(jsonb_set(data,'{a}',COALESCE(data->'a', '{}'::jsonb)),'{a,b}',COALESCE(data->'a'->'b', '{}'::jsonb)),'{a,b,c}','2'::jsonb)

Please review,
Thank you.

Signed-off-by: Romy <romy2232@gmail.com>
Copy link
Owner

@thomas4019 thomas4019 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fix looks good and thanks for adding a test.

@thomas4019 thomas4019 merged commit 5c1dcec into thomas4019:master Feb 16, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants