neumino / thinky

JavaScript ORM for RethinkDB
http://justonepixel.com/thinky/
Other
1.12k stars 128 forks source link

on('ready',function(){ #368

Open doomedramen opened 8 years ago

doomedramen commented 8 years ago

I have tests running on travis-ci but I have to put the timeout on them to 5000> because the first test that runs causes the tables to be created, is there a way to call the auto table creation with a callback so i only run the tests once its ready?

Thanks

neumino commented 8 years ago

The ready event is available for Model, not globally at the moment. It shouldn't be too hard to add though.

doomedramen commented 8 years ago

Would it be possible to call the initialisation of the database/tables manually and then get the callback rather wait for the the first time the db is called?

something lilke:

thinky.ensureReady(function(){ console.log('it is ready'); })

so if its already ...ready it will just call the cb and if its not ready it will do it then call the cb.

neumino commented 8 years ago

Models are available in thinky.models, and you can call ready that will return a promise which will be resolved when the table and its indexes are available.

doomedramen commented 8 years ago

For now I have done this:

var thinky = require('./lib/thinky');
var modelTest = require('./models/project');

var modelCount = 0;
var readyCount = 0;

var calledCallback = false;

var cb = function () {
  console.log('all models are ready');
};

for (var model in thinky.models) {
  if (thinky.models.hasOwnProperty(model)) {
    modelCount++;
    var self = thinky.models[model];
    self.on('ready', function () {
      readyCount++;
      if (readyCount == readyCount && !calledCallback) {
        calledCallback = true;
        cb();
      }
    });
  }
}

when all models are 'ready' it calls the callback.

I will put this in the before function of my tests

neumino commented 8 years ago

Another way to do it is also:

var promises = [];
for(var name in thinky.models) {
  promises.push(thinky.models[name].ready());
}
Promise.all(promises).then(function() {
  // thinky is ready
})
doomedramen commented 8 years ago

That's much tidier, cheers :)

On Tue, 20 Oct 2015, 04:33 Michel notifications@github.com wrote:

Another way to do it is also:

var promises = []; for(var name in thinky.models) { promises.push(thinky.models[name].ready()); } Promise.all(promises).then(function() { // thinky is ready })

— Reply to this email directly or view it on GitHub https://github.com/neumino/thinky/issues/368#issuecomment-149419233.

doomedramen commented 8 years ago

hmmm... any idea why 'ready()' does not exist? code

...
before(function (done) {
    var promises = [];
    for (var name in thinky.models) {
      console.log(name);
      console.log(thinky.models[name]);
      promises.push(thinky.models[name].ready());
    }
    Promise.all(promises).then(function () {
      done();
    })
  });
...

output:

Project
{ [Function: model]
  domain: [Function],
  _events: [Function],
  _maxListeners: [Function],
  setMaxListeners: [Function],
  emit: [Function],
  addListener: [Function],
  on: [Function],
  once: [Function],
  removeListener: [Function],
  removeAllListeners: [Function],
  listeners: [Function] }
TypeError: undefined is not a function
    at Context.<anonymous> (test/server.js:23:41)
doomedramen commented 8 years ago

ignore me!, i was a few versions behind.