You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
54 lines
1.9 KiB
54 lines
1.9 KiB
4 years ago
|
import {WhereBuilder} from '../builder/type/WhereBuilder.ts'
|
||
|
import {logger} from '../../../lib/src/service/logging/global.ts'
|
||
|
import {EscapedValue} from '../builder/types.ts'
|
||
|
|
||
|
export enum FilterOp {
|
||
|
eq = '$eq',
|
||
|
in = '$in',
|
||
|
lt = '$lt',
|
||
|
lte = '$lte',
|
||
|
gt = '$gt',
|
||
|
gte = '$gte',
|
||
|
}
|
||
|
|
||
|
export type QueryFilter = { [key: string]: any }
|
||
|
|
||
|
export function apply_filter_to_where(filter: QueryFilter, where: WhereBuilder): WhereBuilder {
|
||
|
for ( const field in filter ) {
|
||
|
if ( !filter.hasOwnProperty(field) ) continue
|
||
|
const filter_val = filter[field]
|
||
|
if ( Array.isArray(filter_val) ) {
|
||
|
where = where.whereIn(field, filter_val)
|
||
|
} else if ( typeof filter_val === 'object' ) {
|
||
|
for ( const op in filter_val ) {
|
||
|
if ( !filter_val.hasOwnProperty(op) ) continue
|
||
|
switch (op) {
|
||
|
case FilterOp.eq:
|
||
|
where = where.where(field, '=', filter_val[op])
|
||
|
break
|
||
|
case FilterOp.in:
|
||
|
where = where.whereIn(field, filter_val[op])
|
||
|
break
|
||
|
case FilterOp.lt:
|
||
|
where = where.where(field, '<', filter_val[op])
|
||
|
break
|
||
|
case FilterOp.lte:
|
||
|
where = where.where(field, '<=', filter_val[op])
|
||
|
break
|
||
|
case FilterOp.gt:
|
||
|
where = where.where(field, '>', filter_val[op])
|
||
|
break
|
||
|
case FilterOp.gte:
|
||
|
where = where.where(field, '>=', filter_val[op])
|
||
|
break
|
||
|
default:
|
||
|
logger.warn(`Invalid filter operator attempted: ${op}`)
|
||
|
}
|
||
|
}
|
||
|
} else {
|
||
|
where = where.where(field, '=', filter_val)
|
||
|
}
|
||
|
}
|
||
|
|
||
|
return where
|
||
|
}
|