Skip to content

Commit

Permalink
fix: update db filter input type (#624)
Browse files Browse the repository at this point in the history
  • Loading branch information
surajrai-dzango committed Mar 21, 2024
1 parent 96a397b commit 7a24d4c
Show file tree
Hide file tree
Showing 5 changed files with 70 additions and 33 deletions.
61 changes: 41 additions & 20 deletions packages/slonik/src/__test__/helpers/utils.ts
Original file line number Diff line number Diff line change
@@ -1,24 +1,45 @@
import { ApiConfig } from "@dzangolab/fastify-config";

import type { FilterInput, SortInput } from "../../types";
import type { ApiConfig } from "@dzangolab/fastify-config";

const getFilterDataset = () => {
const getFilterDataset = (): FilterInput[] => {
return [
{ key: "name", operator: "ct", value: "Test" },
{ key: "name", operator: "ew", value: "t1" },
{ key: "name", operator: "sw", value: "Test" },
{ key: "name", operator: "eq", value: "Test" },
{ key: "id", operator: "gt", value: 10 },
{ key: "id", operator: "gte", value: 10 },
{ key: "id", operator: "lt", value: 10 },
{ key: "id", operator: "lte", value: 10 },
{ key: "name", operator: "in", value: "Test1, Test2" },
{ key: "id", operator: "bt", value: "10, 20" },
{ key: "id", not: true, operator: "bt", value: "10, 20" },
{ key: "name", operator: "eq", value: "null" },
{ key: "name", not: true, operator: "eq", value: "NULL" },
{ key: "countryCode", operator: "eq", value: "FR" },
{ key: "country_code", operator: "eq", value: "FR" },
] as FilterInput[];
{ key: "name", operator: "sw", value: "s" },
{
AND: [
{ key: "name", operator: "sw", value: "s" },
{ key: "latitude", operator: "gt", value: "40" },
],
},
{
OR: [
{ key: "name", operator: "sw", value: "Test" },
{ key: "name", operator: "ew", value: "t1" },
],
},
{
AND: [
{ key: "id", operator: "gt", value: "10" },
{
OR: [
{ key: "name", operator: "sw", value: "Test" },
{ key: "name", operator: "ew", value: "t1" },
],
},
],
},
{
OR: [
{ key: "id", operator: "gt", value: "10" },
{
AND: [
{ key: "name", operator: "sw", value: "Test" },
{ key: "name", operator: "ew", value: "t1" },
],
},
],
},
];
};

const getLimitAndOffsetDataset = async (count: number, config: ApiConfig) => {
Expand Down Expand Up @@ -84,7 +105,7 @@ const getLimitAndOffsetDataset = async (count: number, config: ApiConfig) => {
];
};

const getSortDataset = () => {
const getSortDataset = (): SortInput[][] => {
return [
[{ key: "name", direction: "ASC" }],
[{ key: "id", direction: "DESC" }],
Expand All @@ -94,7 +115,7 @@ const getSortDataset = () => {
{ key: "id", direction: "DESC" },
{ key: "name", direction: "ASC" },
],
] as SortInput[][];
];
};

export { getFilterDataset, getLimitAndOffsetDataset, getSortDataset };
17 changes: 9 additions & 8 deletions packages/slonik/src/dbFilters.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
import humps from "humps";
import { sql } from "slonik";

import { FilterInput } from "./types";

import type { BaseFilterInput, FilterInput } from "./types";
import type { IdentifierSqlToken, FragmentSqlToken } from "slonik";

const applyFilter = (
filter: FilterInput,
tableIdentifier: IdentifierSqlToken
tableIdentifier: IdentifierSqlToken,
filter: BaseFilterInput
) => {
const key = humps.decamelize(filter.key);
const operator = filter.operator || "eq";
Expand Down Expand Up @@ -87,16 +86,16 @@ const applyFiltersToQuery = (
tableIdentifier: IdentifierSqlToken,
not = false
) => {
if (filters.AND) {
if ("AND" in filters) {
for (const filterData of filters.AND) {
applyFilters(filterData, tableIdentifier);
}
} else if (filters.OR) {
} else if ("OR" in filters) {
for (const filterData of filters.OR) {
applyFilters(filterData, tableIdentifier, true);
}
} else {
const query = applyFilter(filters, tableIdentifier);
const query = applyFilter(tableIdentifier, filters as BaseFilterInput);

if (not) {
orFilter.push(query);
Expand All @@ -114,7 +113,9 @@ const applyFiltersToQuery = (
sql.fragment`(${sql.join(andFilter, sql.fragment` AND `)})`,
sql.fragment`(${sql.join(orFilter, sql.fragment` OR `)})`,
],
sql.fragment`${filters.AND ? sql.fragment` AND ` : sql.fragment` OR `}`
sql.fragment`${
"AND" in filters ? sql.fragment` AND ` : sql.fragment` OR `
}`
);
} else if (andFilter.length > 0) {
queryFilter = sql.join(andFilter, sql.fragment` AND `);
Expand Down
1 change: 1 addition & 0 deletions packages/slonik/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ export { default as formatDate } from "./formatDate";
export { default as migrationPlugin } from "./migrationPlugin";

export type {
BaseFilterInput,
Database,
FilterInput,
PaginatedList,
Expand Down
23 changes: 18 additions & 5 deletions packages/slonik/src/types/database.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,20 +18,33 @@ type operator =
| "in"
| "bt";

type FilterInput = {
AND: FilterInput[];
OR: FilterInput[];
type BaseFilterInput = {
key: string;
operator: operator;
not: boolean;
not?: boolean;
value: string;
};

type FilterInput =
| BaseFilterInput
| {
AND: FilterInput[];
}
| {
OR: FilterInput[];
};

type SortDirection = "ASC" | "DESC";

type SortInput = {
key: string;
direction: SortDirection;
};

export type { Database, FilterInput, SortDirection, SortInput };
export type {
BaseFilterInput,
Database,
FilterInput,
SortDirection,
SortInput,
};
1 change: 1 addition & 0 deletions packages/slonik/src/types/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ export type {
FilterInput,
SortDirection,
SortInput,
BaseFilterInput,
} from "./database";

export type { PaginatedList, Service } from "./service";
Expand Down

0 comments on commit 7a24d4c

Please sign in to comment.