Skip to content

Commit

Permalink
fix(typeorm): Fix array relation insert issue on nested generic contr…
Browse files Browse the repository at this point in the history
…oller (#999)
  • Loading branch information
ktutnik committed Jul 3, 2021
1 parent 65d1cfd commit 9b9354f
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 13 deletions.
14 changes: 3 additions & 11 deletions packages/typeorm/src/repository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,11 +62,6 @@ class TypeORMNestedRepository<P, T> implements NestedRepository<P, T> {
}

async find(pid: any, offset: number, limit: number, query: any, selection: SelectQuery, order: any): Promise<T[]> {
// const parent = await this.nativeParentRepository
// .createQueryBuilder("parent")
// .leftJoinAndSelect(`parent.${this.relation.parentProperty}`, "child")
// .whereInIds(pid)
// .getOne()
return this.nativeRepository.find({
where:
{ [this.relation.childProperty!]: pid, ...query },
Expand All @@ -81,15 +76,12 @@ class TypeORMNestedRepository<P, T> implements NestedRepository<P, T> {
async insert(pid: any, data: Partial<T>) {
const parent = await this.nativeParentRepository.findOne(pid)
if (this.relation.parentProperty) {
const result = await this.nativeRepository.insert(data as any);
const first = result.identifiers[0]
const idName = Object.keys(first)[0]
const id = (first as any)[idName]
const result = await this.nativeRepository.save(data as any);
await this.nativeParentRepository.createQueryBuilder()
.relation(this.relation.parentProperty!)
.of(parent)
.add(id)
return (await this.nativeRepository.findOne(id))!
.add(result)
return result
}
else {
return this.nativeRepository.save({ [this.relation.childProperty!]: pid, ...data })
Expand Down
19 changes: 19 additions & 0 deletions tests/behavior/typeorm/__snapshots__/typeorm-generic.spec.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -653,6 +653,25 @@ Object {
}
`;

exports[`CRUD Many To Many Should able to post from children Ids from nested parent 1`] = `
Array [
Object {
"id": 1,
"images": Array [
Object {
"id": 1,
"url": "https://images.com/img.jpg",
},
Object {
"id": 2,
"url": "https://images.com/img.jpg",
},
],
"name": "Mimi",
},
]
`;

exports[`CRUD Many To Many Should able to post from children Ids from parent 1`] = `
Object {
"animals": Array [
Expand Down
54 changes: 52 additions & 2 deletions tests/behavior/typeorm/typeorm-generic.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2788,6 +2788,56 @@ describe("CRUD", () => {
.expect(200)
expect(result).toMatchSnapshot()
})
it("Should able to post from children Ids from nested parent", async () => {
@Entity()
class User {
@PrimaryGeneratedColumn()
id: number
@Column()
email: string
@Column()
name: string
@genericController()
@OneToMany(x => Animal, x => x.user)
@JoinTable()
animals: Animal[]
}
@Entity()
class Animal {
@PrimaryGeneratedColumn()
id: number
@Column()
name: string
@ManyToOne(x => User, x => x.animals)
user:User
@ManyToMany(x => Image)
@JoinTable()
images: Image[]
}
@Entity()
class Image {
@PrimaryGeneratedColumn()
id: number
@Column()
url: string
}
const app = await createApp([User, Animal, Image], { mode: "production" })
const userRepo = getManager().getRepository(User)
const imageRepo = getManager().getRepository(Image)
const user = await userRepo.save({ email: "john.doe@gmail.com", name: "John Doe" })
const images = await Promise.all([
imageRepo.save({ url: "https://images.com/img.jpg" }),
imageRepo.save({ url: "https://images.com/img.jpg" }),
])
await supertest(app.callback())
.post(`/users/${user.id}/animals`)
.send({ name: "Mimi", images: images.map(x => x.id) })
.expect(200)
const { body: result } = await supertest(app.callback())
.get(`/users/${user.id}/animals?select=name,images`)
.expect(200)
expect(result).toMatchSnapshot()
})
})
})

Expand Down Expand Up @@ -2894,7 +2944,7 @@ describe("Open API", () => {
expect(body.paths["/users"].post.requestBody).toMatchSnapshot()
})
describe("Generic Controller", () => {
function createApp(controller: Class, entity:Class) {
function createApp(controller: Class, entity: Class) {
return new Plumier()
.set({ mode: "production" })
.set(new WebApiFacility({ controller }))
Expand Down Expand Up @@ -2940,7 +2990,7 @@ describe("Open API", () => {

})
describe("Nested Generic Controller", () => {
function createApp(controller: Class, entity:Class[]) {
function createApp(controller: Class, entity: Class[]) {
return new Plumier()
.set({ mode: "production" })
.set(new WebApiFacility({ controller }))
Expand Down

0 comments on commit 9b9354f

Please sign in to comment.