Skip to content
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

Fix/repeating aliases in nested relations #282

Closed
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
56 changes: 28 additions & 28 deletions packages/crud-typeorm/src/typeorm-crud.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,34 @@ export class TypeOrmCrudService<T> extends CrudService<T> {
// select fields
builder.select(select);

// set joins
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In this method, I do not understand why the filters were added before joins? Is there a reason for this? My reason to change ordering is that some filters would do not valid until relation do not add to entityRelationsHash.

const joinOptions = options.query.join || {};
const allowedJoins = objKeys(joinOptions);

if (hasLength(allowedJoins)) {
const eagerJoins: any = {};

for (let i = 0; i < allowedJoins.length; i++) {
/* istanbul ignore else */
if (joinOptions[allowedJoins[i]].eager) {
const cond = parsed.join.find((j) => j && j.field === allowedJoins[i]) || {
field: allowedJoins[i],
};
this.setJoin(cond, joinOptions, builder);
eagerJoins[allowedJoins[i]] = true;
}
}

if (isArrayFull(parsed.join)) {
for (let i = 0; i < parsed.join.length; i++) {
/* istanbul ignore else */
if (!eagerJoins[parsed.join[i].field]) {
this.setJoin(parsed.join[i], joinOptions, builder);
}
}
}
}

// set mandatory where condition from CrudOptions.query.filter
if (isArrayFull(options.query.filter)) {
for (let i = 0; i < options.query.filter.length; i++) {
Expand Down Expand Up @@ -269,34 +297,6 @@ export class TypeOrmCrudService<T> extends CrudService<T> {
}
}

// set joins
const joinOptions = options.query.join || {};
const allowedJoins = objKeys(joinOptions);

if (hasLength(allowedJoins)) {
const eagerJoins: any = {};

for (let i = 0; i < allowedJoins.length; i++) {
/* istanbul ignore else */
if (joinOptions[allowedJoins[i]].eager) {
const cond = parsed.join.find((j) => j && j.field === allowedJoins[i]) || {
field: allowedJoins[i],
};
this.setJoin(cond, joinOptions, builder);
eagerJoins[allowedJoins[i]] = true;
}
}

if (isArrayFull(parsed.join)) {
for (let i = 0; i < parsed.join.length; i++) {
/* istanbul ignore else */
if (!eagerJoins[parsed.join[i].field]) {
this.setJoin(parsed.join[i], joinOptions, builder);
}
}
}
}

/* istanbul ignore else */
if (many) {
// set sort (order by)
Expand Down
45 changes: 27 additions & 18 deletions packages/crud-typeorm/test/2.nested_relations.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,24 +31,12 @@ describe('#crud-typeorm', () => {
model: { type: Project },
query: {
join: {
category: {
eager: true,
},
'category.image': {
eager: true,
},
'category.children': {
eager: true,
},
'category.children.image': {
eager: true,
},
'category.children.children': {
eager: true,
},
'category.children.children.image': {
eager: true,
},
category: {},
'category.image': {},
'category.children': {},
'category.children.image': {},
'category.children.children': {},
'category.children.children.image': {},
},
},
})
Expand Down Expand Up @@ -109,8 +97,29 @@ describe('#crud-typeorm', () => {
describe('has three level of categories in response', () => {
let res: any;
beforeAll(async () => {
const q = new RequestQueryBuilder()
.setJoin({
field: 'category',
})
.setJoin({
field: 'category.image',
})
.setJoin({
field: 'category.children',
})
.setJoin({
field: 'category.children.image',
})
.setJoin({
field: 'category.children.children',
})
.setJoin({
field: 'category.children.children.image',
})
.query();
res = await request(server)
.get('/projects/1')
.query(q)
.expect(200)
.then((res) => res);
});
Expand Down