Get Datasource not-blocking?
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 -- 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?
Yes, my code is equivalent.
Also using following code
var ds = require('loopback')().datasources;
I get {} as result. No other results.
Thanks Thomas
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/
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.
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
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?
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.
Please submit a new issue if needed. closing one month or older issues. thanks!