2018-02-11 12:57:51 +09:00
|
|
|
import { Connectors } from '../server/connectors.js';
|
|
|
|
|
2017-04-15 12:03:25 +09:00
|
|
|
/**
|
|
|
|
* @summary Find by ids, for DataLoader, inspired by https://github.com/tmeasday/mongo-find-by-ids/blob/master/index.js
|
|
|
|
*/
|
|
|
|
const findByIds = async function(collection, ids, context) {
|
|
|
|
|
|
|
|
// get documents
|
2018-03-03 11:39:56 +09:00
|
|
|
const documents = await Connectors.find(collection, { _id: { $in: ids } });
|
2017-04-15 12:03:25 +09:00
|
|
|
|
|
|
|
// order documents in the same order as the ids passed as argument
|
2017-05-23 09:48:48 +09:00
|
|
|
const orderedDocuments = ids.map(id => _.findWhere(documents, {_id: id}));
|
2017-04-15 12:03:25 +09:00
|
|
|
|
|
|
|
return orderedDocuments;
|
2018-12-31 15:22:17 +09:00
|
|
|
};
|
2017-04-15 12:03:25 +09:00
|
|
|
|
2017-04-15 21:39:36 +09:00
|
|
|
export default findByIds;
|