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

Typeorm adapter improvements #36

Open
wants to merge 4 commits into
base: alpha
Choose a base branch
from
Open
Changes from 1 commit
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
Prev Previous commit
Next Next commit
TypeORM adapter: Use left join instead of inner join
Fix issue #25 for TypeORM adapter
ccatterina committed Aug 26, 2022
commit 837a0036c7ddbdf2a6b898fbfa1a480d2b84d888
10 changes: 5 additions & 5 deletions packages/sql/spec/typeorm.spec.ts
Original file line number Diff line number Diff line change
@@ -54,14 +54,14 @@ describe('Condition interpreter for TypeORM', () => {
})
})

it('automatically inner joins relation when condition is set on relation field', () => {
it('automatically left joins relation when condition is set on relation field', () => {
const condition = new FieldCondition('eq', 'projects.name', 'test')
const query = interpret(condition, conn.createQueryBuilder(User, 'u'))

expect(query.getQuery()).to.equal([
'SELECT "u"."id" AS "u_id", "u"."name" AS "u_name"',
'FROM "user" "u"',
'INNER JOIN "project" "projects" ON "projects"."userId"="u"."id"',
'LEFT JOIN "project" "projects" ON "projects"."userId"="u"."id"',
'WHERE "projects"."name" = :0'
].join(' '))
expect(query.getParameters()).to.eql({ 0: 'test' })
@@ -77,7 +77,7 @@ describe('Condition interpreter for TypeORM', () => {
expect(query.getQuery()).to.equal([
'SELECT "u"."id" AS "u_id", "u"."name" AS "u_name"',
'FROM "user" "u"',
'INNER JOIN "project" "projects" ON "projects"."userId"="u"."id"',
'LEFT JOIN "project" "projects" ON "projects"."userId"="u"."id"',
'WHERE ("projects"."name" = :0 and "projects"."active" = :1)'
].join(' '))
expect(query.getParameters()).to.eql({ 0: 'test', 1: true })
@@ -93,8 +93,8 @@ describe('Condition interpreter for TypeORM', () => {
expect(query.getQuery()).to.equal([
'SELECT "u"."id" AS "u_id", "u"."name" AS "u_name"',
'FROM "user" "u"',
'INNER JOIN "project" "projects" ON "projects"."userId"="u"."id"',
' INNER JOIN "review" "projects_reviews" ON "projects_reviews"."projectId"="projects"."id"',
'LEFT JOIN "project" "projects" ON "projects"."userId"="u"."id"',
' LEFT JOIN "review" "projects_reviews" ON "projects_reviews"."projectId"="projects"."id"',
'WHERE ("projects_reviews"."rating" = :0 and "projects"."active" = :1)'
].join(' '))
expect(query.getParameters()).to.eql({ 0: 5, 1: true })
2 changes: 1 addition & 1 deletion packages/sql/src/lib/typeorm.ts
Original file line number Diff line number Diff line change
@@ -24,7 +24,7 @@ function joinRelation<Entity>(relation: string, query: SelectQueryBuilder<Entity
const alias = (i > 0) ? relationParts.slice(0, i).join('_') : query.expressionMap.mainAlias!.name;
const nextJoinAlias = relationParts.slice(0, i + 1).join('_');
if (!query.expressionMap.joinAttributes.some(j => j.alias.name === nextJoinAlias)) {
query.innerJoin(`${alias}.${part}`, nextJoinAlias);
query.leftJoin(`${alias}.${part}`, nextJoinAlias);
}
});
return true;