diff --git a/README.md b/README.md index 67af577ad..4500e9b9a 100644 --- a/README.md +++ b/README.md @@ -169,7 +169,7 @@ GET /comments?_embed=post ``` DELETE /posts/1 -DELETE /posts/1?_embed=comments +DELETE /posts/1?_dependent=comments ``` ## Serving static files diff --git a/src/app.ts b/src/app.ts index 83070fa38..2faf3102c 100644 --- a/src/app.ts +++ b/src/app.ts @@ -110,7 +110,7 @@ export function createApp(db: Low, options: AppOptions = {}) { app.delete('/:name/:id', async (req, res, next) => { const { name = '', id = '' } = req.params - res.locals['data'] = await service.destroyById(name, id) + res.locals['data'] = await service.destroyById(name, id, req.query['dependent']) next() }) diff --git a/src/service.ts b/src/service.ts index 2b108d41a..329c28250 100644 --- a/src/service.ts +++ b/src/service.ts @@ -171,7 +171,7 @@ export class Service { name: string, query: { [key: string]: unknown - _embed?: string[] + _embed?: string | string[] _sort?: string _start?: number _end?: number @@ -424,7 +424,7 @@ export class Service { async destroyById( name: string, id: string, - dependents: string[] = [], + dependent?: string | string[], ): Promise { const items = this.#get(name) if (items === undefined || !Array.isArray(items)) return @@ -435,6 +435,7 @@ export class Service { items.splice(index, 1)[0] nullifyForeignKey(this.#db, name, id) + const dependents = ensureArray(dependent) deleteDependents(this.#db, name, dependents) await this.#db.write()