Skip to content

Commit

Permalink
perf(query-typeorm): Use existing join alias if there is one
Browse files Browse the repository at this point in the history
  • Loading branch information
TriPSs committed May 18, 2022
1 parent 5843ac4 commit 419d5b4
Show file tree
Hide file tree
Showing 6 changed files with 165 additions and 147 deletions.
8 changes: 7 additions & 1 deletion jest.preset.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,13 @@ module.exports = {

collectCoverage: true,
coverageReporters: ['html', 'clover'],
collectCoverageFrom: ['packages/**/*.ts', '!**/__tests__/**', '!**/dist/**', '!**/node_modules/**'],
collectCoverageFrom: [
'packages/**/*.ts',
'!**/__tests__/**',
'!**/dist/**',
'!**/node_modules/**',
'!**/jest.config.ts',
],
moduleNameMapper: {
'@ptc-org/nestjs-query-core': process.cwd() + '/packages/core/src',
'@ptc-org/nestjs-query-graphql': process.cwd() + '/packages/query-graphql/src',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,28 @@ SELECT
"manyToOneRelation"."test_entity_id" AS "manyToOneRelation_test_entity_id",
"manyToOneRelation"."uni_directional_test_entity_id" AS "manyToOneRelation_uni_directional_test_entity_id",
"manyToOneRelation"."uni_directional_relation_test_entity_id" AS "manyToOneRelation_uni_directional_relation_test_entity_id",
"TestEntity"."test_entity_pk" AS "TestEntity_test_entity_pk"
"test_entity"."test_entity_pk" AS "test_entity_testEntityPk"
FROM
"test_relation" "manyToOneRelation"
LEFT JOIN "test_entity" "TestEntity" ON "TestEntity"."many_to_one_relation_id" = "manyToOneRelation"."test_relation_pk"
INNER JOIN "test_entity" "test_entity" ON "test_entity"."many_to_one_relation_id" = "manyToOneRelation"."test_relation_pk"
WHERE
"TestEntity"."test_entity_pk" IN (test-entity-id-1, test-entity-id-2)
"test_entity"."test_entity_pk" IN (test-entity-id-1, test-entity-id-2)
`;

exports[`RelationQueryBuilder #batchSelect should reuse existing join alias if there is one 1`] = `
SELECT
"manyToOneRelation"."test_relation_pk" AS "manyToOneRelation_test_relation_pk",
"manyToOneRelation"."relation_name" AS "manyToOneRelation_relation_name",
"manyToOneRelation"."test_entity_id" AS "manyToOneRelation_test_entity_id",
"manyToOneRelation"."uni_directional_test_entity_id" AS "manyToOneRelation_uni_directional_test_entity_id",
"manyToOneRelation"."uni_directional_relation_test_entity_id" AS "manyToOneRelation_uni_directional_relation_test_entity_id",
"testEntity"."test_entity_pk" AS "testEntity_testEntityPk"
FROM
"test_relation" "manyToOneRelation"
LEFT JOIN "test_entity" "testEntity" ON "testEntity"."test_entity_pk" = "manyToOneRelation"."test_entity_id"
WHERE
(("testEntity"."test_entity_pk" = test))
AND "testEntity"."test_entity_pk" IN (test-entity-1)
`;

exports[`RelationQueryBuilder #select many to many many-to-many custom join table should work with a many-to-many through a join table 1`] = `
Expand Down Expand Up @@ -86,9 +102,9 @@ SELECT
"testEntityUniDirectional"."oneTestRelationTestRelationPk" AS "testEntityUniDirectional_oneTestRelationTestRelationPk"
FROM
"test_entity" "testEntityUniDirectional"
INNER JOIN "test_relation" "TestRelation" ON "TestRelation"."uni_directional_test_entity_id" = "testEntityUniDirectional"."test_entity_pk"
INNER JOIN "test_relation" "test_relation" ON "test_relation"."uni_directional_test_entity_id" = "testEntityUniDirectional"."test_entity_pk"
WHERE
("TestRelation"."test_relation_pk" = test-relation-id-1)
("test_relation"."test_relation_pk" = test-relation-id-1)
`;

exports[`RelationQueryBuilder #select many to one should work with one entity 1`] = `
Expand All @@ -102,9 +118,9 @@ SELECT
"testEntity"."oneTestRelationTestRelationPk" AS "testEntity_oneTestRelationTestRelationPk"
FROM
"test_entity" "testEntity"
INNER JOIN "test_relation" "TestRelation" ON "TestRelation"."test_entity_id" = "testEntity"."test_entity_pk"
INNER JOIN "test_relation" "test_relation" ON "test_relation"."test_entity_id" = "testEntity"."test_entity_pk"
WHERE
("TestRelation"."test_relation_pk" = test-relation-id-1)
("test_relation"."test_relation_pk" = test-relation-id-1)
`;

exports[`RelationQueryBuilder #select one to many should query with a single entity 1`] = `
Expand Down Expand Up @@ -146,9 +162,9 @@ SELECT
"oneTestRelation"."uni_directional_relation_test_entity_id" AS "oneTestRelation_uni_directional_relation_test_entity_id"
FROM
"test_relation" "oneTestRelation"
INNER JOIN "test_entity" "TestEntity" ON "TestEntity"."oneTestRelationTestRelationPk" = "oneTestRelation"."test_relation_pk"
INNER JOIN "test_entity" "test_entity" ON "test_entity"."oneTestRelationTestRelationPk" = "oneTestRelation"."test_relation_pk"
WHERE
("TestEntity"."test_entity_pk" = test-entity-id-1)
("test_entity"."test_entity_pk" = test-entity-id-1)
`;

exports[`RelationQueryBuilder #select with filter should call whereBuilder#build if there is a filter 1`] = `
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import { closeTestConnection, createTestConnection, getTestConnection } from '..
import { TestRelation } from '../__fixtures__/test-relation.entity';
import { TestEntity } from '../__fixtures__/test.entity';
import { RelationQueryBuilder } from '../../src/query';
import { TestEntityRelationEntity } from '../__fixtures__/test-entity-relation.entity';
import { TEST_ENTITIES, TEST_RELATIONS } from '../__fixtures__/seeds';

describe('RelationQueryBuilder', (): void => {
beforeEach(createTestConnection);
Expand Down Expand Up @@ -196,13 +198,17 @@ describe('RelationQueryBuilder', (): void => {
}
];

describe('many to one', () => {
// TODO:: SQL is updated, now make sure that batchQueryRelations keeps working
it('should reuse existing join alias if there is one', () => {
const query: Query<TestRelation> = { filter: { testEntity: { testEntityPk: { eq: 'test' } } } };

const entities = TEST_ENTITIES.slice(0, 1);
expectBatchSQLSnapshot(TestEntity, entities, 'manyToOneRelation', query);
});

describe('many to one', () => {
it('should query with with multiple entities', () => {
expectBatchSQLSnapshot(TestEntity, testEntities, 'manyToOneRelation', {});
});

});
});

Expand Down
2 changes: 1 addition & 1 deletion packages/query-typeorm/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@ptc-org/nestjs-query-typeorm",
"version": "1.0.0-alpha.2",
"version": "1.0.0-alpha.3",
"description": "Typeorm adapter for @ptc-org/nestjs-query-core",
"author": "doug-martin <doug@dougamartin.com>",
"homepage": "https://github.com/tripss/nestjs-query#readme",
Expand Down
Loading

0 comments on commit 419d5b4

Please sign in to comment.