From 6aa0b71bc358bf7055ff400cdb170dad9b9ff047 Mon Sep 17 00:00:00 2001 From: Nik Nasr Date: Thu, 2 Jan 2025 15:31:13 +0000 Subject: [PATCH] add filter conversion Signed-off-by: Nik Nasr --- .../admin-api/src/lib/api/index.d.ts | 2 +- .../admin-api/src/lib/api/output.json | 2 +- .../admin-api/src/lib/api/query.json | 2 +- .../data-access/admin-api/src/lib/api/type.ts | 5 ++ .../query/src/lib/convertFilters.ts | 71 ++++++++++++++++++- 5 files changed, 77 insertions(+), 5 deletions(-) diff --git a/libs/data-access/admin-api/src/lib/api/index.d.ts b/libs/data-access/admin-api/src/lib/api/index.d.ts index 5b297e1f..be683a9c 100644 --- a/libs/data-access/admin-api/src/lib/api/index.d.ts +++ b/libs/data-access/admin-api/src/lib/api/index.d.ts @@ -870,7 +870,7 @@ export interface components { /** @enum {string} */ type: 'DATE'; /** @enum {string} */ - operation: 'BEFORE' | 'AFTER' | 'BETWEEN'; + operation: 'BEFORE' | 'AFTER'; /** Format: date-time */ value: string; }; diff --git a/libs/data-access/admin-api/src/lib/api/output.json b/libs/data-access/admin-api/src/lib/api/output.json index 2738acab..0899fa2a 100644 --- a/libs/data-access/admin-api/src/lib/api/output.json +++ b/libs/data-access/admin-api/src/lib/api/output.json @@ -2846,7 +2846,7 @@ }, "operation": { "type": "string", - "enum": ["BEFORE", "AFTER", "BETWEEN"] + "enum": ["BEFORE", "AFTER"] }, "value": { "type": "string", diff --git a/libs/data-access/admin-api/src/lib/api/query.json b/libs/data-access/admin-api/src/lib/api/query.json index e4d6eaa6..10bdbf92 100644 --- a/libs/data-access/admin-api/src/lib/api/query.json +++ b/libs/data-access/admin-api/src/lib/api/query.json @@ -899,7 +899,7 @@ }, "operation": { "type": "string", - "enum": ["BEFORE", "AFTER", "BETWEEN"] + "enum": ["BEFORE", "AFTER"] }, "value": { "type": "string", diff --git a/libs/data-access/admin-api/src/lib/api/type.ts b/libs/data-access/admin-api/src/lib/api/type.ts index 243bf003..08dfc2f8 100644 --- a/libs/data-access/admin-api/src/lib/api/type.ts +++ b/libs/data-access/admin-api/src/lib/api/type.ts @@ -70,3 +70,8 @@ export type ServiceName = Service['name']; export type DeploymentId = Deployment['id']; export type ServiceType = Service['ty']; export type FilterItem = components['schemas']['FilterItem']; +export type FilterDateItem = components['schemas']['FilterDateItem']; +export type FilterNumberItem = components['schemas']['FilterNumberItem']; +export type FilterStringListItem = + components['schemas']['FilterStringListItem']; +export type FilterStringItem = components['schemas']['FilterStringItem']; diff --git a/libs/data-access/query/src/lib/convertFilters.ts b/libs/data-access/query/src/lib/convertFilters.ts index 09436de5..7c3fdc0e 100644 --- a/libs/data-access/query/src/lib/convertFilters.ts +++ b/libs/data-access/query/src/lib/convertFilters.ts @@ -1,7 +1,74 @@ -import type { FilterItem } from '@restate/data-access/admin-api'; +import type { + FilterItem, + FilterDateItem, + FilterStringItem, + FilterNumberItem, + FilterStringListItem, +} from '@restate/data-access/admin-api'; + +function convertFilterNumberToSqlClause( + filter: FilterNumberItem & Pick +) { + switch (filter.operation) { + case 'EQUALS': + return `${filter.field} = '${filter.value}'`; + case 'NOT_EQUALS': + return `${filter.field} != '${filter.value}'`; + case 'GREATER_THAN': + return `${filter.field} > '${filter.value}'`; + case 'LESS_THAN': + return `${filter.field} < '${filter.value}'`; + } +} + +function convertFilterStringToSqlClause( + filter: FilterStringItem & Pick +) { + switch (filter.operation) { + case 'EQUALS': + return `${filter.field} = '${filter.value}'`; + case 'NOT_EQUALS': + return `${filter.field} != '${filter.value}'`; + } +} + +function convertFilterDateToSqlClause( + filter: FilterDateItem & Pick +) { + switch (filter.operation) { + case 'AFTER': + return `${filter.field} > '${filter.value}'`; + case 'BEFORE': + return `${filter.field} < '${filter.value}'`; + } +} + +function convertFilterStringListToSqlClause( + filter: FilterStringListItem & Pick +) { + switch (filter.operation) { + case 'IN': + return `${filter.field} IN (${filter.value + .map((value) => `'${value}'`) + .join(', ')})`; + case 'NOT_IN': + return `${filter.field} NOT IN (${filter.value + .map((value) => `'${value}'`) + .join(', ')})`; + } +} function convertFilterToSqlClause(filter: FilterItem) { - return ''; + switch (filter.type) { + case 'DATE': + return convertFilterDateToSqlClause(filter); + case 'STRING': + return convertFilterStringToSqlClause(filter); + case 'STRING_LIST': + return convertFilterStringListToSqlClause(filter); + case 'NUMBER': + return convertFilterNumberToSqlClause(filter); + } } export function convertFilters(filters: FilterItem[]) {