2

I have a project with multiple self contained mongoose model files. I have another modelprovider which uses the model name and the request to perform an operation. I want to hook the operation to the pre of 'find'

model file

'use strict';

// load the things we need
var mongoose = require('mongoose');
var config = require('../../config/environment');
var auth_filter = require('../../auth/acl/lib/queryhook');
var modelProviders = require('../../auth/acl/lib/modelprovider');
var invoice_db = mongoose.createConnection(config.mongo.url + '/invoiceDB'); //connect to buyer DB
var path = require('path');


// define the schema for our invoice details model
var invoicedetailSchema = new Schema({
    //SCHEMA INFO
});

var InvoiceModel = invoice_db.model('InvoiceDetail', invoicedetailSchema);


//REGISTER THE HOOKS TO THE MDOEL
auth_filter.registerHooks(InvoiceModel);

promise.promisifyAll(InvoiceModel);
promise.promisifyAll(InvoiceModel.prototype);
module.exports = InvoiceModel;

modelProviders().setModel(InvoiceModel.modelName, path.resolve(__dirname, __filename) );

I know the hooks to find and findOne works only at the schema level but i want to be done at the model level.

registerhooks

'use strict';

var hooks = require('hooks'),
    _ = require('lodash');
var mongoose = require('mongoose');


    exports.registerHooks = function( model) {

        var Qry = mongoose.Query;
        var query = model.find();
        var Sch = mongoose.Schema;
        var schema = model.schema

        _.assign(model, hooks);
        model.hook('find', mongoose.model.prototype.find)
        model.pre('find', function(next){
            console.log('hell')
            return next()
        })
        console.log(model)
    };
    exports.registerHooks_ = function( model) {
        var Qry = mongoose.Query;
        var query = model.find();
        var Sch = mongoose.Schema;
        var schema = model.schema

        _.assign(schema, hooks);
        schema.hook('find', query)
        schema.pre('find', function(next){
            console.log('hell')
            return next()
        })
        console.log(schema)
    };

I know the documentation proposes to do the pre and post at the schema level but is there any way to use the hooks library or the hooker library to solve this problem.

The reason i want to keep it at the model level is I need to access the model name at the 'find' pre hook. If there is a way to do so then it will be awesome

Uma Maheshwaraa
  • 563
  • 2
  • 9
  • 17
  • 1
    I'm not sure if I understand what you're asking, but you can access the model name within the schema's 'find' pre hook callback via `this.model.modelName` – JohnnyHK Jul 26 '15 at 03:44
  • @JohnnyHK sometimes people have to stop staring at the code. your answer works. hook it to schema and access the model through schema thanks! – Uma Maheshwaraa Jul 26 '15 at 04:26
  • @JohnnyHK Have you tried accessing the find query object within pre hook? As in can we add addition query.where and query.populate in pre hook? I tried adding query.where etc but not working – Uma Maheshwaraa Jul 26 '15 at 05:01
  • Yes, accessing the query object worked fine when I tried it. Post a new question with the details if you're still having trouble. – JohnnyHK Jul 26 '15 at 13:48
  • @JohnnyHK Please see the link here for another hook issue [link](http://stackoverflow.com/questions/31639716/modifying-results-in-post-find-mongoose-hook) – Uma Maheshwaraa Jul 26 '15 at 17:12

0 Answers0