2017-02-18 01:56:23 -07:00
|
|
|
'use strict';
|
2014-04-11 15:29:01 -04:00
|
|
|
|
2019-08-05 09:20:00 -04:00
|
|
|
module.exports = function (module) {
|
2021-02-04 00:06:15 -07:00
|
|
|
const helpers = require('./helpers');
|
2015-01-22 16:21:32 -05:00
|
|
|
|
2019-07-09 12:46:49 -04:00
|
|
|
module.setAdd = async function (key, value) {
|
2016-09-28 12:17:56 +03:00
|
|
|
if (!Array.isArray(value)) {
|
|
|
|
|
value = [value];
|
|
|
|
|
}
|
|
|
|
|
if (!value.length) {
|
2019-07-09 12:46:49 -04:00
|
|
|
return;
|
2016-09-28 12:17:56 +03:00
|
|
|
}
|
2025-06-18 13:04:57 -04:00
|
|
|
await module.client.sAdd(key, value.map(String));
|
2014-04-11 15:29:01 -04:00
|
|
|
};
|
|
|
|
|
|
2019-07-09 12:46:49 -04:00
|
|
|
module.setsAdd = async function (keys, value) {
|
2025-12-03 18:18:14 -05:00
|
|
|
if (!Array.isArray(keys) || !keys.length || !value) {
|
2019-07-09 12:46:49 -04:00
|
|
|
return;
|
2019-03-15 14:15:53 -04:00
|
|
|
}
|
2025-12-03 18:18:14 -05:00
|
|
|
if (!Array.isArray(value)) {
|
|
|
|
|
value = [value];
|
|
|
|
|
}
|
|
|
|
|
if (!value.length) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
const batch = module.client.batch();
|
|
|
|
|
keys.forEach((k) => {
|
|
|
|
|
value.forEach(v => batch.sAdd(String(k), String(v)));
|
|
|
|
|
});
|
|
|
|
|
await helpers.execBatch(batch);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
module.setAddBulk = async function (data) {
|
|
|
|
|
if (!data.length) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2019-08-05 09:20:00 -04:00
|
|
|
const batch = module.client.batch();
|
2025-12-03 18:18:14 -05:00
|
|
|
data.forEach(([key, member]) => batch.sAdd(String(key), String(member)));
|
2019-07-09 12:46:49 -04:00
|
|
|
await helpers.execBatch(batch);
|
2014-09-10 21:44:19 -04:00
|
|
|
};
|
|
|
|
|
|
2019-07-09 12:46:49 -04:00
|
|
|
module.setRemove = async function (key, value) {
|
2018-03-17 18:49:33 -04:00
|
|
|
if (!Array.isArray(value)) {
|
|
|
|
|
value = [value];
|
|
|
|
|
}
|
|
|
|
|
if (!Array.isArray(key)) {
|
|
|
|
|
key = [key];
|
|
|
|
|
}
|
2021-05-11 12:18:45 -06:00
|
|
|
if (!value.length) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
2018-11-23 14:39:59 -05:00
|
|
|
|
2021-02-04 00:06:15 -07:00
|
|
|
const batch = module.client.batch();
|
2025-06-18 13:04:57 -04:00
|
|
|
key.forEach(k => batch.sRem(String(k), value.map(String)));
|
2019-07-09 12:46:49 -04:00
|
|
|
await helpers.execBatch(batch);
|
2014-04-11 15:29:01 -04:00
|
|
|
};
|
|
|
|
|
|
2019-07-09 12:46:49 -04:00
|
|
|
module.setsRemove = async function (keys, value) {
|
2021-02-04 00:06:15 -07:00
|
|
|
const batch = module.client.batch();
|
2025-06-18 13:04:57 -04:00
|
|
|
keys.forEach(k => batch.sRem(String(k), String(value)));
|
2019-07-09 12:46:49 -04:00
|
|
|
await helpers.execBatch(batch);
|
2014-09-16 22:25:12 -04:00
|
|
|
};
|
|
|
|
|
|
2019-07-09 12:46:49 -04:00
|
|
|
module.isSetMember = async function (key, value) {
|
2025-06-18 13:04:57 -04:00
|
|
|
const result = await module.client.sIsMember(key, String(value));
|
2019-07-09 12:46:49 -04:00
|
|
|
return result === 1;
|
2014-04-11 15:29:01 -04:00
|
|
|
};
|
|
|
|
|
|
2019-07-09 12:46:49 -04:00
|
|
|
module.isSetMembers = async function (key, values) {
|
2019-08-05 09:20:00 -04:00
|
|
|
const batch = module.client.batch();
|
2025-06-18 13:04:57 -04:00
|
|
|
values.forEach(v => batch.sIsMember(String(key), String(v)));
|
2019-07-09 12:46:49 -04:00
|
|
|
const results = await helpers.execBatch(batch);
|
|
|
|
|
return results ? helpers.resultsToBool(results) : null;
|
2014-04-11 15:29:01 -04:00
|
|
|
};
|
|
|
|
|
|
2019-07-09 12:46:49 -04:00
|
|
|
module.isMemberOfSets = async function (sets, value) {
|
2019-08-05 09:20:00 -04:00
|
|
|
const batch = module.client.batch();
|
2025-06-18 13:04:57 -04:00
|
|
|
sets.forEach(s => batch.sIsMember(String(s), String(value)));
|
2019-07-09 12:46:49 -04:00
|
|
|
const results = await helpers.execBatch(batch);
|
|
|
|
|
return results ? helpers.resultsToBool(results) : null;
|
2015-01-22 16:21:32 -05:00
|
|
|
};
|
2014-04-11 15:29:01 -04:00
|
|
|
|
2019-07-09 12:46:49 -04:00
|
|
|
module.getSetMembers = async function (key) {
|
2025-06-18 13:04:57 -04:00
|
|
|
return await module.client.sMembers(key);
|
2014-04-11 15:29:01 -04:00
|
|
|
};
|
|
|
|
|
|
2019-07-09 12:46:49 -04:00
|
|
|
module.getSetsMembers = async function (keys) {
|
2019-08-05 09:20:00 -04:00
|
|
|
const batch = module.client.batch();
|
2025-06-18 13:04:57 -04:00
|
|
|
keys.forEach(k => batch.sMembers(String(k)));
|
2019-07-09 12:46:49 -04:00
|
|
|
return await helpers.execBatch(batch);
|
2014-08-06 21:30:06 -04:00
|
|
|
};
|
|
|
|
|
|
2019-07-09 12:46:49 -04:00
|
|
|
module.setCount = async function (key) {
|
2025-06-18 13:04:57 -04:00
|
|
|
return await module.client.sCard(key);
|
2014-04-11 15:29:01 -04:00
|
|
|
};
|
|
|
|
|
|
2019-07-09 12:46:49 -04:00
|
|
|
module.setsCount = async function (keys) {
|
2019-08-05 09:20:00 -04:00
|
|
|
const batch = module.client.batch();
|
2025-06-18 13:04:57 -04:00
|
|
|
keys.forEach(k => batch.sCard(String(k)));
|
2019-07-09 12:46:49 -04:00
|
|
|
return await helpers.execBatch(batch);
|
2014-12-21 16:29:32 -05:00
|
|
|
};
|
|
|
|
|
|
2019-07-09 12:46:49 -04:00
|
|
|
module.setRemoveRandom = async function (key) {
|
2025-06-18 13:04:57 -04:00
|
|
|
return await module.client.sPop(key);
|
2014-04-11 15:29:01 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
return module;
|
2017-02-18 02:30:48 -07:00
|
|
|
};
|