Commit 86331847 authored by nanahira's avatar nanahira

add QueryMatchBoolean

parent 9f1476ce
import { QueryCond } from '../bases'; import { QueryCond } from '../bases';
import { Metadata } from '../utility/metadata'; import { Metadata } from '../utility/metadata';
import { import {
applyQueryMatchBoolean,
applyQueryProperty, applyQueryProperty,
applyQueryPropertyLike, applyQueryPropertyLike,
applyQueryPropertySearch, applyQueryPropertySearch,
...@@ -15,3 +16,5 @@ export const QuerySearch = () => QueryCondition(applyQueryPropertySearch); ...@@ -15,3 +16,5 @@ export const QuerySearch = () => QueryCondition(applyQueryPropertySearch);
export const QueryEqualZeroNullable = () => export const QueryEqualZeroNullable = () =>
QueryCondition(applyQueryPropertyZeroNullable); QueryCondition(applyQueryPropertyZeroNullable);
export const QueryMatchBoolean = () => QueryCondition(applyQueryMatchBoolean);
import { SelectQueryBuilder } from 'typeorm'; import { Brackets, SelectQueryBuilder } from 'typeorm';
export function applyQueryProperty<T>( export function createQueryCondition(
obj: T, cond: <T>(
qb: SelectQueryBuilder<T>, obj: T,
entityName: string, qb: SelectQueryBuilder<T>,
...fields: (keyof T & string)[] entityName: string,
field: keyof T & string,
) =>
| void
| SelectQueryBuilder<any>
| string
| { query: string; params: Record<string, any> },
) { ) {
for (const field of fields) { return <T>(
if (obj[field] == null) { obj: T,
continue; qb: SelectQueryBuilder<T>,
entityName: string,
...fields: (keyof T & string)[]
) => {
for (const field of fields) {
if (obj[field] == null) {
continue;
}
const ret = cond(obj, qb, entityName, field);
if (typeof ret === 'string') {
qb.andWhere(ret);
} else if (typeof ret === 'object' && typeof ret['query'] === 'string') {
const _ret = ret as { query: string; params: Record<string, any> };
qb.andWhere(_ret.query, _ret.params);
}
} }
qb.andWhere(`${entityName}.${field} = :${field}`, { [field]: obj[field] }); return qb;
} };
} }
export function applyQueryPropertyLike<T>(
obj: T, export const applyQueryProperty = createQueryCondition(
qb: SelectQueryBuilder<T>, (obj, qb, entityName, field) =>
entityName: string, qb.andWhere(`${entityName}.${field} = :${field}`, { [field]: obj[field] }),
...fields: (keyof T & string)[] );
) {
for (const field of fields) { export const applyQueryPropertyLike = createQueryCondition(
if (obj[field] == null) { (obj, qb, entityName, field) =>
continue;
}
qb.andWhere(`${entityName}.${field} like (:${field} || '%')`, { qb.andWhere(`${entityName}.${field} like (:${field} || '%')`, {
[field]: obj[field], [field]: obj[field],
}); }),
} );
}
export function applyQueryPropertySearch<T>( export const applyQueryPropertySearch = createQueryCondition(
obj: T, (obj, qb, entityName, field) =>
qb: SelectQueryBuilder<T>,
entityName: string,
...fields: (keyof T & string)[]
) {
for (const field of fields) {
if (obj[field] == null) {
continue;
}
qb.andWhere(`${entityName}.${field} like ('%' || :${field} || '%')`, { qb.andWhere(`${entityName}.${field} like ('%' || :${field} || '%')`, {
[field]: obj[field], [field]: obj[field],
}); }),
} );
}
export function applyQueryPropertyZeroNullable<T>( export const applyQueryPropertyZeroNullable = createQueryCondition(
obj: T, (obj, qb, entityName, field) => {
qb: SelectQueryBuilder<T>, if ([0, '0'].indexOf(obj[field] as any) !== -1) {
entityName: string,
...fields: (keyof T & string)[]
) {
for (const field of fields) {
if (obj[field] === undefined) {
// Nothing
} else if ([0, '0'].indexOf(obj[field] as any) !== -1) {
qb.andWhere(`${entityName}.${field} IS NULL`); qb.andWhere(`${entityName}.${field} IS NULL`);
} else { } else {
qb.andWhere(`${entityName}.${field} = :${field}`, { qb.andWhere(`${entityName}.${field} = :${field}`, {
[field]: obj[field], [field]: obj[field],
}); });
} }
} },
} );
export const applyQueryMatchBoolean = createQueryCondition(
(obj, qb, entityName, field) => {
const value = obj[field] as any;
if (value === true || value === 'true' || value === 1 || value === '1') {
qb.andWhere(`${entityName}.${field} = TRUE`);
}
if (value === false || value === 'false' || value === 0 || value === '0') {
qb.andWhere(`${entityName}.${field} = FALSE`);
}
},
);
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment