fix: remote empty zsets when all elements have been removed #yolo

This commit is contained in:
Julian Lam
2022-02-07 15:34:20 -05:00
parent 8908c7a69a
commit 0ac73244bb

View File

@@ -3,6 +3,21 @@
module.exports = function (module) {
const helpers = require('../helpers');
const _deleteOnEmpty = async (keys) => {
// In Redis and Mongo, empty sorted sets are automatically considered non-existant, this matches that behaviour
if (typeof keys === 'string') {
keys = [keys];
} else if (!Array.isArray(keys)) {
throw new Error('[[error:invalid-data]]');
}
await Promise.all(keys.map(async (key) => {
if (!await module.sortedSetCard(key)) {
await module.delete(key);
}
}));
};
module.sortedSetRemove = async function (key, value) {
if (!key) {
return;
@@ -28,6 +43,7 @@ DELETE FROM "legacy_zset"
AND "value" = ANY($2::TEXT[])`,
values: [key, value],
});
await _deleteOnEmpty(key);
};
module.sortedSetsRemove = async function (keys, value) {
@@ -45,6 +61,7 @@ DELETE FROM "legacy_zset"
AND "value" = $2::TEXT`,
values: [keys, value],
});
await _deleteOnEmpty(keys);
};
module.sortedSetsRemoveRangeByScore = async function (keys, min, max) {
@@ -68,6 +85,7 @@ DELETE FROM "legacy_zset"
AND ("score" <= $3::NUMERIC OR $3::NUMERIC IS NULL)`,
values: [keys, min, max],
});
await _deleteOnEmpty(keys);
};
module.sortedSetRemoveBulk = async function (data) {
@@ -87,5 +105,6 @@ DELETE FROM "legacy_zset"
)`,
values: [keys, values],
});
await _deleteOnEmpty(keys);
};
};