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

Partial document update based on JSON Patch #3166

Open
steve-chavez opened this issue Jan 16, 2024 · 3 comments
Open

Partial document update based on JSON Patch #3166

steve-chavez opened this issue Jan 16, 2024 · 3 comments
Labels
difficulty: medium Haskell task involving PostgreSQL IO enhancement a feature, ready for implementation

Comments

@steve-chavez
Copy link
Member

steve-chavez commented Jan 16, 2024

Problem

Currently there's no way to do a partial document update.

Solution

Use a vendored media type based on json patch where we support the operations we lack. As prior art, azure cosmosdb json patch (also see here) does a similar thing and adds support for increment and set, which are not defined in the JSON Patch RFC.

For starters, we can add support for increment. Like so:

PATCH /items?id=eq.1
Content-Type: application/vnd.pgrst.json-patch

[
  { "op": "incr", "path": "/inventory", "value": 10 },
]

Path would refer to any column and the generated SQL would be like UPDATE items SET inventory = inventory + 10.

Later on we can extend this to add other operations, like mult, div, concat, etc.

@steve-chavez steve-chavez added the idea Needs of discussion to become an enhancement, not ready for implementation label Jan 16, 2024
@steve-chavez steve-chavez added enhancement a feature, ready for implementation difficulty: medium Haskell task involving PostgreSQL IO and removed idea Needs of discussion to become an enhancement, not ready for implementation labels Jan 29, 2024
@JanKaul
Copy link

JanKaul commented Feb 14, 2024

Am I assuming correctly that the implementation would involve a new media type handler and a postgres implementation of jsonpatch?

What other steps would be required to implement this functionality as a builtin media type?

@steve-chavez
Copy link
Member Author

steve-chavez commented Feb 14, 2024

@JanKaul I was thinking this should be a built-in media type handler in Haskell code (like the one used for application/vnd.pgrst.plan) but maybe it can be done as you say..

a new media type handler

We would need to finish #2826 for that (media type handlers only work for Accept right now, not for Content-Type).

a postgres implementation of jsonpatch?

That one would be insufficient because it only works on JSON columns, the intention for increments was to work on int, bigint, any type really. We already expose rows as "json documents", so it makes sense to allow json patch for all types of columns.

So we would need plpgsql/dynamic SQL on the function to generate the UPDATE. It could work...


On second thought, since this JSON patch is postgREST-specific it makes more sense to just have it as built-in handler.

@JanKaul
Copy link

JanKaul commented Feb 15, 2024

That makes sense. Thanks for the clarification. I will have a look at the implementation of application/vnd.pgrst.plan.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
difficulty: medium Haskell task involving PostgreSQL IO enhancement a feature, ready for implementation
Development

No branches or pull requests

2 participants