Skip to content

Commit

Permalink
Added $in and $notIn search operators (#98)
Browse files Browse the repository at this point in the history
Resolves #97.
  • Loading branch information
karelklima authored Jan 11, 2024
1 parent e39cad3 commit 718bbb3
Show file tree
Hide file tree
Showing 5 changed files with 70 additions and 1 deletion.
16 changes: 15 additions & 1 deletion docs/v2/filtering.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ for controlled data retrieval.

LDkit allows various search and filtering operations like `$equals`, `$not`,
`$contains`, `$strStarts`, `$strEnds`, `$gt`, `$lt`, `$gte`, `$lte`, `$regex`,
`$langMatches`, and `$filter`. Each is illustrated below with examples.
`$langMatches`, `$in`, `$notIn`, and `$filter`. Each is illustrated below with
examples.

### Simple example

Expand Down Expand Up @@ -70,6 +71,19 @@ await Persons.find({
});
```

### Array functions

```typescript
await Persons.find({
where: {
name: {
$in: ["Ada", "Alan"], // FILTER (?value IN ("Ada", "Alan"))
$notIn: ["Ada", "Alan"], // FILTER (?value NOT IN ("Ada", "Alan"))
},
},
});
```

### Custom filtering

On top of the above, it is possible to specify a custom filter function using
Expand Down
20 changes: 20 additions & 0 deletions library/lens/search_helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ export class SearchHelper {
this.processStringFunctions();
this.processRegex();
this.processLangMatches();
this.processArrayFunctions();
this.processFilter();
}

Expand Down Expand Up @@ -102,6 +103,25 @@ export class SearchHelper {
);
}

private processArrayFunctions() {
const map = {
$in: "IN",
$notIn: "NOT IN",
};

for (const [key, func] of Object.entries(map)) {
const value = this.searchSchema[key];
if (value === undefined) {
continue;
}

const values = (value as unknown[]).map((v) => $`${this.encode(v)}`);
this.addFilter(
$`${this.df.variable(this.varName)} ${func} (${values.join(", ")})`,
);
}
}

private processFilter() {
const value = this.searchSchema.$filter;
if (value === undefined) {
Expand Down
2 changes: 2 additions & 0 deletions library/schema/search.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ export type SearchFilters<T> = T | {
$lte?: T;
$regex?: string;
$langMatches?: string;
$in?: T[];
$notIn?: T[];
$filter?: SparqlValue;
};

Expand Down
31 changes: 31 additions & 0 deletions tests/e2e/search.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -217,3 +217,34 @@ Deno.test("E2E / Search / $filter", async () => {
assertEquals(results.length, 1);
assertEquals(results[0].name, "Quentin Tarantino");
});

Deno.test("E2E / Search / $in", async () => {
const { Directors } = init();

const results = await Directors.find({
where: {
name: {
$in: ["Quentin Tarantino", "Steven Spielberg"],
},
},
});

assertEquals(results.length, 2);
assertEquals(results[0].name, "Quentin Tarantino");
assertEquals(results[1].name, "Steven Spielberg");
});

Deno.test("E2E / Search / $notIn", async () => {
const { Directors } = init();

const results = await Directors.find({
where: {
name: {
$notIn: ["Quentin Tarantino", "Steven Spielberg"],
},
},
});

assertEquals(results.length, 1);
assertEquals(results[0].name, "Stanley Kubrick");
});
2 changes: 2 additions & 0 deletions tests/schema.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ type PropertySearch<T> = T | {
$lte?: T;
$regex?: string;
$langMatches?: string;
$in?: T[];
$notIn?: T[];
$filter?: SparqlValue;
};

Expand Down

0 comments on commit 718bbb3

Please sign in to comment.