Socket
Book a DemoInstallSign in
Socket

hapi-mongodb-promises

Package Overview
Dependencies
Maintainers
1
Versions
3
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

hapi-mongodb-promises

[ ![Codeship Status for peeter-tomberg/hapi-mongodb-promises](https://codeship.io/projects/6d63ee10-02c1-0132-49a3-5eecd43daaed/status?branch=master)](https://codeship.io/projects/30317) [![Coverage Status](https://coveralls.io/repos/peeter-tomberg/hapi-

0.0.0
latest
npmnpm
Version published
Weekly downloads
0
-100%
Maintainers
1
Weekly downloads
 
Created
Source

Codeship Status for peeter-tomberg/hapi-mongodb-promises Coverage Status Code Climate

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 = require("hapi");

var dbOpts = {
    "url": "mongodb://localhost:27017/test"
};

var server = new Hapi.Server(8080);

server.pack.register({
    plugin: require('hapi-mongodb-promises'),
    options: dbOpts
}, function (err) {
    if (err) {
        console.error(err);
        throw err;
    }
});

server.route( {
    "method"  : "GET",
    "path"    : "/users/{id}",
    "handler" : usersHandler
});

function usersHandler(request, reply) {
    var mongo = request.server.plugins['hapi-mongodb-promises'];
    mongo
        .findOneById('users', request.params.id)
        .then(reply)
        .otherwise(function (err) {
            reply(Hapi.error.internal('Internal MongoDB error', err));
        });
};

server.start(function() {
    console.log("Server started at " + server.info.uri);
});

FAQs

Package last updated on 11 Aug 2014

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

About

Packages

Stay in touch

Get open source security insights delivered straight into your inbox.

  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc

U.S. Patent No. 12,346,443 & 12,314,394. Other pending.