Json-server: How to delete multiple items at once?

Created on 22 Sep 2018  Â·  11Comments  Â·  Source: typicode/json-server

There's a list contain some items like this

{
    card: [
       { id: 1, name: 'a' },
       { id: 2, name: 'b' },
       { id: 3, name: 'c' }
    ]
}

I want to delete the first item and the second item.

I try this,but do‘t work

http.delete("path/card/1,2")

thx

Most helpful comment

@michalstol finally, I fork this repo and make it support delete multiple items in a request,like this

http.delete("path/id1,id2")

https://github.com/Moon-Land/json-server

@typicode Would you add this feature?

All 11 comments

I think you can do like that because your path is wrong. Try to delete path by path and push to all Promises to one array and after all use Promise.all. Still, you must be careful and create a handler for a moment when some command will be ended with an error.

@michalstol Promise.all is a solution, But I want to delete multiple items in only one request.
In my scene,I have to limit the number of requests.

You can try in this way. Delete all card and send in post new object without specific records.

@michalstol finally, I fork this repo and make it support delete multiple items in a request,like this

http.delete("path/id1,id2")

https://github.com/Moon-Land/json-server

@typicode Would you add this feature?

Nice :)

@typicode Would you add this feature?

Oh, thanks

I wonder why there's still no such a basic feature to delete all records.
Why do we have to remove all of the items in the array making a call for each item instead of a single one? That's insane.

@Gavin-Gong @michalstol hey guys, you said that Promise.all is a solution, but in my case the server crashes when I try to use it and I can't figure out why.
I'd appreciate it if you check this issue #1037

For now, I modify these lines of codes to get multi-id-delete feature, open file plural.js, change from

resource = db.get(name).removeById(req.params.id).value(); // Remove dependents documents
const removable = db._.getRemovable(db.getState(), opts);
removable.forEach(item => {
db.get(item.name).removeById(item.id).value();
});

to

req.params.id.split(',').filter(id => id !== '' || id !== undefined || id !== null).forEach(id => {

  resource = db.get(name).removeById(id).value(); // Remove dependents documents
  const removable = db._.getRemovable(db.getState(), opts);
  removable.forEach(item => {
    db.get(item.name).removeById(item.id).value();
  });

});

We usually install json-server global, for example with yarn, the code will be located at
%LocalAppData%\Yarn\Data\global\node_modules\json-server\lib\server\router

@lloydbanks Regarding server crashes, yeah, it definitely happens with multiple 'deletes.' NS what threshold is maybe more than 3 per second?

Seems like this is a very simple feature to implement. Are there any intentions to implement it in the near future?

Was this page helpful?
0 / 5 - 0 ratings

Related issues

shikaan picture shikaan  Â·  3Comments

jasonlimantoro picture jasonlimantoro  Â·  4Comments

boydenhartog picture boydenhartog  Â·  3Comments

0plus1 picture 0plus1  Â·  3Comments

zenith77 picture zenith77  Â·  3Comments