-
-
Notifications
You must be signed in to change notification settings - Fork 6.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: support multiple
JoinColumn
s in EntitySchema (#6397)
update type definition and schema transformer so that - like the decorator - the EntitySchema can define composite `JoinColumn` definitions Closes: #5444
- Loading branch information
1 parent
c6336aa
commit 298a3b9
Showing
5 changed files
with
146 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
import {EntitySchemaOptions} from "../../../../src/entity-schema/EntitySchemaOptions"; | ||
import {Post} from "./Post"; | ||
|
||
export class Author { | ||
id: number; | ||
|
||
publisherId: number; | ||
|
||
name: string; | ||
|
||
posts: Post[]; | ||
} | ||
|
||
export const AuthorSchema: EntitySchemaOptions<Author> = { | ||
name: "Author", | ||
|
||
target: Author, | ||
|
||
columns: { | ||
id: { | ||
primary: true, | ||
type: Number | ||
}, | ||
|
||
publisherId: { | ||
primary: true, | ||
type: Number | ||
}, | ||
|
||
name: { | ||
type: "varchar" | ||
} | ||
}, | ||
|
||
relations: { | ||
posts: { | ||
target: () => Post, | ||
type: "one-to-many" | ||
} | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
import {EntitySchemaOptions} from "../../../../src/entity-schema/EntitySchemaOptions"; | ||
import {Author} from "./Author"; | ||
|
||
export class Post { | ||
authorPublisherId: number; | ||
|
||
authorId: number; | ||
|
||
id: number; | ||
|
||
title: string; | ||
|
||
author: Author; | ||
} | ||
|
||
export const PostSchema: EntitySchemaOptions<Post> = { | ||
name: "Post", | ||
|
||
target: Post, | ||
|
||
columns: { | ||
authorPublisherId: { | ||
primary: true, | ||
type: Number | ||
}, | ||
|
||
authorId: { | ||
primary: true, | ||
type: Number | ||
}, | ||
|
||
id: { | ||
primary: true, | ||
type: Number | ||
}, | ||
|
||
title: { | ||
type: "varchar" | ||
} | ||
}, | ||
|
||
relations: { | ||
author: { | ||
target: () => Author, | ||
type: "many-to-one", | ||
eager: true, | ||
joinColumn: [ | ||
{ name: "authorPublisherId", referencedColumnName: "publisherId" }, | ||
{ name: "authorId", referencedColumnName: "id" }, | ||
] | ||
} | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
import { EntitySchemaTransformer } from "../../../src/entity-schema/EntitySchemaTransformer"; | ||
|
||
import {expect} from "chai"; | ||
|
||
import { Post, PostSchema } from "./entity/Post"; | ||
import { Author, AuthorSchema } from "./entity/Author"; | ||
import {EntitySchema} from "../../../src"; | ||
|
||
|
||
describe("github issues > #5444 EntitySchema missing support for multiple joinColumns in relations", () => { | ||
it("Update query returns the number of affected rows", async () => { | ||
const transformer = new EntitySchemaTransformer(); | ||
|
||
const actual = transformer.transform( | ||
[ | ||
new EntitySchema<Author>(AuthorSchema), | ||
new EntitySchema<Post>(PostSchema) | ||
] | ||
); | ||
|
||
const joinColumns = actual.joinColumns; | ||
|
||
expect(joinColumns.length).to.eq(2); | ||
expect(joinColumns).to.deep.eq([ | ||
{ | ||
target: Post, | ||
propertyName: "author", | ||
name: "authorPublisherId", | ||
referencedColumnName: "publisherId" | ||
}, | ||
{ | ||
target: Post, | ||
propertyName: "author", | ||
name: "authorId", | ||
referencedColumnName: "id" | ||
}, | ||
|
||
]); | ||
}); | ||
}); |