Reputation: 21
I am trying to work with Models with associations. I have two models defined as following:
User.js
module.exports = {schema: true, attributes: {
userId: {
autoIncrement: true,
type: integer,
primaryKey: true,
},
userId: {type: integer},
email: {type:string, size:200},
addressId: {
model:UserAddress
}
}
Address.js
module.exports = {schema: true, attributes: {
addressId: {
autoIncrement: true,
type:integer,
primaryKey:true
},
userId: {type:integer},
address: {type:string, size:250}
}
So far, I was able to CREATE records in both models by POSTing following JSON to
http://localhost:1337/user/
{
"userName": "John",
"userId": "3",
"email":"[email protected]",
"addressId": {
"userId": "3",
"address": "123 abc street"
}
}
I was able to UPDATE both records using PUT with this URL:
http://localhost:1337/user/update/1
and JSON below, note that I have added addressId in the nested JSON in this case.
{
"userName": "John",
"userId": "3",
"accountLogin":"[email protected]",
"addressId": {
"addressId":1,
"userId": "3",
"streetAddress": "123 abc street"
}
}
However, when I first created a record in the User table without an address and later wanted to add it, I had no luck to make it work. What I did was using PUT against http://localhost:1337/user/update/1
with following JSON, since the address is new, there's no addressId.
{
"userName": "John Doe",
"accountLogin":"[email protected]",
"addressId": {
"userId": "3",
"streetAddress": "123 abc street"
}
}
Is there a way to do what I was trying to do in Sails.js? I could just Post to http://localhost:1337/address
to create a new record, but I want to make my API consistent is possible. The more serious problem is, when above JSON was sent, it crashed the backend. It seems to me that user can easily crash backend by sending invalid JSON, how would I protect backend from this kind of crashing?
Upvotes: 2
Views: 4098
Reputation: 874
Hy,
In order to add a new object to a collection within an object you can use the sails built in (blueprint) "add" method. I checked sails documentation on the website but was not success full however if you check the add
blueprint in the github repository it does exactly what you are lookiung for.
Here is what says the method description :
/**
* Add Record To Collection
*
* post /:modelIdentity/:id/:collectionAttr/:childid
* * /:modelIdentity/:id/:collectionAttr/add/:childid
*
* Associate one record with the collection attribute of another.
* e.g. add a Horse named "Jimmy" to a Farm's "animals".
* If the record being added has a primary key value already, it will
* just be linked. If it doesn't, a new record will be created, then
* linked appropriately. In either case, the association is bidirectional.
*
* @param {Integer|String} parentid - the unique id of the parent record
* @param {Integer|String} id [optional]
* - the unique id of the child record to add
* Alternatively, an object WITHOUT a primary key may be POSTed
* to this endpoint to create a new child record, then associate
* it with the parent.
*
* @option {String} model - the identity of the model
* @option {String} alias - the name of the association attribute (aka "alias")
*/
And to answer your next question yes there is also a remove method ;)
You can find more info there : https://github.com/balderdashy/sails/tree/master/lib/hooks/blueprints/actions
Upvotes: 1