0.1.4 • Published 6 years ago

mongoose-helpers-setup-db v0.1.4

Weekly downloads
1
License
MIT
Repository
github
Last release
6 years ago

mongoose-helpers-setup-db

NPM version Build Status NSP Status

returns a function that will return a mongoose connection promise when called; it should be used in conjunction with the http-server-request-handlers-db module.

the initial intent of this module is to provide a way to re-initialize a mongoose connection, on an http request, if the connection threw an error, on application startup or was lost after startup.

see the examples directory for an actual implementation using these two modules together.

table of contents

notes

logging

  • error events will be logged to the console
  • additional logging - if options.connection.debug is set to true the following events will also be logged to the console
    • connected
    • connecting
    • disconnected
    • open
    • reconnected

errors

  • if an error occurs, the error will be attached to the db object as db.error.

installation

npm install mongoose-helpers-setup-db

api

/**
 * @param {Object} user_options
 * @param {Object} user_options.connection
 * @param {Array} user_options.schemas
 *
 * @returns {Function}
 */
function setupDb( user_options )

event types

  • connected
  • connecting
  • disconnected
  • error
  • open
  • reconnected

usage

basic

var express = require( 'express' )
var connection = require( 'mongoose-helpers-connection' )
var mongoose = require( 'mongoose' )
var setupDb = require( 'mongoose-helpers-setup-db' )
var app = express()

var User = new mongoose.Schema(
  {
    displayName: String,
    email: String,
    id: String,
    photo: String
  }
)

var options = {
  connection: {
    debug: config.debug,
    uri: {
      database: config.database,
      password: config.password,
      username: config.username
    }
  },
  schemas: [
    {
      'User': User
    }
  ]
}

setupDb( options )
  .then(
    function ( db ) {
      app.db = db
    }
  )
  .catch(
    function ( err ) {
      app.db = {
        error: err
      }
    }
  )

// elsewhere in your app on a route
app.db.model( 'User' )
  .findOne(
    { 'id': id },
    function( err, existing_user ) {
       ...
    }
  )

license

MIT License