mongoose-q icon indicating copy to clipboard operation
mongoose-q copied to clipboard

Mongoose Q for multiple databases

Open nguyenxuantuong opened this issue 10 years ago • 5 comments

I have two mongo database instances to connect to. For this reason, I have to use: conn1 = mongoose.createConnection('localhost', "db-name-1"); conn2 = mongoose.createConnection('localhost', "db-name-2");

Then to create mongoose model, I have to use: conn1.model(modelName, modelSchema) instead of using mongoose.model(modelName, modelSchema),

I did put var mongoose = require('mongoose-q')() at the beginning of the file; but it seems that i doesn't work as expected. More specifically, old mongoose syntax (without Q suffix working fine), but new one such as modelObj.createQ(item) does not working. It doesn't even throw error. I guess because the connection pool is different.

How I can make mongooseQ to work in case I need to maintain 2 connection pools (each with different model set)?

Thanks.

nguyenxuantuong avatar Jan 19 '15 09:01 nguyenxuantuong

I am using mongoose-q with multiple connections without problem.

fghibellini avatar Jan 19 '15 09:01 fghibellini

Can you post me a sample code on how you did it? It would be very helpful. Thanks.

nguyenxuantuong avatar Jan 19 '15 09:01 nguyenxuantuong

I just required mongoose-q, created a connection and a model on it, then you use it just as expected. You're writing that it doesn't throw any errors, are you sure you're ending all your promise chains with .done() ?

fghibellini avatar Jan 19 '15 09:01 fghibellini

I was able to figure out the issue why no error even being thrown. It's hard to notice at the first place. In case anyone has same problem, recheck if you are using any mongoose plugin. In my case, I was using the autoIncrement plugin, which has been initialised using autoIncrement.initialize(mongoose) which using default connection. Because I opened 2 connections using moongoose.createConnection which are different from default connection, the command like: modelItem.createQ just keep blocking without showing any error (because default connection was not opened at the first place). Those error, unfortunately is hard to debug....

nguyenxuantuong avatar Jan 19 '15 14:01 nguyenxuantuong

yep a lot of mongoose plugins have this problem.

fghibellini avatar Jan 19 '15 14:01 fghibellini