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

patches don't depopulate objects #187

Closed
ReadWriteError opened this issue Feb 28, 2024 · 0 comments · Fixed by #199
Closed

patches don't depopulate objects #187

ReadWriteError opened this issue Feb 28, 2024 · 0 comments · Fixed by #199

Comments

@ReadWriteError
Copy link
Contributor

Describe the bug
When performing a query based update like model.updateOne() mongoose will depopulate object referenced by ID but the patch will be generated from the update as is.

To Reproduce
The following code will produce the undesirable behavior.

mongoose.plugin(patchHistoryPlugin, {
    eventCreated: "created",
    eventUpdated: "updated",
    eventDeleted: "deleted"
})

const Test1 = mongoose.model('test1', {
  text: {type: String}
})
const Test2 = mongoose.model('test2', {
  text: {type: String},
  other: {type: Schema.Types.ObjectId, ref: 'test1', required: false}
})

const test1 = new Test1({text: 'test1'})
const test2 = new Test2({text: 'test2'})
await test1.save()
await test2.save()

await Test2.updateOne({ _id: test2._id }, {
  other: {_id: test1._id, text: 'test1'}
}).exec()

The history patch that will be generated is this.

[
  {
    op: 'add',
    path: '/other',
    value: { _id: '65df6830149fb9e15254264a', text: 'test1' }
  }
]

Expected behavior
The patch should resemble what happens in the mongodb server and the patch should look like this.

[ { op: 'add', path: '/other', value: '65df679a925014b9758aaa1a' } ]
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 a pull request may close this issue.

1 participant