Skip to content
This repository has been archived by the owner on Aug 6, 2021. It is now read-only.

Latest commit

 

History

History
143 lines (103 loc) · 6.62 KB

File metadata and controls

143 lines (103 loc) · 6.62 KB

Add (blueprint)

Add a foreign record (e.g. a comment) to one of this record's collections (e.g. "comments").

PUT /:model/:id/:association/:fk

This action adds a reference to some other record (the "foreign", or "child" record) onto a particular collection of this record (the "primary", or "parent" record).

  • If the specified :id does not correspond with a primary record that exists in the database, this responds using res.notFound().
  • If the specified :fk does not correspond with a foreign record that exists in the database, this responds using res.notFound().
  • If the primary record is already associated with this foreign record, this action will not modify any records. (Note that currently, in the case of a many-to-many association, it will add duplicate junction records! To resolve this, add a multi-column index at the database layer, if possible. We are currently working on a friendlier solution/default for users of MongoDB, sails-disk, and other NoSQL databases.)
  • Note that if the association is "2-way" (meaning it has via), then the foreign key or collection it points to with that via will also be updated on the foreign record.

Parameters

Parameter Type Details
model ((string)) The identity of the containing model for the parent record.

e.g. 'employee' (in /employee/7/involvedinPurchases/47)
id ((string)) The desired parent record's primary key value.

e.g. '7' (in /employee/7/involvedInPurchases/47)
association ((string)) The name of the collection attribute.

e.g. 'involvedInPurchases'
fk ((string)) The primary key value (usually id) of the child record to add to this collection.

e.g. '47'

Example

Add purchase #47 to the list of purchases that Dolly (employee #7) has been involved in:

PUT /employee/7/involvedInPurchases/47

Run in Postman

Expected response

This returns "Dolly", the parent record. Notice she is now involved in purchase #47:

{
  "id": 7,
  "name": "Dolly",
  "createdAt": 1485462079725,
  "updatedAt": 1485476060873,
  "involvedInPurchases": [
    {
      "amount": 10000,
      "createdAt": 1485476060873,
      "updatedAt": 1485476060873,
      "id": 47,
      "cashier": 7
    }
  ]
}
Using jQuery
$.put('/employee/7/involvedInPurchases/47', function (purchases) {
  console.log(purchases);
});
Using Angular
$http.put('/employee/7/involvedInPurchases/47')
.then(function (purchases) {
  console.log(purchases);
});
Using sails.io.js
io.socket.put('/employee/7/involvedInPurchases/47', function (purchases) {
  console.log(purchases);
});
Using cURL
curl http://localhost:1337/employee/7/involvedInPurchases/47 -X "PUT"

Socket notifications

If you have WebSockets enabled for your app, then every client subscribed to the primary record will receive a notification in which the notification event name is the primary model identity (e.g. 'employee'), and the message has the following format:

id: <the parent record primary key value>,
verb: 'addedTo',
attribute: <the parent record collection attribute name>,
addedIds: <the now-added child records' primary key values>

For instance, continuing the example above, all clients subscribed to Dolly, aka employee #7, (except for the client making the request) would receive the following message:

{
  id: 7,
  verb: 'addedTo',
  attribute: 'involvedInPurchases',
  addedIds: [ 47 ]
}

Clients subscribed to the child record receive an additional notification:

Assuming involvedInPurchases had a via, then either updated or addedTo notifications would also be sent to any clients who were subscribed to purchase #47, the child record we just added.

If the via-linked attribute on the other side is also plural (e.g. cashiers), then another addedTo notification will be sent. Otherwise, if the via points at a singular attribute (e.g. cashier) then the updated notification will be sent.

Finally, a third notification might be sent:

If adding this purchase to Dolly's collection would "steal" it from another employee's involvedInPurchases, then any clients subscribed to that other, stolen-from employee record (e.g. Motoki, employee #12) would receive a removedFrom notification (see Blueprints > remove from.

Notes

  • If you'd like to spend some more time with Dolly, a more detailed walkthrough related to 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.

  • The example above assumes "rest" blueprints are enabled, and that your project contains at least an 'Employee' model with attribute: involvedInPurchases: {collection: 'Purchase', via: 'cashier'} as well as a Purchase model with attribute: cashier: {model: 'Employee'}. You can quickly achieve this by running:

    $ sails new foo
    $ cd foo
    $ sails generate model purchase
    $ sails generate model employee

...then editing api/models/Purchase.js and api/models/Employee.js.