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

How to modify PUT to simulate PATCH #131

Closed
dheerajbhaskar opened this issue Sep 5, 2015 · 2 comments
Closed

How to modify PUT to simulate PATCH #131

dheerajbhaskar opened this issue Sep 5, 2015 · 2 comments
Labels

Comments

@dheerajbhaskar
Copy link

Hi guys,

Goal

We need to update documents in our collections via REST. Until JSON PATCH comes out, how can we modify PUT so that it doesn't replace the whole document, but updates the given document with new key:value pairs sent via REST. The route shouldn't update keys not mentioned in PUT.

If you know a better way to update the document which has been already implemented in restivus, please let me know.

PS: I know restivus tries to stick to standards of REST API and PUT is only supposed to replace, but we need to make do with a workaround until JSON PATCH support comes out.

@kahmali
Copy link
Owner

kahmali commented Sep 24, 2015

Wow. I am so sorry that this went unanswered for so long! At some point in the last few weeks I typed up a response to this and must have gotten sidetracked and never sent it. I'm so sorry about that!

I also apologize for not making it possible to simply provide a PATCH method on collection routes. I completely understand the need for partially updating a resource. I thought I was going to implement the PATCH support a lot sooner, or I would have made more of an effort to allow a custom PATCH method to be defined on collections in the interim. As soon as I have some time to update Restivus I will be sure to add support for this.

In the meantime, you can provide a custom put endpoint on all your collection routes. I would write the function where it was accessible to all collection routes, and reuse it throughout your API. Here's an example (it's in coffeescript to save me time and energy. If you need to convert it to JS go here: http://js2.coffee/):

Api = new Restivus()

# Do your partial patch (need to pass in collection since Restivus is evil and 
# doesn't do it for you in collection routes
patch = (collection) ->
  return ->
    collection.update @urlParams.id, {$set: @bodyParams}
    entity = collection.findOne @urlParams.id
    # Need to check for success/failure, instead of just assuming success here
    {status: 'success', data: entity}

Items = new Mongo.Collection 'Items'

Api.addCollection Items,
  endpoints:
    put: 
      action: patch(Items)

Widgets = new Mongo.Collection 'Widgets'

Api.addCollection Widgets,
  endpoints:
    put: 
      action: patch(Widgets)

I just freehanded that code in a bit of a rush. If you see me doing anything glaringly stupid there just point it out and I'll fix it.

Let me know if that helps. Again, I can't apologize enough for the insanely delayed response. Sorry about that!

@dheerajbhaskar
Copy link
Author

Thanks for getting back @kahmali and for the brilliant work so far.
We made our own custom put implementation.

Although, I'm not very familiar with CoffeScript, I see that there's not $set operator implementation, is that on purpose?

I shall go ahead and close this issue, though we can continue to discuss this.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants