Skip to content

Commit

Permalink
More filter types
Browse files Browse the repository at this point in the history
  • Loading branch information
jm42 committed Apr 13, 2024
1 parent 7bfe235 commit 5c52371
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 27 deletions.
39 changes: 26 additions & 13 deletions packages/utils/src/queries/filter.ts
Original file line number Diff line number Diff line change
@@ -1,28 +1,39 @@
import {
EqFilter,
FieldFilter,
GteFilter,
InFilter,
LikeFilter,
LteFilter,
NeqFilter,
NinFilter,
} from '../types/filter';

export function createLikeFilter(
key: string,
value?: string,
): { [key: string]: { _ilike: string } } {
): FieldFilter<LikeFilter> {
return value ? { [key]: { _ilike: `%${value}%` } } : {};
}

export function createEqFilter(
key: string,
value?: string | number,
): { [key: string]: { _eq: string | number } } {
): FieldFilter<EqFilter> {
return value ? { [key]: { _eq: value } } : {};
}

export function createNeqFilter(
key: string,
value?: string | number,
): { [key: string]: { _neq: string | number } } {
): FieldFilter<NeqFilter> {
return value ? { [key]: { _neq: value } } : {};
}

export function createBoolFilter(
key: string,
value?: boolean,
): { [key: string]: { _eq: 'true' | 'false' } } {
): FieldFilter<EqFilter<'true' | 'false'>> {
return typeof value === 'boolean'
? { [key]: { _eq: value ? 'true' : 'false' } }
: {};
Expand All @@ -32,12 +43,7 @@ export function createAddressFilter(
key: string,
filterZeroAddress: boolean,
value?: string,
): {
[key: string]: {
_neq?: string;
_eq?: string;
};
} {
): FieldFilter<Partial<EqFilter> & Partial<NeqFilter>> {
return filterZeroAddress || value
? {
[key]: {
Expand All @@ -52,16 +58,23 @@ export function createAddressFilter(

export function createInFilter(
key: string,
values?: Array<string | number>,
): { [key: string]: { _in: Array<string | number> } } {
values?: Array<string | number | boolean>,
): FieldFilter<InFilter> {
return values && values.length ? { [key]: { _in: values } } : {};
}

export function createNinFilter(
key: string,
values?: Array<string | number | boolean>,
): FieldFilter<NinFilter> {
return values && values.length ? { [key]: { _nin: values } } : {};
}

export function createBetweenFilter(
key: string,
from?: string,
to?: string,
): { [key: string]: { _gte?: string; _lte?: string } } {
): FieldFilter<Partial<GteFilter> & Partial<LteFilter>> {
const betweenFilter: { _gte?: string; _lte?: string } = {};
if (from) {
betweenFilter._gte = from;
Expand Down
68 changes: 54 additions & 14 deletions packages/utils/src/types/filter.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,62 @@
interface FieldFilter {
[key: string]: {
_eq?: string | number | boolean;
_neq?: string | number | boolean;
_ilike?: string;
_in?: Array<string | number | boolean>;
_nin?: Array<string | number | boolean>;
_gte?: string;
_lte?: string;
};
}

interface ConditionFilter {
type Value = string | number | boolean;

export interface EqFilter<V = Value> {
_eq: V;
}

export interface NeqFilter<V = Value> {
_neq: V;
}

export interface LikeFilter<V = Value> {
_ilike: V;
}

export interface InFilter<V = Value> {
_in: Array<V>;
}

export interface NinFilter<V = Value> {
_nin: Array<V>;
}

export interface GtFilter<V = Value> {
_gt: V;
}

export interface GteFilter<V = Value> {
_gte: V;
}

export interface LtFilter<V = Value> {
_lt: V;
}

export interface LteFilter<V = Value> {
_lte: V;
}

export type AnyFilter = Partial<EqFilter> &
Partial<NeqFilter> &
Partial<LikeFilter> &
Partial<InFilter> &
Partial<NinFilter> &
Partial<GtFilter> &
Partial<GteFilter> &
Partial<LtFilter> &
Partial<LteFilter>;

export type FieldFilter<F = AnyFilter> = {
[key: string]: F;
};

export interface ConditionFilter {
_or?: Filter[];
_and?: Filter[];
_not?: Filter;
}

type Filter = FieldFilter | ConditionFilter | { [key: string]: Filter };
export type Filter = FieldFilter | ConditionFilter | { [key: string]: Filter };

export interface FilterVariables {
where: Filter;
Expand Down

0 comments on commit 5c52371

Please sign in to comment.