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: preserve id on update #533

Merged
merged 1 commit into from
Jul 26, 2019
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 6 additions & 5 deletions lib/mongodb.js
Original file line number Diff line number Diff line change
Expand Up @@ -1608,23 +1608,24 @@ MongoDB.prototype.update = MongoDB.prototype.updateAll = function updateAll(

where = self.buildWhere(modelName, where, options);

delete data[idName];
Copy link
Contributor

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.

Copy link
Contributor Author

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.

Copy link
Contributor

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.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@bajtos @b-admike @jannyHou thoughts?

Deleting the id is ideal and works perfectly, unless data is referenced in a controller. In the controller data exists as an instance of the Model, but it is the same object.

So, I guess we should go ahead with deep copy of data without the id.

Copy link
Contributor

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.

data = self.toDatabase(modelName, data);
let updateData = Object.assign({}, data);
delete updateData[idName];
updateData = self.toDatabase(modelName, updateData);

// Check for other operators and sanitize the data obj
data = self.parseUpdateData(modelName, data, options);
updateData = self.parseUpdateData(modelName, updateData, options);

this.execute(
modelName,
'updateMany',
where,
data,
updateData,
{upsert: false},
function(err, info) {
if (err) return cb && cb(err);

if (self.debug)
debug('updateAll.callback', modelName, where, data, err, info);
debug('updateAll.callback', modelName, where, updateData, err, info);

const affectedCount = info.result ? info.result.n : undefined;

Expand Down
21 changes: 21 additions & 0 deletions test/mongodb.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -1032,6 +1032,27 @@ describe('mongodb connector', function() {
});

describe('updateAll', function() {
it('should not mutate the input data object', async function() {
const user = await User.create({name: 'Al', age: 31, email: 'al@strongloop'});
const userId = user.id;
const userData = user.toObject();
userData.age = 100;

await User.update(userData);
userData.should.have.property('id', userId);
});

it('should not mutate the input model instance', async function() {
bajtos marked this conversation as resolved.
Show resolved Hide resolved
const user = await User.create({name: 'Al', age: 31, email: 'al@strongloop'});
const userId = user.id;
user.age = 100;
user.name = 'Albert';

await User.update(user);
user.should.have.property('id', userId);
user.should.have.property('name', 'Albert');
});

it('should update the instance matching criteria', function(done) {
User.create({name: 'Al', age: 31, email: 'al@strongloop'}, function(
err1,
Expand Down