Skip to content
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
11 changes: 10 additions & 1 deletion addon/adapters/pouch.js
Original file line number Diff line number Diff line change
Expand Up @@ -340,7 +340,16 @@ export default DS.RESTAdapter.extend({
},

queryRecord: function(store, type, query) {
return this.query(store, type, query);
return this.query(store, type, query).then(results => {
let result = {};
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You need to edit the original results object. Since this can have additional entries, which are sideloaded.

let recordType = this.getRecordTypeName(type);
if(results[pluralize(recordType)].length > 0){
result[recordType] = results[pluralize(recordType)][0];
} else {
result[recordType] = null;
}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

after using the singular recordType to set the expected single item, you need to delete results[pluralize(recordType)]; to remove the plural entry

return result;
});
},

/**
Expand Down
28 changes: 28 additions & 0 deletions tests/integration/adapters/pouch-basics-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,34 @@ test('can query multi-field queries', function (assert) {
}).finally(done);
});

test('queryRecord returns null when no record is found', function (assert) {
var done = assert.async();
Ember.RSVP.Promise.resolve().then(() => {
return this.db().createIndex({ index: {
fields: ['data.flavor'] }
}).then(() => {
return this.db().bulkDocs([
{ _id: 'tacoSoup_2_C', data: { flavor: 'al pastor', ingredients: ['X', 'Y'] } },
{ _id: 'tacoSoup_2_D', data: { flavor: 'black bean', ingredients: ['Z'] } },
{ _id: 'foodItem_2_X', data: { name: 'pineapple' }},
{ _id: 'foodItem_2_Y', data: { name: 'pork loin' }},
{ _id: 'foodItem_2_Z', data: { name: 'black beans' }}
]);
});
}).then(() => {
return this.store().queryRecord('taco-soup', {
filter: {flavor: 'all pastor' }
});
}).then((found) => {
assert.equal(found, null, 'should be null');
done();
}).catch((error) => {
console.error('error in test', error);
assert.ok(false, 'error in test:' + error);
done();
});
});

function savingHasMany() {
return !config.emberpouch.dontsavehasmany;
}
Expand Down