const _ = require('lodash'); /** * SingletonResolver is a cached loader for a single result. */ class SingletonResolver { constructor(resolver) { this._cache = null; this._resolver = resolver; } load() { if (this._cache) { return this._cache; } let promise = this._resolver(arguments).then((result) => { return result; }); // Set the promise on the cache. this._cache = promise; return promise; } } /** * This joins a set of results with a specific keys and sets an empty array in * place if it was not found. * @param {Array} ids ids to locate * @param {String} key key to group by * @return {Array} array of results */ const arrayJoinBy = (ids, key) => (items) => { const itemsByKey = _.groupBy(items, key); return ids.map((id) => { if (id in itemsByKey) { return itemsByKey[id]; } return []; }); }; /** * This joins a set of results with a specific keys and sets null in place if it * was not found. * @param {Array} ids ids to locate * @param {String} key key to group by * @return {Array} array of results */ const singleJoinBy = (ids, key) => (items) => { const itemsByKey = _.groupBy(items, key); return ids.map((id) => { if (id in itemsByKey) { return itemsByKey[id][0]; } return null; }); }; module.exports = { singleJoinBy, arrayJoinBy, SingletonResolver };