2024-02-05 15:29:49 +00:00
|
|
|
const Redis = require("ioredis");
|
|
|
|
const redis = new Redis(process.env.REDIS_URI);
|
|
|
|
|
|
|
|
type WhatsappData = {
|
|
|
|
whatsappId: string;
|
|
|
|
contactId: string;
|
|
|
|
identifier: string;
|
|
|
|
value?: string;
|
2024-03-15 14:59:32 +00:00
|
|
|
history?: string;
|
2024-02-05 15:29:49 +00:00
|
|
|
};
|
|
|
|
|
2024-03-13 14:19:40 +00:00
|
|
|
type getData = {
|
|
|
|
key: string;
|
|
|
|
value?: string;
|
|
|
|
parse?: boolean;
|
|
|
|
};
|
|
|
|
|
2024-02-20 15:33:36 +00:00
|
|
|
export async function set(key: string, value: string | object) {
|
2024-03-06 21:14:12 +00:00
|
|
|
if (typeof value == "object") await redis.set(key, JSON.stringify(value));
|
|
|
|
else {
|
|
|
|
await redis.set(key, value);
|
|
|
|
}
|
2024-02-05 15:29:49 +00:00
|
|
|
}
|
|
|
|
|
2024-03-13 14:19:40 +00:00
|
|
|
export async function getSimple(key: string) {
|
|
|
|
const value: any = await redis.get(key);
|
|
|
|
return value;
|
|
|
|
}
|
2024-03-06 21:14:12 +00:00
|
|
|
|
2024-03-13 14:19:40 +00:00
|
|
|
export async function get({ key, value, parse }: getData) {
|
|
|
|
if (key.includes("*")) {
|
2024-03-15 14:59:32 +00:00
|
|
|
const keys = await redis.keys(key);
|
2024-03-06 21:14:12 +00:00
|
|
|
if (keys.length > 0) {
|
|
|
|
for (const key of keys) {
|
2024-03-15 14:59:32 +00:00
|
|
|
const val = await redis.get(key);
|
2024-03-13 14:19:40 +00:00
|
|
|
if (val.includes(value)) {
|
|
|
|
if (parse) return JSON.parse(val);
|
|
|
|
return val;
|
|
|
|
}
|
2024-03-06 21:14:12 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
} else {
|
|
|
|
const value: any = await redis.get(key);
|
2024-03-13 14:19:40 +00:00
|
|
|
|
|
|
|
if (parse) return JSON.parse(value);
|
|
|
|
|
|
|
|
return value;
|
2024-03-06 21:14:12 +00:00
|
|
|
}
|
2024-02-05 15:29:49 +00:00
|
|
|
}
|
|
|
|
|
2024-03-07 14:47:42 +00:00
|
|
|
export async function del(key: string) {
|
|
|
|
await redis.del(key);
|
|
|
|
}
|
|
|
|
|
2024-03-06 21:14:12 +00:00
|
|
|
export async function clearAllKeys(...keys: string[]) {
|
|
|
|
for (const key of keys) {
|
|
|
|
// Retrieve all keys matching the pattern '*'
|
|
|
|
const del_keys = await redis.keys(key);
|
2024-03-04 18:53:50 +00:00
|
|
|
|
2024-03-06 21:14:12 +00:00
|
|
|
// If there are keys, delete them
|
|
|
|
if (del_keys.length > 0) {
|
|
|
|
console.log("del_keys: ", del_keys);
|
|
|
|
await redis.del(...del_keys);
|
|
|
|
}
|
2024-03-04 18:53:50 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-02-21 14:23:51 +00:00
|
|
|
export async function findByContain(
|
|
|
|
key: string,
|
|
|
|
keyName: string,
|
|
|
|
substring: string
|
|
|
|
) {
|
2024-02-20 15:33:36 +00:00
|
|
|
// const keys = await redis.keys("*" + substring + "*");
|
2024-02-21 14:23:51 +00:00
|
|
|
// const keys = await redis.keys("user:*");
|
|
|
|
|
|
|
|
const keys = await redis.keys(key);
|
2024-02-20 15:33:36 +00:00
|
|
|
|
|
|
|
const results: any[] = [];
|
2024-02-21 14:23:51 +00:00
|
|
|
|
2024-02-20 15:33:36 +00:00
|
|
|
for (const key of keys) {
|
|
|
|
const value = await redis.get(key);
|
|
|
|
if (value) {
|
2024-02-21 14:23:51 +00:00
|
|
|
const obj = JSON.parse(value);
|
|
|
|
if (
|
|
|
|
substring
|
|
|
|
?.trim()
|
|
|
|
?.toLowerCase()
|
|
|
|
.includes(obj[keyName]?.trim()?.toLowerCase())
|
|
|
|
) {
|
|
|
|
results.push(obj);
|
|
|
|
}
|
2024-02-20 15:33:36 +00:00
|
|
|
}
|
2024-03-06 21:14:12 +00:00
|
|
|
}
|
2024-02-20 15:33:36 +00:00
|
|
|
return results;
|
|
|
|
}
|
|
|
|
|
2024-02-05 15:29:49 +00:00
|
|
|
export async function createObject({
|
|
|
|
whatsappId,
|
|
|
|
contactId,
|
|
|
|
identifier,
|
2024-03-15 14:59:32 +00:00
|
|
|
value,
|
|
|
|
history = ""
|
2024-02-05 15:29:49 +00:00
|
|
|
}: WhatsappData) {
|
|
|
|
const key = `whatsappId:${whatsappId}:contactId:${contactId}:identifier:${identifier}`;
|
|
|
|
const result = await redis.hmset(
|
|
|
|
key,
|
|
|
|
"whatsappId",
|
|
|
|
whatsappId,
|
|
|
|
"contactId",
|
|
|
|
contactId,
|
|
|
|
"identifier",
|
|
|
|
identifier,
|
|
|
|
"value",
|
2024-03-15 14:59:32 +00:00
|
|
|
value,
|
|
|
|
"history",
|
|
|
|
history
|
2024-02-05 15:29:49 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
await redis.expire(key, 300);
|
|
|
|
}
|
|
|
|
|
|
|
|
export async function updateObject({
|
|
|
|
whatsappId,
|
|
|
|
contactId,
|
|
|
|
identifier,
|
|
|
|
value
|
|
|
|
}: WhatsappData) {
|
|
|
|
const key = `whatsappId:${whatsappId}:contactId:${contactId}:identifier:${identifier}`;
|
|
|
|
|
|
|
|
await redis.hset(key, "value", value);
|
|
|
|
}
|
|
|
|
|
|
|
|
export async function findObject(
|
|
|
|
whatsappId: string,
|
|
|
|
contactId: string,
|
|
|
|
identifier: string
|
|
|
|
) {
|
|
|
|
const key = `whatsappId:${whatsappId}:contactId:${contactId}:identifier:${identifier}`;
|
|
|
|
const result = await redis.hmget(
|
|
|
|
key,
|
|
|
|
"whatsappId",
|
|
|
|
"contactId",
|
|
|
|
"identifier",
|
2024-03-15 14:59:32 +00:00
|
|
|
"value",
|
|
|
|
"history"
|
2024-02-05 15:29:49 +00:00
|
|
|
);
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
export async function deleteObject(
|
|
|
|
whatsappId: string,
|
|
|
|
contactId: string,
|
|
|
|
identifier: string
|
|
|
|
) {
|
|
|
|
const key = `whatsappId:${whatsappId}:contactId:${contactId}:identifier:${identifier}`;
|
|
|
|
const deletedCount = await redis.del(key);
|
|
|
|
}
|
|
|
|
|
|
|
|
export async function deleteKeysWithPattern(
|
|
|
|
whatsappId: string,
|
|
|
|
contactId: string
|
|
|
|
) {
|
|
|
|
const pattern = `whatsappId:${whatsappId}:contactId:${contactId}:*`;
|
|
|
|
|
|
|
|
let cursor = "0";
|
|
|
|
|
|
|
|
do {
|
|
|
|
const [newCursor, keys] = await redis.scan(
|
|
|
|
cursor,
|
|
|
|
"MATCH",
|
|
|
|
pattern,
|
|
|
|
"COUNT",
|
|
|
|
"100"
|
|
|
|
);
|
|
|
|
|
|
|
|
for (const key of keys) {
|
|
|
|
await redis.del(key);
|
|
|
|
}
|
|
|
|
|
|
|
|
cursor = newCursor;
|
|
|
|
} while (cursor !== "0");
|
|
|
|
}
|
|
|
|
|
|
|
|
export async function getHashesWithPattern(
|
|
|
|
whatsappId: string,
|
|
|
|
contactId: string
|
|
|
|
) {
|
|
|
|
const pattern = `whatsappId:${whatsappId}:contactId:${contactId}:*`;
|
|
|
|
|
|
|
|
let cursor = "0";
|
|
|
|
const hashes = [];
|
|
|
|
|
|
|
|
do {
|
|
|
|
const [newCursor, keys] = await redis.scan(
|
|
|
|
cursor,
|
|
|
|
"MATCH",
|
|
|
|
pattern,
|
|
|
|
"COUNT",
|
|
|
|
"100"
|
|
|
|
);
|
|
|
|
|
|
|
|
for (const key of keys) {
|
|
|
|
const hash = await redis.hgetall(key);
|
|
|
|
hashes.push(hash);
|
|
|
|
}
|
|
|
|
|
|
|
|
cursor = newCursor;
|
|
|
|
} while (cursor !== "0");
|
|
|
|
|
|
|
|
return hashes;
|
|
|
|
}
|