Commit 86331847 authored by nanahira's avatar nanahira

add QueryMatchBoolean

parent 9f1476ce
import { QueryCond } from '../bases';
import { Metadata } from '../utility/metadata';
import {
applyQueryMatchBoolean,
applyQueryProperty,
applyQueryPropertyLike,
applyQueryPropertySearch,
......@@ -15,3 +16,5 @@ export const QuerySearch = () => QueryCondition(applyQueryPropertySearch);
export const QueryEqualZeroNullable = () =>
QueryCondition(applyQueryPropertyZeroNullable);
export const QueryMatchBoolean = () => QueryCondition(applyQueryMatchBoolean);
import { SelectQueryBuilder } from 'typeorm';
import { Brackets, SelectQueryBuilder } from 'typeorm';
export function applyQueryProperty<T>(
obj: T,
qb: SelectQueryBuilder<T>,
entityName: string,
...fields: (keyof T & string)[]
export function createQueryCondition(
cond: <T>(
obj: T,
qb: SelectQueryBuilder<T>,
entityName: string,
field: keyof T & string,
) =>
| void
| SelectQueryBuilder<any>
| string
| { query: string; params: Record<string, any> },
) {
for (const field of fields) {
if (obj[field] == null) {
continue;
return <T>(
obj: T,
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,
qb: SelectQueryBuilder<T>,
entityName: string,
...fields: (keyof T & string)[]
) {
for (const field of fields) {
if (obj[field] == null) {
continue;
}
export const applyQueryProperty = createQueryCondition(
(obj, qb, entityName, field) =>
qb.andWhere(`${entityName}.${field} = :${field}`, { [field]: obj[field] }),
);
export const applyQueryPropertyLike = createQueryCondition(
(obj, qb, entityName, field) =>
qb.andWhere(`${entityName}.${field} like (:${field} || '%')`, {
[field]: obj[field],
});
}
}
}),
);
export function applyQueryPropertySearch<T>(
obj: T,
qb: SelectQueryBuilder<T>,
entityName: string,
...fields: (keyof T & string)[]
) {
for (const field of fields) {
if (obj[field] == null) {
continue;
}
export const applyQueryPropertySearch = createQueryCondition(
(obj, qb, entityName, field) =>
qb.andWhere(`${entityName}.${field} like ('%' || :${field} || '%')`, {
[field]: obj[field],
});
}
}
}),
);
export function applyQueryPropertyZeroNullable<T>(
obj: T,
qb: SelectQueryBuilder<T>,
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) {
export const applyQueryPropertyZeroNullable = createQueryCondition(
(obj, qb, entityName, field) => {
if ([0, '0'].indexOf(obj[field] as any) !== -1) {
qb.andWhere(`${entityName}.${field} IS NULL`);
} else {
qb.andWhere(`${entityName}.${field} = :${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