Skip to content

Commit

Permalink
Add "any" support for our filter queries
Browse files Browse the repository at this point in the history
  • Loading branch information
habdelra committed Apr 5, 2024
1 parent 230eebd commit fc91df4
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 3 deletions.
54 changes: 54 additions & 0 deletions packages/host/tests/unit/query-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -750,6 +750,60 @@ module('Unit | query', function (hooks) {
assert.deepEqual(getIds(cards), [mango.id], 'results are correct');
});

test(`can use 'any' to combine multiple filters`, async function (assert) {
let { mango, vangogh, ringo } = testCards;
await setupIndex(client, [
{
card: mango,
data: {
search_doc: {
name: 'Mango',
},
},
},
{
card: vangogh,
data: {
search_doc: {
name: 'Van Gogh',
},
},
},
{
card: ringo,
data: {
search_doc: {
name: 'Ringo',
},
},
},
]);

let { cards, meta } = await client.search(
{
filter: {
on: { module: `${testRealmURL}person`, name: 'Person' },
any: [
{
eq: { name: 'Mango' },
},
{
not: { eq: { name: 'Ringo' } },
},
],
},
},
loader,
);

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

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

Expand Down
10 changes: 7 additions & 3 deletions packages/runtime-common/indexer/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import {
separatedByCommas,
addExplicitParens,
asExpressions,
any,
every,
fieldQuery,
fieldValue,
Expand Down Expand Up @@ -275,17 +276,20 @@ export class IndexerDBClient {
} 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)),
);
} else if ('any' in filter) {
return any(
filter.any.map((i) => this.filterCondition(i, filter.on ?? on)),
);
}

// TODO handle filters for: any, every, contains, and range
// TODO handle filter for range
// refer to hub v2 for a good reference:
// https://github.dev/cardstack/cardstack/blob/d36e6d114272a9107a7315d95d2f0f415e06bf5c/packages/hub/pgsearch/pgclient.ts

// TODO assert "notNever()" after we have implemented all the filters so we
// TODO assert "notNever()" after we have implemented the "range" filter so we
// get type errors if new filters are introduced
throw new Error(`Unknown filter: ${JSON.stringify(filter)}`);
}
Expand Down

0 comments on commit fc91df4

Please sign in to comment.