From 3917022a481523895ff695345a18b17ae1673ab3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bar=C4=B1=C5=9F=20Soner=20U=C5=9Fakl=C4=B1?= Date: Wed, 19 Dec 2018 11:09:19 -0500 Subject: [PATCH] fix: #7139 --- src/database/mongo/hash.js | 10 ++++------ src/database/mongo/helpers.js | 23 +++++++++++++++++++++-- test/database/hash.js | 22 ++++++++++++++++++++++ 3 files changed, 47 insertions(+), 8 deletions(-) diff --git a/src/database/mongo/hash.js b/src/database/mongo/hash.js index 0f1437e010..54a45edf60 100644 --- a/src/database/mongo/hash.js +++ b/src/database/mongo/hash.js @@ -16,10 +16,9 @@ module.exports = function (db, module) { if (!key || !data) { return callback(); } - if (data.hasOwnProperty('')) { - delete data['']; - } - db.collection('objects').updateOne({ _key: key }, { $set: data }, { upsert: true, w: 1 }, function (err) { + + const writeData = helpers.serializeData(data); + db.collection('objects').updateOne({ _key: key }, { $set: writeData }, { upsert: true, w: 1 }, function (err) { if (err) { return callback(err); } @@ -34,7 +33,6 @@ module.exports = function (db, module) { return callback(); } var data = {}; - field = helpers.fieldToString(field); data[field] = value; module.setObject(key, data, callback); }; @@ -76,7 +74,7 @@ module.exports = function (db, module) { if (err) { return callback(err); } - + data = data.map(helpers.deserializeData); var map = helpers.toMap(data); unCachedKeys.forEach(function (key) { cachedData[key] = map[key] || null; diff --git a/src/database/mongo/helpers.js b/src/database/mongo/helpers.js index 29c83ea3b9..a02c658a3d 100644 --- a/src/database/mongo/helpers.js +++ b/src/database/mongo/helpers.js @@ -22,8 +22,27 @@ helpers.fieldToString = function (field) { field = field.toString(); } // if there is a '.' in the field name it inserts subdocument in mongo, replace '.'s with \uff0E - field = field.replace(/\./g, '\uff0E'); - return field; + return field.replace(/\./g, '\uff0E'); +}; + +helpers.serializeData = function (data) { + const serialized = {}; + for (const field in data) { + if (data.hasOwnProperty(field) && field !== '') { + serialized[helpers.fieldToString(field)] = data[field]; + } + } + return serialized; +}; + +helpers.deserializeData = function (data) { + const deserialized = {}; + for (const field in data) { + if (data.hasOwnProperty(field)) { + deserialized[field.replace(/\uff0E/g, '.')] = data[field]; + } + } + return deserialized; }; helpers.valueToString = function (value) { diff --git a/test/database/hash.js b/test/database/hash.js index 925c45e02b..392379e66b 100644 --- a/test/database/hash.js +++ b/test/database/hash.js @@ -52,6 +52,17 @@ describe('Hash methods', function () { }); }); }); + + it('should work for field names with "." in them', function (done) { + db.setObject('dotObject', { 'my.dot.field': 'foo' }, function (err) { + assert.ifError(err); + db.getObject('dotObject', function (err, data) { + assert.ifError(err); + assert.equal(data['my.dot.field'], 'foo'); + done(); + }); + }); + }); }); describe('setObjectField()', function () { @@ -70,6 +81,17 @@ describe('Hash methods', function () { done(); }); }); + + it('should work for field names with "." in them', function (done) { + db.setObjectField('dotObject2', 'my.dot.field', 'foo2', function (err) { + assert.ifError(err); + db.getObjectField('dotObject2', 'my.dot.field', function (err, value) { + assert.ifError(err); + assert.equal(value, 'foo2'); + done(); + }); + }); + }); }); describe('getObject()', function () {