Created
March 29, 2016 09:13
-
-
Save shafi-shaikat/e7b3d09a5dee6322f11e to your computer and use it in GitHub Desktop.
Sailsjs Seed Generator for https://github.com/lykmapipo/sails-hook-seed
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* SeedGeneratorService | |
* | |
* @description :: Will generate seeds from the model specied in the development/production directory | |
* @help :: See http://sailsjs.org/documentation/concepts/services/creating-a-service | |
*/ | |
module.exports = { | |
_config: { | |
dir: sails.config.appPath + '/seeds' | |
}, | |
//Get Layouts by reading the directory | |
//Will be used for layout select box | |
generate: function(model_names, destination_dir) { | |
var fs = require('fs'); | |
var path = require('path'); | |
var processSeedRequest = function() { | |
if(model_names.length) { | |
model_name = model_names.shift(); | |
var model_name_lower = model_name.toLowerCase(); //since sails keep all models name in lower case | |
if(sails.models[model_name_lower]) { | |
sails.models[model_name_lower].find() | |
.sort('id ASC') | |
.populateAll() | |
.exec(function(err, rows) { | |
//console.log(rows); | |
var seedFilePath = path.join(SeedGeneratorService._config.dir, destination_dir, model_name + 'Seed.js'); | |
console.log(seedFilePath); | |
//console.log(fs.readFileSync(seedFilePath)); | |
fs.exists(seedFilePath, function(exists) { | |
console.log(exists); | |
if(exists) | |
{ | |
fs.unlink(seedFilePath, function(err) { | |
console.log('Old seed file deleted.'); | |
startExport(rows, seedFilePath); | |
}); | |
} | |
else | |
startExport(rows, seedFilePath); | |
}); | |
}); | |
} | |
var startExport = function(data, file_path) { | |
console.log('Importing'); | |
//console.log(JSON.stringify(data, null, 4)); | |
var file_content = "module.exports = function(done) {\r\n"; | |
file_content += " var data = "; | |
data = JSON.stringify(data, null, 4); //just remove attributes, which are function | |
data = JSON.parse(data); | |
data.map(function(obj) { | |
var keys = Object.keys(obj); | |
for(var i = 0, total = keys.length; i<total; i++) | |
{ | |
if(obj[keys[i]] instanceof Array) { | |
obj[keys[i]] = obj[keys[i]].map(function(innerObj) { | |
return innerObj.id; | |
}); | |
} | |
else if(obj[keys[i]] instanceof Object) { | |
obj[keys[i]] = obj[keys[i]].id; | |
} | |
} | |
return obj; | |
}); | |
file_content += JSON.stringify(data, null, 4); | |
file_content += ";\r\n"; | |
file_content += "//remember to tell when your are done\r\n done(null, data);\r\n};"; | |
fs.writeFile(file_path, file_content, function(err) { | |
if(err) { | |
console.log(err); | |
} else { | |
console.log("seed saved to " + file_path); | |
} | |
processSeedRequest(); | |
}); | |
}; | |
} | |
else | |
{ | |
console.log('Seed generation is completed..'); | |
} | |
}; | |
processSeedRequest(); | |
} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment