forked from typeorm/typeorm
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: update operation in MongoDB impact all matched documents
Update operation should update all matched documents, but now it updates the first found document Closes: typeorm#7788
- Loading branch information
Showing
4 changed files
with
80 additions
and
2 deletions.
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,65 @@ | ||
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 > #7788 MongoDB update 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 update all documents related to search pattern", () => Promise.all(connections.map(async connection => { | ||
const testEntityRepository = connection.getMongoRepository(TestEntity); | ||
|
||
// save few posts | ||
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.update({ name: "Test" }, { name: "Updated" }); | ||
|
||
const loadedEntities = await testEntityRepository.find(); | ||
|
||
expect(loadedEntities[0]).to.be.instanceOf(TestEntity); | ||
expect(loadedEntities[0]!.id).to.be.eql(firstEntity.id); | ||
expect(loadedEntities[0]!.name).to.be.equal("Updated"); | ||
|
||
expect(loadedEntities[1]).to.be.instanceOf(TestEntity); | ||
expect(loadedEntities[1]!.id).to.be.eql(secondEntity.id); | ||
expect(loadedEntities[1]!.name).to.be.equal("Updated"); | ||
|
||
expect(loadedEntities[2]).to.be.instanceOf(TestEntity); | ||
expect(loadedEntities[2]!.id).to.be.eql(thirdEntity.id); | ||
expect(loadedEntities[2]!.name).to.be.equal("Original"); | ||
|
||
expect(loadedEntities[3]).to.be.instanceOf(TestEntity); | ||
expect(loadedEntities[3]!.id).to.be.eql(fourthEntity.id); | ||
expect(loadedEntities[3]!.name).to.be.equal("Updated"); | ||
}))); | ||
}); |
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