Skip to content

Commit

Permalink
fix(typeorm): Give proper error message when nested generic controlle…
Browse files Browse the repository at this point in the history
…r used on array relation without inverse property (#1000)
  • Loading branch information
ktutnik committed Jul 3, 2021
1 parent 9b9354f commit e4ee57f
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 1 deletion.
4 changes: 3 additions & 1 deletion packages/typeorm/src/repository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,9 +62,11 @@ class TypeORMNestedRepository<P, T> implements NestedRepository<P, T> {
}

async find(pid: any, offset: number, limit: number, query: any, selection: SelectQuery, order: any): Promise<T[]> {
if(!this.relation.childProperty)
throw new Error(`Relation without inverse property is not supported by Nested Generic Controller`)
return this.nativeRepository.find({
where:
{ [this.relation.childProperty!]: pid, ...query },
{ [this.relation.childProperty]: pid, ...query },
skip: offset,
take: limit,
relations: selection.relations,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -689,6 +689,14 @@ Object {
}
`;

exports[`CRUD Many To Many Should give proper error on get many method 1`] = `
Array [
Array [
[Error: Relation without inverse property is not supported by Nested Generic Controller],
],
]
`;

exports[`CRUD Many To Many Should serve many to many properly 1`] = `
User {
"animals": Array [
Expand Down
31 changes: 31 additions & 0 deletions tests/behavior/typeorm/typeorm-generic.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2751,6 +2751,37 @@ describe("CRUD", () => {
const result = await getRepository(User).findOne(user.id, { relations: ["animals"] })
expect(result).toMatchSnapshot()
})
it("Should give proper error on get many method", async () => {
@Entity()
class User {
@PrimaryGeneratedColumn()
id: number
@Column()
email: string
@Column()
name: string
@genericController()
@ManyToMany(x => Animal)
@JoinTable()
animals: Animal[]
}

@Entity()
class Animal {
@PrimaryGeneratedColumn()
id: number
@Column()
name: string
}
const app = await createApp([User, Animal], { mode: "production" })
const fn = jest.fn()
app.on("error", e => fn(e))
const user = await createUser(User)
await supertest(app.callback())
.get(`/users/${user.id}/animals`)
.expect(500)
expect(fn.mock.calls).toMatchSnapshot()
})
it("Should able to post from children Ids from parent", async () => {
@genericController()
@Entity()
Expand Down

0 comments on commit e4ee57f

Please sign in to comment.