Open doomedramen opened 8 years ago
The ready event is available for Model, not globally at the moment. It shouldn't be too hard to add though.
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.
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.
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
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
})
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.
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)
ignore me!, i was a few versions behind.
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