I'm using Lerna to handle multiple packages in my repo.
Basically, I have a package #pkg/server where my Node/Express App is and an other package #pkg/model where my mongoose Schemas reside.
My REST API is using some of the models, with an import ModelA from '#pkg/model'.
The problem is that both of these packages have mongoose as a dependency (the #pkg/server uses mongoose to establish a connection to the mongoDB server, and #pkg/model uses mongoose to define schemas and models).
In my server, whenever I try to call ModelA.find(...), it just hangs forever. From the mongoose docs, it looks like I have to register a model to a mongoose connection and not to the mongoose object itself.
My first guess is to pass the connection object created by the server to the #pkg/model and then register the models to this connection object.
My question is, is this the right way to do it? Or is there a way to do something like (in the #pkg/server) mongoose.loadSchemas(#pkg/model) ?
What I have ended up doing, is that I'm passing the connection to the package requiring mongoose thus sharing the connection between packages.
Some package can register models, an other one can make queries, etc...
Related
I am creating a Node Module which will perform some databasebase queries using mongoose. What I want is to access the mongoose connection variable across the module which is created in the main application. I have read that Mongoose is a singleton object to I guess it should work but it isn't working at all. ANy help will be appreciated
I'm using the Mongoose next to the Node.js. The question is:
Does the following command for creation of the ObjectID value make a call to the server?
mongoose.Types.ObjectId()
I've checked my local MongoDB server log, and it doesn't show anything like a call to the single local MongoDB node/server for requesting a new ObjectID value. However, I'm not sure if the default server log is about all the operations(trivial and essential ops), or not!
NOTE: trivial ops here means the non-manipulative data operations!
A very simple script suggests that it does not make a call to the server:
Installing mongoose by running npm install mongoose
Then make a 2-liner index.js:
var mongoose = require('mongoose');
console.log(new mongoose.Types.ObjectId);
Because we haven't even connected yet.
What my team has been doing: server.js requires server-config.js (Express routing, API requests, etc), which requires query.js (where my Mongoose MongoDB schema is defined) which in turn requires db-config.js (where I actually connect to Mongo). Then in my db-config I have to require the query.js Schema anyways, which creates a cyclic dependency.
Wouldn't it be better just to require db-config instead of query.js inside of server-config? That way, the database connection is started and then the cyclic dependency is broken.
I read that in a nodejs application, if we use a mongoose object using
var obj = require('mongoose');
then, wherever we create a new mongoose object (say, in another file), then the same cached copy is used throughout. In that case, how can we connect to multiple database instances of mongoDB in the same nodejs app ?
You can use mongoose.createConnection to connect to a separate database, and then using the connection object that generates, you can define models from that database using connection.model.
What is the correct way to access a mongodb database from Express ?
Right now, I am including my database handler db.js which contains mongoose.connect( 'mongodb://localhost/db' ); every time I need to do a database call.
Should I use the same connection and passing my db object through callbacks or I can just include my db file every time ?
In other words, is mongoose.connect always re-using the same connection ?
Edit: my source code is public here, I am fairly new to nodejs/express applications and I am not sure if my application is structured properly...
You only need to connect to your database once. In your other files, you need to include your models and use them to read / write to your database collections.
Edit: Looking at your code -- why don't you move your connect into your initialization script, and then include db.js to access your models?