mirror of
https://github.com/typicode/json-server.git
synced 2025-07-25 19:22:57 +08:00
feat: rename _embed to _dependent for delete action
This commit is contained in:
@ -169,7 +169,7 @@ GET /comments?_embed=post
|
||||
|
||||
```
|
||||
DELETE /posts/1
|
||||
DELETE /posts/1?_embed=comments
|
||||
DELETE /posts/1?_dependent=comments
|
||||
```
|
||||
|
||||
## Serving static files
|
||||
|
@ -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()
|
||||
})
|
||||
|
||||
|
@ -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()
|
||||
|
Reference in New Issue
Block a user