Add option disabling periodic change rectification

When `Model.settings.changeCleanupInterval` is set to a negative value,
no periodic cleanup is performed at all.
This commit is contained in:
kobaska 2016-11-21 19:19:11 +11:00 committed by Miroslav Bajtoš
parent fe1c0b605b
commit b3a5bc739b
2 changed files with 76 additions and 2 deletions

View File

@ -1597,14 +1597,16 @@ module.exports = function(registry) {
var idDefn = idProp && idProp.defaultFn;
if (idType !== String || !(idDefn === 'uuid' || idDefn === 'guid')) {
deprecated('The model ' + this.modelName + ' is tracking changes, ' +
'which requries a string id with GUID/UUID default value.');
'which requires a string id with GUID/UUID default value.');
}
Model.observe('after save', rectifyOnSave);
Model.observe('after delete', rectifyOnDelete);
if (runtime.isServer) {
// Only run if the run time is server
// Can switch off cleanup by setting the interval to -1
if (runtime.isServer && cleanupInterval > 0) {
// initial cleanup
cleanup();

View File

@ -11,6 +11,7 @@ var defineModelTestsWithDataSource = require('./util/model-tests');
var PersistedModel = loopback.PersistedModel;
var expect = require('chai').expect;
var debug = require('debug')('test');
var runtime = require('./../lib/runtime');
describe('Replication / Change APIs', function() {
this.timeout(10000);
@ -60,6 +61,77 @@ describe('Replication / Change APIs', function() {
};
});
describe('cleanup check for enableChangeTracking', function() {
describe('when no changeCleanupInterval set', function() {
it('should call rectifyAllChanges if running on server', function(done) {
var calls = mockRectifyAllChanges(SourceModel);
SourceModel.enableChangeTracking();
if (runtime.isServer) {
expect(calls).to.eql(['rectifyAllChanges']);
} else {
expect(calls).to.eql([]);
}
done();
});
});
describe('when changeCleanupInterval set to -1', function() {
var Model;
beforeEach(function() {
Model = this.Model = PersistedModel.extend(
'Model-' + tid,
{id: {id: true, type: String, defaultFn: 'guid'}},
{trackChanges: true, changeCleanupInterval: -1});
Model.attachTo(dataSource);
});
it('should not call rectifyAllChanges', function(done) {
var calls = mockRectifyAllChanges(Model);
Model.enableChangeTracking();
expect(calls).to.eql([]);
done();
});
});
describe('when changeCleanupInterval set to 10000', function() {
var Model;
beforeEach(function() {
Model = this.Model = PersistedModel.extend(
'Model-' + tid,
{id: {id: true, type: String, defaultFn: 'guid'}},
{trackChanges: true, changeCleanupInterval: 10000});
Model.attachTo(dataSource);
});
it('should call rectifyAllChanges if running on server', function(done) {
var calls = mockRectifyAllChanges(Model);
Model.enableChangeTracking();
if (runtime.isServer) {
expect(calls).to.eql(['rectifyAllChanges']);
} else {
expect(calls).to.eql([]);
}
done();
});
});
function mockRectifyAllChanges(Model) {
var calls = [];
Model.rectifyAllChanges = function(cb) {
calls.push('rectifyAllChanges');
process.nextTick(cb);
};
return calls;
}
});
describe('optimization check rectifyChange Vs rectifyAllChanges', function() {
beforeEach(function initialData(done) {
var data = [{name: 'John', surname: 'Doe'}, {name: 'Jane', surname: 'Roe'}];