json-server icon indicating copy to clipboard operation
json-server copied to clipboard

How to delete multiple items at once?

Open Gavin-Gong opened this issue 6 years ago • 14 comments

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

Gavin-Gong avatar Sep 22 '18 14:09 Gavin-Gong

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 avatar Sep 23 '18 06:09 michalstol

@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.

Gavin-Gong avatar Sep 23 '18 09:09 Gavin-Gong

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

michalstol avatar Sep 23 '18 14:09 michalstol

@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?

Gavin-Gong avatar Sep 25 '18 07:09 Gavin-Gong

Nice :)

michalstol avatar Sep 25 '18 07:09 michalstol

@typicode Would you add this feature?

Oh, thanks

azabroflovski avatar Jun 19 '19 01:06 azabroflovski

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.

lloydbanks avatar Oct 12 '19 11:10 lloydbanks

@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

lloydbanks avatar Oct 13 '19 12:10 lloydbanks

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`

bum avatar Oct 31 '19 15:10 bum

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

manavm1990 avatar Mar 07 '20 14:03 manavm1990

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

bmmpt avatar Sep 18 '20 17:09 bmmpt

Is there anything new about this functionality in 2021?

FelipeSabino-del avatar Oct 04 '21 18:10 FelipeSabino-del

I also had this problem, and solved it by removing the watch flag when starting json-server. This way the server doesn't reload automatically and all the requests are successfully handled.

trindadeptr avatar Mar 08 '22 17:03 trindadeptr

this is what I did!

const headers = { 'Accept': 'application/json', 'Content-Type': 'application/json' }

let delArray = [1,3,4,5] let delFetch = delArray.map(eleid => { return fetch(config.url + '/dataposts/' + eleid, { method: 'DELETE', headers: headers, }); });

const res = await Promise.all([delFetch]);

It works like deleteMany (Not actually)

nikhil647 avatar May 07 '22 06:05 nikhil647

https://github.com/nicolasbarb/json-server/commit/d4b3a0e58048ad53673f4d283d866c7c69f9da9f#comments

aekkader avatar Dec 29 '22 20:12 aekkader

Hello !, I have equal problems same you but If I test this line : //Delete all product in order deleteAllProductInOrder(){ return this.http.delete("http://localhost:3000/orders"); }

I have this bug that display in my console : payement.component.ts:55

DELETE http://localhost:3000/orders 404 (Not Found) ERROR HttpErrorResponse {headers: HttpHeaders, status: 404, statusText: 'Not Found', url: 'http://localhost:3000/orders', ok: false, …}

Benandry avatar Apr 04 '23 07:04 Benandry

I'm having the same issue now, have no idea when this feature would be added

Jayprecode avatar Apr 27 '23 22:04 Jayprecode

i found a way this could work

  • you fetch the data you want to delete then map....since all items in the array of data has a unique id, then you map through those data calling the delete endpoint individually .... i.e that clears all the items one by one

export const getSessions = async () => { const response = await axios.get("http://localhost:3001/sessions"); return formatSuccessResponse(response); }; const { data: s } = getSessions();

export const deleteSession = async (id: string) => { const response = await axios.delete(http://localhost:3001/sessions/${id}); return response; };

<Button onClick={() => { s.map(({ id }) => { deleteSession(id as string); return; }); } />

Jayprecode avatar Apr 27 '23 23:04 Jayprecode