Skip to content

Commit 419d5b4

Browse files
committed
perf(query-typeorm): Use existing join alias if there is one
1 parent 5843ac4 commit 419d5b4

File tree

6 files changed

+165
-147
lines changed

6 files changed

+165
-147
lines changed

jest.preset.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,13 @@ module.exports = {
55

66
collectCoverage: true,
77
coverageReporters: ['html', 'clover'],
8-
collectCoverageFrom: ['packages/**/*.ts', '!**/__tests__/**', '!**/dist/**', '!**/node_modules/**'],
8+
collectCoverageFrom: [
9+
'packages/**/*.ts',
10+
'!**/__tests__/**',
11+
'!**/dist/**',
12+
'!**/node_modules/**',
13+
'!**/jest.config.ts',
14+
],
915
moduleNameMapper: {
1016
'@ptc-org/nestjs-query-core': process.cwd() + '/packages/core/src',
1117
'@ptc-org/nestjs-query-graphql': process.cwd() + '/packages/query-graphql/src',

packages/query-typeorm/__tests__/query/__snapshots__/relation-query.builder.spec.ts.snap

Lines changed: 25 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,28 @@ SELECT
77
"manyToOneRelation"."test_entity_id" AS "manyToOneRelation_test_entity_id",
88
"manyToOneRelation"."uni_directional_test_entity_id" AS "manyToOneRelation_uni_directional_test_entity_id",
99
"manyToOneRelation"."uni_directional_relation_test_entity_id" AS "manyToOneRelation_uni_directional_relation_test_entity_id",
10-
"TestEntity"."test_entity_pk" AS "TestEntity_test_entity_pk"
10+
"test_entity"."test_entity_pk" AS "test_entity_testEntityPk"
1111
FROM
1212
"test_relation" "manyToOneRelation"
13-
LEFT JOIN "test_entity" "TestEntity" ON "TestEntity"."many_to_one_relation_id" = "manyToOneRelation"."test_relation_pk"
13+
INNER JOIN "test_entity" "test_entity" ON "test_entity"."many_to_one_relation_id" = "manyToOneRelation"."test_relation_pk"
1414
WHERE
15-
"TestEntity"."test_entity_pk" IN (test-entity-id-1, test-entity-id-2)
15+
"test_entity"."test_entity_pk" IN (test-entity-id-1, test-entity-id-2)
16+
`;
17+
18+
exports[`RelationQueryBuilder #batchSelect should reuse existing join alias if there is one 1`] = `
19+
SELECT
20+
"manyToOneRelation"."test_relation_pk" AS "manyToOneRelation_test_relation_pk",
21+
"manyToOneRelation"."relation_name" AS "manyToOneRelation_relation_name",
22+
"manyToOneRelation"."test_entity_id" AS "manyToOneRelation_test_entity_id",
23+
"manyToOneRelation"."uni_directional_test_entity_id" AS "manyToOneRelation_uni_directional_test_entity_id",
24+
"manyToOneRelation"."uni_directional_relation_test_entity_id" AS "manyToOneRelation_uni_directional_relation_test_entity_id",
25+
"testEntity"."test_entity_pk" AS "testEntity_testEntityPk"
26+
FROM
27+
"test_relation" "manyToOneRelation"
28+
LEFT JOIN "test_entity" "testEntity" ON "testEntity"."test_entity_pk" = "manyToOneRelation"."test_entity_id"
29+
WHERE
30+
(("testEntity"."test_entity_pk" = test))
31+
AND "testEntity"."test_entity_pk" IN (test-entity-1)
1632
`;
1733

1834
exports[`RelationQueryBuilder #select many to many many-to-many custom join table should work with a many-to-many through a join table 1`] = `
@@ -86,9 +102,9 @@ SELECT
86102
"testEntityUniDirectional"."oneTestRelationTestRelationPk" AS "testEntityUniDirectional_oneTestRelationTestRelationPk"
87103
FROM
88104
"test_entity" "testEntityUniDirectional"
89-
INNER JOIN "test_relation" "TestRelation" ON "TestRelation"."uni_directional_test_entity_id" = "testEntityUniDirectional"."test_entity_pk"
105+
INNER JOIN "test_relation" "test_relation" ON "test_relation"."uni_directional_test_entity_id" = "testEntityUniDirectional"."test_entity_pk"
90106
WHERE
91-
("TestRelation"."test_relation_pk" = test-relation-id-1)
107+
("test_relation"."test_relation_pk" = test-relation-id-1)
92108
`;
93109

94110
exports[`RelationQueryBuilder #select many to one should work with one entity 1`] = `
@@ -102,9 +118,9 @@ SELECT
102118
"testEntity"."oneTestRelationTestRelationPk" AS "testEntity_oneTestRelationTestRelationPk"
103119
FROM
104120
"test_entity" "testEntity"
105-
INNER JOIN "test_relation" "TestRelation" ON "TestRelation"."test_entity_id" = "testEntity"."test_entity_pk"
121+
INNER JOIN "test_relation" "test_relation" ON "test_relation"."test_entity_id" = "testEntity"."test_entity_pk"
106122
WHERE
107-
("TestRelation"."test_relation_pk" = test-relation-id-1)
123+
("test_relation"."test_relation_pk" = test-relation-id-1)
108124
`;
109125

110126
exports[`RelationQueryBuilder #select one to many should query with a single entity 1`] = `
@@ -146,9 +162,9 @@ SELECT
146162
"oneTestRelation"."uni_directional_relation_test_entity_id" AS "oneTestRelation_uni_directional_relation_test_entity_id"
147163
FROM
148164
"test_relation" "oneTestRelation"
149-
INNER JOIN "test_entity" "TestEntity" ON "TestEntity"."oneTestRelationTestRelationPk" = "oneTestRelation"."test_relation_pk"
165+
INNER JOIN "test_entity" "test_entity" ON "test_entity"."oneTestRelationTestRelationPk" = "oneTestRelation"."test_relation_pk"
150166
WHERE
151-
("TestEntity"."test_entity_pk" = test-entity-id-1)
167+
("test_entity"."test_entity_pk" = test-entity-id-1)
152168
`;
153169

154170
exports[`RelationQueryBuilder #select with filter should call whereBuilder#build if there is a filter 1`] = `

packages/query-typeorm/__tests__/query/relation-query.builder.spec.ts

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ import { closeTestConnection, createTestConnection, getTestConnection } from '..
44
import { TestRelation } from '../__fixtures__/test-relation.entity';
55
import { TestEntity } from '../__fixtures__/test.entity';
66
import { RelationQueryBuilder } from '../../src/query';
7+
import { TestEntityRelationEntity } from '../__fixtures__/test-entity-relation.entity';
8+
import { TEST_ENTITIES, TEST_RELATIONS } from '../__fixtures__/seeds';
79

810
describe('RelationQueryBuilder', (): void => {
911
beforeEach(createTestConnection);
@@ -196,13 +198,17 @@ describe('RelationQueryBuilder', (): void => {
196198
}
197199
];
198200

199-
describe('many to one', () => {
200-
// TODO:: SQL is updated, now make sure that batchQueryRelations keeps working
201+
it('should reuse existing join alias if there is one', () => {
202+
const query: Query<TestRelation> = { filter: { testEntity: { testEntityPk: { eq: 'test' } } } };
203+
204+
const entities = TEST_ENTITIES.slice(0, 1);
205+
expectBatchSQLSnapshot(TestEntity, entities, 'manyToOneRelation', query);
206+
});
201207

208+
describe('many to one', () => {
202209
it('should query with with multiple entities', () => {
203210
expectBatchSQLSnapshot(TestEntity, testEntities, 'manyToOneRelation', {});
204211
});
205-
206212
});
207213
});
208214

packages/query-typeorm/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@ptc-org/nestjs-query-typeorm",
3-
"version": "1.0.0-alpha.2",
3+
"version": "1.0.0-alpha.3",
44
"description": "Typeorm adapter for @ptc-org/nestjs-query-core",
55
"author": "doug-martin <[email protected]>",
66
"homepage": "https://github.com/tripss/nestjs-query#readme",

0 commit comments

Comments
 (0)