feat: add ability to check for existence of a field

This commit is contained in:
2024-01-12 10:58:49 +01:00
parent 40d72fa0cc
commit b67a0a9c5a
3 changed files with 31 additions and 8 deletions

View File

@@ -0,0 +1,6 @@
---
"@openstapps/backend": minor
"@openstapps/core": minor
---
Add the ability to filter by existence of a field

View File

@@ -21,16 +21,31 @@ import {QueryDslSpecificQueryContainer} from '../../types/util.js';
*/
export function buildValueFilter(
filter: SCSearchValueFilter,
): QueryDslSpecificQueryContainer<'term'> | QueryDslSpecificQueryContainer<'terms'> {
return Array.isArray(filter.arguments.value)
? {
terms: {
[`${filter.arguments.field}.raw`]: filter.arguments.value,
):
| QueryDslSpecificQueryContainer<'exists'>
| QueryDslSpecificQueryContainer<'term'>
| QueryDslSpecificQueryContainer<'terms'> {
switch (typeof filter.arguments.value) {
case 'undefined': {
return {
exists: {
field: filter.arguments.field,
},
}
: {
};
}
case 'string': {
return {
term: {
[`${filter.arguments.field}.raw`]: filter.arguments.value,
},
};
}
case 'object': {
return {
terms: {
[`${filter.arguments.field}.raw`]: filter.arguments.value,
},
};
}
}
}

View File

@@ -33,6 +33,8 @@ export interface SCValueFilterArguments extends SCSearchAbstractFilterArguments
/**
* Value to filter. One or more values has to match the field exactly.
*
* Leaving the value out will check if the field exists.
*/
value: string | string[];
value?: string | string[];
}