graphbrainz/src/loaders.js

49 lines
1.4 KiB
JavaScript
Raw Normal View History

2016-08-08 07:54:06 +00:00
import DataLoader from 'dataloader'
2016-08-20 05:59:32 +00:00
import MusicBrainz from './api'
2016-08-08 07:54:06 +00:00
2016-08-20 05:59:32 +00:00
const client = new MusicBrainz()
2016-08-08 07:54:06 +00:00
2016-08-20 05:59:32 +00:00
export const lookupLoader = new DataLoader(keys => {
2016-08-08 07:54:06 +00:00
return Promise.all(keys.map(key => {
2016-08-20 05:59:32 +00:00
const [ entityType, id, params ] = key
return client.lookup(entityType, id, params).then(entity => {
if (entity) {
entity.entityType = entityType
}
return entity
})
2016-08-08 07:54:06 +00:00
}))
}, {
2016-08-20 05:59:32 +00:00
cacheKeyFn: (key) => client.getLookupURL(...key)
})
export const browseLoader = new DataLoader(keys => {
return Promise.all(keys.map(key => {
const [ entityType, params ] = key
const pluralName = entityType.endsWith('s') ? entityType : `${entityType}s`
return client.browse(entityType, params).then(list => {
list[pluralName].forEach(entity => {
entity.entityType = entityType
})
return list
})
}))
}, {
cacheKeyFn: (key) => client.getBrowseURL(...key)
})
export const searchLoader = new DataLoader(keys => {
return Promise.all(keys.map(key => {
const [ entityType, query, params ] = key
const pluralName = entityType.endsWith('s') ? entityType : `${entityType}s`
return client.search(entityType, query, params).then(list => {
list[pluralName].forEach(entity => {
entity.entityType = entityType
})
return list
})
}))
}, {
cacheKeyFn: (key) => client.getSearchURL(...key)
2016-08-08 07:54:06 +00:00
})