forked from canada-ca/tracker
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathload-domain-by-key.js
More file actions
35 lines (31 loc) · 997 Bytes
/
load-domain-by-key.js
File metadata and controls
35 lines (31 loc) · 997 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 loadDomainByKey = ({ query, userKey, i18n }) =>
new DataLoader(async (ids) => {
let cursor
try {
cursor = await query`
WITH domains
FOR domain IN domains
FILTER domain._key IN ${ids}
RETURN MERGE({ id: domain._key, _type: "domain" }, domain)
`
} catch (err) {
console.error(
`Database error occurred when user: ${userKey} running loadDomainByKey: ${err}`,
)
throw new Error(i18n._(t`Unable to load domain. Please try again.`))
}
const domainMap = {}
try {
await cursor.forEach((domain) => {
domainMap[domain._key] = domain
})
} catch (err) {
console.error(
`Cursor error occurred when user: ${userKey} running loadDomainByKey: ${err}`,
)
throw new Error(i18n._(t`Unable to load domain. Please try again.`))
}
return ids.map((id) => domainMap[id])
})