diff --git a/addon/adapters/pouch.js b/addon/adapters/pouch.js index 2d46991..de40306 100644 --- a/addon/adapters/pouch.js +++ b/addon/adapters/pouch.js @@ -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 = {}; + let recordType = this.getRecordTypeName(type); + if(results[pluralize(recordType)].length > 0){ + result[recordType] = results[pluralize(recordType)][0]; + } else { + result[recordType] = null; + } + return result; + }); }, /** diff --git a/tests/integration/adapters/pouch-basics-test.js b/tests/integration/adapters/pouch-basics-test.js index 9573f10..d455dd8 100644 --- a/tests/integration/adapters/pouch-basics-test.js +++ b/tests/integration/adapters/pouch-basics-test.js @@ -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; }