Skip to content

fix(json-api-nestjs): Add filter by null #93

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Oct 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

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 @@ -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
Loading