implement

Signed-off-by: Fredrik Adelöw <freben@gmail.com>
This commit is contained in:
Fredrik Adelöw
2026-02-20 10:55:24 +01:00
parent f076495eea
commit f3b3b1dafb
2 changed files with 32 additions and 1 deletions
@@ -181,6 +181,18 @@ describe.each(databases.eachSupportedId())(
).resolves.toEqual(['bare-e']);
});
it('filters with $hasPrefix', async () => {
await expect(
query({ 'metadata.name': { $hasPrefix: 'service' } }),
).resolves.toEqual(['service-a', 'service-b']);
});
it('filters with $hasPrefix case-insensitively', async () => {
await expect(
query({ 'metadata.name': { $hasPrefix: 'Service' } }),
).resolves.toEqual(['service-a', 'service-b']);
});
it('handles nested logical operators', async () => {
await expect(
query({
@@ -61,6 +61,12 @@ function isInValue(
return typeof value === 'object' && value !== null && '$in' in value;
}
function isHasPrefixValue(
value: FilterPredicateValue,
): value is { $hasPrefix: string } {
return typeof value === 'object' && value !== null && '$hasPrefix' in value;
}
function isFieldExpression(filter: FilterPredicate): boolean {
if (typeof filter !== 'object' || filter === null) {
return false;
@@ -202,6 +208,19 @@ function applyPredicateInStrategy(
.where({ key: normalizedKey })
.whereIn('value', values);
this.andWhere(onEntityIdField, 'in', matchQuery);
} else if (isHasPrefixValue(value)) {
// Handle $hasPrefix
const prefix = value.$hasPrefix.toLowerCase();
const escaped = prefix.replace(/[%_\\]/g, c => `\\${c}`);
const matchQuery = knex<DbSearchRow>('search')
.select('search.entity_id')
.where({ key: normalizedKey })
.andWhereRaw('?? like ? escape ?', [
'value',
`${escaped}%`,
'\\',
]);
this.andWhere(onEntityIdField, 'in', matchQuery);
} else if (isPrimitive(value)) {
// Handle direct value match
const matchQuery = knex<DbSearchRow>('search')
@@ -214,7 +233,7 @@ function applyPredicateInStrategy(
} else {
// Reject unsupported/invalid predicate values
throw new InputError(
`Invalid filter predicate value for field "${key}": expected a primitive value, $exists, or $in operator, but got ${JSON.stringify(
`Invalid filter predicate value for field "${key}": expected a primitive value, $exists, $in, or $hasPrefix operator, but got ${JSON.stringify(
value,
)}`,
);