Merge pull request #638 from PradnyaBaviskar/issue-418-autoupdate
Promisify 'autoupdate'
This commit is contained in:
commit
d495a26a76
|
@ -830,9 +830,12 @@ DataSource.prototype.autoupdate = function (models, cb) {
|
||||||
models = undefined;
|
models = undefined;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
cb = cb || utils.createPromiseCallback();
|
||||||
|
|
||||||
if (!this.connector.autoupdate) {
|
if (!this.connector.autoupdate) {
|
||||||
// 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
|
||||||
|
@ -846,7 +849,8 @@ DataSource.prototype.autoupdate = function (models, cb) {
|
||||||
models = models || Object.keys(attachedModels);
|
models = models || Object.keys(attachedModels);
|
||||||
|
|
||||||
if (models.length === 0) {
|
if (models.length === 0) {
|
||||||
return process.nextTick(cb);
|
process.nextTick(cb);
|
||||||
|
return cb.promise;
|
||||||
}
|
}
|
||||||
|
|
||||||
var invalidModels = models.filter(function (m) {
|
var invalidModels = models.filter(function (m) {
|
||||||
|
@ -854,16 +858,16 @@ DataSource.prototype.autoupdate = 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.autoupdate(models, cb);
|
this.connector.autoupdate(models, cb);
|
||||||
|
return cb.promise;
|
||||||
};
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
|
@ -548,6 +548,95 @@ describe('Memory connector', function() {
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
describe('With mocked autoupdate', function() {
|
||||||
|
var ds, model;
|
||||||
|
beforeEach(function() {
|
||||||
|
ds = new DataSource({
|
||||||
|
connector: 'memory'
|
||||||
|
});
|
||||||
|
|
||||||
|
ds.connector.autoupdate = function(models, cb) {
|
||||||
|
process.nextTick(cb);
|
||||||
|
};
|
||||||
|
|
||||||
|
model = ds.createModel('m1', {
|
||||||
|
name: String
|
||||||
|
});
|
||||||
|
|
||||||
|
ds.automigrate();
|
||||||
|
|
||||||
|
ds.createModel('m1', {
|
||||||
|
name: String,
|
||||||
|
address: String
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
it('autoupdates all models', function(done) {
|
||||||
|
ds.autoupdate(function(err, result){
|
||||||
|
done(err);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
it('autoupdates all models - promise variant', function(done) {
|
||||||
|
ds.autoupdate()
|
||||||
|
.then(function(result) {
|
||||||
|
done();
|
||||||
|
})
|
||||||
|
.catch(function(err){
|
||||||
|
done(err);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
it('autoupdates one model', function(done) {
|
||||||
|
ds.autoupdate('m1', function(err) {
|
||||||
|
done(err);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
it('autoupdates one model - promise variant', function(done) {
|
||||||
|
ds.autoupdate('m1')
|
||||||
|
.then(function(result) {
|
||||||
|
done();
|
||||||
|
})
|
||||||
|
.catch(function(err){
|
||||||
|
done(err);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
it('autoupdates one or more models in an array', function(done) {
|
||||||
|
ds.autoupdate(['m1'], function(err) {
|
||||||
|
done(err);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
it('autoupdates one or more models in an array - promise variant', function(done) {
|
||||||
|
ds.autoupdate(['m1'])
|
||||||
|
.then(function(result) {
|
||||||
|
done();
|
||||||
|
})
|
||||||
|
.catch(function(err){
|
||||||
|
done(err);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
it('autoupdate reports errors for models not attached', function(done) {
|
||||||
|
ds.autoupdate(['m1', 'm2'], function(err) {
|
||||||
|
err.should.be.an.instanceOf(Error);
|
||||||
|
done();
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
it('autoupdate reports errors for models not attached - promise variant', function(done) {
|
||||||
|
ds.autoupdate(['m1', 'm2'])
|
||||||
|
.then(function(){
|
||||||
|
done(new Error('automigrate() should have failed'));
|
||||||
|
})
|
||||||
|
.catch(function(err){
|
||||||
|
err.should.be.an.instanceOf(Error);
|
||||||
|
done();
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
describe('Optimized connector', function() {
|
describe('Optimized connector', function() {
|
||||||
|
|
Loading…
Reference in New Issue