Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,6 @@
"description": "Operand type",
"additionalProperties": false,
"minProperties": 1,
"maxProperties": 1,
"properties": {
"eq": {
"type": "string"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@ describe('QueryTransformSchema', () => {
});

describe('Check filter', () => {
it('Error if operand for field more one', async () => {
it('Should be correct if operand for field more one', async () => {
const resultInput: QueryParams<Users> = {
...resultInputMock,
filter: {
Expand All @@ -197,16 +197,16 @@ describe('QueryTransformSchema', () => {
relation: null,
},
} as any;
expect.assertions(3);
expect.assertions(0);
try {
await pipe.transform(resultInput);
} catch (e) {
expect(e).toBeInstanceOf(BadRequestException);
const countError = e.response.message.filter(
(item) => item.source.parameter.split('/')[1] === 'filter'
).length;
expect(e.response.message.length).toBe(countError);
expect(e.response.message.length).toBeGreaterThan(0);
// const countError = e.response.message.filter(
// (item) => item.source.parameter.split('/')[1] === 'filter'
// ).length;
// expect(e.response.message.length).toBe(countError);
// expect(e.response.message.length).toBeGreaterThan(0);
}
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,105 +62,112 @@ export class UtilsMethode {
let i = 0;

for (field in filter) {
const operand = Object.keys(filter[field]).pop();
const value =
operand === FilterOperand.like
? `%${filter[field][operand]}%`
: filter[field][operand];

if (relations.has(field)) {
const relation = metadata.relations.find(
(item) => item.propertyName === field
);
const {
inverseSidePropertyPath,
inverseEntityMetadata: { target, name },
} = relation;
const resourceRelationName = snakeToCamel(name);
const primaryColumn = metadata.primaryColumns[0].databaseName;
switch (relation.relationType) {
case 'many-to-many': {
const { inverseJoinColumns, joinColumns } =
relation.isManyToManyOwner ? relation : relation.inverseRelation;
const relationProps = relation.isManyToManyOwner
? relation
: relation.inverseRelation;
const { joinTableName } = relationProps;
const { databaseName: queryJoinPropsName } =
relation.isManyToManyOwner
? inverseJoinColumns[0]
: joinColumns[0];
const { databaseName: selectJoinPropsName } =
relation.isManyToManyOwner
? joinColumns[0]
: inverseJoinColumns[0];
const onQuery = `${joinTableName}.${queryJoinPropsName} = ${resourceRelationName}.${primaryColumn}`;
const selectQuery = `${joinTableName}.${selectJoinPropsName}`;

const query = builder
.subQuery()
.select(selectQuery)
.from(joinTableName, joinTableName)
.leftJoin(resourceRelationName, resourceRelationName, onQuery)
.where(`${selectQuery} = ${builder.alias}.${primaryColumn}`)
.getQuery();
resultExpression.push({
expression: OperandMapForNullRelation[operand].replace(
'EXPRESSION',
query
),
params: null,
});
break;
const operands = Object.keys(filter[field]);
for (const operand of operands) {
const value =
operand === FilterOperand.like
? `%${filter[field][operand]}%`
: filter[field][operand];

if (relations.has(field)) {
const relation = metadata.relations.find(
(item) => item.propertyName === field
);
const {
inverseSidePropertyPath,
inverseEntityMetadata: { target, name },
} = relation;
const resourceRelationName = snakeToCamel(name);
const primaryColumn = metadata.primaryColumns[0].databaseName;
switch (relation.relationType) {
case 'many-to-many': {
const { inverseJoinColumns, joinColumns } =
relation.isManyToManyOwner
? relation
: relation.inverseRelation;
const relationProps = relation.isManyToManyOwner
? relation
: relation.inverseRelation;
const { joinTableName } = relationProps;
const { databaseName: queryJoinPropsName } =
relation.isManyToManyOwner
? inverseJoinColumns[0]
: joinColumns[0];
const { databaseName: selectJoinPropsName } =
relation.isManyToManyOwner
? joinColumns[0]
: inverseJoinColumns[0];
const onQuery = `${joinTableName}.${queryJoinPropsName} = ${resourceRelationName}.${primaryColumn}`;
const selectQuery = `${joinTableName}.${selectJoinPropsName}`;

const query = builder
.subQuery()
.select(selectQuery)
.from(joinTableName, joinTableName)
.leftJoin(resourceRelationName, resourceRelationName, onQuery)
.where(`${selectQuery} = ${builder.alias}.${primaryColumn}`)
.getQuery();
resultExpression.push({
expression: OperandMapForNullRelation[operand].replace(
'EXPRESSION',
query
),
params: null,
});
break;
}
case 'one-to-many': {
const query = builder
.subQuery()
.select(`${resourceRelationName}.${inverseSidePropertyPath}`)
.from(target, resourceRelationName)
.where(
`${resourceRelationName}.${inverseSidePropertyPath} = ${builder.alias}.id`
)
.getQuery();
resultExpression.push({
expression: OperandMapForNullRelation[operand].replace(
'EXPRESSION',
query
),
params: null,
});
break;
}
default: {
resultExpression.push({
expression: `${preparedResourceName}.${field.toString()} ${OperandMapForNull[
operand
].replace(
'EXPRESSION',
UtilsMethode.getParamName(
`${preparedResourceName}.${field}`,
i
)
)}`,
params: null,
});
}
}
case 'one-to-many': {
const query = builder
.subQuery()
.select(`${resourceRelationName}.${inverseSidePropertyPath}`)
.from(target, resourceRelationName)
.where(
`${resourceRelationName}.${inverseSidePropertyPath} = ${builder.alias}.id`
)
.getQuery();
resultExpression.push({
expression: OperandMapForNullRelation[operand].replace(
'EXPRESSION',
query
} else {
resultExpression.push({
expression: `${preparedResourceName}.${field.toString()} ${OperandsMap[
operand
].replace(
'EXPRESSION',
UtilsMethode.getParamName(`${preparedResourceName}.${field}`, i)
)}`,
params: {
name: UtilsMethode.getParamName(
`${preparedResourceName}.${field}`,
i
),
params: null,
});
break;
}
default: {
resultExpression.push({
expression: `${preparedResourceName}.${field.toString()} ${OperandMapForNull[
operand
].replace(
'EXPRESSION',
UtilsMethode.getParamName(`${preparedResourceName}.${field}`, i)
)}`,
params: null,
});
}
val: value,
},
});
}
} else {
resultExpression.push({
expression: `${preparedResourceName}.${field.toString()} ${OperandsMap[
operand
].replace(
'EXPRESSION',
UtilsMethode.getParamName(`${preparedResourceName}.${field}`, i)
)}`,
params: {
name: UtilsMethode.getParamName(
`${preparedResourceName}.${field}`,
i
),
val: value,
},
});
i++;
}
i++;
}

return resultExpression;
Expand Down
9 changes: 5 additions & 4 deletions libs/json-api-nestjs/src/lib/types/query.types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,10 +47,11 @@ export type Operands = {
[P in FilterOperand]: Record<
P,
P extends MapFilterOperandTypeString ? string : string[]
> &
Partial<Record<Exclude<FilterOperand, P>, never>> extends infer O
? { [Q in keyof O]: O[Q] }
: never;
>;
// &
// Partial<Record<Exclude<FilterOperand, P>, never>> extends infer O
// ? { [Q in keyof O]: O[Q] }
// : never;
}[FilterOperand];

export type OperandsRelation = {
Expand Down