const fs = require('fs');
const { promisify } = require('util');
const mongodb = require('mongodb');
const request = require('request');

async function readMongoDocuments(file) {
  const ISODate = (d) => new Date(d);
  const ObjectId = (id) => mongodb.ObjectID.createFromHexString(id);
  return eval(await fs.promises.readFile(file, 'utf-8'));
}

async function insertTestData(url, users, posts) {
  const mongoClient = await mongodb.MongoClient.connect(url);
  const db = mongoClient.db();
  await db.collection('user').deleteMany({});
  await db.collection('post').deleteMany({});
  await db.collection('user').insertMany(users);
  await db.collection('post').insertMany(posts);
  await mongoClient.close();
}

/* refresh openwhyd's in-memory cache of users, to allow this user to login */
async function refreshOpenwhydCache(urlPrefix = 'http://localhost:8080') {
  await promisify(request.post)(urlPrefix + '/testing/refresh');
}

module.exports = {
  readMongoDocuments,
  insertTestData,
  refreshOpenwhydCache,
};

@@ -380,20 +380,25 @@
 rl, 
-users, posts
+docsPerCollection
 ) %7B%0A
@@ -502,46 +502,88 @@
 ait 
-db.collection('user').deleteMany(%7B%7D);%0A
+Promise.all(%0A    Object.keys(docsPerCollection).map(async (collection) =%3E %7B%0A    
   aw
@@ -600,22 +600,26 @@
 lection(
-'post'
+collection
 ).delete
@@ -628,16 +628,20 @@
 ny(%7B%7D);%0A
+    
   await 
@@ -658,14 +658,18 @@
 ion(
-'user'
+collection
 ).in
@@ -681,62 +681,49 @@
 any(
-users);%0A  await db.collection('post').insertMany(posts
+docsPerCollection%5Bcollection%5D);%0A    %7D)%0A  
 );%0A 

Allow importing test data for any db collection