Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add support for more operators than contains #76

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
62 changes: 58 additions & 4 deletions src/utils/filter/default-filter.parser.ts
Original file line number Diff line number Diff line change
@@ -1,23 +1,77 @@
import { Like, Raw } from 'typeorm'
import { Like, Not, Raw } from 'typeorm'
import { Property } from '../../Property.js'
import { FilterParser } from './filter.types.js'
import { OPERATORS } from './filter.utils.js'

const uuidRegex = /^[0-9A-F]{8}-[0-9A-F]{4}-[5|4|3|2|1][0-9A-F]{3}-[89AB][0-9A-F]{3}-[0-9A-F]{12}$/i

const OPERATOR_SEPARATOR = '~';

const MATCHING_PATTERNS = {
EQ: 'equals',
NE: 'notEquals',
CO: 'contains',
EW: 'endsWith',
SW: 'startsWith',
};

export const DefaultParser: FilterParser = {
isParserForType: (filter) => filter.property.type() === 'string',
parse: (filter, fieldKey) => {
const value = filter.value;
if (
uuidRegex.test(filter.value.toString())
uuidRegex.test(value.toString())
|| (filter.property as Property).column.type === 'uuid'
) {
return {
filterKey: fieldKey,
filterValue: Raw((alias) => `CAST(${alias} AS CHAR(36)) = :value`, {
value: filter.value,
value,
}),
}
}
return { filterKey: fieldKey, filterValue: Like(`%${filter.value}%`) }

if (typeof value === 'object') {
if (value[MATCHING_PATTERNS.SW]) {
return {
filterKey: fieldKey, filterValue: Like(`${value[MATCHING_PATTERNS.SW]}%`)
}
} else if (value[MATCHING_PATTERNS.EW]) {
return {
filterKey: fieldKey, filterValue: Like(`%${value[MATCHING_PATTERNS.EW]}`)
}
} else if (value[MATCHING_PATTERNS.EQ]) {
return {
filterKey: fieldKey, filterValue: value[MATCHING_PATTERNS.EQ]
}
} else if (value[MATCHING_PATTERNS.NE]) {
return {
filterKey: fieldKey, filterValue: Not(value[MATCHING_PATTERNS.NE])
}
} else {
const orPrefix = `${OPERATORS.OR}${OPERATOR_SEPARATOR}`;
if (value[`${orPrefix}${MATCHING_PATTERNS.SW}`]) {
return {
filterKey: fieldKey, filterValue: Like(`${value[`${orPrefix}${MATCHING_PATTERNS.SW}`]}%`), operator: OPERATORS.OR
}
} else if (value[`${orPrefix}${MATCHING_PATTERNS.EW}`]) {
return {
filterKey: fieldKey, filterValue: Like(`%${value[`${orPrefix}${MATCHING_PATTERNS.EW}`]}`), operator: OPERATORS.OR
}
} else if (value[`${orPrefix}${MATCHING_PATTERNS.EQ}`]) {
return {
filterKey: fieldKey, filterValue: value[`${orPrefix}${MATCHING_PATTERNS.EQ}`], operator: OPERATORS.OR
}
} else if (value[`${orPrefix}${MATCHING_PATTERNS.NE}`]) {
return {
filterKey: fieldKey, filterValue: Not(value[`${orPrefix}${MATCHING_PATTERNS.NE}`]), operator: OPERATORS.OR
}
} else if (value[OPERATORS.OR]) {
return { filterKey: fieldKey, filterValue: Like(`%${value[OPERATORS.OR]}%`), operator: OPERATORS.OR }
}
}
}

return { filterKey: fieldKey, filterValue: Like(`%${value}%`) }
},
}
37 changes: 29 additions & 8 deletions src/utils/filter/filter.converter.ts
Original file line number Diff line number Diff line change
@@ -1,29 +1,50 @@
import { Filter } from 'adminjs'
import { BaseEntity, FindOptionsWhere } from 'typeorm'
import { DefaultParser } from './default-filter.parser.js'
import { parsers } from './filter.utils.js'
import { OPERATORS, parsers } from './filter.utils.js'

export const convertFilter = (
filterObject?: Filter,
): FindOptionsWhere<BaseEntity> => {
): FindOptionsWhere<BaseEntity>[] | {} => {
if (!filterObject) {
return {}
}

const { filters } = filterObject ?? {}
const where = {}

const andStatements = {}
const orStatements: {[key: string]: any;}[] = []
Object.entries(filters ?? {}).forEach(([fieldKey, filter]) => {
const parser = parsers.find((p) => p.isParserForType(filter))

if (parser) {
const { filterValue, filterKey } = parser.parse(filter, fieldKey)
where[filterKey] = filterValue
const { filterValue, filterKey, operator } = parser.parse(filter, fieldKey)
if (operator === OPERATORS.OR) {
orStatements.push({ [filterKey]: filterValue })
} else {
andStatements[filterKey] = filterValue
}
} else {
const { filterValue, filterKey } = DefaultParser.parse(filter, fieldKey)
where[filterKey] = filterValue
const { filterValue, filterKey, operator } = DefaultParser.parse(filter, fieldKey)
if (operator === OPERATORS.OR) {
orStatements.push({ [filterKey]: filterValue })
} else {
andStatements[filterKey] = filterValue
}
}
})

return where
if(Object.keys(andStatements).length > 0 || orStatements.length > 0) {
return [andStatements, ...orStatements];
}

if(Object.keys(andStatements).length > 0) {
return andStatements;
}

if(orStatements.length > 0) {
return orStatements;
}

return {};
}
2 changes: 1 addition & 1 deletion src/utils/filter/filter.types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,5 @@ export type FilterParser = {
parse: (
filter: FilterElement,
fieldKey: string
) => { filterKey: string; filterValue: any };
) => { filterKey: string; filterValue: any;operator?: string; };
};
5 changes: 5 additions & 0 deletions src/utils/filter/filter.utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,8 @@ export const parsers = [
ReferenceParser,
JSONParser,
]

export const OPERATORS = {
AND: 'and',
OR: 'or',
};