Hapi-MongoDB-promises
This is a mongo db connection plugin and a promise wrapper for the mongo API. It enables your Hapi application to connect to mongodb and run queries.
It takes 2 options :
-
url: MongoDB connection string (eg.
mongodb://user:pass@localhost:27017
), -
settings: Optional. Provide extra settings to the connection, see documentation.
Several functions are exposed by this plugin :
find(collection, query)
: Returns a promise that resolves with the array of items matched.findOne(collection, query)
: Returns a promise that resolves with the item matched.findOneById(collection, id)
: Returns a promise that resolves with the item matched.- Autowraps the id in a ObjectId if not already done so.
insert(collection, document)
: Returns a promise that resolves with the array of items added.insertOne(collection, document)
: Returns a promise that resolves with the first item added.update(collection, query, update, options)
- Returns a promise that resolves with the result of the query
This plugin also exposes some objects :
db
: connection object to the database, if you need to run queries not available via the wrapper.
Install:
npm install hapi-mongodb-promises --save
Usage example :
var Hapi = ; var dbOpts = "url": "mongodb://localhost:27017/test"; var server = 8080; serverpack; server; { var mongo = requestserverplugins'hapi-mongodb-promises'; mongo ;}; serverstart { console;};