"use strict";

var async = require('async');
var mongoose = require('mongoose');
require('./models');

function generateID() {
  var objectId = new mongoose.Types.ObjectId();
  return objectId.toHexString();
}

function populateIdField(record) {
  if (record._id) {
    return;
  }
  if (record.id) {
    record._id = record.id;
  } else {
    record._id = generateID();
  }
}

function importer(connection, popoloObject, callback) {
  var collections = [
    ['persons', 'Person'],
    ['people', 'Person'],
    ['organizations', 'Organization'],
    ['memberships', 'Membership'],
    ['posts', 'Post'],
  ];
  async.each(collections, function(collectionPair, done) {
    var key = collectionPair[0];
    var modelName = collectionPair[1];
    if (!popoloObject[key] || !Array.isArray(popoloObject[key])) {
      return done();
    }
    // Ensure all records have a valid _id field
    popoloObject[key].forEach(populateIdField);
    connection.model(modelName).create(popoloObject[key], done);
  }, callback);
}

module.exports = importer;

@@ -604,16 +604,34 @@
 %5D,%0A  %5D;%0A
+  var stats = %7B%7D;%0A
   async.
@@ -947,16 +947,28 @@
 ld);%0A   
+ var Model =
  connect
@@ -987,16 +987,26 @@
 delName)
+%0A    Model
 .create(
@@ -1037,19 +1037,149 @@
 ;%0A  
-%7D, callback
+  stats%5Bkey%5D = popoloObject%5Bkey%5D.length;%0A  %7D, function(err) %7B%0A    if (err) %7B%0A      return callback(err);%0A    %7D%0A    callback(null, stats);%0A  %7D
 );%0A%7D

Add stats to import