Dawid Pura
Dawid Pura

Reputation: 1029

Use node-restful with mongoose.createConnection

Two constraints: using node-restful and creating non-global connection (because of testing purpose) using mongoose.createConnection

Code below won't work:

var someSchema = require('./someSchema');
var restful = require('node-restful');
var model = restful.model('some', someSchema);
//far far away...
var connection = restful.mongoose.createConnection();
connection.on('connected', function() {
  restful.register(app, '/somes');
}

GET /somes causing infinite loop somewhere or not triggering callback. Anyway, node-restful model.js code shows no support for connection and model don't know anything about current connection. Does anybody know how to provide connection to model? I am stuck.

Upvotes: 1

Views: 178

Answers (1)

Robert Moskal
Robert Moskal

Reputation: 22553

I looked in

https://github.com/baugarten/node-restful/blob/master/lib/model.js

I see that the code registers models with mongoose:

var result = mongoose.model.apply(mongoose, arguments)...

passing in the mongoose variable.

So node-restful always looks for a model registered on the default mongoose connection. The one you create by calling:

mongoose.connect("mongodb://localhost/resources");

You'll have to rewrite the node-restful code to allow passing in a connection instead of just using the mongoose instance. I'm not sure how easy or hard that will be do to.

Upvotes: 1

Related Questions