Skip to content

Commit

Permalink
Merge pull request #214 from jlami/eventually-consistent-docs-flag
Browse files Browse the repository at this point in the history
test + flag + doc for eventually-consistent
  • Loading branch information
broerse authored Jan 26, 2018
2 parents fa05f2a + 0bf4575 commit 1a32c40
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 5 deletions.
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -550,6 +550,14 @@ function changeProjectDatabase(dbName, dbUser, dbPassword) {
```


## Eventually Consistent

Following the CouchDB consistency model, we have introduced `ENV.emberPouch.eventuallyConsistent`. This feature is on by default. So if you want the old behavior you'll have to disable this flag.

`findRecord` now returns a long running Promise if the record is not found. It only rejects the promise if a deletion of the record is found. Otherwise this promise will wait for eternity to resolve.
This makes sure that belongsTo relations that have been loaded in an unexpected order will still resolve correctly. This makes sure that ember-data does not set the belongsTo to null if the Pouch replicate would have loaded the related object later on. (This only works for async belongsTo, sync versions will need this to be implemented in relational-pouch)


## Installation

* `git clone` this repository
Expand Down
8 changes: 6 additions & 2 deletions addon/adapters/pouch.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ import { pluralize } from 'ember-inflector';

import {
extractDeleteRecord,
shouldSaveRelationship
shouldSaveRelationship,
configFlagDisabled
} from '../utils';

const {
Expand Down Expand Up @@ -418,7 +419,10 @@ export default DS.RESTAdapter.extend({
}
}

return this._eventuallyConsistent(recordTypeName, id);
if (configFlagDisabled(this, 'eventuallyConsistent'))
throw "Document of type '" + recordTypeName + "' with id '" + id + "' not found.";
else
return this._eventuallyConsistent(recordTypeName, id);
});
},

Expand Down
22 changes: 19 additions & 3 deletions addon/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,25 @@ export function shouldSaveRelationship(container, relationship) {
if (relationship.kind === 'belongsTo') return true;

//TODO: save default locally? probably on container?
let config = Ember.getOwner(container).resolveRegistration('config:environment');
let saveDefault = config['emberPouch'] && config['emberPouch']['saveHasMany'];
//default is false if not specified
let saveDefault = configFlagEnabled('saveHasMany');//default is false if not specified

return saveDefault;
}

export function configFlagDisabled(container, key) {
//default is on
let config = Ember.getOwner(container).resolveRegistration('config:environment');
let result = config['emberPouch'] &&
(typeof config['emberPouch'][key] !== 'undefined') &&
!config['emberPouch'][key];

return result;
}

export function configFlagEnabled(container, key) {
//default is off
let config = Ember.getOwner(container).resolveRegistration('config:environment');
let result = config['emberPouch'] && config['emberPouch'][key];

return result;
}
22 changes: 22 additions & 0 deletions tests/integration/adapters/pouch-basics-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -437,6 +437,28 @@ test('delete cascade null', function (assert) {
'deleted soup should have cascaded to a null value for the belongsTo');
}).finally(done);
});

module('not eventually consistent', { beforeEach: function() {
config.emberPouch.eventuallyConsistent = false;
},
afterEach: function() {
config.emberPouch.eventuallyConsistent = true;
}
}, function() {
test('not found', function (assert) {
assert.expect(2);
assert.ok(config.emberPouch.eventuallyConsistent == false, 'eventuallyConsistent is false');
let done = assert.async();

Ember.RSVP.Promise.resolve().then(() => this.store().findRecord('food-item', 'non-existent')
.then(() => assert.ok(false))
.catch(() => {
assert.ok(true, 'item is not found');
done();
}));
});
});

};

let syncAsync = function() {
Expand Down

0 comments on commit 1a32c40

Please sign in to comment.