<commit_before>var mongoose = require('mongoose');

var counterSchema = new mongoose.Schema({
    model: {
        type: String,
        require: true
    },
    field: {
        type: String,
        require: true
    },
    c: {
        type: Number,
        default: 0
    }
});
counterSchema.index({ field: 1, model: 1 }, { unique: true, required: true, index: -1 });
var Counter = mongoose.connection.model('MAIC-Counter', counterSchema);

module.exports = exports = function (schema, options) {
    var settings = {
            model: null,
            field: '_id',
            startAt: 0,
            incrementBy: 1,
            incrementOnUpdate: false
        },
        fields = {},
        ready = false;

    switch (typeof(options)) {
        case 'String':
            settings.model = options;
            break;
        case 'Object':
            extend(settings, options);
            break;
    }

    fields[settings.field] = {
        type: Number,
        unique: true,
        require: true
    };
    schema.add(fields);

    Counter.findOne(
        { model: settings.model, field: settings.field },
        function (err, res) {
            if (!res) {
                var counter = new Counter({ model: settings.model, field: settings.field, c: settings.startAt });
                counter.save(function () {
                    ready = true;
                });
            }
            else
                ready = true;
        }
    );

    schema.pre('save', function (next) {
        var doc = this;
        (function save() {
            if (ready) {
                Counter.collection.findAndModify(
                    { model: settings.model, field: settings.field },
                    null,
                    { $inc: { c: settings.incrementBy } },
                    { new: true, upsert: true },
                    function (err, res) {
                        if (err) return next(err);
                        if (typeof(doc[settings.field]) !== 'number' || settings.incrementOnUpdate)
                            doc[settings.field] = res.c - 1;
                        next();
                    }
                );
            }
            else
                setTimeout(save, 5);
        })();
    });
};<commit_msg>Fixing bug that caused counter to increment on updates, causing new documents to skip IDs.<commit_after>var mongoose = require('mongoose');

var counterSchema = new mongoose.Schema({
    model: {
        type: String,
        require: true
    },
    field: {
        type: String,
        require: true
    },
    c: {
        type: Number,
        default: 0
    }
});
counterSchema.index({ field: 1, model: 1 }, { unique: true, required: true, index: -1 });
var Counter = mongoose.connection.model('MAIC-Counter', counterSchema);

module.exports = exports = function (schema, options) {
    var settings = {
            model: null,
            field: '_id',
            startAt: 0,
            incrementBy: 1,
            incrementOnUpdate: false
        },
        fields = {},
        ready = false;

    switch (typeof(options)) {
        case 'String':
            settings.model = options;
            break;
        case 'Object':
            extend(settings, options);
            break;
    }

    fields[settings.field] = {
        type: Number,
        unique: true,
        require: true
    };
    schema.add(fields);

    Counter.findOne(
        { model: settings.model, field: settings.field },
        function (err, res) {
            if (!res) {
                var counter = new Counter({ model: settings.model, field: settings.field, c: settings.startAt });
                counter.save(function () {
                    ready = true;
                });
            }
            else
                ready = true;
        }
    );

    schema.pre('save', function (next) {
        if (typeof(doc[settings.field]) !== 'number' || settings.incrementOnUpdate) {
            var doc = this;
            (function save() {
                if (ready) {
                    Counter.collection.findAndModify(
                        { model: settings.model, field: settings.field },
                        null,
                        { $inc: { c: settings.incrementBy } },
                        { new: true, upsert: true },
                        function (err, res) {
                            if (err) return next(err);
                            doc[settings.field] = res.c - 1;
                            next();
                        }
                    );
                }
                else
                    setTimeout(save, 5);
            })();
        }
        else
            next();
    });
};
{'fraction_non_alphanumeric': 0.11726039016115351, 'fraction_numerical': 0.0033927056827820186, 'mean_word_length': 1.2504770992366412, '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': '992379', 'n_tokens_mistral': 1247, 'n_tokens_neox': 1195, 'n_words': 304}