Manage Connection in Mongoose across Modules - node.js

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

Related

How to use mongoose in two packages?

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...

Create MySql schema in the model in express application

I am using express-MVC-generator for creating app skeleton in node js and once I have my project structured, I need to change default database in mongo for a MySQL database but I canĀ“t find how to create a MySQL schema from database in the model file. Is there a way to do that?
After research over the web, I found that the best option to solve my problem is to use sequelize, sequelize-cli to generate the models from an existing database.
https://www.npmjs.com/package/sequelize-cli

how to get all collection names from a specific db in mongo using nodejs

Recently I want to get all collection names from a db in mongo. And my develop environment is nodejs + express + mongoose. I tried mongooser.conncetion.db.getCollectionNames(or listCollections or collections), but any does not work. I guess the it is the version problem, maybe the latest mongoose api does not contain those functions.
Consequently, how to obtain all collection names from a db using mongoose?
Thank you so much for any help!

Does Mongoose allow for Multiple Database Connections?

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.

How to properly structure database calls in an express/mongoose application?

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?

Resources