<commit_before>var async = require('async'),
    mongoose = require('mongoose'),
    savedFixtures = {};

// Create documents
var fixtures = module.exports = function(dataset, db, callback) {
    if (typeof db === 'function') {
        callback = db;
        db = mongoose;
    }

    // Load fixture
    if (typeof dataset === 'string') {
        dataset = savedFixtures[dataset];
    }

    // Error handling
    if (typeof dataset !== 'object') {
        if (callback) {
            return callback.call(fixtures, new Error('Dataset not a valid object or does not exist as a named fixture.'));
        }
    }

    async.map(Object.keys(dataset), function(tableName, done) {
        var model = db.model(tableName);

        if (model) {
            model.create(dataset[tableName], function(err) {
                done(err, Array.prototype.slice.call(arguments, 1));
            });
        } else {
            done(new Error(tableName + ' does not exist'));
        }
    }, function(err, documentSet) {        
        if (callback) {
            callback.call(fixtures, err, Array.prototype.concat.apply([], documentSet));
        }
    });
};

// Save named fixture (for later)<commit_msg>Fixes #7 - Use insertMany instead of create to save documents in order<commit_after>var async = require('async'),
    mongoose = require('mongoose'),
    savedFixtures = {};

// Create documents
var fixtures = module.exports = function(dataset, db, callback) {
    if (typeof db === 'function') {
        callback = db;
        db = mongoose;
    }

    // Load fixture
    if (typeof dataset === 'string') {
        dataset = savedFixtures[dataset];
    }

    // Error handling
    if (typeof dataset !== 'object') {
        if (callback) {
            return callback.call(fixtures, new Error('Dataset not a valid object or does not exist as a named fixture.'));
        }
    }

    async.map(Object.keys(dataset), function(tableName, done) {
        var model = db.model(tableName);

        if (model) {
            model.insertMany(dataset[tableName], function(err) {
                done(err, Array.prototype.slice.call(arguments, 1));
            });
        } else {
            done(new Error(tableName + ' does not exist'));
        }
    }, function(err, documentSet) {        
        if (callback) {
            callback.call(fixtures, err, Array.prototype.concat.apply([], documentSet));
        }
    });
};

// Save named fixture (for later)
{'fraction_non_alphanumeric': 0.13512413512413513, 'fraction_numerical': 0.001221001221001221, 'mean_word_length': 2.192207792207792, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '447445', 'n_tokens_mistral': 683, 'n_tokens_neox': 675, 'n_words': 182}