yieldb
Simple, expressive and yieldable MongoDB for co/koa.
var co = ;var connect = connect;
yieldb
makes working with mongodb and
co/koa a breeze.
connect
Connecting to mongodb is easy.
var co = ;var connect = connect;
Replica-sets, sharding and all the features available through the mongodb driver are supported out of the box.
collections
yieldb
Collections provide a simple, constistent interface to work with mongodb collections.
Call db.col(collectionName)
to get a collection.
var co = ;var connect = connect;
Key features:
yieldable
Each collection method returns a yieldable. For example:
var User = db; User;var doc = User;
Note that these yieldables are also Promise
s for maximal compatibility
with other modules:
var User = db; User
_id casting
Any collection method which accepts selector arguments will benefit from
auto-casting _id
hexStrings to ObjectId
. For example:
var User = db;var ObjectId = ObjectID; // the following are equivalentvar doc = User;var doc = User;var doc = User;
query building
Where it makes sense, collection methods return an instance of mquery. This means you can use all the query builder helper methods in mquery.
var User = db;var docs = User ;
The methods which do not return an mquery
are
insert()
drop()
index()
indexes()
.aggregate()
However, these methods do return Promise
s like every other collection method.
In the future, aggregate
will likely return an mquery
once support has been
added.
promises
As mentioned above, all collection methods return Promises
. Since most
collection methods also return an mquery
instance, we get Promise
support
for free everywhere. Call the query builders then()
method anywhere in
the chain to receive a
bluebird Promise
.
db
db methods
col(name)
Returns a collection
instance
var users = db;
close
Returns a promise which closes the db
connection.
db;
drop
Returns a promise which deletes the entire database.
db;
listCollections
Returns a promise which lists existing collections in the database.
db;
ping
Returns a promise which sends a ping to the database.
db;
serverStatus
Returns a promise which responds with MongoDB status.
db;
collection methods
find
Returns a yieldable mquery instance.
db;
findOne
Returns a yieldable mquery instance.
db;
insert
Accepts either a single object or array of objects.
Objects which do not have an _id
will receive one assigned a new ObjectId
.
Returns a yieldable promise;
db; db;
update
Returns a yieldable mquery instance.
db;
Default options:
multi: true fullResult: true
remove
Returns a yieldable mquery instance.
db;
drop
Returns a yieldable promise.
db;
aggregate
Accepts an array of pipeline operations and returns a yieldable promise.
db;
The promise also has it's own stream()
method if that's what you're after.
db;
findOneAndUpdate
Returns a yieldable mquery instance.
db
findOneAndRemove
Returns a yieldable mquery instance.
db
count
Returns a yieldable mquery instance.
db
distinct
Returns a yieldable mquery instance.
db;
where
Returns a yieldable mquery instance.
db
index
Creates an index. Returns a yieldable promise.
dbindexindexDefinition options;
dropIndex
Drops an index. Returns a yieldable promise.
db;
indexes
Retreives an array of all defined indexes for this collection. Returns a yieldable promise.
var indexes = db;
setOptions
Sets default collection options which apply to all methods of this collection.
var User = db;User; User;
Options are passed to the mquery.setOptions method. All mquery options are supported.
Installation
npm install yieldb --save
Development
running tests
make test
runs testsmake test-cov
runs tests + test coveragemake open-cov
opens test coverage results in your browser
verbose logging
yieldb
supports the debug
module for help during development.
Enable verbose logging by setting your DEBUG
env variable like so:
DEBUG=yieldb* npm test
```
## Sponsored by
[Pebble Technology!](https://getpebble.com)
## License
[MIT](https://github.com/pebble/yieldb/blob/master/LICENSE)