Skip to content

Commit

Permalink
Merge pull request #1142 from cardstack/cs-6659-support-for-every-fil…
Browse files Browse the repository at this point in the history
…ter-queries

Add "every" filter support for our queries
  • Loading branch information
habdelra authored Apr 5, 2024
2 parents 4e3ac0d + 6accd31 commit 4820358
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 0 deletions.
62 changes: 62 additions & 0 deletions packages/host/tests/unit/query-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -532,6 +532,68 @@ module('Unit | query', function (hooks) {
);
});

test(`can use 'every' to combine multiple filters`, async function (assert) {
let { mango, vangogh, ringo } = testCards;
await setupIndex(client, [
{
card: mango,
data: {
search_doc: {
name: 'Mango',
address: {
street: '123 Main Street',
city: 'Barksville',
},
},
},
},
{
card: vangogh,
data: {
search_doc: {
name: 'Van Gogh',
address: {
street: '456 Grand Blvd',
city: 'Barksville',
},
},
},
},
{
card: ringo,
data: {
search_doc: {
name: 'Ringo',
address: {
street: '100 Treat Street',
city: 'Waggington',
},
},
},
},
]);

let { cards, meta } = await client.search(
{
filter: {
on: { module: `${testRealmURL}person`, name: 'Person' },
every: [
{
eq: { 'address.city': 'Barksville' },
},
{
not: { eq: { 'address.street': '456 Grand Blvd' } },
},
],
},
},
loader,
);

assert.strictEqual(meta.page.total, 1, 'the total results meta is correct');
assert.deepEqual(getIds(cards), [mango.id], 'results are correct');
});

test(`gives a good error when query refers to missing card`, async function (assert) {
await setupIndex(client, []);

Expand Down
5 changes: 5 additions & 0 deletions packages/runtime-common/indexer/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -251,6 +251,11 @@ export class IndexerDBClient {
return this.eqCondition(filter, on);
} else if ('not' in filter) {
return this.notCondition(filter, on);
} else if ('every' in filter) {
// on = filter.on ?? on;
return every(
filter.every.map((i) => this.filterCondition(i, filter.on ?? on)),
);
}

// TODO handle filters for: any, every, contains, and range
Expand Down

0 comments on commit 4820358

Please sign in to comment.