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

Added $in and $notIn search operators #98

Merged
merged 2 commits into from
Jan 11, 2024
Merged
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
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
Loading