feat: rename _embed to _dependent for delete action

This commit is contained in:
typicode
2024-01-22 15:59:43 +01:00
parent 3710dce4a0
commit 86d0c26b6e
3 changed files with 5 additions and 4 deletions

View File

@ -169,7 +169,7 @@ GET /comments?_embed=post
```
DELETE /posts/1
DELETE /posts/1?_embed=comments
DELETE /posts/1?_dependent=comments
```
## Serving static files

View File

@ -110,7 +110,7 @@ export function createApp(db: Low<Data>, 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()
})

View File

@ -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<Item | undefined> {
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()