Added seed parameters to make sure data is already in db at startup

This commit is contained in:
Patrick McDonagh
2016-05-03 12:39:28 -05:00
parent 7c4a29dbb1
commit e83390d3ce
5 changed files with 116 additions and 5 deletions

View File

@@ -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"
},
]
};

View File

@@ -18,5 +18,16 @@ module.exports = {
type: 'string',
unique: true
}
}
},
seedData: [
{
dType: 'CLX',
},
{
dType: 'Micro800',
},
{
dType: 'E300',
},
]
};

View File

@@ -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'
}
]
};

View File

@@ -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);
};

View File

@@ -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();
}
});
}
};