|
|
@ -363,34 +363,59 @@ module.exports = function (module) {
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
module.getSortedSetMembers = async function (key) {
|
|
|
|
module.getSortedSetMembers = async function (key) {
|
|
|
|
const data = await module.getSortedSetsMembers([key]);
|
|
|
|
const data = await getSortedSetsMembersWithScores([key], false);
|
|
|
|
|
|
|
|
return data && data[0];
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
module.getSortedSetMembersWithScores = async function (key) {
|
|
|
|
|
|
|
|
const data = await getSortedSetsMembersWithScores([key], true);
|
|
|
|
return data && data[0];
|
|
|
|
return data && data[0];
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
module.getSortedSetsMembers = async function (keys) {
|
|
|
|
module.getSortedSetsMembers = async function (keys) {
|
|
|
|
|
|
|
|
return await getSortedSetsMembersWithScores(keys, false);
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
module.getSortedSetsMembersWithScores = async function (keys) {
|
|
|
|
|
|
|
|
return await getSortedSetsMembersWithScores(keys, true);
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
async function getSortedSetsMembersWithScores(keys, withScores) {
|
|
|
|
if (!Array.isArray(keys) || !keys.length) {
|
|
|
|
if (!Array.isArray(keys) || !keys.length) {
|
|
|
|
return [];
|
|
|
|
return [];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
const arrayOfKeys = keys.length > 1;
|
|
|
|
const arrayOfKeys = keys.length > 1;
|
|
|
|
const projection = { _id: 0, value: 1 };
|
|
|
|
const projection = { _id: 0, value: 1 };
|
|
|
|
|
|
|
|
if (withScores) {
|
|
|
|
|
|
|
|
projection.score = 1;
|
|
|
|
|
|
|
|
}
|
|
|
|
if (arrayOfKeys) {
|
|
|
|
if (arrayOfKeys) {
|
|
|
|
projection._key = 1;
|
|
|
|
projection._key = 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
const data = await module.client.collection('objects').find({
|
|
|
|
const data = await module.client.collection('objects').find({
|
|
|
|
_key: arrayOfKeys ? { $in: keys } : keys[0],
|
|
|
|
_key: arrayOfKeys ? { $in: keys } : keys[0],
|
|
|
|
}, { projection: projection }).toArray();
|
|
|
|
}, { projection: projection })
|
|
|
|
|
|
|
|
.sort({ score: 1 })
|
|
|
|
|
|
|
|
.toArray();
|
|
|
|
|
|
|
|
|
|
|
|
if (!arrayOfKeys) {
|
|
|
|
if (!arrayOfKeys) {
|
|
|
|
return [data.map(item => item.value)];
|
|
|
|
return [withScores ?
|
|
|
|
|
|
|
|
data.map(i => ({ value: i.value, score: i.score })) :
|
|
|
|
|
|
|
|
data.map(item => item.value),
|
|
|
|
|
|
|
|
];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
const sets = {};
|
|
|
|
const sets = {};
|
|
|
|
data.forEach((item) => {
|
|
|
|
data.forEach((item) => {
|
|
|
|
sets[item._key] = sets[item._key] || [];
|
|
|
|
sets[item._key] = sets[item._key] || [];
|
|
|
|
sets[item._key].push(item.value);
|
|
|
|
if (withScores) {
|
|
|
|
|
|
|
|
sets[item._key].push({ value: item.value, score: item.score });
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
|
|
|
sets[item._key].push(item.value);
|
|
|
|
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
return keys.map(k => sets[k] || []);
|
|
|
|
return keys.map(k => sets[k] || []);
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
module.sortedSetIncrBy = async function (key, increment, value) {
|
|
|
|
module.sortedSetIncrBy = async function (key, increment, value) {
|
|
|
|
if (!key) {
|
|
|
|
if (!key) {
|
|
|
|