Last active
July 15, 2017 23:24
-
-
Save serkanserttop/64fc2d4465fb154066db to your computer and use it in GitHub Desktop.
Loopback Model Discovery, bug?
This file contains hidden or 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
//DataSource.prototype.discoverSchemas | |
//async.parallel(tasks, function (err, results) { | |
// console.log(results); | |
//https://github.com/strongloop/loopback-datasource-juggler/blob/master/lib/datasource.js#L1205 | |
[ | |
[ | |
{ owner: 'chosendb', | |
tableName: 'chosentable', | |
columnName: 'id' | |
}, | |
{ owner: 'chosendb', | |
tableName: 'chosentable', | |
columnName: 'name' | |
}, | |
{ owner: 'chosendb', | |
tableName: 'chosentable', | |
columnName: 'some_column' | |
}, | |
{ owner: 'chosendb2', | |
tableName: 'chosentable', | |
columnName: 'id' | |
}, | |
{ owner: 'chosendb2', | |
tableName: 'chosentable', | |
columnName: 'name' | |
}, | |
{ owner: 'chosendb5', | |
tableName: 'chosentable', | |
columnName: 'id' | |
}, | |
{ owner: 'chosendb5', | |
tableName: 'chosentable', | |
columnName: 'name' | |
}, | |
{ owner: 'chosendb5', | |
tableName: 'chosentable', | |
columnName: 'chosentable_features__id' | |
} | |
], | |
[ | |
{ owner: 'chosendb', | |
tableName: 'chosentable', | |
columnName: 'id' | |
}, | |
{ owner: 'chosendb2', | |
tableName: 'chosentable', | |
columnName: 'id' | |
}, | |
{ owner: 'chosendb5', | |
tableName: 'chosentable', | |
columnName: 'id' | |
} | |
] | |
] |
This file contains hidden or 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
// | |
// WARNING: THIS FILE WILL NOT RUN UNTIL YOU INSTALL, ADD, AND CONFIGURE A | |
// MYSQL DATA SOURCE, FOLLOWING INSTRUCTIONS IN "Getting Started with LoopBack" | |
// See http://docs.strongloop.com/display/LB2/Connect+an+API+to+a+datasource | |
// | |
var fs = require('fs'); | |
var loopback = require('loopback'); | |
var app_dir = '/app_dir/'; | |
/* | |
var dataSource = loopback.createDataSource('mysql', { | |
"host": "localhost", | |
"port": 3306, | |
"database": "chosendb5", | |
"username": "root", | |
"password": "pass" | |
}); | |
var db = 'chosendb5', owner = 'root'; | |
*/ | |
var app = require('./server'); | |
var dataSource = app.dataSources.chosendb5; | |
//console.log(dataSource); | |
var db = 'chosendb5', owner = 'root'; | |
function capitaliseFirstLetter(string){ | |
return string.charAt(0).toUpperCase() + string.slice(1); | |
} | |
function jsFileString(model_name){ | |
return '' + | |
'module.exports = function(' + capitaliseFirstLetter(model_name) + ') {\n' + | |
'\t\n' + | |
'};'; | |
} | |
function autoGenerateModelFiles(){ | |
dataSource.discoverModelDefinitions({schema:db}, function (err, models) { | |
models.forEach(function (model) { | |
dataSource.discoverSchema(model.name, {associations: true}, function (err, schema) { | |
if( schema.options.mysql.schema !== db ){ | |
console.log('options.mysql.schema !== db', schema); | |
} | |
fs.writeFile( app_dir + 'common/models/' + model.name + '.json', JSON.stringify(schema, null, ' '), function (err) { | |
if (err) throw err; | |
console.log('Saved ' + model.name); | |
}); | |
fs.writeFile( app_dir + 'common/models/' + model.name + '.js', jsFileString(model.name), function (err) { | |
if (err) throw err; | |
console.log('Created ' + model.name + '.json file'); | |
}); | |
}); | |
}); | |
}); | |
} | |
autoGenerateModelFiles(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment