Closed jedireza closed 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.
This is awesome, will try it right now on one of my projects and see how it goes, will update shortly :)
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:
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 });
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
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!
Thanks for mentioning this. I've included the same code that's in the example app on the readme. Good call.
Looks great! Looking forward to having this, will let me eliminate a load of extra code I've got 👍
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 newwith(...)
method: (Ex:Model.with('named').count({})
).Resolves #11 Resolves #24 Resolves #27
TODO:
mongodb@3.x.x
async
/await
.constructWithSchema
has been removed. The default behavior is to always construct with the schema.collection
has been renamed tocollectionName
.connect
method now expects aconnection
object with keysuri
anddb
.pagedFind
method no longer accepts thefields
orsort
arguments. Use theoptions
argument instead.