Skip to content

Node.js based forum software built for the modern web

feat: add failing test for list append/prepend with list (#9303)

Barış Soner Uşaklı8f0386d9ac01
diff --git a/src/database/mongo/list.js b/src/database/mongo/list.js
index 1c4fb63140..dbebc4f971 100644
--- a/src/database/mongo/list.js
+++ b/src/database/mongo/list.js
@@ -7,12 +7,23 @@ module.exports = function (module) {
if (!key) {
return;
}
-
- value = helpers.valueToString(value);
-
+ 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 module.client.collection('objects').updateOne({
+ _key: key,
+ }, {
+ $push: {
+ array: {
+ $each: value,
+ $position: 0,
+ },
+ },
+ }, {
+ upsert: true,
+ });
} else {
await module.listAppend(key, value);
}
@@ -22,8 +33,19 @@ module.exports = function (module) {
if (!key) {
return;
}
- value = helpers.valueToString(value);
- await module.client.collection('objects').updateOne({ _key: key }, { $push: { array: value } }, { upsert: true });
+ value = Array.isArray(value) ? value : [value];
+ value = value.map(helpers.valueToString);
+ await module.client.collection('objects').updateOne({
+ _key: key,
+ }, {
+ $push: {
+ array: {
+ $each: value,
+ },
+ },
+ }, {
+ upsert: true,
+ });
};
module.listRemoveLast = async function (key) {
diff --git a/src/database/postgres/list.js b/src/database/postgres/list.js
index c2b91a85ac..1f771cb719 100644
--- a/src/database/postgres/list.js
+++ b/src/database/postgres/list.js
@@ -9,16 +9,28 @@ module.exports = function (module) {
}
await module.transaction(async (client) => {
+ async function doPrepend(value) {
+ await client.query({
+ name: 'listPrepend',
+ text: `
+ INSERT INTO "legacy_list" ("_key", "array")
+ VALUES ($1::TEXT, ARRAY[$2::TEXT])
+ ON CONFLICT ("_key")
+ DO UPDATE SET "array" = ARRAY[$2::TEXT] || "legacy_list"."array"`,
+ values: [key, value],
+ });
+ }
+
await helpers.ensureLegacyObjectType(client, key, 'list');
- await client.query({
- name: 'listPrepend',
- text: `
-INSERT INTO "legacy_list" ("_key", "array")
-VALUES ($1::TEXT, ARRAY[$2::TEXT])
-ON CONFLICT ("_key")
-DO UPDATE SET "array" = ARRAY[$2::TEXT] || "legacy_list"."array"`,
- values: [key, value],
- });
+ if (Array.isArray(value)) {
+ // TODO: perf make single query
+ for (const v of value) {
+ // eslint-disable-next-line
+ await doPrepend(v);
+ }
+ return;
+ }
+ await doPrepend(value);
});
};
@@ -26,18 +38,28 @@ DO UPDATE SET "array" = ARRAY[$2::TEXT] || "legacy_list"."array"`,
if (!key) {
return;
}
-
await module.transaction(async (client) => {
+ async function doAppend(value) {
+ await client.query({
+ name: 'listAppend',
+ text: `
+ INSERT INTO "legacy_list" ("_key", "array")
+ VALUES ($1::TEXT, ARRAY[$2::TEXT])
+ ON CONFLICT ("_key")
+ DO UPDATE SET "array" = "legacy_list"."array" || ARRAY[$2::TEXT]`,
+ values: [key, value],
+ });
+ }
await helpers.ensureLegacyObjectType(client, key, 'list');
- await client.query({
- name: 'listAppend',
- text: `
-INSERT INTO "legacy_list" ("_key", "array")
-VALUES ($1::TEXT, ARRAY[$2::TEXT])
-ON CONFLICT ("_key")
-DO UPDATE SET "array" = "legacy_list"."array" || ARRAY[$2::TEXT]`,
- values: [key, value],
- });
+ if (Array.isArray(value)) {
+ // TODO: perf make single query
+ for (const v of value) {
+ // eslint-disable-next-line
+ await doAppend(v);
+ }
+ return;
+ }
+ await doAppend(value);
});
};
diff --git a/test/database/list.js b/test/database/list.js
index ae2c8744ad..2d4c3ac454 100644
--- a/test/database/list.js
+++ b/test/database/list.js
@@ -21,6 +21,16 @@ describe('List methods', () => {
done();
});
});
+
+ it('should append each element to list', async () => {
+ await db.listAppend('arrayListAppend', ['a', 'b', 'c']);
+ let values = await db.getListRange('arrayListAppend', 0, -1);
+ assert.deepStrictEqual(values, ['a', 'b', 'c']);
+
+ await db.listAppend('arrayListAppend', ['d', 'e']);
+ values = await db.getListRange('arrayListAppend', 0, -1);
+ assert.deepStrictEqual(values, ['a', 'b', 'c', 'd', 'e']);
+ });
});
describe('listPrepend()', () => {
@@ -52,6 +62,16 @@ describe('List methods', () => {
done();
});
});
+
+ it('should prepend each element to list', async () => {
+ await db.listPrepend('arrayListPrepend', ['a', 'b', 'c']);
+ let values = await db.getListRange('arrayListPrepend', 0, -1);
+ assert.deepStrictEqual(values, ['c', 'b', 'a']);
+
+ await db.listPrepend('arrayListPrepend', ['d', 'e']);
+ values = await db.getListRange('arrayListPrepend', 0, -1);
+ assert.deepStrictEqual(values, ['e', 'd', 'c', 'b', 'a']);
+ });
});
describe('getListRange()', () => {