diff --git a/www/api/models/Data_type.js b/www/api/models/Data_type.js index 3ebcfeb..a95a453 100644 --- a/www/api/models/Data_type.js +++ b/www/api/models/Data_type.js @@ -17,6 +17,23 @@ module.exports = { data_type: { type: 'string', unique: true + }, + plc_type:{ + type: 'string' } - } + }, + seedData:[ + { + data_type: "Floating Point", + plc_type: "REAL" + }, + { + data_type: "Integer", + plc_type: "INT" + }, + { + data_type: "Boolean", + plc_type: "BOOL" + }, + ] }; diff --git a/www/api/models/Device_type.js b/www/api/models/Device_type.js index 3136dc6..54c7b2a 100644 --- a/www/api/models/Device_type.js +++ b/www/api/models/Device_type.js @@ -18,5 +18,16 @@ module.exports = { type: 'string', unique: true } - } + }, + seedData: [ + { + dType: 'CLX', + }, + { + dType: 'Micro800', + }, + { + dType: 'E300', + }, + ] }; diff --git a/www/api/models/Tag_class.js b/www/api/models/Tag_class.js index d7f7284..245eaf2 100644 --- a/www/api/models/Tag_class.js +++ b/www/api/models/Tag_class.js @@ -21,5 +21,27 @@ module.exports = { description: { type: 'string' } - } + }, + seedData:[ + { + class_type: 'stroke', + description: 'Stroke Information' + }, + { + class_type: 'history', + description: 'Historical Data' + }, + { + class_type: 'gaugeoff', + description: 'Gauge Off Data' + }, + { + class_type: 'welltest', + description: 'Well Test Data' + }, + { + class_type: 'custom', + description: 'Stroke Information' + } + ] }; diff --git a/www/config/bootstrap.js b/www/config/bootstrap.js index 96208a1..008bff7 100644 --- a/www/config/bootstrap.js +++ b/www/config/bootstrap.js @@ -13,5 +13,10 @@ module.exports.bootstrap = function(cb) { // It's very important to trigger this callback method when you are finished // with the bootstrap! (otherwise your server will never lift, since it's waiting on the bootstrap) - cb(); + + async.series([ + Data_type.seed, + Device_type.seed, + Tag_class.seed + ],cb); }; diff --git a/www/config/models.js b/www/config/models.js index 859c911..4428ce9 100644 --- a/www/config/models.js +++ b/www/config/models.js @@ -27,6 +27,62 @@ module.exports.models = { * See http://sailsjs.org/#!/documentation/concepts/ORM/model-settings.html * * * ***************************************************************************/ - migrate: 'alter' + migrate: 'alter', + + /** + * This method adds records to the database + * + * To use add a variable 'seedData' in your model and call the + * method in the bootstrap.js file + */ + seed: function (callback) { + var self = this; + var modelName = self.adapter.identity.charAt(0).toUpperCase() + self.adapter.identity.slice(1); + if (!self.seedData) { + sails.log.debug('No data avaliable to seed ' + modelName); + callback(); + return; + } + self.count().exec(function (err, count) { + if (!err && count === 0) { + sails.log.debug('Seeding ' + modelName + '...'); + if (self.seedData instanceof Array) { + self.seedArray(callback); + }else{ + self.seedObject(callback); + } + } else { + sails.log.debug(modelName + ' had models, so no seed needed'); + callback(); + } + }); + }, + seedArray: function (callback) { + var self = this; + var modelName = self.adapter.identity.charAt(0).toUpperCase() + self.adapter.identity.slice(1); + self.createEach(self.seedData).exec(function (err, results) { + if (err) { + sails.log.debug(err); + callback(); + } else { + sails.log.debug(modelName + ' seed planted'); + callback(); + } + }); + }, + seedObject: function (callback) { + var self = this; + var modelName = self.adapter.identity.charAt(0).toUpperCase() + self.adapter.identity.slice(1); + self.create(self.seedData).exec(function (err, results) { + if (err) { + sails.log.debug(err); + callback(); + } else { + sails.log.debug(modelName + ' seed planted'); + callback(); + } + }); + + } };