I\'m trying to get Loopback to discover and build my first table. I\'ve used the simple example on their page at the bottom here:
http://docs.strongloop.com/display/LB/D
discovery api is used to only discover the schema not to create models for now. please use the following project to create models with one to one and one to many relationships and all the models.
https://github.com/savsharma2/loopback-sql-create-model-with-relation/
Building off of @Underskay's answer, I did something like
var fs = require('fs');
var app = require(__dirname + '/server/server');
function makePromise(f, parent) {
return function(...args) {
return new Promise((resolve, reject) => {
f.call(parent, ...args, (err, ...data) => {
if (err) return reject(err);
resolve(data.length === 1 ? data[0] : data);
});
});
};
}
var readFile = makePromise(fs.readFile, fs);
var writeFile = makePromise(fs.writeFile, fs);
function writeSchemas(schemas) {
return Promise.all(schemas.map(data => {
var schema = data[Object.keys(data)[0]];
return writeFile('common/models/' + schema.name + '.json', JSON.stringify(schema, null, '\t'));
}))
.then(() => readFile('server/model-config.json'))
.then(JSON.parse)
.then(conf => {
for (let schema of schemas)
conf[schema[Object.keys(schema)[0]].name] = { "dataSource": "mysql" };
return conf;
})
.then(conf => writeFile('server/model-config.json', JSON.stringify(conf, null, '\t')));
}
function getSchemas(ds) {
var discoverSchemas = makePromise(ds.discoverSchemas, ds);
return makePromise(ds.discoverModelDefinitions, ds)({})
.then(tables => Promise.all(tables.map(t => discoverSchemas(t.name, { relations: true }))))
.then(data => { ds.disconnect(); return data; });
}
Promise.resolve(app.datasources.mysql)
.then(ds => getSchemas(ds))
.then(schemas => writeSchemas(schemas))
.catch(err => log.error(err));
Use Arc for this. Run slc arc from the project folder and it will show up the gui tool called arc in default browser. If you've not already registered, sign up and log in. You will be directed to GUI tool of StrongLoop, the Arc. Select your model from list on the left pane. You'll be able to see save and migrate button. Just click the migrate button and your table will be created into model.(within millisecs!)
Cheers!
Seems that discovery scripts only shows the output and doesn't create the model files. I found some instructions on loopback docs:
http://docs.strongloop.com/display/public/LB/Discovering+models+from+relational+databases
In section Basic Procedure, the second step:
2. Use fs.writeFile() to save the output in common/models/model-name.json.
So you can try the following approach:
{
"db": {
"name": "db",
"connector": "memory"
},
"accountDs": {
"host": "mysqlServerName",
"port": 3306,
"database": "databaseName",
"username": "username",
"password": "password!",
"name": "accountDs",
"connector": "mysql"
}
}
Create the models folder if doesn't exist: yourloopbackproject/common/models.
Create discovery-and-build.js script on yourloopbackproject/server/bin folder:
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.accountDs;
function schemaCB(err, schema) {
if(schema) {
console.log("Auto discovery success: " + schema.name);
var outputName = outputPath + '/' +schema.name + '.json';
fs.writeFile(outputName, JSON.stringify(schema, null, 2), function(err) {
if(err) {
console.log(err);
} else {
console.log("JSON saved to " + outputName);
}
});
}
if(err) {
console.error(err);
return;
}
return;
};
dataSource.discoverSchema('tableName',{schema:'schemaName'},schemaCB);
This script is based on: http://www.reddit.com/r/strongloop/comments/2upy76/autodiscoveryjs_recipe/
After the script execution you will find a .json file on models folder. Go to step 3 on Basic Procedure section: http://docs.strongloop.com/display/public/LB/Discovering+models+from+relational+databases
Follow these steps to expose your model over REST: http://docs.strongloop.com/display/public/LB/Exposing+models+over+REST
I hope this helps!