Join GitHub today
GitHub is home to over 31 million developers working together to host and review code, manage projects, and build software together.
Sign upopenUri doesn't flush buffer commands on open (and why createConnection returns a Promise?) #5404
Comments
ValYouW
changed the title
Shouldn't openUri call onOpen once connection is opened?
Shouldn't openUri call onOpen once connection is opened? (or why createConnection returns a Promise?)
Jun 27, 2017
phased90
referenced this issue
Jun 27, 2017
Closed
DeprecationWarning: `open()` is deprecated in mongoose >= 4.11.0, use `openUri()` instead #5399
This comment has been minimized.
This comment has been minimized.
agoldis
commented
Jun 29, 2017
•
I have faced the issue when a query on a collection is executed before mongoose connects. The code below never executes the const mongoose = require('mongoose')
mongoose.Promise = Promise
const schema = mongoose.Schema({}, {strict: false});
const Model = mongoose.model('mymodel', schema);
Model.find().exec().then(console.log)
mongoose.connect('mongodb://localhost:27017/testingBug', { useMongoClient: true })
.then(() => {
console.log('connected')
})
.catch(console.error) Here's what happens:
As @ValYouW suggests, calling @ValYouW I suggest to change the title to something like UPDATE: mongoose.connect('mongodb://localhost:27017/testingBug', { useMongoClient: true }, function (ignore, connection) {
connection.onOpen()
}).then(() => {
console.log('connected')
})
.catch(console.error) |
ValYouW
changed the title
Shouldn't openUri call onOpen once connection is opened? (or why createConnection returns a Promise?)
openUri doesn't flush buffer commands on open (and why createConnection returns a Promise?)
Jun 30, 2017
This comment has been minimized.
This comment has been minimized.
this seems like a legit enhancement to me, i'll add it to the docket. Thoughts @vkarpov15 ? |
varunjayaraman
added
the
enhancement
label
Jul 1, 2017
varunjayaraman
added this to the 4.11.2 milestone
Jul 1, 2017
vkarpov15
modified the milestones:
4.11.1,
4.11.2
Jul 2, 2017
vkarpov15
closed this
in
6c025c8
Jul 2, 2017
This comment has been minimized.
This comment has been minimized.
You're right about the One of the changes for |
This comment has been minimized.
This comment has been minimized.
@vkarpov15 Thank you. Returning Promise from createConnection/connect doesn't give users the option to use the "command buffering" feature of mongoose (which is cool) on app startup (unless it is a single-connection and all operations are done on the default connection). |
This comment has been minimized.
This comment has been minimized.
Yeah I can see that being frustrating. I suppose we can decorate connections with |
vkarpov15
reopened this
Jul 5, 2017
vkarpov15
modified the milestones:
4.11.2,
4.11.1
Jul 5, 2017
This comment has been minimized.
This comment has been minimized.
@vkarpov15 thanks! |
This comment has been minimized.
This comment has been minimized.
tomgrossman
commented
Jul 6, 2017
@ValYouW I agree! I have the exact same issue. 3 different databases, so 3 different |
vkarpov15
closed this
in
4495146
Jul 10, 2017
This comment has been minimized.
This comment has been minimized.
Fixes, will be released with 4.11.2 |
This comment has been minimized.
This comment has been minimized.
tomgrossman
commented
Jul 25, 2017
@vkarpov15 can you please give an example how this solution helps? Because I can't still define a model for this connection since it is a promise and not a connection object. |
This comment has been minimized.
This comment has been minimized.
@tomgrossman you should be able to in 4.11.4. Is this not the case? |
This comment has been minimized.
This comment has been minimized.
tomgrossman
commented
Jul 31, 2017
@vkarpov15 seems to work, thanks! :) |
ValYouW commentedJun 26, 2017
•
edited
mongoose 4.11.0
When using
openUri
the connectiononOpen
is not getting called and as a result all collections are in "buffer" mode, maybe the following line should be replaced to_this.onOpen()
https://github.com/Automattic/mongoose/blob/master/lib/connection.js#L739
[Edit] I'll elaborate as I forgot to mention I did a little hack :)
What I was trying to achieve is make
createConnection
to always return the Connection object, whether it was called withuseMongoClient
or not. The fact that it returns a Promise is breaking my code as now I need to wait for the Promise to resolve before I can define models on this connection.So maybe the correct question is "Why createConnection doesn't return the connection but a Promise?", since connection has a buffer mechanism, and it is an emitter that emits an "open" event it make sense to get it immediately (like it was before 4.11.0).
Thoughts?