Skip to content

Commit

Permalink
fix: Fix TypeORM helper error on OneToMany relation without inverse p…
Browse files Browse the repository at this point in the history
…roperty configuration (#974)
  • Loading branch information
ktutnik committed Jun 14, 2021
1 parent aca5751 commit c06c017
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 6 deletions.
12 changes: 6 additions & 6 deletions packages/typeorm/src/helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,12 +49,12 @@ function normalizeEntityNoCache(type: Class) {
throw new Error(`Relation property ${target.name}.${col.propertyName} uses unsupported data type`)
const rawType: Class = col.type()
if (col.relationType === "many-to-many" || col.relationType === "one-to-many") {
const inverse = col.inverseSideProperty!
const inverseProperty = typeof inverse === "string" ? inverse : inverseSideParser(inverse)
const decorators = [
reflect.type(x => [rawType]),
entity.relation({ inverseProperty })
]
const decorators = [reflect.type(x => [rawType])]
const inverse = col.inverseSideProperty
if(inverse){
const inverseProperty = typeof inverse === "string" ? inverse : inverseSideParser(inverse)
decorators.push(entity.relation({ inverseProperty }))
}
Reflect.decorate(decorators, target.prototype, col.propertyName, void 0)
}
else {
Expand Down
13 changes: 13 additions & 0 deletions tests/behavior/typeorm/__snapshots__/typeorm.spec.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -375,6 +375,19 @@ Array [
]
`;

exports[`TypeOrm Facility Should not error when many to many relation doesn't specify inverse relation 1`] = `
MyEntity {
"children": Array [
Child {
"id": 1,
"name": "Poo",
},
],
"id": 1,
"name": "Mimi",
}
`;

exports[`TypeOrm Facility Should throw error when no entities specified 1`] = `
Array [
Array [
Expand Down
27 changes: 27 additions & 0 deletions tests/behavior/typeorm/typeorm.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -271,6 +271,33 @@ describe("TypeOrm", () => {
expect(extract(MyEntity)).toMatchSnapshot()
expect(extract(Child)).toMatchSnapshot()
})
it("Should not error when many to many relation doesn't specify inverse relation", async () => {
@Entity()
class MyEntity {
@PrimaryGeneratedColumn()
id: number
@Column()
name: string
@ManyToMany(x => Child)
@JoinTable()
children: Child[]
}
@Entity()
class Child {
@PrimaryGeneratedColumn()
id: number
@Column()
name: string
}
await createApp([MyEntity, Child])
const parentRepo = getManager().getRepository(MyEntity)
const repo = getManager().getRepository(Child)
const parent = await parentRepo.insert({ name: "Mimi" })
const inserted = await repo.insert({ name: "Poo" })
await parentRepo.createQueryBuilder().relation("children").of(parent.raw).add(inserted.raw)
const result = await parentRepo.findOne(parent.raw, { relations: ["children"] })
expect(result).toMatchSnapshot()
})
it("Should throw error when no entities specified", async () => {
class UsersController {
get() { }
Expand Down

0 comments on commit c06c017

Please sign in to comment.