loopback icon indicating copy to clipboard operation
loopback copied to clipboard

Get Datasource not-blocking?

Open bolognesi opened this issue 9 years ago • 8 comments

Hello, I have a problem getting datasources object in autoupdate or automigrate scripts. It seems that obtaining the datasources requires a few seconds, but the method is not blocking, so the result returned immediately is empty/not valid.

Description/Steps to reproduce

content of simple script (in folder /bin):

var app = require(path.resolve(myDir, '../server/server')); var ds = app.datasources; // here ds is {}

while if you add a timeout (for instance 3000 ms)

var app = require(path.resolve(myDir, '../server/server')); setTimeout(function() { var ds = app.datasources; // here ds is correct },3000);

Expected result

Additional information

linux x64 7.10.0

├── [email protected] ├── [email protected] ├── [email protected] ├── [email protected] ├── [email protected]

bolognesi avatar Jun 12 '17 08:06 bolognesi

@bolognesi -- Your code:

var app = require(path.resolve(myDir, '../server/server'));
var ds = app.datasources; // here ds is {}

is equivalent to the following? It depends on your server/server.js.

var app = require('loopback')();
var ds = app.datasources;

or

var ds = require('loopback')().datasources;

Do you get {} or something else with ds in the above code?

Setogit avatar Jun 12 '17 18:06 Setogit

Yes, my code is equivalent. Also using following code var ds = require('loopback')().datasources; I get {} as result. No other results.

Thanks Thomas

bolognesi avatar Jun 13 '17 06:06 bolognesi

What's your environment such as OS version, Node version, etc.? Does the "hello world" app build and run with no problem in your environment? http://loopback.io/getting-started/

Setogit avatar Jun 13 '17 16:06 Setogit

Loopback installed on Ubuntu 16.04 LTS linux x64 7.10.0 ├── [email protected] ├── [email protected] ├── [email protected] ├── [email protected] ├── [email protected]

Yes, the "hello world" app build and run with no problem. The problem is only when I want to use autoupdate or automigrate and I need to get the datasources.

bolognesi avatar Jun 14 '17 06:06 bolognesi

Great. In the server/server.js of http://loopback.io/getting-started/:

// Bootstrap the application, configure models, datasources and middleware.
// Sub-apps like REST API are mounted via boot scripts.
boot(app, __dirname, function(err) {
  if (err) throw err;

  // start the server if `$ node server.js`
  if (require.main === module) app.start();
  // @bolognesi : Is app.datasources correct here?
});

As the comment says datasources are boot strapped here. app.datasources should be non-empty once the app is bootstrapped Your app goes through such bootstrap phase too. If you can identify such point of code in your app (post bootstrap), you can avoid things like setTimeout

Setogit avatar Jun 14 '17 15:06 Setogit

yes, the datasources are correct in function boot.

But the autoupdate/automigrate js scripts should not be launched separately? I mean, outside server.js and also without the server.js running?

bolognesi avatar Jun 15 '17 06:06 bolognesi

Yes, you can calldataSource.automigrate ( http://apidocs.strongloop.com/loopback-datasource-juggler/#datasource-prototype-automigrate ) as many as you want once the dataSource is bootstrapped (instantiated, initialized, ..) first.

Setogit avatar Jun 15 '17 16:06 Setogit

Please submit a new issue if needed. closing one month or older issues. thanks!

Setogit avatar Jul 21 '17 20:07 Setogit