Skip to content

Commit

Permalink
Merge pull request #46 from klerick/condition-by-reletion
Browse files Browse the repository at this point in the history
fix(json-api-nestjs): fix test for prev commit
  • Loading branch information
klerick authored Apr 24, 2023
2 parents 09140ec + 35c1833 commit 6e19cb2
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 50 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -317,14 +317,19 @@ describe('GetAll methode test', () => {
},
});

expect(joinSpy).toBeCalledTimes(2);
expect(joinSpy).toBeCalledTimes(3);
expect(joinSpy).toHaveBeenNthCalledWith(
1,
`${aliasString}.comments`,
'comments'
);
expect(joinSpy).toHaveBeenNthCalledWith(
2,
`${aliasString}.manager`,
'manager'
);
expect(joinSpy).toHaveBeenNthCalledWith(
2,
3,
`${aliasString}.addresses`,
'addresses'
);
Expand Down Expand Up @@ -353,7 +358,7 @@ describe('GetAll methode test', () => {
expect(result.meta.totalItems).toBe(0);
expect(result.meta.pageSize).toBe(page.size);

expect(resultJoinSpy).toBeCalledTimes(2);
expect(resultJoinSpy).toBeCalledTimes(4);
expect(resultJoinSpy).toHaveBeenNthCalledWith(
1,
`${aliasString}.${include[0]}`,
Expand Down Expand Up @@ -469,14 +474,19 @@ describe('GetAll methode test', () => {
},
});

expect(joinSpy).toBeCalledTimes(2);
expect(joinSpy).toBeCalledTimes(3);
expect(joinSpy).toHaveBeenNthCalledWith(
1,
`${alisString}.comments`,
'comments'
);
expect(joinSpy).toHaveBeenNthCalledWith(
2,
`${alisString}.manager`,
'manager'
);
expect(joinSpy).toHaveBeenNthCalledWith(
2,
3,
`${alisString}.addresses`,
'addresses'
);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,22 +1,22 @@
import { BadRequestException } from '@nestjs/common';
import { OrderByCondition } from 'typeorm/find-options/OrderByCondition';
import {BadRequestException} from '@nestjs/common';
import {OrderByCondition} from 'typeorm/find-options/OrderByCondition';

import { TypeormMixinService } from '../../typeorm.mixin';
import { ServiceOptions, SortType } from '../../../../../types';
import { ResourceObject } from '../../../../../types-common';
import {TypeormMixinService} from '../../typeorm.mixin';
import {ServiceOptions, SortType} from '../../../../../types';
import {ResourceObject} from '../../../../../types-common';

import { snakeToCamel } from '../../../../../helper';
import {snakeToCamel} from '../../../../../helper';

export async function getAll<T>(
this: TypeormMixinService<T>,
options: ServiceOptions<T>
): Promise<ResourceObject<T>> {
const startTime = Date.now();
const { filter, include, sort, page, fields } = options.query;
const {filter, include, sort, page, fields} = options.query;
if (this.config.requiredSelectField && fields === null) {
throw new BadRequestException([
{
source: { parameter: '/fields' },
source: {parameter: '/fields'},
detail: 'Fields params in query is required',
},
]);
Expand Down Expand Up @@ -54,7 +54,7 @@ export async function getAll<T>(
fieldsSelect.add(`${rel}.${propsName}`);
}

const { target, ...other } = fields;
const {target, ...other} = fields;
const targetArray =
[...target, this.repository.metadata.primaryColumns[0].propertyName] ||
[];
Expand All @@ -69,7 +69,7 @@ export async function getAll<T>(
});
}

const { target, relation } = filter;
const {target, relation} = filter;

const expressionObjectForRelation = relation
? this.UtilsMethode.applyQueryFilterRelation(
Expand All @@ -94,14 +94,14 @@ export async function getAll<T>(
const joinForCommonQuery = {};

for (let i = 0; i < expressionObjectLength; i++) {
const { expression, params, selectInclude } = expressionObject[i];
const {expression, params, selectInclude} = expressionObject[i];
if (selectInclude) {
joinForCommonQuery[`${preparedResourceName}.${selectInclude}`] =
selectInclude;
}
builder[i === 0 ? 'where' : 'andWhere'](expression);
if (params) {
builder.setParameters(params ? { [params.name]: params.val } : {});
builder.setParameters(params ? {[params.name]: params.val} : {});
}
}
for (let i = 0; i < includeLength; i++) {
Expand All @@ -116,7 +116,7 @@ export async function getAll<T>(
builder.select(`${preparedResourceName}.${primaryColumn}`, subQueryIdAlias);

if (sort) {
const { target, ...otherSort } = sort;
const {target, ...otherSort} = sort;
const targetOrder = Object.entries<SortType>(
target || {}
).reduce<OrderByCondition>((acum, [key, val]) => {
Expand Down Expand Up @@ -172,19 +172,27 @@ export async function getAll<T>(

const resultBuilder = resultBuilderQuery
.select([...fieldsSelect])
.whereInIds(resultIds.map((i) => i[`${countAlias}_${primaryColumn}`]));
const ids = resultIds.map((i) => i[`${countAlias}_${primaryColumn}`]);
if (ids.length > 0) {
resultBuilder.whereInIds(resultIds.map((i) => i[`${countAlias}_${primaryColumn}`]));
}

for (let i = 0; i < expressionObjectForRelation.length; i++) {
const { expression, params, selectInclude } =
const {expression, params, selectInclude} =
expressionObjectForRelation[i];
if (selectInclude) {
if (selectInclude && !include.includes(selectInclude as any)) {
resultBuilder.leftJoin(
`${preparedResourceName}.${selectInclude}`,
selectInclude
);
}
resultBuilder.andWhere(expression);
resultBuilder.setParameters(params ? { [params.name]: params.val } : {});
if (i === 0 && ids.length === 0) {
resultBuilder.where(expression);
} else {
resultBuilder.andWhere(expression);
}

resultBuilder.setParameters(params ? {[params.name]: params.val} : {});
}

const result = await resultBuilder.getRawMany();
Expand All @@ -206,7 +214,7 @@ export async function getAll<T>(
pageNumber: page.number,
totalItems: count,
pageSize: page.size,
...(this.config.debug ? { debug } : {}),
...(this.config.debug ? {debug} : {}),
},
data,
included,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -483,7 +483,8 @@ describe('Utils methode test', () => {
)}`
)
.getQuery();
const check = `${alias}.id IN (${resultQuery})`;
// const check = `${alias}.id IN (${resultQuery})`;
const check = `comments.kind = :params_comments.kind_0`;
expect(expression[i].expression).toBe(check);
expect(expression[i].params.val).toBe(
filter[relName][fieldName][operand]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -267,32 +267,18 @@ export class UtilsMethode {
break;
}
case 'one-to-many': {
if (paramsField !== null) {
resultExpression.push({
expression: `${relationProperty}.${relationFieldProperty.toString()} ${currentOperandMap[
operand
].replace('EXPRESSION', paramsField)}`,
params: {
val: value,
name: paramsField,
},
selectInclude: relationProperty,
});
break;
}
const query = builder
.subQuery()
.select(`${resourceName}.${inverseSidePropertyPath}`)
.from(target, resourceName)
.where(
`${resourceName}.${relationFieldProperty.toString()} ${currentOperandMap[
operand
].replace('EXPRESSION', paramsField)}`
)
.getQuery();
resultExpression.push({
expression: `${preparedResourceName}.id IN ${query}`,
params: null,
expression: `${relationProperty}.${relationFieldProperty.toString()} ${currentOperandMap[
operand
].replace('EXPRESSION', paramsField)}`,
params:
paramsField === null
? null
: {
val: value,
name: paramsField,
},
selectInclude: relationProperty,
});
break;
}
Expand Down

0 comments on commit 6e19cb2

Please sign in to comment.