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

Conversation

roseline124
Copy link
Contributor

@roseline124 roseline124 commented Aug 5, 2022

Description of change

Fixes #9250

Fix filterSoftDelted of MongoEntityManager not to reset query filter.
Previous version replaced cursor query filter with where this.deletedDateColumn.propertyName == null.
So there is a bug that the cursor queries all of not deleted data if the entity has DeletedDateColumn.

Pull-Request Checklist

  • Code is up-to-date with the master branch
  • npm run format to apply prettier formatting
  • npm run test passes with this change
  • This pull request links relevant issues as Fixes #0000
  • There are new or updated unit tests validating the change
  • Documentation has been updated to reflect this change (interface was not changed)
  • The new commits follow conventions explained in CONTRIBUTING.md

@roseline124 roseline124 changed the title Fix/mongodb issue 9250 fix: find query mongodb properly with @DeleteDateColumn() Aug 5, 2022
@pleerock pleerock merged commit e49d0c8 into typeorm:master Aug 24, 2022
@pleerock
Copy link
Member

Thank you for contribution! 🎉

wirekang pushed a commit to wirekang/typeorm that referenced this pull request Aug 25, 2022
* fix: use existing query and filter deleted data

* test: add mongo-repository query test with DeleteDateColumn

* style: lint MongoEntityManager

* fix: not override $or with query.$or

* test: not override query.$or
nordinh pushed a commit to nordinh/typeorm that referenced this pull request Aug 29, 2022
* fix: use existing query and filter deleted data

* test: add mongo-repository query test with DeleteDateColumn

* style: lint MongoEntityManager

* fix: not override $or with query.$or

* test: not override query.$or
@serebro
Copy link

serebro commented Sep 2, 2023

I have found a problem with current solution.

Let say I have three records in a collection users

[
    { "username" : "user1", "deletedAt" : null) },
    { "username" : "user2", "deletedAt" : ISODate("2023-09-02T18:26:48.253+0000") },
    { "username" : "user3" }
]

and Users entity

@Entity()
export class Users {
    @ObjectIdColumn()
    id: ObjectId

    @Column()
    username: string

    @DeleteDateColumn()
    deletedAt: Date
}

I make a query

const options = {
        where: {
            $or: [
                { username: { $regex: '^user' } },
            ]
        }
}
DataSource.manager.find(Users, options)

My query transforms by the current filterSoftDeleted function to this mongodb query

{
    $or: [
        { deletedAt: { $eq: null } },
        { username: { $regex: '^user' } },
    ]
}

As a result I receive all three records instead of two rows (one record "user2" is marked as deleted).
Expected result should be the following two records

[
    { "username" : "user1", "deletedAt" : null) },
    { "username" : "user3" }
]

It can be achieved by the following code

    protected filterSoftDeleted<Entity>(
        cursor: FindCursor<Entity>,
        deleteDateColumn: ColumnMetadata,
        query?: ObjectLiteral,
    ) {
        cursor.filter({...(query || {}), [deleteDateColumn.databasePath]: { $ne: {$type: 9} }});
    }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

MongoDB - FindOne / Find doesn't work with @DeleteDateColumn()
3 participants