Skip to content

Commit

Permalink
Merge pull request #27 from PavloNaumenko/fix/combine-and-or-error
Browse files Browse the repository at this point in the history
fix: combine "$and" + "$or" conditions
  • Loading branch information
zaro committed Mar 13, 2024
2 parents 2de7857 + 3c236d4 commit 6a393d9
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 63 deletions.
104 changes: 41 additions & 63 deletions packages/crud-typeorm/src/typeorm-crud.service.ts
Expand Up @@ -921,79 +921,57 @@ export class TypeOrmCrudService<T> extends CrudService<T, DeepPartial<T>> {
if (isObject(object)) {
const operators = objKeys(object);

if (operators.length === 1) {
if (operators.length === 1 && operators[0] !== '$or') {
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,
customOperators,
);
} else {
this.builderSetWhere(
builder,
condition,
field,
value,
customOperators,
operator,
);
}
this.builderSetWhere(builder, condition, field, value, customOperators, operator);
} else {
/* istanbul ignore else */
if (operators.length > 1) {
this.builderAddBrackets(
builder,
condition,
new Brackets((qb: any) => {
operators.forEach((operator: ComparisonOperator) => {
const value = object[operator];

if (operator !== '$or') {
this.builderSetWhere(
this.builderAddBrackets(
builder,
condition,
new Brackets((qb: any) => {
operators.forEach((operator: ComparisonOperator) => {
const value = object[operator];

if (operator !== '$or') {
this.builderSetWhere(
qb,
condition,
field,
value,
customOperators,
operator,
);
} else {
const orKeys = objKeys(object.$or);

if (orKeys.length === 1) {
this.setSearchFieldObjectCondition(
qb,
condition,
field,
value,
object.$or,
customOperators,
operator,
);
} else {
const orKeys = objKeys(object.$or);

if (orKeys.length === 1) {
this.setSearchFieldObjectCondition(
qb,
condition,
field,
object.$or,
customOperators,
);
} else {
this.builderAddBrackets(
qb,
condition,
new Brackets((qb2: any) => {
this.setSearchFieldObjectCondition(
qb2,
'$or',
field,
object.$or,
customOperators,
);
}),
);
}
this.builderAddBrackets(
qb,
condition,
new Brackets((qb2: any) => {
this.setSearchFieldObjectCondition(
qb2,
'$or',
field,
object.$or,
customOperators,
);
}),
);
}
});
}),
);
}
}
});
}),
);
}
}
}
Expand Down
10 changes: 10 additions & 0 deletions packages/crud-typeorm/test/b.query-params.spec.ts
Expand Up @@ -813,6 +813,16 @@ describe('#crud-typeorm', () => {
.expect(200);
expect(res.body).toBeArrayOfSize(14);
});
it('should return with search, 21', async () => {
const query = qb
.search({ name: { $eq: 'Project1' }, companyId: { $or: { $notnull: true, $eq: 1 } } })
.query();
const res = await projects2()
.query(query)
.expect(200);
expect(res.body).toBeArrayOfSize(1);
expect(res.body[0].id).toBe(1);
});
it('should return with default filter, 1', async () => {
const query = qb.search({ name: 'Project11' }).query();
const res = await projects3()
Expand Down

0 comments on commit 6a393d9

Please sign in to comment.