Skip to content

Commit

Permalink
add filter conversion
Browse files Browse the repository at this point in the history
Signed-off-by: Nik Nasr <[email protected]>
  • Loading branch information
nikrooz committed Jan 2, 2025
1 parent ab7d619 commit 6aa0b71
Show file tree
Hide file tree
Showing 5 changed files with 77 additions and 5 deletions.
2 changes: 1 addition & 1 deletion libs/data-access/admin-api/src/lib/api/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
};
Expand Down
2 changes: 1 addition & 1 deletion libs/data-access/admin-api/src/lib/api/output.json
Original file line number Diff line number Diff line change
Expand Up @@ -2846,7 +2846,7 @@
},
"operation": {
"type": "string",
"enum": ["BEFORE", "AFTER", "BETWEEN"]
"enum": ["BEFORE", "AFTER"]
},
"value": {
"type": "string",
Expand Down
2 changes: 1 addition & 1 deletion libs/data-access/admin-api/src/lib/api/query.json
Original file line number Diff line number Diff line change
Expand Up @@ -899,7 +899,7 @@
},
"operation": {
"type": "string",
"enum": ["BEFORE", "AFTER", "BETWEEN"]
"enum": ["BEFORE", "AFTER"]
},
"value": {
"type": "string",
Expand Down
5 changes: 5 additions & 0 deletions libs/data-access/admin-api/src/lib/api/type.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'];
71 changes: 69 additions & 2 deletions libs/data-access/query/src/lib/convertFilters.ts
Original file line number Diff line number Diff line change
@@ -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<FilterItem, 'field'>
) {
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<FilterItem, 'field'>
) {
switch (filter.operation) {
case 'EQUALS':
return `${filter.field} = '${filter.value}'`;
case 'NOT_EQUALS':
return `${filter.field} != '${filter.value}'`;
}
}

function convertFilterDateToSqlClause(
filter: FilterDateItem & Pick<FilterItem, 'field'>
) {
switch (filter.operation) {
case 'AFTER':
return `${filter.field} > '${filter.value}'`;
case 'BEFORE':
return `${filter.field} < '${filter.value}'`;
}
}

function convertFilterStringListToSqlClause(
filter: FilterStringListItem & Pick<FilterItem, 'field'>
) {
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[]) {
Expand Down

0 comments on commit 6aa0b71

Please sign in to comment.