Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: delete operation in MongoDB impact all matched documents (#7811)
Delete operation should remove all matched documents, but now it removes the first found document Closes: #7809
- Loading branch information
Showing
3 changed files
with
68 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,13 @@ | ||
import {Column, Entity, ObjectIdColumn, PrimaryColumn} from "../../../../src"; | ||
|
||
@Entity("test") | ||
export class TestEntity { | ||
@ObjectIdColumn() | ||
_id: string; | ||
|
||
@PrimaryColumn() | ||
id: string; | ||
|
||
@Column() | ||
name: string; | ||
} |
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 { expect } from "chai"; | ||
import { Connection } from "../../../src"; | ||
import { closeTestingConnections, createTestingConnections, reloadTestingDatabases } from "../../utils/test-utils"; | ||
import { TestEntity } from "./entity/test.entity"; | ||
|
||
describe("github issues > #7809 MongoDB delete make changes only to first matched document", () => { | ||
|
||
let connections: Connection[]; | ||
before(async () => { | ||
connections = await createTestingConnections({ | ||
enabledDrivers: ["mongodb"], | ||
entities: [TestEntity], | ||
schemaCreate: false, | ||
dropSchema: true | ||
}); | ||
}); | ||
beforeEach(() => reloadTestingDatabases(connections)); | ||
after(() => closeTestingConnections(connections)); | ||
|
||
it("should delete all documents related to search pattern", () => Promise.all(connections.map(async connection => { | ||
const testEntityRepository = connection.getRepository(TestEntity); | ||
|
||
// save few documents | ||
const firstEntity = new TestEntity(); | ||
firstEntity.id = "1"; | ||
firstEntity.name = "Test"; | ||
await testEntityRepository.save(firstEntity); | ||
|
||
const secondEntity = new TestEntity(); | ||
secondEntity.id = "2"; | ||
secondEntity.name = "Test"; | ||
await testEntityRepository.save(secondEntity); | ||
|
||
const thirdEntity = new TestEntity(); | ||
thirdEntity.id = "3"; | ||
thirdEntity.name = "Original"; | ||
await testEntityRepository.save(thirdEntity); | ||
|
||
const fourthEntity = new TestEntity(); | ||
fourthEntity.id = "4"; | ||
fourthEntity.name = "Test"; | ||
await testEntityRepository.save(fourthEntity); | ||
|
||
await testEntityRepository.delete({ name: "Test" }); | ||
|
||
const loadedEntities = await testEntityRepository.find(); | ||
|
||
expect(loadedEntities.length).to.be.eql(1); | ||
expect(loadedEntities[0]).to.be.instanceOf(TestEntity); | ||
expect(loadedEntities[0]!.id).to.be.eql(thirdEntity.id); | ||
expect(loadedEntities[0]!.name).to.be.equal("Original"); | ||
}))); | ||
}); |