0

I am trying to create an REST API which should connect to an existing table in mysql database and return the data with respective to the parameter we send.

Actually nodejs and strongloop is new to me, this is first time am working with them. I have followed their docs and created a table in mysql my running a file like below

I have followed the commands to create model, properties etc from the below github docs

https://github.com/strongloop/loopback-example-database

create-test-data.js

var server = require('./server');
var dataSource = server.dataSources.accountDB;
var Account = server.models.account;
var accounts = [
    { email: 'foo@bar.com',
      created: new Date(),
      modified: new Date()
    }, {
      email: 'bar@bar.com',
      created: new Date(),
      modified: new Date()
    } ];

var count = accounts.length;
dataSource.automigrate('account', function(er) {
  if (er) throw er;
  accounts.forEach(function(account) {
    Account.create(account, function(er, result) {
      if (er) return;
      console.log('Record created:', result);
      count--;
      if(count === 0) {
        console.log('done');
        dataSource.disconnect();
      }
    });
  });
});

This automatically creating table and records in my database, I don't want this.

Actually I already have a different table, which I want to connect with strongloop.

I am completely clueless, any help would be appreciated.

vamsi
  • 1,488
  • 3
  • 28
  • 66

3 Answers3

3

I found this trying to do the same thing. I fixed it so it would end gracefully. Works great for me. Original: https://gist.github.com/serkanserttop/64fc2d4465fb154066db#file-discover-js

var path = require('path');
var app = require(path.resolve(__dirname, '../server'));
var fs = require('fs');
var loopback = require('loopback');
var app_dir = './';
require('node-babel')();
var dataSource = app.dataSources.accountDs;
var db = 'myDB',
    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) {
        var count = models.length;
        console.log(models.length);
        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');
                });
                count = count - 1;
                if (len === 0) {
                    console.log("DONE!", count);
                    dataSource.disconnect();
                    return;
                }
            });
        });
    });
}
0

What you actually need is to discover model from database. There is a documentation available on a strongloop page.

http://docs.strongloop.com/display/public/LB/Discovering+models+from+relational+databases;jsessionid=1FC0E473B7F589F4F1EFC0F25D269E3E

http://docs.strongloop.com/display/public/LB/Database+discovery+API

Here is a working example:

var ds = app.dataSources.accountDB;
ds.discoverModelDefinitions(function (err, models) {
  models.forEach(function (model) {
    ds.discoverSchema( model.name, function (err, schema){
      console.log(schema);
    });
  });
});

Put this code somewhere inside server.js (i.e. inside boot method). I assume that you have setup datasource correctly and also have loppback mysql connector installed.This will loop through all tables and "schema" will contain model definition discovered from database.

A.Z.
  • 1,638
  • 2
  • 18
  • 27
0

You can use slc arc to generate the models based on your MySQL tables, after that you should be able to use the API to perform the basic CRUD operations. In the following link you can find more information about it:

https://strongloop.com/node-js/arc/

Ariskay
  • 168
  • 1
  • 7