Nodejs
The following code will automatically create nodejs rest api using loopback.io. This will automatically create the models and also add the models in model-config.json.
--------------------------------------------------------------------------------
var path = require('path');
var fs = require('fs');
var app = require(path.resolve(__dirname, '../server'));
var outputPath = path.resolve(__dirname, '../../common/models');
var dataSource = app.dataSources.mysql;
dataSource.discoverModelDefinitions({schema: 'base_live'}, function(err, tables) {
var modelConfigPath = path.resolve(__dirname, '../model-config.json');
fs.readFile(modelConfigPath, 'utf8', function(err, data) {
if(err) {
console.log(err);
} else {
modelConfig = JSON.parse(data);
for (var i = tables.length - 1; i >= 0; i--) {
dataSource.discoverSchema(tables[i].name, {schema:'base_live', relations:true}, function (err, table){
if(table) {
console.log("Auto discovery success: " + table.name);
table.name = table.name.substr(3);
var outputName = outputPath + '/' + table.name + '.json';
modelConfig[table.name] = {dataSource: "mysql", public: true};
fs.writeFile(outputName, JSON.stringify(table, null, 2), function(err) {
if(err) {
console.log(err);
} else {
console.log("JSON saved to " + outputName);
fs.writeFile(modelConfigPath, JSON.stringify(modelConfig, null, "\t"), function(err) {
if(err) {
console.log(err);
} else {
console.log(modelConfigPath + " updated.");
}
});
}
});
}
if(err) {
console.error(err);
return;
}
});
}
}
});
});
---------------------------------------------------------------------------------