Skip to content

Commit

Permalink
Merge pull request #93 from klerick/nestjs-json-api-92
Browse files Browse the repository at this point in the history
fix(json-api-nestjs): Add filter by null
  • Loading branch information
klerick authored Oct 27, 2024
2 parents 1691f06 + 3af99ff commit 32df862
Show file tree
Hide file tree
Showing 4 changed files with 91 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ import {
TransformDataService,
TypeormUtilsService,
} from '../../../../mixin/service';
import { Equal, Repository } from 'typeorm';
import { Equal, IsNull, Repository } from 'typeorm';
import { EntityPropsMapService } from '../../../../service';

function getDefaultQuery<R extends Entity>() {
Expand Down Expand Up @@ -235,6 +235,21 @@ describe('getAll', () => {
comments = await commentsRepository.find();
});

it('Target props with null', async () => {
const spyOnTransformData = jest.spyOn(
transformDataService,
'transformData'
);

const query = getDefaultQuery<Users>();
query.filter.target = {
id: { eq: '1' },
firstName: {eq: null},
};
await typeormService.getAll(query);
expect(spyOnTransformData).toHaveBeenCalledTimes(0);
});

it('Target props', async () => {
const spyOnTransformData = jest.spyOn(
transformDataService,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -250,7 +250,7 @@ export async function getAll<E extends Entity>(
);
}
const resultData = await resultQuery.getMany();

console.log(resultData);
const { included, data } =
this.transformDataService.transformData(resultData);
return {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -247,6 +247,64 @@ describe('TypeormUtilsService', () => {
});

describe('getFilterExpressionForTarget', () => {
it('expression for target field with null', () => {

const nullableField = 'id'
const notNullableField = 'login'
const query = getDefaultQuery<Users>();
query.filter.target = {
[nullableField]: {
[FilterOperand.eq]: null,
},
[notNullableField]: {
[FilterOperand.ne]: null,
},
};

function guardField<R extends any>(
filter: any,
field: any
): asserts field is keyof R {
if (filter && !(field in filter))
throw new Error('field not exist in query filter');
}

const result =
typeormUtilsServiceUser.getFilterExpressionForTarget(query);
const mainAliasCheck = 'Users';


for (const item of result) {
const { params, alias, expression, selectInclude } = item;
expect(selectInclude).toBe(undefined);
if (!alias) {
expect(alias).not.toBe(undefined);
throw new Error('alias in undefined for result');
}
const [mainAlias, field] = alias.split('.');
expect(mainAlias).toBe(mainAliasCheck);
guardField(query.filter.target, field);
const filterName: any = query.filter.target[field];
if (!filterName) {
expect(filterName).not.toBe(undefined);
throw new Error('filterName in undefined from query');
}

expect(params).toBe(undefined)

if (field === nullableField) {
expect(expression).toBe('IS NULL');
continue;
}

if (field === notNullableField) {
expect(expression).toBe('IS NOT NULL');
continue;
}

throw new Error('filed is incorrect');
}
})
it('expression for target field', () => {
const valueTest = (filterOperand: FilterOperand) =>
`test for ${filterOperand}`;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -243,6 +243,22 @@ export class TypeormUtilsService<E extends Entity> {
const paramsName = this.getParamName(fieldWithAlias);

if (!isTargetField(this._relationFields, fieldName)) {

if (
(operand === FilterOperand.ne || operand === FilterOperand.eq) &&
(valueConditional === 'null' || valueConditional === null)
) {
const expression = OperandMapExpressionForNull[operand].replace(
EXPRESSION,
paramsName
);
resultExpression.push({
alias: fieldWithAlias,
expression,
});
continue;
}

const expression = OperandsMapExpression[operand].replace(
EXPRESSION,
paramsName
Expand Down

0 comments on commit 32df862

Please sign in to comment.