npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

mongoose-connection-promise

v0.2.3

Published

Convenience library to connect Mongoose to a MongoDB instance using promises.

Downloads

74

Readme

mongoose-connection-promise

Convenience library to connect Mongoose to a MongoDB instance using promises.

NPM version David CircleCI codecov Greenkeeper badge XO code style


Note: With the introduction of mongoose > 5.x this library becomes obsolete as the handling of connections has been greatly improved in mongoose > 5.x. Read here for a good summary of the changes/improvements introduced.

Therefore this library will not be updated for mongoose > 5.x.


Table of Contents

(TOC generated by verb using markdown-toc)

Install

Install with npm

$ npm install mongoose-connection-promise

Install with yarn

$ yarn add mongoose-connection-promise

Motivation

Although mongoose does not force you to wait until a mongoose connection has been created, the author of this module prefers to not start with any application before we know that a connection has been established successfully.

mongoose-connection-promise helps connecting and disonnecting to MongoDB in mongoose reliably.

Usage

Use mongoose-connection-promise in express.js

Connect mongoose to a MongoDB instance using the default settings:


const express = require('express');
const MongooseConnection = require('mongoose-connection-promise');

const app = express();

// Initialize using the default settings, which is assuming MongoDB to run
// at mongodb://localhost:27017
const mongooseConnection = new MongooseConnection();

console.log(mongooseConnection.config.host); // Returns localhost
console.log(mongooseConnection.config.port): // Returns 27017

mongooseConnection.connect()
  .then(connection => {
    app.db = connection;
    const port = 3003;
    app.listen(port, err => {
      if (err) {
        console.log('Could not start express server');
      } else {
        console.log(`Express server started at port ${port}`);
      }
    });
  })
  .catch(err => {
    console.log('Error creating a mongoose connection', err);
  });

Pass in options:

const MongooseConnection = require('mongoose-connection-promise');

const opts = {
  username: 'foo',
  password: 'bar',
  host: 'mongo.local',
  port: 27018,
  debug: true
};

const mongooseConnection = new MongooseConnection(opts);

mongooseConnection.connect()
  .then(connection => {
    // successfully connected
  })
  .catch(err => {
    // an error occurred
  });

API

Configuration

Define a configuration object to pass to the constructor.

If no options are defined, the default options will be used: See index.js => defaultOpts for more information about the current default options.

Params

Example

// Default Options:
const defaultOpts = {
   debug: false,
   host: 'localhost',
   port: 27017,
   database: '',
   connectOptions: {
     db: {},
     server: {
       auto_reconnect: true
     },
     replset: {},
     user: {},
     pass: {},
     auth: {},
     mongos: {}
   }
};

.constructor()

Initialize a new MongooseConnection.

Params

  • {Configuration}: opts - Options to initialize MongooseConnection.

.DEFAULT_CONFIG

Returns the default options of mongoose-connection-promise

.connect()

Connect mongoose to the given instance of MongoDB.

  • returns {Promise}

.get()

Get an existing connection or create a new one.

In contrary to .connect() this method will not create a new connection if MongooseConnection is already connected, but the existing connection will be re-used and returned.

  • returns {Promise<NavtiveConnection,Error>}: Returns the connection to MongoDB.

.disconnect()

Disconnects all mongoose connections.

  • returns {Promise<void,Error>}

.isConnected()

Indicates whether there is a current and ready-to-use mongoose connection.

  • returns {boolean}

.defaultOptions()

Return the default options (DEPRECATED).

  • returns: object

Test

Start the MongoDB docker container:

$ npm run dc-dev-up

Then run the tests:

$ npm run test

Author

Stefan Walther

License

MIT


This file was generated by verb-generate-readme, v0.6.0, on February 22, 2018.