fix: fixed fetching sending RowpacketData class

This commit is contained in:
원더 2021-01-07 01:21:24 +09:00
parent fbab707439
commit dcb0d08bb4
2 changed files with 19 additions and 50 deletions

View File

@ -3,13 +3,13 @@ import * as Query from './Query'
const Fetch = { const Fetch = {
bot: new DataLoader( bot: new DataLoader(
async (ids: string[]) => await Promise.all(ids.map((el: string) => Query.getBot(el))), async (ids: string[]) => (await Promise.all(ids.map((el: string) => Query.getBot(el)))).map(row => ({ ...row })),
{ {
batchScheduleFn: callback => setTimeout(callback, 1000), batchScheduleFn: callback => setTimeout(callback, 1000),
} }
), ),
user: new DataLoader( user: new DataLoader(
async (ids: string[]) => await Promise.all(ids.map((el: string) => Query.getUser(el))), async (ids: string[]) => (await Promise.all(ids.map((el: string) => Query.getUser(el)))).map(row => ({ ...row })),
{ {
batchScheduleFn: callback => setTimeout(callback, 1000), batchScheduleFn: callback => setTimeout(callback, 1000),
} }

View File

@ -18,59 +18,28 @@ export const knex = knsexy({
}) })
export async function getBot(id: string, owners=true):Promise<Bot> { export async function getBot(id: string, owners=true):Promise<Bot> {
const res = await knex('bots') 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 })
.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]) { if(res[0]) {
res[0].category = JSON.parse(res[0].category) res[0].category = JSON.parse(res[0].category)
res[0].owners = JSON.parse(res[0].owners) res[0].owners = JSON.parse(res[0].owners)
if (owners) res[0].owners = res[0].owners.map(async (u: string) => await getUser(u)) if(owners) res[0].owners = await Promise.all(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].owners = res[0].owners.filter((el: User|null)=> el).map((row: User)=> ({...row}))
res[0].vanity = res[0].vanity && ( res[0].boosted || res[0].trusted || res[0].partnered ) res[0].vanity = res[0].vanity && ( res[0].boosted || res[0].trusted || res[0].partnered )
} }
return res[0] || null return res[0] || null
} }
export async function getUser(id: string, bots=true):Promise<User> { export async function getUser(id: string, bots=true):Promise<User> {
const res = await knex('users') const res = await knex('users').select(['id', 'avatar', 'tag', 'username', 'perm', 'github']).where({ id })
.select(['id', 'avatar', 'tag', 'username', 'perm', 'github'])
.where({ id })
if(res[0]) { if(res[0]) {
const owned = await knex('bots') const owned = await knex('bots').select(['id']).where('owners', 'like', `%${id}%`)
.select(['id']) if(bots) res[0].bots = await Promise.all(owned.map(async b=> await getBot(b.id, false)))
.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) else res[0].bots = owned.map(async b=> b.id)
res[0].bots = await Promise.all(res[0].bots.filter((el: Bot | null) => el)) res[0].bots = res[0].bots.filter((el:Bot|null)=> el).map((row: User)=> ({...row}))
} }
return res[0] || null return res[0] || null
} }