Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test + flag + doc for eventually-consistent #214

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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