Hook to a specific Mongoose model query - node.js

I have self contained model in invoice.js
'use strict';
// load the things we need
var mongoose = require('mongoose');
var auth_filter = require('../../auth/acl/lib/queryhook');
var invoice_db = mongoose.createConnection(config.mongo.url + '/invoiceDB');
// PROMISE LIBRARY USED FOR ASYNC FLOW
var promise = require("bluebird");
var Schema = mongoose.Schema, ObjectId = Schema.Types.ObjectId;
// define the schema for our invoice details model
var invoicedetailSchema = new Schema({
//SCHEMA INFO
});
var InvoiceModel = invoice_db.model('InvoiceDetail', invoicedetailSchema);
// create the model for seller and expose it to our app
auth_filter.registerHooks(InvoiceModel);
module.exports = InvoiceModel;
I want to hook to the pre query for this model. I am trying to accomplish that using hooks but i am not successful with that. I am registering the hook using auth_filter file as below
'use strict';
var hooks = require('hooks'),
_ = require('lodash');
exports.registerHooks = function (model) {
model.pre('find', function(next,query) {
console.log('test find');
next();
});
model.pre('query', function(next,query) {
console.log('test query');
next();
});
};
What am I doing wrong? I want to keep the hooks separate so that I can call for a lot of different models.

Query hooks need to be defined on the schema, not the model. Also, there is no 'query' hook, and the query object is passed to the hook callback as this instead of as a parameter.
So change registerHooks to be:
exports.registerHooks = function (schema) {
schema.pre('find', function(next) {
var query = this;
console.log('test find');
next();
});
};
And then call it with the schema before creating your model:
var invoicedetailSchema = new Schema({
//SCHEMA INFO
});
auth_filter.registerHooks(invoicedetailSchema);
var InvoiceModel = invoice_db.model('InvoiceDetail', invoicedetailSchema);

Related

mocha --watch and mongoose models

If I leave mocha watching for changes, every time I save a file mongoose throws the following error:
OverwriteModelError: Cannot overwrite Client model once compiled
I know that mongoose won't allow to define a model twice, but I don't know how to make it work with mocha --watch.
// client.js
var mongoose = require('mongoose');
var Schema = mongoose.Schema;
var clientSchema = new Schema({
secret: { type: String, required: true, unique: true },
name: String,
description: String,
grant_types: [String],
created_at: { type: Date, default: Date.now }
});
module.exports = mongoose.model('Client', clientSchema);
And here is the test
// client-test.js
var chai = require('chai');
var chaiHttp = require('chai-http');
var mongoose = require('mongoose');
var server = require('../../app');
var Client = require('../../auth/models').Client;
var should = chai.should();
chai.use(chaiHttp);
describe('client endpoints', function() {
after(function(done) {
mongoose.connection.close();
done();
});
it('should get a single client on /auth/client/{clientId} GET', function(done) {
var clt = new Client({
name: 'my app name',
description: 'super usefull and nice app',
grant_types: ['password', 'refresh_token']
});
clt.save(function(err) {
chai.request(server)
.get('/auth/client/' + clt._id.toString())
.end(function(err, res) {
res.should.have.status(200);
res.should.be.json;
res.body.should.have.property('client_id');
res.body.should.not.have.property('secret');
res.body.should.have.property('name');
res.body.should.have.property('description');
done();
});
});
});
});
I had the same issue. My solution was to check whether the model was created/compiled yet, and if not then do so, otherwise just retrieve the model.
using mongoose.modelNames() you can get an array of the names of your models. Then use .indexOf to check if the model you want to get is in the array or not. If it is not, then compile the model, for example: mongoose.model("User", UserSchema), but if it is already defined (as is the case with mocha --watch), simply retrieve the model (don't compile it again), which you can do with for example: mongoose.connection.model("User").
This is a function which returns a function to do this checking logic, which itself returns the model (either by compiling it or just retrieving it).
const mongoose = require("mongoose");
//returns a function which returns either a compiled model, or a precompiled model
//s is a String for the model name e.g. "User", and model is the mongoose Schema
function getModel(s, model) {
return function() {
return mongoose.modelNames().indexOf(s) === -1
? mongoose.model(s, model)
: mongoose.connection.model(s);
};
}
module.exports = getModel;
This means you have to require your model a bit differently, since you are likely replacing something like this:
module.exports = mongoose.model("User", UserSchema);
which returns the model itself,
with this:
module.exports = getModel("User", UserSchema);
which returns a function to return the model, either by compiling it or just retrieving it. This means when you require the 'User' model, you would want to call the function returned by getModel:
const UserModel = require("./models/UserModel")();
I hope this helps.
Here is a simpler code for the function getModel() that George is proposing
function getModel(modelName, modelSchema) {
return mongoose.models[modelName] // Check if the model exists
? mongoose.model(modelName) // If true, only retrieve it
: mongoose.model(modelName, modelSchema) // If false, define it
}
For a larger explanation on how to define and require the model, look here
Hope this helps :)
This worked for me,
place on the top of your test file:
const mongoose = require("mongoose");
mongoose.models = {};
mongoose.modelSchemas = {};

Mongoose findById is returning null

