Skip to content

Instantly share code, notes, and snippets.

@chrisoverstreet
Last active July 1, 2020 18:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save chrisoverstreet/544934bb42e6b1598b0100b007d9e40f to your computer and use it in GitHub Desktop.
Save chrisoverstreet/544934bb42e6b1598b0100b007d9e40f to your computer and use it in GitHub Desktop.
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