Skip to content
Open
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
15 changes: 7 additions & 8 deletions lib/mongo-object.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ export default class MongoObject {
const blackboxKeys = this._blackboxKeys;

this._affectedKeys = {};
this._affectedPositions = {};
this._genericAffectedKeys = {};
this._positionsByGenericKey = {};
this._positionsThatCreateGenericKey = {};
Expand Down Expand Up @@ -74,6 +75,7 @@ export default class MongoObject {
// Mark that this position affects this generic and non-generic key
if (currentPosition) {
self._affectedKeys[currentPosition] = affectedKey;
self._affectedPositions[affectedKey] = currentPosition;
self._genericAffectedKeys[currentPosition] = affectedKeyGeneric;

const positionInfo = {
Expand Down Expand Up @@ -263,6 +265,7 @@ export default class MongoObject {
}

this._affectedKeys[position] = affectedKey;
this._affectedPositions[affectedKey] = position;
} else { // Otherwise attempt to keep moving deeper into the object.
// If we're setting (as opposed to deleting) a key and we hit a place
// in the ancestor chain where the keys are not yet created, create them.
Expand All @@ -273,6 +276,9 @@ export default class MongoObject {
current[subkey] = Number.isNaN(nextPiece) ? {} : [];
createdObjectsOrArrays = true;
}
else if (isEmpty(current[subkey])) {
createdObjectsOrArrays = true;
}

// Move deeper into the object
current = current[subkey];
Expand Down Expand Up @@ -366,14 +372,7 @@ export default class MongoObject {
* Example: 'foo[bar][0]'
*/
getPositionForKey(key) {
const positions = Object.getOwnPropertyNames(this._affectedKeys);
for (let index = 0; index < positions.length; index++) {
const position = positions[index];
// We return the first one we find. While it's
// possible that multiple update operators could
// affect the same non-generic key, we'll assume that's not the case.
if (this._affectedKeys[position] === key) return position;
}
return this._affectedPositions[key];
}

/**
Expand Down
11 changes: 11 additions & 0 deletions lib/mongo-object.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -550,4 +550,15 @@ describe('MongoObject', () => {
},
}, { $set: { bar: 'foo' } }, '$set[bar]', 'bar', 'foo', '$set');
});

it('should behave correctly when an empty nested object is provided', function () {
const mDoc = new MongoObject({
flex: {
dev: {}
}
});
mDoc.setValueForPosition("flex[dev][key]", "test");

expect(mDoc.getFlatObject()).toEqual({ "flex.dev.key": "test" });
});
});