Skip to content

Conversation

@vinicius-lino-figueiredo
Copy link

@vinicius-lino-figueiredo vinicius-lino-figueiredo commented Aug 7, 2025

Make model.compareThings actually work for objects; model.compareThings would
only check field values and object length, ignoring object keys. Now it works
for those specific cases where it would not before.

In this code:

const db = new Datastore()
db.ensureIndex({ fieldName: "uniqueField", unique: true }, function(err) {
  db.insert({ uniqueField: { one: 'a' } }, function() {
    db.insert({ uniqueField: { another: 'a' } }, function(err) {
    })
  })
})

the second insert would return an error, when comparing {one:1} and {another:1},
the old function would:

  1. List keys from both fields (['one'], ['another']);
  2. Sort them (no change);
  3. Compare the respective values ('a' and 'a')
  4. Since they are all equal, compare lengths (1 and 1) and return result (legths
    are equal too).

Now, if length comparison does not return a difference, we compare the key list.

Make model.compareThings actually work for objects. Before, any docs
with same values (not keys) and length would count as equal
(like {a: 1} and {b: 1}) and that would make index throw an unexpected
constraint violation error for some specific cases.
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.

1 participant