Skip to content

Commit

Permalink
feat: rename _embed to _dependent for delete action
Browse files Browse the repository at this point in the history
  • Loading branch information
typicode committed Jan 22, 2024
1 parent 3710dce commit 86d0c26
Show file tree
Hide file tree
Showing 3 changed files with 5 additions and 4 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ GET /comments?_embed=post

```
DELETE /posts/1
DELETE /posts/1?_embed=comments
DELETE /posts/1?_dependent=comments
```

## Serving static files
Expand Down
2 changes: 1 addition & 1 deletion src/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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()
})

Expand Down
5 changes: 3 additions & 2 deletions src/service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ export class Service {
name: string,
query: {
[key: string]: unknown
_embed?: string[]
_embed?: string | string[]
_sort?: string
_start?: number
_end?: number
Expand Down Expand Up @@ -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
Expand All @@ -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()
Expand Down

0 comments on commit 86d0c26

Please sign in to comment.