localhost_oa_based/src/shared/database/field-search/index.ts

25 lines
845 B
TypeScript
Raw Normal View History

2024-03-04 16:34:54 +08:00
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;
};