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-harakiri

v1.1.2

Published

A connection handler for Mongoose that ensures a stable connection or kills itself

Downloads

8

Readme

mongoose-harakiri

Mongoose Harakiri ensures a stable connection with mongoose, and kills itself if it can't establish one

Behavior

  • Replaces good old mongoose.connect call
  • Doesn't create a new connection if mongoose is already connected
  • If it can't connect, it kills itself, so no more hanging processes!
  • If it loses connection and can't reconnect, it kills itself, so no more hanging processes! This is useful in sharded clusters where reconnect to Mongos is broken in MongoClient
  • If there are any problems with mongoose, it kills itself, so no more hanging processes!
  • Gives you nice log messages about every action so that you can clearly see what went on
  • It uses SSL by default when NODE_ENV is set to anything other than development (but does not validate it.)
  • It authenticates against the admin database

Motivation

In the era of cloud computing, orchestration tools, and Kubernetes, we need reliable database connections more than ever. Since the orchestration tool can respawn a container that exits with an error, it's preferable to surface connection problems and explicitly try to re-initiate the connection by killing and spawning the whole container again. Also, applications that rely on databases shouldn't be alive if they aren't actually connected to a database, because they are in fact useless.

Usage

connect(connectionString, [options]) => Promise<Mongoose>

Connects to a given mongo server. Optionally takes connection options. Returns a promise containing the mongoose connection.

const mongooseHarakiri = require('mongoose-harakiri')

mongooseHarakiri.connect('mongodb://user:password@mongodb:27017')

Passing connection options:

mongooseHarakiri.connect('mongodb://user:password@mongodb:27017', { ssl: false })

disconnect() => Promise<void>

Disconnects a connection if there is one. Beware: If you have the killProcessOnDisconnect option enabled, this will kill your process.

options

Every property can be individually overwritten. Passing an options object does not disable the default options. For example you could use this code to activate SSL & SSL validation and authenticate against the admin database:

{
  ssl: true,
  sslValidate: true,
  authSource: 'admin',
  killProcessOnDisconnect: true
}

The default mongoose-harakiri behavior can also be overwritten using this option:

  • killProcessOnDisconnect: If set to true, on any disconnect mongoose-harakiri will kill itself. This is very useful when dealing with the broken MongoClient reconnection logic, which makes it impossible to reconnect to a mongos proxy in front of a mongo sharded cluster. If you set this to false mongoose will try to reconnect and kill itself if it fails (See the mongoose reconnectTries and reconnectInterval settings for more details).

setLogger(logger)

The console.* functions are used by default for logging. You can replace these with your own logger instance that contains the functions log, warn, info, debug and error (for example winston).

Running tests locally

To run the tests in development you need a running MongoDB instance.

Via brew:

brew install mongodb
brew services start mongodb

Via docker:

docker run -d --rm -p 27017:27017 mongo:3.4

Then you can run tests:

npm test

MIT License

Copyright (c) 2018 unu

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.