thedodd/wither

Add MongoDB connection pool to wither

nasa8x opened this issue ยท 5 comments

I am using Mongoose with Node.js and find it handy to only call once to connect to the database.

mongoose.connect('mongodb://localhost/dbname');

then manipulate adding, modifying, deleting without adding database parameters:
User.updateOne({_ id: ''}, {name: 'Your name'});

Would be great if you add this to wither

Alternatively, you can manage pools connected to mongodb using r2d2_mongodb
https://docs.rs/r2d2-mongodb/0.2.2/r2d2_mongodb/

Thank you!

saghm commented

It's worth noting that the mongodb crate uses connections pools under the hood, so wither already has this by default. The driver will make a separate connection pool per server in the cluster (e.g. three pools in a three node replica set) and will save/reuse connections for subsequent operations. The connection pools can be customized with the max_pool_size, min_pool_size, and wait_queue_timeout client options, and the pool can be monitoring by passing in an implementer of CmapEventHandler through the cmap_event_handler client option.

@saghm thanks for the response!

@nasa8x thanks for opening the issue. Hopefully Saghm's response helps. To be sure, the expected use of Wither is that you will open your DB connection (which has its own connection pooling as Saghm has pointed out), and then you just pass that to the various methods on your model, like so:

User::find_one_and_update(db.clone(), ...).await?;

// or if you have a user instance, then you can just do:
user_instance.update(db.clone(), ...).await?;

Also, I'm hoping to remove the db.clone() calls, and just pass references around. We already have a ticket open to knock this out as part of the 0.9.0 milestone.

@nasa8x mind closing this issue if everything is g2g here?

Ok thank you

@nasa8x this can still be achieved using wither with a bit of work, you can check a full example on this project https://github.com/ndelvalle/rustapi, example: https://github.com/ndelvalle/rustapi/blob/a6843a2fba1b98404977edcee876a840b31a16a0/src/routes/cat.rs#L38-L42