Last active
July 1, 2020 18:30
-
-
Save chrisoverstreet/544934bb42e6b1598b0100b007d9e40f to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import DataLoader from 'dataloader'; | |
import UserPermission from '../../../models/user-permission.model'; | |
async function batchUserPermissions(userIds) { | |
const userPermissionListMap = new Map(); | |
const userPermissions = await UserPermission.query() | |
.select(['userId', 'permission']) | |
.whereIn('userId', userIds); | |
userPermissions.forEach(({ userId, permission }) => { | |
if (userPermissionListMap.has(userId)) { | |
userPermissionListMap.get(userId).push(permission); | |
} else { | |
userPermissionListMap.set(userId, [permission]); | |
} | |
}); | |
return userIds.map(userId => userPermissionListMap.get(userId) || []); | |
} | |
export default function createUserPermissionsLoader() { | |
return new DataLoader(batchUserPermissions); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment