From 9cf417298777568682dff553f0ab85a86796cc36 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EC=9B=90=EB=8D=94?= Date: Mon, 8 Feb 2021 22:40:14 +0900 Subject: [PATCH] feat: added search method --- utils/Query.ts | 69 ++++++++++++++++---------------------------------- 1 file changed, 22 insertions(+), 47 deletions(-) diff --git a/utils/Query.ts b/utils/Query.ts index b1204d9..3bfbd7a 100644 --- a/utils/Query.ts +++ b/utils/Query.ts @@ -52,10 +52,14 @@ async function getBot(id: string, owners=true):Promise { res[0].owners = JSON.parse(res[0].owners) res[0].vanity = ((res[0].trusted || res[0].partnered) && res[0].vanity) ?? null if (owners) + { res[0].owners = await Promise.all( res[0].owners.map(async (u: string) => await get._rawUser.load(u)) ) - res[0].owners = res[0].owners.filter((el: User | null) => el).map((row: User) => ({ ...row })) + res[0].owners = res[0].owners.filter((el: User | null) => el).map((row: User) => ({ ...row })) + } + + } return res[0] ?? null @@ -72,11 +76,12 @@ async function getUser(id: string, bots = true):Promise { const discordUser = await get.discord.user.load(id) res[0].tag = discordUser.discriminator res[0].username = discordUser.username - if (bots) res[0].bots = await Promise.all(owned.map(async b => await get._rawBot.load(b.id))) - else res[0].bots = owned.map(async b => b.id) - res[0].bots = res[0].bots.filter((el: Bot | null) => el).map((row: User) => ({ ...row })) + if (bots) { + res[0].bots = await Promise.all(owned.map(async b => await get._rawBot.load(b.id))) + res[0].bots = res[0].bots.filter((el: Bot | null) => el).map((row: User) => ({ ...row })) + } + else res[0].bots = owned.map(el => el.id) } - return res[0] || null } @@ -142,48 +147,11 @@ async function getBotList(type: ListType, page = 1, query?: string):Promise ({ ...row })) , { cacheMap: new TLRU({ maxStoreSize: 50, maxAgeMs: 3000000 }) }), + search: new DataLoader( + async (key: string[]) => + (await Promise.all(key.map(async (k: string) => { + const json = JSON.parse(k) + return await getBotList('SEARCH', json.page, json.query) + }))).map(row => ({ ...row })) + , { cacheMap: new TLRU({ maxStoreSize: 50, maxAgeMs: 3000000 }) }), votes: new DataLoader( async (pages: number[]) => (await Promise.all(pages.map(async (page: number) => await getBotList('VOTE', page)))).map(row => ({ ...row }))