Skip to content

Commit

Permalink
Merge pull request #4124 from EctorParking/fix/relation-alias
Browse files Browse the repository at this point in the history
Allow join relation alias override from naming strategies
  • Loading branch information
pleerock authored May 12, 2019
2 parents 12cc870 + 0434521 commit 79110d9
Show file tree
Hide file tree
Showing 7 changed files with 69 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/find-options/FindOptionsUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -234,7 +234,7 @@ export class FindOptionsUtils {

public static joinEagerRelations(qb: SelectQueryBuilder<any>, alias: string, metadata: EntityMetadata) {
metadata.eagerRelations.forEach(relation => {
const relationAlias = alias + "_" + relation.propertyPath.replace(".", "_");
const relationAlias = qb.connection.namingStrategy.eagerJoinRelationAlias(alias, relation.propertyPath);
qb.leftJoinAndSelect(alias + "." + relation.propertyPath, relationAlias);
this.joinEagerRelations(qb, relationAlias, relation.inverseEntityMetadata);
});
Expand Down
3 changes: 3 additions & 0 deletions src/naming-strategy/DefaultNamingStrategy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -148,4 +148,7 @@ export class DefaultNamingStrategy implements NamingStrategyInterface {
return prefix + tableName;
}

eagerJoinRelationAlias(alias: string, propertyPath: string): string {
return alias + "_" + propertyPath.replace(".", "_");
}
}
4 changes: 4 additions & 0 deletions src/naming-strategy/NamingStrategyInterface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -117,4 +117,8 @@ export interface NamingStrategyInterface {
*/
prefixTableName(prefix: string, tableName: string): string;

/**
* Gets the name of the alias used for relation joins.
*/
eagerJoinRelationAlias(alias: string, propertyPath: string): string;
}
15 changes: 15 additions & 0 deletions test/github-issues/2200/entity/Booking.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import {Entity, PrimaryGeneratedColumn, ManyToOne, JoinColumn} from "../../../../src/index";
import { Contact } from "./Contact";

@Entity()
export class Booking {

@PrimaryGeneratedColumn()
id: number;

@ManyToOne(type => Contact, contact => contact.bookings, { eager: true })
@JoinColumn({
name: "contact_id",
})
contact: Contact;
}
12 changes: 12 additions & 0 deletions test/github-issues/2200/entity/Contact.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import {Entity, PrimaryGeneratedColumn, OneToMany} from "../../../../src/index";
import { Booking } from "./Booking";

@Entity()
export class Contact {

@PrimaryGeneratedColumn()
id: number;

@OneToMany(type => Booking, booking => booking.contact)
bookings: Booking[];
}
27 changes: 27 additions & 0 deletions test/github-issues/2200/issue-2200.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import "reflect-metadata";
import {closeTestingConnections, createTestingConnections, reloadTestingDatabases} from "../../utils/test-utils";
import {Connection} from "../../../src/connection/Connection";
import {Booking} from "./entity/Booking";
import {NamingStrategyUnderTest} from "./naming/NamingStrategyUnderTest";


describe("github issue > #2200 Bug - Issue with snake_case naming strategy", () => {

let connections: Connection[];
let namingStrategy = new NamingStrategyUnderTest();

before(async () => connections = await createTestingConnections({
entities: [__dirname + "/entity/*{.js,.ts}"],
namingStrategy
}));
beforeEach(() => {
return reloadTestingDatabases(connections);
});
after(() => closeTestingConnections(connections));


it("Renammed alias allow to query correctly", () => Promise.all(connections.map(async connection => {
await connection.getRepository(Booking).find({take: 10});
})));

});
7 changes: 7 additions & 0 deletions test/github-issues/2200/naming/NamingStrategyUnderTest.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { DefaultNamingStrategy } from "../../../../src/naming-strategy/DefaultNamingStrategy";
import { NamingStrategyInterface } from "../../../../src/naming-strategy/NamingStrategyInterface";

export class NamingStrategyUnderTest extends DefaultNamingStrategy implements NamingStrategyInterface {
eagerJoinRelationAlias(alias: string, propertyPath: string): string {
return alias + "__" + propertyPath.replace(".", "_");
}}

0 comments on commit 79110d9

Please sign in to comment.