lib/index.js
function setMiddleware(Model, modelName, path) {
        var RefModel;

        // We only apply the middleware on the provided 
        // paths in the plugin options.
        if (opts.onDeleteRestrict.indexOf(path) === -1) {
            return;
        }

        RefModel = models[modelName];
        RefModel.schema.pre('remove', function (next) {
            var doc = this,
                q = {};

            q[path] = doc._id;

            Model
                .findOne(q)
                .exec()
                .then(function (doc) {
                    if (doc) {
                        return next(new Error('Unable to delete as ref exist in ' + Model.modelName + ' id:' + doc._id));
                    }
                    next();
                }, next);
        });
    }
function setMiddleware(Model, modelName, path) {
        var RefModel;

        // We only apply the middleware on the provided 
        // paths in the plugin options.
        if (opts.onDeleteRestrict.indexOf(path) === -1) {
            return;
        }

        RefModel = models[modelName];
        RefModel.schema.pre('remove', function (next) {
            var doc = this,
                q = {};

            q[path] = doc._id;

            Model
                .findOne(q)
                .exec()
                .then(function (doc) {
                    if (doc) {
                        return next(new Error('Unable to delete as ref exist in ' + Model.modelName + ' id:' + doc._id));
                    }
                    next();
                }, next);
        });
    }
function setMiddleware ( Model , modelName , path ) { var RefModel ; // We only apply the middleware on the provided  // paths in the plugin options. if ( opts . onDeleteRestrict . indexOf ( path ) === - 1 ) { return ; } RefModel = models [ modelName ] ; RefModel . schema . pre ( 'remove' , function ( next ) { var doc = this , q = { } ; q [ path ] = doc . _id ; Model . findOne ( q ) . exec ( ) . then ( function ( doc ) { if ( doc ) { return next ( new Error ( 'Unable to delete as ref exist in ' + Model . modelName + ' id:' + doc . _id ) ) ; } next ( ) ; } , next ) ; } ) ; }
Sets middleware on the referenced models

@param {object} Model - The current model, where this plugin is running
@param {string} modelName - the model that is being referenced
@param {string} path - the property with the reference
Sets middleware on the referenced models