25 lines
845 B
TypeScript
25 lines
845 B
TypeScript
|
import { isNumber } from 'lodash';
|
||
|
import { Like, ObjectLiteral, ObjectType } from 'typeorm';
|
||
|
export const fieldSearch = <T>(entity: Partial<T>): ObjectLiteral => {
|
||
|
let result = {};
|
||
|
for (let key in entity) {
|
||
|
if (entity.hasOwnProperty(key)) {
|
||
|
switch (typeof entity[key]) {
|
||
|
case 'number':
|
||
|
result = { ...result, ...(isNumber(entity[key]) ? { [key]: entity[key] } : null) };
|
||
|
break;
|
||
|
case 'string':
|
||
|
result = { ...result, ...(entity[key] ? { [key]: Like(`%${entity[key]}%`) } : null) };
|
||
|
break;
|
||
|
case 'boolean':
|
||
|
result = { ...result, ...(entity[key] === true ? { [key]: 1 } : { [key]: 0 }) };
|
||
|
break;
|
||
|
default:
|
||
|
result = { ...result, ...(entity[key] ? { [key]: entity[key] } : null) };
|
||
|
break;
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
return result;
|
||
|
};
|