So I have this schema:
var mongoose = require('mongoose');
var Schema = mongoose.Schema;
var TreeSchema = new Schema({
}, { collection: 'treeLocations' });
var TreeDetailsSchema = new Schema({
}, { collection: 'treeInfo' });
module.exports = mongoose.model('Tree', TreeSchema);
module.exports = mongoose.model('TreeDetail', TreeDetailsSchema, "treeInfo");
And I am calling by ID like this:
var TreeDetails = require('./app/models/tree').model('TreeDetail');
router.route('/api/trees/:tree_id')
.get(function(req, res) {
TreeDetails.findById(req.params.tree_id, function(err, treedetail) {
if (err)
res.send(err);
res.json(treedetail);
});
});
For some reason - http://localhost:3000/api/trees/5498517ab68ca1ede0612d0a which is a real tree, is returning null
Something that might help you help me:
I was following this tutorial: https://scotch.io/tutorials/build-a-restful-api-using-node-and-express-4
The only thing I can think of that changed is that I have a collection name. Might that be it?
The step that I don't see is how you actually connect to MongoDB and after that, how you get the Model from the connection.
// connect to MongoDB
var db = mongoose.createConnection('mongodb://user:pass#host:port/database');
// now obtain the model through db, which is the MongoDB connection
TreeDetails = db.model('TreeDetails');
This last step is how you associate your model with the connected mongo database.
More info on Mongoose.model
There are several ways to establish a connection to MongoDB with mongoose, the tutorial uses:
mongoose.connect('mongodb://node:node#novus.modulusmongo.net:27017/Iganiq8o');
(Personally I prefer the more explicit mongoose.createConnection as shown in the example)
(I used mongoose 4.3.1 for this example)
My steps to reproduce, in order to provide a working example (without creating a webservice for it):
var mongoose = require('mongoose'),
TreeDetails, db;
// create the model schema
mongoose.model('TreeDetails', mongoose.Schema({
// .. your field definitions
}, {collection: 'treeInfo'}));
db = mongoose.createConnection('mongodb://user:pass#host/example');
TreeDetails = db.model('TreeDetails');
TreeDetails.findById('5671ac9217fb1730bb69e8bd', function(error, document) {
if (error) {
throw new Error(error);
}
console.log(document);
});
Instead of:
var TreeDetails = require('./app/models/tree').model('TreeDetail');
try:
var mongoose = require('mongoose'),
TreeDetails = mongoose.model('TreeDetail');
Defining the collection name shouldn't give you any issues. It's just what the collection will be called in the database / when using the mongo shell.
And just to be sure, try logging req.params.tree_id before calling findById to make sure it's coming through as you suspect.

What is the best way to hook to existing mongoose.model

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

Using mongoose and Q.spread gives array callback parameters

I have the code below:
var mongoose = require('mongoose');
var Q = require("q")
mongoose.connect("mongodb://localhost/testdb");
var Schema = mongoose.Schema;
var employeeSchema = new Schema({
name:String
})
var Employee = mongoose.model('Employee', employeeSchema);
var departmentSchema = new Schema({
name:String
})
var Department = mongoose.model('Department', departmentSchema);
var employee = new Employee();
employee.name = "T.Smith";
var dept = new Department();
dept.name = "Sales";
Q.spread([
Q.nfcall(employee.save.bind(employee)),
Q.nfcall(dept.save.bind(dept))
],function(emp,dept){
console.log(JSON.stringify(emp));
console.log(JSON.stringify(dept));
mongoose.disconnect();
})
The log statements will yield the results below:
[{"__v":0,"name":"T.Smith","_id":"5358f3c53cd354bc70fe619f"},1]
[{"__v":0,"name":"Sales","_id":"5358f3c53cd354bc70fe61a0"},1]
Why are the results an array instead of a single object?
I get the same if i replace the last block of code with this block:
Q.all([
Q.nfcall(employee.save.bind(employee)),
Q.nfcall(dept.save.bind(dept))
]).spread(function(emp,dept){
console.log(JSON.stringify(emp));
console.log(JSON.stringify(dept));
mongoose.disconnect();
})
By the way, which block is recommended?
The save callback signature is function(err, result, numberAffected) which doesn't conform to node callback convention. nfcall expects a node callback signature, which is function(err, result). To avoid loss of information, the promise returned by nfcall resolves to [result, numberAffected].
Using .bind and Q.nfcall at call sites is very ugly anyway, so you can create a method that does all this:
mongoose.Model.prototype.saveForResult = function() {
return Q.nbind(this.save, this)().spread(function(result, numberAffected) {
return result;
});
};
Then:
Q.spread([
employee.saveForResult(),
dept.saveForResult()
],function(emp,dept){
console.log(JSON.stringify(emp));
console.log(JSON.stringify(dept));
mongoose.disconnect();
})

How to save element into collection with out defining in schema

I am trying to add an element into schema object in a middleware pre save method, I defined the config as strict: false. So expecting this new element to be added and saved to the object. Below is my code which I am working on.
var mongoose = require('mongoose');
var connection = mongoose.connect('mongodb://localhost/my_database');
var Schema = mongoose.Schema
var User = new Schema({
author : String
, type : String
}, { strict: false });
var MyUserModel = mongoose.model('User', User); //create and access the model User
var u = new MyUserModel({author:'mark',type:'novel'});
u.pre('save', function(next, req){
var self = this;
self.view = 'ALL';
console.log("pre save");
next();
});
u.save(function(err){
if (err) console.log(err);
});
The problem is unable to save the new element(view) into the collection.
You need to use set:
self.set('view', 'ALL');
For properties that aren't in the schema, Mongoose cannot create a setter (whereas for properties that are in the schema, it does, and so self.author = '...' would work), so you have to set it explicitly.

Resources