jedireza / mongo-models

:package: Map JavaScript classes to MongoDB collections
MIT License
67 stars 28 forks source link

async/await and multiple db connections #25

Closed jedireza closed 6 years ago

jedireza commented 7 years ago

This PR brings us into the async/await world.

This PR also adds support for multiple db connections via an optional named connection API. So if you only have one connection, or use the default, you connect and call functions like normal: (Ex: Model.count({})). And if you have multiple connections, you can specify the name of the connection when connecting and then use the name via the new with(...) method: (Ex: Model.with('named').count({})).

Resolves #11 Resolves #24 Resolves #27

TODO:

jedireza commented 6 years ago

This is ready for a final review if anyone is interested. I'll probably merge this evening if I don't hear anything. As always please file issues if you find anything that needs to be corrected.

dwmkerr commented 6 years ago

This is awesome, will try it right now on one of my projects and see how it goes, will update shortly :)

dwmkerr commented 6 years ago

Looking good! The only suggestion I would make would be to put the details of how MongoModels.connect works in the README, initially I was just passing my connection string (as I would for MongoClient.connect) but the connection string needs to be in an object, along with the DB name. Maybe something like:


Connecting to MongoDB

MongoModels uses the standard MongoClient.connect function to establish a connection. However, to support connections to multiple databases, the database name must also be provided when connecting:

// 'uri' is passed to MongoClient.connect, 'db' is used internally by MongoModels.
const db = await MongoModels.connect({ uri: connectionString, db: dbName });
jedireza commented 6 years ago

Thanks for test driving @dwmkerr.

However, to support connections to multiple databases, the database name must also be provided when connecting:

We need the db name now because MongoClient.connect no longer returns a MongoDB.Db, rather it returns a MongoDB.MongoClient. And in order to get a DB instance from the client, you client.db(dbName).

Did you have a chance to review the API docs? You can see a preview of the new docs here: https://github.com/jedireza/mongo-models/blob/async-await-and-multiple-connections/API.md

dwmkerr commented 6 years ago

Yep the API docs look great, the only thing I was wondering is whether the same info in a cut down form should be on the main README, as almost the first thing anyone is going to do is build a model then connect. If they've got the info on the README they'll get started quickly then can refer to the API docs for details as needed, but it is not a big thing at all!

jedireza commented 6 years ago

Thanks for mentioning this. I've included the same code that's in the example app on the readme. Good call.

dwmkerr commented 6 years ago

Looks great! Looking forward to having this, will let me eliminate a load of extra code I've got 👍