loopbackio / loopback-connector-elastic-search

Strongloop Loopback connector for Elasticsearch
MIT License
78 stars 57 forks source link

Connector calls don't wait until its initialized #7

Open pulkitsinghal opened 9 years ago

pulkitsinghal commented 9 years ago

Any pointers to code would be appreciated.

I thought I had already accomplished the right flow via these lines of code:

dataSource.connector.connect(callback);
...
ESConnector.prototype.connect = function (callback) {
...
if(self.settings.mappings) {
  self.setupMappings(callback);
}
...

But apparently not.

mrfelton commented 8 years ago

connectors emit a connected event once the connecter has finished initialising. Are connectors supposed to queue up their queries somehow until the connected event has been emitted?

As a workaround in our application, we have a boot script like this which ensures that queries are not made against the connector until it is truly ready:

'use strict';

var logger = require('../modules/fc/logger');

module.exports = function (app, cb) {
  logger.debug('Boot waiting for ElasticSearch initialization...');

  // As soon as the datasource has been connected, move on.
  app.dataSources.elasticsearch.once('connected', function () {
    logger.debug('ElasticSearch connected, boot continuing');
    cb();
  });

};