-
-
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: TreeRepository based entities primary column supports custom nam…
…e. (#6942) Previously "id" was hardcoded, thus errors when an entity has a custom primary column name other than "id". Now it fetches the column name from metadata instead.
- Loading branch information
Showing
3 changed files
with
72 additions
and
1 deletion.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
import { Entity, PrimaryGeneratedColumn, Column, Tree, TreeParent, TreeChildren } from "../../../../src"; | ||
|
||
@Entity() | ||
@Tree("closure-table") | ||
export class Category { | ||
@PrimaryGeneratedColumn() | ||
cat_id: number; | ||
|
||
@Column() | ||
cat_name: string; | ||
|
||
@TreeParent() | ||
parent: Category; | ||
|
||
@TreeChildren({ cascade: true }) | ||
children: Category[]; | ||
} |
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,54 @@ | ||
import "reflect-metadata"; | ||
import { Category } from "./entity/Category"; | ||
import { Connection } from "../../../src/connection/Connection"; | ||
import { | ||
closeTestingConnections, | ||
createTestingConnections, | ||
reloadTestingDatabases, | ||
} from "../../../test/utils/test-utils"; | ||
|
||
describe("github issues > #6947 Custom primary column for TreeRepository based entities unable to get tree descendants", () => { | ||
let connections: Connection[]; | ||
before( | ||
async () => | ||
(connections = await createTestingConnections({ | ||
entities: [Category], | ||
})) | ||
); | ||
beforeEach(() => reloadTestingDatabases(connections)); | ||
after(() => closeTestingConnections(connections)); | ||
|
||
it("entities with custom primary column names should work", () => | ||
Promise.all( | ||
connections.map(async (connection) => { | ||
const categoryRepository = connection.getTreeRepository( | ||
Category | ||
); | ||
|
||
const parent = new Category(); | ||
parent.cat_name = "parent"; | ||
await categoryRepository.save(parent); | ||
|
||
const child = new Category(); | ||
child.cat_name = "child"; | ||
child.parent = parent; | ||
await categoryRepository.save(child); | ||
|
||
const tree = await categoryRepository.findDescendantsTree( | ||
(await categoryRepository.findOne({ cat_name: "parent" }))! | ||
); | ||
|
||
tree.should.deep.include({ | ||
cat_id: 1, | ||
cat_name: "parent", | ||
children: [ | ||
{ | ||
cat_id: 2, | ||
cat_name: "child", | ||
children: [], | ||
}, | ||
], | ||
}); | ||
}) | ||
)); | ||
}); |