Skip to content

Commit

Permalink
refactor: remove dupe code
Browse files Browse the repository at this point in the history
  • Loading branch information
barisusakli committed Feb 17, 2021
1 parent 4c07808 commit 5286f20
Showing 1 changed file with 9 additions and 16 deletions.
25 changes: 9 additions & 16 deletions src/database/mongo/list.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,22 +8,10 @@ module.exports = function (module) {
return;
}
value = Array.isArray(value) ? value : [value];
value = value.map(helpers.valueToString);
value.reverse();
const exists = await module.isObjectField(key, 'array');
if (exists) {
await module.client.collection('objects').updateOne({
_key: key,
}, {
$push: {
array: {
$each: value,
$position: 0,
},
},
}, {
upsert: true,
});
await listPush(key, value, { $position: 0 });
} else {
await module.listAppend(key, value);
}
Expand All @@ -34,19 +22,24 @@ module.exports = function (module) {
return;
}
value = Array.isArray(value) ? value : [value];
value = value.map(helpers.valueToString);
await listPush(key, value);
};

async function listPush(key, values, position) {
values = values.map(helpers.valueToString);
await module.client.collection('objects').updateOne({
_key: key,
}, {
$push: {
array: {
$each: value,
$each: values,
...(position || {}),
},
},
}, {
upsert: true,
});
};
}

module.listRemoveLast = async function (key) {
if (!key) {
Expand Down

0 comments on commit 5286f20

Please sign in to comment.