v1.18.x
Barış Soner Uşaklı 6 years ago
parent 591cd0925b
commit 3917022a48

@ -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;

@ -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) {

@ -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 () {

Loading…
Cancel
Save