2024-02-27 00:20:15 +00:00
|
|
|
import redis from './redis.js';
|
|
|
|
|
2024-09-10 00:47:03 +00:00
|
|
|
export async function getIdsBySlug(slugs, domain, toMap) {
|
2024-04-02 03:55:53 +00:00
|
|
|
if (!slugs) {
|
|
|
|
return [];
|
|
|
|
}
|
|
|
|
|
2024-02-27 00:20:15 +00:00
|
|
|
const ids = await Promise.all(slugs.map(async (slug) => {
|
|
|
|
if (!slug) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2024-03-27 22:54:23 +00:00
|
|
|
/* this, naturally, fails if the slug is 69 etc.
|
2024-02-27 00:20:15 +00:00
|
|
|
if (Number(slug)) {
|
|
|
|
return Number(slug); // already an ID or missing
|
|
|
|
}
|
2024-03-27 22:54:23 +00:00
|
|
|
*/
|
2024-02-27 00:20:15 +00:00
|
|
|
|
|
|
|
const id = await redis.hGet(`traxxx:${domain}:id_by_slug`, slug);
|
|
|
|
|
|
|
|
return Number(id);
|
|
|
|
}));
|
|
|
|
|
2024-09-10 00:47:03 +00:00
|
|
|
if (toMap) {
|
|
|
|
return Object.fromEntries(slugs.map((slug, index) => [slug, ids[index]]));
|
|
|
|
}
|
|
|
|
|
2024-02-27 00:20:15 +00:00
|
|
|
return ids.filter(Boolean);
|
|
|
|
}
|