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
DELETE /posts/1?_embed=comments DELETE /posts/1?_dependent=comments
``` ```
## Serving static files ## 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) => { app.delete('/:name/:id', async (req, res, next) => {
const { name = '', id = '' } = req.params 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() next()
}) })

View File

@ -171,7 +171,7 @@ export class Service {
name: string, name: string,
query: { query: {
[key: string]: unknown [key: string]: unknown
_embed?: string[] _embed?: string | string[]
_sort?: string _sort?: string
_start?: number _start?: number
_end?: number _end?: number
@ -424,7 +424,7 @@ export class Service {
async destroyById( async destroyById(
name: string, name: string,
id: string, id: string,
dependents: string[] = [], dependent?: string | string[],
): Promise<Item | undefined> { ): Promise<Item | undefined> {
const items = this.#get(name) const items = this.#get(name)
if (items === undefined || !Array.isArray(items)) return if (items === undefined || !Array.isArray(items)) return
@ -435,6 +435,7 @@ export class Service {
items.splice(index, 1)[0] items.splice(index, 1)[0]
nullifyForeignKey(this.#db, name, id) nullifyForeignKey(this.#db, name, id)
const dependents = ensureArray(dependent)
deleteDependents(this.#db, name, dependents) deleteDependents(this.#db, name, dependents)
await this.#db.write() await this.#db.write()