Remove a foreign record (e.g. a comment) from one of this record's collections (e.g. "comments").
DELETE /:model/:id/:association/:fk
This action removes a reference to some other record (the "foreign" or "child" record) from a collection of this record (the "primary" or "parent" record). Note that this does not actually destroy the foreign record, it just unlinks it.
res.notFound()
.res.notFound()
.via
), then the foreign key or collection it points to with that via
will also be updated on the foreign record.Parameter | Type | Details |
---|---|---|
model | The identity of the containing model for the parent record. e.g. 'store' (in /store/16/employeesOfTheMonth/7 ) |
|
id | The desired parent record's primary key value. e.g. '16' (in /store/16/employeesOfTheMonth/7 ) |
|
association | The name of the collection attribute. e.g. 'employeesOfTheMonth' |
|
fk | The primary key value (usually id) of the child record to remove from the collection. e.g. '7' |
Say you're building an app for a small chain of grocery stores. Each store has a giant television screen that displays the current "Employees of the Month" at that store, so that customers and team members see it when they walk in the door. In order to be sure it is up to date, you build a scheduled job (e.g. using cron) that runs on the first day of every month to change the "Employees of the Month" for each store in their system.
Let's say that, as a part of this scheduled job, we send a request to remove Dolly (employee #7) from store #16's employeesOfTheMonth
:
DELETE /store/16/employeesOfTheMonth/7
{
"id": 16,
"name": "Parmer and N. Lamar",
"createdAt": 1485552033435,
"updatedAt": 1485552048794,
"employeesOfTheMonth": [
{
"id": 12,
"name": "Motoki",
"createdAt": 1485462079725,
"updatedAt": 1485476060873
},
{
"id": 4,
"name": "Timothy",
"createdAt": 1485462079727,
"updatedAt": 1485476090874
}
]
}
If you have WebSockets enabled for your app, then every client subscribed to the parent record will receive a notification about the removed child, where the notification event name is that of the parent model identity (e.g. store
), and the “message” has the following format:
id: <the parent record's primary key value>,
verb: 'removedFrom',
attribute: <the parent record collection attribute name>,
removedIds: <the now-removed child records' primary key values>
For instance, continuing the example above, all clients subscribed to employee #7 (except for the client making the request) would receive the following message:
{
id: 16,
verb: 'removedFrom',
attribute: 'employeesOfTheMonth',
removedIds: [ 7 ]
}
Clients subscribed to the child record receive an additional notification:
Assuming employeesOfTheMonth
was defined with a via
, then either updated
or removedFrom
notifications would also be sent to any clients who were subscribed to Dolly, the child record we removed.
If the
via
-linked attribute on the other side is also plural (e.g.employeeOfTheMonthAtStores
), then anotherremovedFrom
notification will be sent. Otherwise, if thevia
points at a singular attribute (e.g.employeeOfTheMonthAtStore
) then theupdated
notification will be sent.
- If you'd like to spend some more time with Dolly, a more detailed walkthrough for the example above is available here.
- This action is for dealing with plural ("collection") attributes. If you want to set or unset a singular ("model") attribute, just use update and set the foreign key to the id of the new foreign record (or
null
to clear the association).- If you want to completely replace the set of records in the collection with another set, use the replace blueprint.