Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: mongodb repository.find filters soft deleted rows (#8581)
Closes: #7113
- Loading branch information
1 parent
600bd4e
commit f7c1f7d
Showing
4 changed files
with
163 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,10 @@ | ||
import { DeleteDateColumn, Entity, ObjectID, ObjectIdColumn } from "../../../../src"; | ||
|
||
@Entity() | ||
export class Configuration { | ||
@ObjectIdColumn() | ||
_id: ObjectID; | ||
|
||
@DeleteDateColumn() | ||
deletedAt?: Date; | ||
} |
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,104 @@ | ||
import "reflect-metadata"; | ||
import { createTestingConnections, closeTestingConnections, reloadTestingDatabases } from "../../utils/test-utils"; | ||
import { Connection } from "../../../src/connection/Connection"; | ||
import { expect } from "chai"; | ||
import { Configuration } from "./entity/Configuration"; | ||
import { ConfigurationRepository } from "./repository/ConfigurationRepository"; | ||
|
||
describe("github issues > #7113 Soft deleted docs still being pulled in Mongodb", () => { | ||
|
||
let connections: Connection[]; | ||
before(async () => connections = await createTestingConnections({ | ||
entities: [__dirname + "/entity/*{.js,.ts}"], | ||
schemaCreate: true, | ||
dropSchema: true, | ||
enabledDrivers: ["mongodb"], | ||
})); | ||
beforeEach(() => reloadTestingDatabases(connections)); | ||
after(() => closeTestingConnections(connections)); | ||
|
||
it("should not pull soft deleted docs with find", () => Promise.all(connections.map(async connection => { | ||
|
||
const repository = connection.getCustomRepository(ConfigurationRepository); | ||
const configuration = new Configuration(); | ||
|
||
await repository.save(configuration); | ||
|
||
await repository.deleteConfiguration(configuration); | ||
|
||
const withoutDeleted = await repository.findAllConfigurations(); | ||
expect(withoutDeleted.length).to.be.eq(0); | ||
|
||
const withDeleted = await repository.find({ withDeleted: true }); | ||
expect(withDeleted.length).to.be.eq(1); | ||
|
||
const withOtherOption = await repository.find({order: { _id: "ASC" }}); | ||
expect(withOtherOption.length).to.be.eq(0); | ||
|
||
}))); | ||
|
||
it("should not pull soft deleted docs with findAndCount", () => Promise.all(connections.map(async connection => { | ||
|
||
const repository = connection.getCustomRepository(ConfigurationRepository); | ||
const configuration = new Configuration(); | ||
|
||
await repository.save(configuration); | ||
|
||
await repository.softRemove(configuration); | ||
|
||
const withoutDeletedAndCount = await repository.findAndCount(); | ||
expect(withoutDeletedAndCount[0].length).to.be.eq(0); | ||
|
||
const withDeletedAndCount = await repository.findAndCount({ withDeleted: true }); | ||
expect(withDeletedAndCount[0].length).to.be.eq(1); | ||
|
||
const withOtherOptionAndCount = await repository.findAndCount({order: { _id: "ASC" }}); | ||
expect(withOtherOptionAndCount[0].length).to.be.eq(0); | ||
|
||
}))); | ||
|
||
it("should not pull soft deleted docs with findByIds", () => Promise.all(connections.map(async connection => { | ||
|
||
const repository = connection.getCustomRepository(ConfigurationRepository); | ||
const configuration = new Configuration(); | ||
|
||
await repository.save(configuration); | ||
|
||
await repository.softRemove(configuration); | ||
|
||
const withoutDeletedById = await repository.findByIds([configuration._id]); | ||
expect(withoutDeletedById.length).to.be.eq(0); | ||
|
||
const withDeletedById = await repository.findByIds([configuration._id], | ||
{ withDeleted: true }); | ||
expect(withDeletedById.length).to.be.eq(1); | ||
|
||
const withOtherOptionById = await repository.findByIds([configuration._id], | ||
{ cache: true }); | ||
expect(withOtherOptionById.length).to.be.eq(0); | ||
|
||
}))); | ||
|
||
it("should not pull soft deleted docs with findOne", () => Promise.all(connections.map(async connection => { | ||
|
||
const repository = connection.getCustomRepository(ConfigurationRepository); | ||
const configuration = new Configuration(); | ||
|
||
await repository.save(configuration); | ||
|
||
await repository.softRemove(configuration); | ||
|
||
const withoutDeletedOne = await repository.findOne(configuration._id); | ||
expect(withoutDeletedOne).to.be.undefined; | ||
|
||
const withDeletedOne = await repository.findOne(configuration._id, | ||
{ withDeleted: true }); | ||
expect(withDeletedOne).not.to.be.undefined; | ||
|
||
const withOtherOptionOne = await repository.findOne(configuration._id, | ||
{ cache: true }); | ||
expect(withOtherOptionOne).to.be.undefined; | ||
|
||
}))); | ||
|
||
}); |
18 changes: 18 additions & 0 deletions
18
test/github-issues/7113/repository/ConfigurationRepository.ts
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,18 @@ | ||
import { EntityRepository, MongoRepository, ObjectID } from "../../../../src"; | ||
import { Configuration } from "../entity/Configuration"; | ||
|
||
@EntityRepository(Configuration) | ||
export class ConfigurationRepository extends MongoRepository<Configuration> { | ||
|
||
async findAllConfigurations(): Promise<Configuration[]> { | ||
const configurations = await this.find(); | ||
return configurations; | ||
} | ||
|
||
async deleteConfiguration( | ||
configuration: Configuration | ||
): Promise<ObjectID> { | ||
await this.softRemove(configuration); | ||
return configuration._id; | ||
} | ||
} |