Merge pull request #630 from PradnyaBaviskar/issue-418

Promisify 'automigrate'
This commit is contained in:
Miroslav Bajtoš 2015-06-16 10:46:07 +03:00
commit ecb8968166
2 changed files with 75 additions and 5 deletions

View File

@ -775,9 +775,12 @@ DataSource.prototype.automigrate = function (models, cb) {
models = undefined; models = undefined;
} }
cb = cb || utils.createPromiseCallback();
if (!this.connector.automigrate) { if (!this.connector.automigrate) {
// NOOP // NOOP
return cb && process.nextTick(cb); process.nextTick(cb);
return cb.promise;
} }
// First argument is a model name // First argument is a model name
@ -791,7 +794,8 @@ DataSource.prototype.automigrate = function (models, cb) {
models = models || Object.keys(attachedModels); models = models || Object.keys(attachedModels);
if (models.length === 0) { if (models.length === 0) {
return cb && process.nextTick(cb); process.nextTick(cb);
return cb.promise;
} }
var invalidModels = models.filter(function (m) { var invalidModels = models.filter(function (m) {
@ -799,16 +803,16 @@ DataSource.prototype.automigrate = function (models, cb) {
}); });
if (invalidModels.length) { if (invalidModels.length) {
return process.nextTick(function () { process.nextTick(function () {
if (cb) {
cb(new Error('Cannot migrate models not attached to this datasource: ' + cb(new Error('Cannot migrate models not attached to this datasource: ' +
invalidModels.join(' '))); invalidModels.join(' ')));
}
}); });
return cb.promise;
} }
} }
this.connector.automigrate(models, cb); this.connector.automigrate(models, cb);
return cb.promise;
}; };
/** /**

View File

@ -463,25 +463,91 @@ describe('Memory connector', function() {
}); });
}); });
it('automigrate all models - promise variant', function(done) {
ds.automigrate()
.then(function(result) {
done();
})
.catch(function(err){
done(err);
});
});
it('automigrate one model', function(done) { it('automigrate one model', function(done) {
ds.automigrate('m1', function(err) { ds.automigrate('m1', function(err) {
done(err); done(err);
}); });
}); });
it('automigrate one model - promise variant', function(done) {
ds.automigrate('m1')
.then(function(result) {
done();
})
.catch(function(err){
done(err);
});
});
it('automigrate one or more models in an array', function(done) { it('automigrate one or more models in an array', function(done) {
ds.automigrate(['m1'], function(err) { ds.automigrate(['m1'], function(err) {
done(err); done(err);
}); });
}); });
it('automigrate one or more models in an array - promise variant', function(done) {
ds.automigrate(['m1'])
.then(function(result) {
done();
})
.catch(function(err){
done(err);
});
});
it('automigrate reports errors for models not attached', function(done) { it('automigrate reports errors for models not attached', function(done) {
ds.automigrate(['m1', 'm2'], function(err) { ds.automigrate(['m1', 'm2'], function(err) {
err.should.be.an.instanceOf(Error); err.should.be.an.instanceOf(Error);
done(); done();
}); });
}); });
it('automigrate reports errors for models not attached - promise variant', function(done) {
ds.automigrate(['m1', 'm2'])
.then(function(){
done(new Error('automigrate() should have failed'));
})
.catch(function(err){
err.should.be.an.instanceOf(Error);
done();
}); });
});
});
describe('automigrate when NO models are attached', function() {
var ds;
beforeEach(function() {
ds = new DataSource({
connector: 'memory'
});
});
it('automigrate does NOT report error when NO models are attached', function(done) {
ds.automigrate(function(err) {
done();
})
});
it('automigrate does NOT report error when NO models are attached - promise variant', function(done) {
ds.automigrate()
.then(done)
.catch(function(err){
done(err);
});
});
});
}); });
describe('Optimized connector', function() { describe('Optimized connector', function() {