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

Implement queries #7 #74

Closed
wants to merge 4 commits into from
Closed
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
33 changes: 29 additions & 4 deletions addon/adapters/pouch.js
Original file line number Diff line number Diff line change
Expand Up @@ -204,10 +204,35 @@ export default DS.RESTAdapter.extend({
return this.db.rel.find(this.getRecordTypeName(type), ids);
},

findQuery: function(/* store, type, query */) {
throw new Error(
"findQuery not yet supported by ember-pouch. " +
"See https://github.com/nolanlawson/ember-pouch/issues/7.");
matchQuery: function (record, query) {
for(let property in query) {
if (query.hasOwnProperty(property)) {
const value = query[property];
if (Object.prototype.toString.call(value) === '[object RegExp]') {
if (!value.test(record[property])) {
return false;
}
} else if (record[property] !== value) {
return false;
}
}
}
//all query items were matched:
return true;
},

findQuery: function(store, type, query) {
var self = this;
this._init(store, type);
var recordTypeName = this.getRecordTypeName(type);
return this.db.rel.find(recordTypeName).then((res) => {
const records = res[pluralize(recordTypeName)];
const results = {};
results[pluralize(recordTypeName)] = records.filter((record) => {
return self.matchQuery(record, query);
});
return results;
});
},

find: function (store, type, id) {
Expand Down
1 change: 1 addition & 0 deletions tests/dummy/app/models/taco-soup.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,6 @@ export default DS.Model.extend({
rev: DS.attr('string'),

flavor: DS.attr('string'),
breadFlour: DS.attr('string', undefined),
ingredients: DS.hasMany('food-item', { async: true })
});
98 changes: 95 additions & 3 deletions tests/integration/adapters/pouch-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,13 +54,13 @@ function store() {
}

test('can find all', function (assert) {
assert.expect(3);
assert.expect(4);

var done = assert.async();
Ember.RSVP.Promise.resolve().then(() => {
return db().bulkDocs([
{ _id: 'tacoSoup_2_A', data: { flavor: 'al pastor' } },
{ _id: 'tacoSoup_2_B', data: { flavor: 'black bean' } },
{ _id: 'tacoSoup_2_A', data: { flavor: 'al pastor', breadFlour: 'wheat'} },
{ _id: 'tacoSoup_2_B', data: { flavor: 'black bean', breadFlour: 'wheat'} },
{ _id: 'burritoShake_2_X', data: { consistency: 'smooth' } }
]);
}).then(() => {
Expand All @@ -71,6 +71,8 @@ test('can find all', function (assert) {
'should have extracted the IDs correctly');
assert.deepEqual(found.mapBy('flavor'), ['al pastor', 'black bean'],
'should have extracted the attributes also');
assert.deepEqual(found.mapBy('breadFlour'), ['wheat', 'wheat'],
'should have extracted the attributes also');
done();
}).catch((error) => {
console.error('error in test', error);
Expand Down Expand Up @@ -134,6 +136,96 @@ test('can find associated records', function (assert) {
});
});

test('can find black bean tacos (query equal)', function (assert) {
assert.expect(4);

var done = assert.async();
Ember.RSVP.Promise.resolve().then(() => {
return db().bulkDocs([
{ _id: 'tacoSoup_2_A', data: { flavor: 'al pastor', breadFlour: 'wheat'} },
{ _id: 'tacoSoup_2_B', data: { flavor: 'black bean', breadFlour: 'wheat' } },
{ _id: 'tacoSoup_2_C', data: { flavor: 'red bean', breadFlour: 'wheat' } },
{ _id: 'tacoSoup_2_D', data: { flavor: 'green bean', breadFlour: 'wheat' } },
{ _id: 'tacoSoup_2_E', data: { flavor: 'black bean', breadFlour: 'barley' } },
{ _id: 'burritoShake_2_X', data: { consistency: 'smooth' } }
]);
}).then(() => {
return store().find('taco-soup', {flavor: 'black bean'});
}).then((found) => {
assert.equal(found.get('length'), 2, 'should have found the 2 taco soups with the black bean flavor');
assert.deepEqual(found.mapBy('id'), ['B', 'E'],
'should have extracted the IDs correctly');
assert.deepEqual(found.mapBy('flavor'), ['black bean', 'black bean'],
'should have extracted the attributes also');
assert.deepEqual(found.mapBy('breadFlour'), ['wheat', 'barley'],
'should have extracted the attributes also');
done();
}).catch((error) => {
console.error('error in test', error);
assert.ok(false, 'error in test:' + error);
done();
});
});

test('can find black bean tacos with barley breadFlour (multiple equal query)', function (assert) {
assert.expect(4);

var done = assert.async();
Ember.RSVP.Promise.resolve().then(() => {
return db().bulkDocs([
{ _id: 'tacoSoup_2_A', data: { flavor: 'al pastor', breadFlour: 'wheat'} },
{ _id: 'tacoSoup_2_B', data: { flavor: 'black bean', breadFlour: 'wheat' } },
{ _id: 'tacoSoup_2_C', data: { flavor: 'red bean', breadFlour: 'wheat' } },
{ _id: 'tacoSoup_2_D', data: { flavor: 'green bean', breadFlour: 'wheat' } },
{ _id: 'tacoSoup_2_E', data: { flavor: 'black bean', breadFlour: 'barley' } },
{ _id: 'burritoShake_2_X', data: { consistency: 'smooth' } }
]);
}).then(() => {
return store().find('taco-soup', {flavor: 'black bean', breadFlour: 'barley'});
}).then((found) => {
assert.equal(found.get('length'), 1, 'should have found the taco soup with the black bean flavor');
assert.deepEqual(found.mapBy('id'), ['E'],
'should have extracted the IDs correctly');
assert.deepEqual(found.mapBy('flavor'), ['black bean'],
'should have extracted the attributes also');
assert.deepEqual(found.mapBy('breadFlour'), ['barley'],
'should have extracted the attributes also');
done();
}).catch((error) => {
console.error('error in test', error);
assert.ok(false, 'error in test:' + error);
done();
});
});

test('can find any bean tacos (query regexp)', function (assert) {
assert.expect(3);

var done = assert.async();
Ember.RSVP.Promise.resolve().then(() => {
return db().bulkDocs([
{ _id: 'tacoSoup_2_A', data: { flavor: 'al pastor' } },
{ _id: 'tacoSoup_2_B', data: { flavor: 'black bean' } },
{ _id: 'tacoSoup_2_C', data: { flavor: 'red bean' } },
{ _id: 'tacoSoup_2_D', data: { flavor: 'green bean' } },
{ _id: 'burritoShake_2_X', data: { consistency: 'smooth' } }
]);
}).then(() => {
return store().find('taco-soup', {flavor: /.*bean/ });
}).then((found) => {
assert.equal(found.get('length'), 3, 'should have found the taco soup with the black bean flavor');
assert.deepEqual(found.mapBy('id'), ['B', 'C', 'D'],
'should have extracted the IDs correctly');
assert.deepEqual(found.mapBy('flavor'), ['black bean', 'red bean', 'green bean'],
'should have extracted the attributes also');
done();
}).catch((error) => {
console.error('error in test', error);
assert.ok(false, 'error in test:' + error);
done();
});
});

test('create a new record', function (assert) {
assert.expect(1);

Expand Down