Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: find query mongodb properly with @DeleteDateColumn() #9262

Merged
merged 5 commits into from
Aug 24, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
26 changes: 17 additions & 9 deletions src/entity-manager/MongoEntityManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -180,10 +180,10 @@ export class MongoEntityManager extends EntityManager {
),
)
if (deleteDateColumn && !optionsOrConditions.withDeleted) {
this.filterSoftDeleted(cursor, deleteDateColumn)
this.filterSoftDeleted(cursor, deleteDateColumn, query)
}
} else if (deleteDateColumn) {
this.filterSoftDeleted(cursor, deleteDateColumn)
this.filterSoftDeleted(cursor, deleteDateColumn, query)
}
return await cursor.toArray()
}
Expand Down Expand Up @@ -1168,8 +1168,16 @@ export class MongoEntityManager extends EntityManager {
protected filterSoftDeleted<Entity>(
cursor: Cursor<Entity>,
deleteDateColumn: ColumnMetadata,
query?: ObjectLiteral,
) {
cursor.filter({ $where: `this.${deleteDateColumn.propertyName}==null` })
const { $or, ...restQuery } = query ?? {}
cursor.filter({
$or: [
{ [deleteDateColumn.propertyName]: { $eq: null } },
...(Array.isArray($or) ? $or : []),
],
...restQuery,
})
}

/**
Expand Down Expand Up @@ -1214,10 +1222,10 @@ export class MongoEntityManager extends EntityManager {
),
)
if (deleteDateColumn && !findOneOptionsOrConditions.withDeleted) {
this.filterSoftDeleted(cursor, deleteDateColumn)
this.filterSoftDeleted(cursor, deleteDateColumn, query)
}
} else if (deleteDateColumn) {
this.filterSoftDeleted(cursor, deleteDateColumn)
this.filterSoftDeleted(cursor, deleteDateColumn, query)
}

// const result = await cursor.limit(1).next();
Expand Down Expand Up @@ -1256,10 +1264,10 @@ export class MongoEntityManager extends EntityManager {
),
)
if (deleteDateColumn && !optionsOrConditions.withDeleted) {
this.filterSoftDeleted(cursor, deleteDateColumn)
this.filterSoftDeleted(cursor, deleteDateColumn, query)
}
} else if (deleteDateColumn) {
this.filterSoftDeleted(cursor, deleteDateColumn)
this.filterSoftDeleted(cursor, deleteDateColumn, query)
}
return cursor.toArray()
}
Expand Down Expand Up @@ -1295,10 +1303,10 @@ export class MongoEntityManager extends EntityManager {
),
)
if (deleteDateColumn && !optionsOrConditions.withDeleted) {
this.filterSoftDeleted(cursor, deleteDateColumn)
this.filterSoftDeleted(cursor, deleteDateColumn, query)
}
} else if (deleteDateColumn) {
this.filterSoftDeleted(cursor, deleteDateColumn)
this.filterSoftDeleted(cursor, deleteDateColumn, query)
}
const [results, count] = await Promise.all<any>([
cursor.toArray(),
Expand Down
16 changes: 16 additions & 0 deletions test/functional/mongodb/basic/mongo-repository/entity/Post.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { Entity } from "../../../../../../src/decorator/entity/Entity"
import { Column } from "../../../../../../src/decorator/columns/Column"
import { ObjectIdColumn } from "../../../../../../src/decorator/columns/ObjectIdColumn"
import { ObjectID } from "../../../../../../src/driver/mongodb/typings"
import { DeleteDateColumn } from "../../../../../../src"

@Entity()
export class Post {
Expand All @@ -17,3 +18,18 @@ export class Post {
// @Column(() => Counters)
// counters: Counters;
}

@Entity()
export class PostWithDeleted {
@ObjectIdColumn()
id: ObjectID

@Column()
title: string

@Column()
text: string

@DeleteDateColumn()
deletedAt: Date | null
}
100 changes: 98 additions & 2 deletions test/functional/mongodb/basic/mongo-repository/mongo-repository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,15 @@ import {
createTestingConnections,
reloadTestingDatabases,
} from "../../../../utils/test-utils"
import { Post } from "./entity/Post"
import { Post, PostWithDeleted } from "./entity/Post"
import { MongoRepository } from "../../../../../src/repository/MongoRepository"

describe("mongodb > MongoRepository", () => {
let connections: DataSource[]
before(
async () =>
(connections = await createTestingConnections({
entities: [Post],
entities: [Post, PostWithDeleted],
enabledDrivers: ["mongodb"],
})),
)
Expand Down Expand Up @@ -186,4 +186,100 @@ describe("mongodb > MongoRepository", () => {
expect(loadedPosts[0]).to.not.have.property("unreal")
}),
))

// Github issue #9250
describe("with DeletedDataColumn", () => {
it("with $or query", () =>
Promise.all(
connections.map(async (connection) => {
const postRepository =
connection.getMongoRepository(PostWithDeleted)
await seedPosts(postRepository)
const loadedPosts = await postRepository.find({
where: {
$or: [{ deletedAt: { $ne: null } }],
},
})
expect(loadedPosts).to.have.length(3)
}),
))

it("filter delete data", () =>
Promise.all(
connections.map(async (connection) => {
const postRepository =
connection.getMongoRepository(PostWithDeleted)
await seedPosts(postRepository)

const loadedPosts = await postRepository.find()
const filteredPost = loadedPosts.find(
(post) => post.title === "deleted",
)

expect(filteredPost).to.be.undefined
expect(loadedPosts).to.have.length(2)
}),
))

describe("findOne filtered data properly", () => {
it("findOne()", () =>
Promise.all(
connections.map(async (connection) => {
const postRepository =
connection.getMongoRepository(PostWithDeleted)
await seedPosts(postRepository)

const loadedPost = await postRepository.findOne({
where: { title: "notDeleted" },
})
const loadedPostWithDeleted =
await postRepository.findOne({
where: { title: "deleted" },
withDeleted: true,
})

expect(loadedPost?.title).to.eql("notDeleted")
expect(loadedPostWithDeleted?.title).to.eql("deleted")
}),
))

it("findOneBy()", () =>
Promise.all(
connections.map(async (connection) => {
const postRepository =
connection.getMongoRepository(PostWithDeleted)
await seedPosts(postRepository)

const loadedPost = await postRepository.findOneBy({
where: { title: "notDeleted" },
})
const loadedPostWithDeleted =
await postRepository.findOne({
where: { title: "deleted" },
withDeleted: true,
})

expect(loadedPost?.title).to.eql("notDeleted")
expect(loadedPostWithDeleted?.title).to.eql("deleted")
}),
))
})
})
})

async function seedPosts(postRepository: MongoRepository<PostWithDeleted>) {
await postRepository.save({
title: "withoutDeleted",
text: "withoutDeleted",
})
await postRepository.save({
title: "notDeleted",
text: "notDeleted",
deletedAt: null,
})
await postRepository.save({
title: "deleted",
text: "deleted",
deletedAt: new Date(),
})
}