mirror of
https://github.com/NodeBB/NodeBB.git
synced 2025-10-26 08:36:12 +01:00
test: return empty array on db.exists([]);
This commit is contained in:
@@ -17,11 +17,14 @@ module.exports = function (module) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (Array.isArray(key)) {
|
if (Array.isArray(key)) {
|
||||||
|
if (!key.length) {
|
||||||
|
return [];
|
||||||
|
}
|
||||||
const data = await module.client.collection('objects').find({
|
const data = await module.client.collection('objects').find({
|
||||||
_key: { $in: key },
|
_key: { $in: key },
|
||||||
}, { _id: 0, _key: 1 }).toArray();
|
}, { _id: 0, _key: 1 }).toArray();
|
||||||
|
|
||||||
const map = {};
|
const map = Object.create(null);
|
||||||
data.forEach((item) => {
|
data.forEach((item) => {
|
||||||
map[item._key] = true;
|
map[item._key] = true;
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -16,38 +16,59 @@ module.exports = function (module) {
|
|||||||
if (!key) {
|
if (!key) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
const isArray = Array.isArray(key);
|
||||||
// Redis/Mongo consider empty zsets as non-existent, match that behaviour
|
if (isArray && !key.length) {
|
||||||
const type = await module.type(key);
|
return [];
|
||||||
if (type === 'zset') {
|
|
||||||
if (Array.isArray(key)) {
|
|
||||||
const members = await Promise.all(key.map(key => module.getSortedSetRange(key, 0, 0)));
|
|
||||||
return members.map(member => member.length > 0);
|
|
||||||
}
|
|
||||||
const members = await module.getSortedSetRange(key, 0, 0);
|
|
||||||
return members.length > 0;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (Array.isArray(key)) {
|
async function checkIfzSetsExist(keys) {
|
||||||
|
const members = await Promise.all(
|
||||||
|
keys.map(key => module.getSortedSetRange(key, 0, 0))
|
||||||
|
);
|
||||||
|
return members.map(member => member.length > 0);
|
||||||
|
}
|
||||||
|
|
||||||
|
async function checkIfKeysExist(keys) {
|
||||||
const res = await module.pool.query({
|
const res = await module.pool.query({
|
||||||
name: 'existsArray',
|
name: 'existsArray',
|
||||||
text: `
|
text: `
|
||||||
SELECT o."_key" k
|
SELECT o."_key" k
|
||||||
FROM "legacy_object_live" o
|
FROM "legacy_object_live" o
|
||||||
WHERE o."_key" = ANY($1::TEXT[])`,
|
WHERE o."_key" = ANY($1::TEXT[])`,
|
||||||
values: [key],
|
values: [keys],
|
||||||
});
|
});
|
||||||
return key.map(k => res.rows.some(r => r.k === k));
|
return keys.map(k => res.rows.some(r => r.k === k));
|
||||||
|
}
|
||||||
|
|
||||||
|
// Redis/Mongo consider empty zsets as non-existent, match that behaviour
|
||||||
|
if (isArray) {
|
||||||
|
const types = await Promise.all(key.map(module.type));
|
||||||
|
const zsetKeys = key.filter((_key, i) => types[i] === 'zset');
|
||||||
|
const otherKeys = key.filter((_key, i) => types[i] !== 'zset');
|
||||||
|
const [zsetExits, otherExists] = await Promise.all([
|
||||||
|
checkIfzSetsExist(zsetKeys),
|
||||||
|
checkIfKeysExist(otherKeys),
|
||||||
|
]);
|
||||||
|
const existsMap = Object.create(null);
|
||||||
|
zsetKeys.forEach((k, i) => { existsMap[k] = zsetExits[i]; });
|
||||||
|
otherKeys.forEach((k, i) => { existsMap[k] = otherExists[i]; });
|
||||||
|
return key.map(k => existsMap[k]);
|
||||||
|
}
|
||||||
|
const type = await module.type(key);
|
||||||
|
if (type === 'zset') {
|
||||||
|
const members = await module.getSortedSetRange(key, 0, 0);
|
||||||
|
return members.length > 0;
|
||||||
}
|
}
|
||||||
const res = await module.pool.query({
|
const res = await module.pool.query({
|
||||||
name: 'exists',
|
name: 'exists',
|
||||||
text: `
|
text: `
|
||||||
SELECT EXISTS(SELECT *
|
SELECT EXISTS(SELECT *
|
||||||
FROM "legacy_object_live"
|
FROM "legacy_object_live"
|
||||||
WHERE "_key" = $1::TEXT
|
WHERE "_key" = $1::TEXT
|
||||||
LIMIT 1) e`,
|
LIMIT 1) e`,
|
||||||
values: [key],
|
values: [key],
|
||||||
});
|
});
|
||||||
|
|
||||||
return res.rows[0].e;
|
return res.rows[0].e;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
@@ -14,6 +14,9 @@ module.exports = function (module) {
|
|||||||
|
|
||||||
module.exists = async function (key) {
|
module.exists = async function (key) {
|
||||||
if (Array.isArray(key)) {
|
if (Array.isArray(key)) {
|
||||||
|
if (!key.length) {
|
||||||
|
return [];
|
||||||
|
}
|
||||||
const batch = module.client.batch();
|
const batch = module.client.batch();
|
||||||
key.forEach(key => batch.exists(key));
|
key.forEach(key => batch.exists(key));
|
||||||
const data = await helpers.execBatch(batch);
|
const data = await helpers.execBatch(batch);
|
||||||
|
|||||||
@@ -64,12 +64,15 @@ describe('Key methods', () => {
|
|||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should work for an array of keys', (done) => {
|
it('should work for an array of keys', async () => {
|
||||||
db.exists(['testKey', 'doesnotexist'], (err, exists) => {
|
assert.deepStrictEqual(
|
||||||
assert.ifError(err);
|
await db.exists(['testKey', 'doesnotexist']),
|
||||||
assert.deepStrictEqual(exists, [true, false]);
|
[true, false]
|
||||||
done();
|
);
|
||||||
});
|
assert.deepStrictEqual(
|
||||||
|
await db.exists([]),
|
||||||
|
[]
|
||||||
|
);
|
||||||
});
|
});
|
||||||
|
|
||||||
describe('scan', () => {
|
describe('scan', () => {
|
||||||
|
|||||||
Reference in New Issue
Block a user