Skip to content

Node.js based forum software built for the modern web

refactor: remove dupe code

Barış Soner Uşaklı5286f20862d3
diff --git a/src/database/mongo/list.js b/src/database/mongo/list.js
index dbebc4f971..740d9ffd5e 100644
--- a/src/database/mongo/list.js
+++ b/src/database/mongo/list.js
@@ -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);
}
@@ -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) {