- Make sure you have sails-mongo adapters added in the module otherwise use,
npm install sails-mongo --save
Define a connection in config/connections.js. You can make it a default connection under config/models.js if you are not using other database.
Then, you would have to create a Model with definitions similar to the content of the JSON that the external API is returning.
For eg. if the external API is returning.
[{
attribute1: '<value>',
attribute2: '<value>',
attribute3: '<value>'
}]
You would want to create a Model under /api/models called Model.js like below,
module.exports = {
attributes: {
attribute1: {type: 'integer'},
attribute2: {type: 'string'},
attribute3: {type: 'datetime'}
}
};
Refer to this to get more idea on creating a model.
Then in your controller you can just do,
var externalJSON = <API result JSON>;
Model.create(externalJSON)
.exec(function(err, user){
//Processing
});