add reverse support to db.processSortedSet (#11826)

This commit is contained in:
Barış Soner Uşaklı
2023-07-20 22:26:55 -04:00
committed by GitHub
parent 8ab9c72c6b
commit 9b1cc57604
4 changed files with 8 additions and 5 deletions

View File

@@ -563,12 +563,12 @@ module.exports = function (module) {
let done = false;
const ids = [];
const project = { _id: 0, _key: 0 };
const sort = options.reverse ? -1 : 1;
if (!options.withScores) {
project.score = 0;
}
const cursor = await module.client.collection('objects').find({ _key: setKey }, { projection: project })
.sort({ score: 1 })
.sort({ score: sort })
.batchSize(options.batch);
if (processFn && processFn.constructor && processFn.constructor.name !== 'AsyncFunction') {

View File

@@ -664,6 +664,7 @@ SELECT z."value",
module.processSortedSet = async function (setKey, process, options) {
const client = await module.pool.connect();
const batchSize = (options || {}).batch || 100;
const sort = options.reverse ? 'DESC' : 'ASC';
const cursor = client.query(new Cursor(`
SELECT z."value", z."score"
FROM "legacy_object_live" o
@@ -671,7 +672,7 @@ SELECT z."value", z."score"
ON o."_key" = z."_key"
AND o."type" = z."type"
WHERE o."_key" = $1::TEXT
ORDER BY z."score" ASC, z."value" ASC`, [setKey]));
ORDER BY z."score" ${sort}, z."value" ${sort}`, [setKey]));
if (process && process.constructor && process.constructor.name !== 'AsyncFunction') {
process = util.promisify(process);