Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
}
// search: {$and: [{}, {}, ...]}
else {
this.builderAddBrackets(
builder,
condition,
new Brackets((qb: any) => {
search.$and.forEach((item: any) => {
this.setSearchCondition(qb, item, '$and');
});
}),
);
}
}
// search: {$or: [...], ...}
else if (isArrayFull(search.$or)) {
// search: {$or: [...]}
if (keys.length === 1) {
// search: {$or: [{}]}
if (search.$or.length === 1) {
this.setSearchCondition(builder, search.$or[0], condition);
}
// search: {$or: [{}, {}, ...]}
else {
this.builderAddBrackets(
builder,
condition,
new Brackets((qb: any) => {
search.$or.forEach((item: any) => {
this.setSearchCondition(qb, item, '$or');
});
}),
public getParamFilters(parsed: CrudRequest['parsed']): ObjectLiteral {
let filters = {};
/* istanbul ignore else */
if (hasLength(parsed.paramsFilter)) {
for (const filter of parsed.paramsFilter) {
filters[filter.field] = filter.value;
}
}
return filters;
}
dto: DeepPartial,
parsed: CrudRequest['parsed'],
): T {
/* istanbul ignore if */
if (!isObject(dto)) {
return undefined;
}
if (hasLength(parsed.paramsFilter)) {
for (const filter of parsed.paramsFilter) {
dto[filter.field] = filter.value;
}
}
/* istanbul ignore if */
if (!hasLength(objKeys(dto))) {
return undefined;
}
return dto instanceof this.entityType
? Object.assign(dto, parsed.authPersist)
: plainToClass(this.entityType, { ...dto, ...parsed.authPersist });
}
public async createMany(
req: CrudRequest,
dto: CreateManyDto>,
): Promise {
/* istanbul ignore if */
if (!isObject(dto) || !isArrayFull(dto.bulk)) {
this.throwBadRequestException(`Empty data. Nothing to save.`);
}
const bulk = dto.bulk
.map((one) => this.prepareEntityBeforeSave(one, req.parsed))
.filter((d) => !isUndefined(d));
/* istanbul ignore if */
if (!hasLength(bulk)) {
this.throwBadRequestException(`Empty data. Nothing to save.`);
}
return this.repo.save(bulk, { chunk: 50 });
}
{
$or: [
parser.convertFilterToSearch(parser.filter[0]),
parser.convertFilterToSearch(parser.or[0]),
],
},
]
: [
{
$or: [
{ $and: parser.filter.map(parser.convertFilterToSearch) },
{ $and: parser.or.map(parser.convertFilterToSearch) },
],
},
];
} else if (hasLength(parser.filter)) {
search = parser.filter.map(parser.convertFilterToSearch);
} else {
if (hasLength(parser.or)) {
search =
parser.or.length === 1
? [parser.convertFilterToSearch(parser.or[0])]
: /* istanbul ignore next */ [
{
$or: parser.or.map(parser.convertFilterToSearch),
},
];
}
}
return [...paramsSearch, ...optionsFilter, ...search];
}
private parseValue(val: any) {
try {
const parsed = JSON.parse(val);
if (!isDate(parsed) && isObject(parsed)) {
// throw new Error('Don\'t support object now');
return val;
} else if (
typeof parsed === 'number' &&
parsed.toLocaleString('fullwide', { useGrouping: false }) !== val
) {
// JS cannot handle big numbers. Leave it as a string to prevent data loss
return val;
}
return parsed;
} catch (ignored) {
if (isDateString(val)) {
return new Date(val);
}
protected prepareEntityBeforeSave(
dto: DeepPartial,
parsed: CrudRequest['parsed'],
): T {
/* istanbul ignore if */
if (!isObject(dto)) {
return undefined;
}
if (hasLength(parsed.paramsFilter)) {
for (const filter of parsed.paramsFilter) {
dto[filter.field] = filter.value;
}
}
/* istanbul ignore if */
if (!hasLength(objKeys(dto))) {
return undefined;
}
return dto instanceof this.entityType
? Object.assign(dto, parsed.authPersist)
protected setSearchFieldObjectCondition(
builder: SelectQueryBuilder,
condition: SConditionKey,
field: string,
object: any,
) {
/* istanbul ignore else */
if (isObject(object)) {
const operators = objKeys(object);
if (operators.length === 1) {
const operator = operators[0] as ComparisonOperator;
const value = object[operator];
if (isObject(object.$or)) {
const orKeys = objKeys(object.$or);
this.setSearchFieldObjectCondition(
builder,
orKeys.length === 1 ? condition : '$or',
field,
object.$or,
);
} else {
this.builderSetWhere(builder, condition, field, value, operator);
}),
);
}
}
});
}),
);
}
}
// search: {...}
else {
// search: {foo}
if (keys.length === 1) {
const field = keys[0];
const value = search[field];
if (!isObject(value)) {
this.builderSetWhere(builder, condition, field, value);
} else {
this.setSearchFieldObjectCondition(builder, condition, field, value);
}
}
// search: {foo, ...}
else {
this.builderAddBrackets(
builder,
condition,
new Brackets((qb: any) => {
keys.forEach((field: string) => {
const value = search[field];
if (!isObject(value)) {
this.builderSetWhere(qb, '$and', field, value);
} else {
static setExtraModels(swaggerModels: any) {
/* istanbul ignore else */
if (swaggerConst) {
const meta = Swagger.getExtraModels(swaggerModels.get);
const models: any[] = [
...meta,
...objKeys(swaggerModels)
.map((name) => swaggerModels[name])
.filter((one) => one && one.name !== swaggerModels.get.name),
];
R.set(swaggerConst.DECORATORS.API_EXTRA_MODELS, models, swaggerModels.get);
}
}