Skip to content

Commit

Permalink
fix: entity manager remove using entity schemas (#9221)
Browse files Browse the repository at this point in the history
Currently the entity manager remove method does not recognize the first param if it is an entity schema.
  • Loading branch information
Yavanosta committed Aug 24, 2022
1 parent 8dcd61e commit f045536
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 1 deletion.
1 change: 1 addition & 0 deletions src/entity-manager/EntityManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -478,6 +478,7 @@ export class EntityManager {
const target =
arguments.length > 1 &&
(typeof targetOrEntity === "function" ||
InstanceChecker.isEntitySchema(targetOrEntity) ||
typeof targetOrEntity === "string")
? (targetOrEntity as Function | string)
: undefined
Expand Down
37 changes: 36 additions & 1 deletion test/functional/entity-schema/basic/entity-schema-basic.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import "reflect-metadata"
import { expect } from "chai"
import {
closeTestingConnections,
createTestingConnections,
Expand All @@ -19,7 +20,7 @@ describe("entity schemas > basic functionality", () => {
beforeEach(() => reloadTestingDatabases(connections))
after(() => closeTestingConnections(connections))

it("should perform basic operations with entity", () =>
it("should perform basic operations with entity using repository", () =>
Promise.all(
connections.map(async (connection) => {
const postRepository = connection.getRepository(PostEntity)
Expand All @@ -30,13 +31,47 @@ describe("entity schemas > basic functionality", () => {
})
await postRepository.save(post)

const loadedPost = await postRepository.findOneBy({
title: "First Post",
})
loadedPost!.id.should.be.equal(post.id)
loadedPost!.title.should.be.equal("First Post")
loadedPost!.text.should.be.equal("About first post")

await postRepository.remove(loadedPost!)

const loadedPostAfterRemove = await postRepository.findOneBy({
title: "First Post",
})
expect(loadedPostAfterRemove).to.be.null
}),
))

it("should perform basic operations with entity using manager", () =>
Promise.all(
connections.map(async (connection) => {
const post = connection.manager.create(PostEntity, {
id: 1,
title: "First Post",
text: "About first post",
})
await connection.manager.save(PostEntity, post)

const loadedPost = await connection.manager.findOneBy(
PostEntity,
{ title: "First Post" },
)
loadedPost!.id.should.be.equal(post.id)
loadedPost!.title.should.be.equal("First Post")
loadedPost!.text.should.be.equal("About first post")

await connection.manager.remove(PostEntity, loadedPost!)

const loadedPostAfterRemove =
await connection.manager.findOneBy(PostEntity, {
title: "First Post",
})
expect(loadedPostAfterRemove).to.be.null
}),
))
})
1 change: 1 addition & 0 deletions test/functional/entity-schema/basic/entity/PostEntity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ export const PostEntity = new EntitySchema<Post>({
categories: {
type: "many-to-many",
target: "category", // CategoryEntity
joinTable: true,
},
},
})

0 comments on commit f045536

Please sign in to comment.