[perf] remove n+1 on /admin/list
This commit is contained in:
parent
c85ed3e515
commit
b0c4fbc8d4
|
@ -7,6 +7,9 @@ export default async (db, user) => {
|
||||||
} else if (user.avatarSource === 'gravatar') {
|
} else if (user.avatarSource === 'gravatar') {
|
||||||
return gravatar(user);
|
return gravatar(user);
|
||||||
} else if (user.avatarSource) {
|
} else if (user.avatarSource) {
|
||||||
|
if (user.payload) {
|
||||||
|
return JSON.parse(user.payload).avatar;
|
||||||
|
}
|
||||||
const auth = await db.get(SQL`
|
const auth = await db.get(SQL`
|
||||||
SELECT payload FROM authenticators
|
SELECT payload FROM authenticators
|
||||||
WHERE type = ${user.avatarSource}
|
WHERE type = ${user.avatarSource}
|
||||||
|
|
|
@ -13,9 +13,10 @@ const router = Router();
|
||||||
router.get('/admin/list', handleErrorAsync(async (req, res) => {
|
router.get('/admin/list', handleErrorAsync(async (req, res) => {
|
||||||
return res.json(await caches.admins.fetch(async () => {
|
return res.json(await caches.admins.fetch(async () => {
|
||||||
const admins = await req.db.all(SQL`
|
const admins = await req.db.all(SQL`
|
||||||
SELECT u.username, p.teamName, p.locale, u.id, u.email, u.avatarSource, p.credentials, p.credentialsLevel, p.credentialsName
|
SELECT u.username, p.teamName, p.locale, u.id, u.email, u.avatarSource, p.credentials, p.credentialsLevel, p.credentialsName, a.payload
|
||||||
FROM users u
|
FROM users u
|
||||||
LEFT JOIN profiles p ON p.userId = u.id
|
LEFT JOIN profiles p ON p.userId = u.id
|
||||||
|
LEFT JOIN authenticators a ON u.id = a.userId AND a.type = u.avatarSource AND (a.validUntil IS NULL OR a.validUntil > ${now()})
|
||||||
WHERE p.teamName IS NOT NULL
|
WHERE p.teamName IS NOT NULL
|
||||||
AND p.teamName != ''
|
AND p.teamName != ''
|
||||||
ORDER BY RANDOM()
|
ORDER BY RANDOM()
|
||||||
|
@ -34,6 +35,7 @@ router.get('/admin/list', handleErrorAsync(async (req, res) => {
|
||||||
admin.avatar = await avatar(req.db, admin);
|
admin.avatar = await avatar(req.db, admin);
|
||||||
delete admin.id;
|
delete admin.id;
|
||||||
delete admin.email;
|
delete admin.email;
|
||||||
|
delete admin.payload;
|
||||||
if (admin.credentials) {
|
if (admin.credentials) {
|
||||||
admin.credentials = admin.credentials.split('|');
|
admin.credentials = admin.credentials.split('|');
|
||||||
}
|
}
|
||||||
|
|
Reference in New Issue