mirror of
https://github.com/typicode/json-server.git
synced 2025-08-01 09:13:32 +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
|
||||||
DELETE /posts/1?_embed=comments
|
DELETE /posts/1?_dependent=comments
|
||||||
```
|
```
|
||||||
|
|
||||||
## Serving static files
|
## Serving static files
|
||||||
|
@ -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()
|
||||||
})
|
})
|
||||||
|
|
||||||
|
@ -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()
|
||||||
|
Reference in New Issue
Block a user