Skip to content

Instantly share code, notes, and snippets.

@serkanserttop
Last active July 15, 2017 23:24
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save serkanserttop/64fc2d4465fb154066db to your computer and use it in GitHub Desktop.
Save serkanserttop/64fc2d4465fb154066db to your computer and use it in GitHub Desktop.
Loopback Model Discovery, bug?
//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'
}
]
]
//
// 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