UNPKG

1.07 kBJavaScriptView Raw
1'use strict';
2
3const _ = require('lodash');
4
5const fixtures = {
6
7 /**
8 *
9 * @param {Object[]} _fixtures
10 * @param {Collection} collection
11 * @returns {Promise}
12 */
13 ensureInCollection (_fixtures, collection) {
14 const fixtureIds = [];
15 const objectMap = {};
16
17 _fixtures.forEach((object) => {
18 fixtureIds.push(object._id);
19 objectMap[object._id.toString()] = object;
20 });
21
22 return collection.find({
23 _id: { $in: fixtureIds }
24 }, {
25 fields: {
26 _id: true
27 }
28 }).toArray().then((existing) => {
29
30 existing.forEach((object) => {
31 const stringId = object._id.toString();
32 delete objectMap[stringId];
33 });
34
35 const filteredObjects = _.toArray(objectMap);
36
37 if (filteredObjects.length) {
38 return collection.insertMany(filteredObjects);
39 }
40
41 return Promise.resolve(null);
42 });
43
44 }
45
46};
47
48module.exports = fixtures;