-
Notifications
You must be signed in to change notification settings - Fork 235
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: preserve id on update #533
Conversation
I wonder why Do we need to add tests for this? |
@@ -1606,8 +1606,6 @@ MongoDB.prototype.update = MongoDB.prototype.updateAll = function updateAll( | |||
const idName = this.idName(modelName); | |||
|
|||
where = self.buildWhere(modelName, where, options); | |||
|
|||
delete data[idName]; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think we should clone data
, excluding id
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why clone? data
could be deeply nested, and similar operations are performed in other places. It will introduce a new level of complexity and add to the scope.
data
is passed around multiple layers and maybe be referenced from a controller, for example in the reported issue.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For update to an existing document in mongodb, we delete the id on purpose IIRC. The current code changes the original input as complained by loopbackio/loopback-next#3267. I think the fix should leave data
intact, but remove id
from the object to be written to the database.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If we only have to remove id
, shadow copy is probably good enough.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please add a test to verify the changes and prevent regressions in the future.
Removing id (PK) from data
makes sense to me. I agree with @raymondfeng that a shallow clone is enough, e.g.:
data = Object.assign({}, data);
delete data[idName];
cac1f57
to
e60fc5b
Compare
test/mongodb.test.js
Outdated
it('should preserve the id', async function() { | ||
let user = await User.create({name: 'Al', age: 31, email: 'al@strongloop'}); | ||
const userId = user.id; | ||
user = user.toObject(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Let's call this property userData
to make it clear that it's a plain-data object, not a model instance. (And also to avoid a mutable variable user
.)
user = user.toObject(); | |
userData = user.toObject(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I am afraid my comment was not resolved :(
Let's call this property
userData
to make it clear that it's a plain-data object, not a model instance. (And also to avoid a mutable variableuser
.)
user = user.toObject(); | |
const userData = user.toObject(); |
e60fc5b
to
4683616
Compare
4683616
to
422d636
Compare
@bajtos @raymondfeng looks good now? |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The patch overall LGTM 👍 ; I have one question for the tests.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks like you haven't addressed one of my comments, PTAL. The rest of the pull request looks good.
test/mongodb.test.js
Outdated
it('should preserve the id', async function() { | ||
let user = await User.create({name: 'Al', age: 31, email: 'al@strongloop'}); | ||
const userId = user.id; | ||
user = user.toObject(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I am afraid my comment was not resolved :(
Let's call this property
userData
to make it clear that it's a plain-data object, not a model instance. (And also to avoid a mutable variableuser
.)
user = user.toObject(); | |
const userData = user.toObject(); |
422d636
to
51743c0
Compare
@bajtos errr thanks for pointing out. Updated. |
Don't delete the id on update
51743c0
to
c5200e2
Compare
Leave the id property alone on update.
Description
Related issues
Checklist
guide