isekai-main
Barış Soner Uşaklı 3 years ago committed by GitHub
parent 856ba78a5f
commit 0ce4b87d85
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -14,6 +14,9 @@ module.exports = function (module) {
} }
const writeData = helpers.serializeData(data); const writeData = helpers.serializeData(data);
if (!Object.keys(writeData).length) {
return;
}
try { try {
if (isArray) { if (isArray) {
const bulk = module.client.collection('objects').initializeUnorderedBulkOp(); const bulk = module.client.collection('objects').initializeUnorderedBulkOp();
@ -39,9 +42,18 @@ module.exports = function (module) {
const writeData = data.map(helpers.serializeData); const writeData = data.map(helpers.serializeData);
try { try {
const bulk = module.client.collection('objects').initializeUnorderedBulkOp(); let bulk;
keys.forEach((key, i) => bulk.find({ _key: key }).upsert().updateOne({ $set: writeData[i] })); keys.forEach((key, i) => {
await bulk.execute(); if (Object.keys(writeData[i]).length) {
if (!bulk) {
bulk = module.client.collection('objects').initializeUnorderedBulkOp();
}
bulk.find({ _key: key }).upsert().updateOne({ $set: writeData[i] });
}
});
if (bulk) {
await bulk.execute();
}
} catch (err) { } catch (err) {
if (err && err.message.startsWith('E11000 duplicate key error')) { if (err && err.message.startsWith('E11000 duplicate key error')) {
return await module.setObjectBulk(keys, data); return await module.setObjectBulk(keys, data);

@ -11,7 +11,9 @@ module.exports = function (module) {
if (data.hasOwnProperty('')) { if (data.hasOwnProperty('')) {
delete data['']; delete data[''];
} }
if (!Object.keys(data).length) {
return;
}
await module.transaction(async (client) => { await module.transaction(async (client) => {
const dataString = JSON.stringify(data); const dataString = JSON.stringify(data);
async function setOne(key) { async function setOne(key) {

@ -41,7 +41,11 @@ module.exports = function (module) {
return; return;
} }
const batch = module.client.batch(); const batch = module.client.batch();
keys.forEach((k, i) => batch.hmset(k, data[i])); keys.forEach((k, i) => {
if (Object.keys(data[i]).length) {
batch.hmset(k, data[i]);
}
});
await helpers.execBatch(batch); await helpers.execBatch(batch);
cache.del(keys); cache.del(keys);
}; };

@ -72,7 +72,7 @@ describe('Hash methods', () => {
}); });
}); });
it('should set multiple keys to different okjects', async () => { it('should set multiple keys to different objects', async () => {
const keys = ['bulkKey1', 'bulkKey2']; const keys = ['bulkKey1', 'bulkKey2'];
const data = [{ foo: '1' }, { baz: 'baz' }]; const data = [{ foo: '1' }, { baz: 'baz' }];
@ -80,6 +80,39 @@ describe('Hash methods', () => {
const result = await db.getObjects(keys); const result = await db.getObjects(keys);
assert.deepStrictEqual(result, data); assert.deepStrictEqual(result, data);
}); });
it('should not error if object is empty', async () => {
const keys = ['bulkKey3', 'bulkKey4'];
const data = [{ foo: '1' }, { }];
await db.setObjectBulk(keys, data);
const result = await db.getObjects(keys);
assert.deepStrictEqual(result, [{ foo: '1' }, null]);
});
it('should not error if object is empty', async () => {
const keys = ['bulkKey5'];
const data = [{ }];
await db.setObjectBulk(keys, data);
const result = await db.getObjects(keys);
assert.deepStrictEqual(result, [null]);
});
it('should not error if object is empty', async () => {
const keys = ['bulkKey6', 'bulkKey7'];
const data = {};
await db.setObject(keys, data);
const result = await db.getObjects(keys);
assert.deepStrictEqual(result, [null, null]);
});
it('should not error if object is empty', async () => {
await db.setObject('emptykey', {});
const result = await db.getObject('emptykey');
assert.deepStrictEqual(result, null);
});
}); });
describe('setObjectField()', () => { describe('setObjectField()', () => {

Loading…
Cancel
Save