From b4b62445ec13e7c42024c006cccd3ef1dc7bddf6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EC=9B=90=EB=8D=94?= Date: Wed, 6 Jan 2021 13:25:09 +0900 Subject: [PATCH] style: prettier --- types/index.ts | 60 +++++++++++++++++++++++----------------------- utils/Fetch.ts | 20 ++++++++++------ utils/Query.ts | 65 +++++++++++++++++++++++++++++++++++++------------- 3 files changed, 91 insertions(+), 54 deletions(-) diff --git a/types/index.ts b/types/index.ts index 5f9ea90..0c9ae7f 100644 --- a/types/index.ts +++ b/types/index.ts @@ -1,37 +1,37 @@ export interface Bot { - id: string - name: string - tag: string - avatar: string - status: Status - lib: Library - prefix: string - votes: number - servers: number - intro: string - desc: string - category: Category[] - web?: string - git?: string - url?: string - discord?: string - verified: boolean - trusted: boolean - partnered: boolean - vanity: string | null - bg: string - banner: string - owners: User[] | string[] + id: string + name: string + tag: string + avatar: string + status: Status + lib: Library + prefix: string + votes: number + servers: number + intro: string + desc: string + category: Category[] + web?: string + git?: string + url?: string + discord?: string + verified: boolean + trusted: boolean + partnered: boolean + vanity: string | null + bg: string + banner: string + owners: User[] | string[] } export interface User { - id: string - avatar: string - tag: string - username: string - perm: number - github: string - bots: Bot[] | string[] + id: string + avatar: string + tag: string + username: string + perm: number + github: string + bots: Bot[] | string[] } export type Status = 'online' | 'offline' | 'dnd' | 'idle' | 'streaming' diff --git a/utils/Fetch.ts b/utils/Fetch.ts index cbceba6..f2ccc64 100644 --- a/utils/Fetch.ts +++ b/utils/Fetch.ts @@ -2,12 +2,18 @@ import DataLoader from 'dataloader' import * as Query from './Query' const Fetch = { - bot: new DataLoader(async (ids:string[]) => await Promise.all(ids.map((el:string)=> Query.getBot(el))), { - batchScheduleFn: callback => setTimeout(callback, 1000) - }), - user: new DataLoader(async (ids:string[]) => await Promise.all(ids.map((el:string)=> Query.getUser(el))), { - batchScheduleFn: callback => setTimeout(callback, 1000) - }), + bot: new DataLoader( + async (ids: string[]) => await Promise.all(ids.map((el: string) => Query.getBot(el))), + { + batchScheduleFn: callback => setTimeout(callback, 1000), + } + ), + user: new DataLoader( + async (ids: string[]) => await Promise.all(ids.map((el: string) => Query.getUser(el))), + { + batchScheduleFn: callback => setTimeout(callback, 1000), + } + ), } -export default Fetch \ No newline at end of file +export default Fetch diff --git a/utils/Query.ts b/utils/Query.ts index 1e22298..f67a914 100644 --- a/utils/Query.ts +++ b/utils/Query.ts @@ -17,29 +17,60 @@ export const knex = knsexy({ }, }) -export async function getBot(id: string, owners=true):Promise { - const res = await knex('bots').select(['id', 'owners', 'lib', 'prefix', 'votes', 'servers', 'intro', 'desc', 'web', 'git', 'url', 'category', 'status', 'name', 'avatar', 'tag', 'verified', 'trusted', 'partnered', 'discord', 'boosted', 'state', 'vanity', 'bg', 'banner']).where({ id }).orWhere({ vanity: id, boosted: 1 }) - if(res[0]) { +export async function getBot(id: string, owners = true): Promise { + const res = await knex('bots') + .select([ + 'id', + 'owners', + 'lib', + 'prefix', + 'votes', + 'servers', + 'intro', + 'desc', + 'web', + 'git', + 'url', + 'category', + 'status', + 'name', + 'avatar', + 'tag', + 'verified', + 'trusted', + 'partnered', + 'discord', + 'boosted', + 'state', + 'vanity', + 'bg', + 'banner', + ]) + .where({ id }) + .orWhere({ vanity: id, boosted: 1 }) + if (res[0]) { res[0].category = JSON.parse(res[0].category) res[0].owners = JSON.parse(res[0].owners) - if(owners) res[0].owners = res[0].owners.map(async (u: string) => await getUser(u)) - res[0].owners = await Promise.all(res[0].owners.filter((el: User|null)=> el)) - res[0].vanity = res[0].vanity && ( res[0].boosted || res[0].trusted || res[0].partnered ) - + if (owners) res[0].owners = res[0].owners.map(async (u: string) => await getUser(u)) + res[0].owners = await Promise.all(res[0].owners.filter((el: User | null) => el)) + res[0].vanity = res[0].vanity && (res[0].boosted || res[0].trusted || res[0].partnered) } - + return res[0] || null } -export async function getUser(id: string, bots=true):Promise { - const res = await knex('users').select(['id', 'avatar', 'tag', 'username', 'perm', 'github']).where({ id }) - if(res[0]) { - const owned = await knex('bots').select(['id']).where('owners', 'like', `%${id}%`) - if(bots) res[0].bots = owned.map(async b=> await getBot(b.id, false)) - else res[0].bots = owned.map(async b=> b.id) - res[0].bots = await Promise.all(res[0].bots.filter((el:Bot|null)=> el)) +export async function getUser(id: string, bots = true): Promise { + const res = await knex('users') + .select(['id', 'avatar', 'tag', 'username', 'perm', 'github']) + .where({ id }) + if (res[0]) { + const owned = await knex('bots') + .select(['id']) + .where('owners', 'like', `%${id}%`) + if (bots) res[0].bots = owned.map(async b => await getBot(b.id, false)) + else res[0].bots = owned.map(async b => b.id) + res[0].bots = await Promise.all(res[0].bots.filter((el: Bot | null) => el)) } - + return res[0] || null - }