forked from canada-ca/tracker
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathload-user-by-key.js
More file actions
35 lines (31 loc) · 967 Bytes
/
load-user-by-key.js
File metadata and controls
35 lines (31 loc) · 967 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
import DataLoader from 'dataloader'
import { t } from '@lingui/macro'
export const loadUserByKey = ({ query, userKey, i18n }) =>
new DataLoader(async (ids) => {
let cursor
try {
cursor = await query`
WITH users
FOR user IN users
FILTER user._key IN ${ids}
RETURN MERGE({ id: user._key, _type: "user" }, user)
`
} catch (err) {
console.error(
`Database error occurred when user: ${userKey} running loadUserByKey: ${err}`,
)
throw new Error(i18n._(t`Unable to load user(s). Please try again.`))
}
const userMap = {}
try {
await cursor.forEach((user) => {
userMap[user._key] = user
})
} catch (err) {
console.error(
`Cursor error occurred when user: ${userKey} running loadUserByKey: ${err}`,
)
throw new Error(i18n._(t`Unable to load user(s). Please try again.`))
}
return ids.map((id) => userMap[id])
})