Skip to content

Commit

Permalink
Merge pull request #1141 from cardstack/cs-6660-support-for-not-filte…
Browse files Browse the repository at this point in the history
…r-queries

Add "not" filter support for our queries
  • Loading branch information
habdelra authored Apr 5, 2024
2 parents 94abfaa + 78c1c1c commit 4e3ac0d
Show file tree
Hide file tree
Showing 2 changed files with 113 additions and 2 deletions.
94 changes: 94 additions & 0 deletions packages/host/tests/unit/query-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -438,6 +438,100 @@ module('Unit | query', function (hooks) {
assert.deepEqual(getIds(cards), [mangoBirthday.id], 'results are correct');
});

test(`can search with a 'not' filter`, 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' },
not: { eq: { name: 'Mango' } },
},
},
loader,
);

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

test('can handle a filter with double negatives', 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' },
not: { not: { not: { eq: { name: 'Mango' } } } },
},
},
loader,
);

assert.strictEqual(meta.page.total, 2, 'the total results meta is correct');
assert.deepEqual(
getIds(cards),
[ringo.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
21 changes: 19 additions & 2 deletions packages/runtime-common/indexer/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,12 @@ import {
fieldValue,
fieldArity,
} from './expression';
import { type Query, type Filter, type EqFilter } from '../query';
import {
type Query,
type Filter,
type EqFilter,
type NotFilter,
} from '../query';
import { type SerializedError } from '../error';
import { type DBAdapter } from '../db';
import { type SearchEntryWithErrors } from '../search-index';
Expand Down Expand Up @@ -244,12 +249,16 @@ export class IndexerDBClient {

if ('eq' in filter) {
return this.eqCondition(filter, on);
} else if ('not' in filter) {
return this.notCondition(filter, on);
}

// TODO handle filters for: any, every, not, contains, and range
// TODO handle filters for: any, every, contains, and 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
// get type errors if new filters are introduced
throw new Error(`Unknown filter: ${JSON.stringify(filter)}`);
}

Expand All @@ -272,6 +281,14 @@ export class IndexerDBClient {
]);
}

private notCondition(filter: NotFilter, on: CodeRef): CardExpression {
on = filter.on ?? on;
return every([
this.typeCondition(on),
['NOT', ...addExplicitParens(this.filterCondition(filter.not, on))],
]);
}

private fieldFilter(
key: string,
value: JSONTypes.Value,
Expand Down

0 comments on commit 4e3ac0d

Please sign in to comment.