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

@potentii/mongo-connection

v1.0.1

Published

Package to deal with connecting to MongoDB using mongoose

Downloads

3

Readme

Mongo connection

A utility to help connect to MongoDB using mongoose.

Content

Installing

To install, simply do:

npm i @potentii/mongo-connection

Make sure you have also installed mongoose in your project, as it is a peer dependency.

Using

To connect using mongoose, simply do:

const { mongo, ConnOpts } = require('@potentii/mongo-connection');

const opts = new ConnOpts()
    .host('127.0.0.1')
    .port('27017');

mongo.connect(opts)
    // 'pool' is the mongoose connection pool
    // now the 'mongo' singleton stores this reference, so you can access it from anywhere, just calling 'mongo.pool'
    .then(pool => {
        // use as pool.model('User', new Schema(...))
        // or as mongo.pool.model('User', new Schema(...))
    });

API

mongo

It's a singleton that holds the reference to the main MongoPool instance.

Establishing a new connection at the start of the application:

const { mongo, ConnOpts } = require('@potentii/mongo-connection');

const opts = new ConnOpts(); // complete with parameters

mongo.connect(opts)
    .then(...);

Then you can access the connection pool using:

const { mongo, ConnOpts } = require('@potentii/mongo-connection');

mongo.pool.model('User', new Schema(...));

ConnOpts

Connection options builder utility.

Instance methods

  • user( user :String ) : ConnOpts - Sets the username.
  • pass( pass :String ) : ConnOpts - Sets the password.
  • host( host :String ) : ConnOpts - Sets the hostname (The default is 127.0.0.1).
  • port( port :String ) : ConnOpts - Sets the port number (The default is 27017).
  • dbName( dbName :String ) : ConnOpts - Sets the database name.
  • authSource( authSource :String ) : ConnOpts - Sets the authentication source collection (The default is admin).
  • withoutAuthOnUrl( withoutAuthOnUrl :Boolean ) : ConnOpts - Sets if the connection string should not have authentication details (may not work in some environments)__(The default is false).
  • poolSize( poolSize :Number ) : ConnOpts - Sets the size of the pool (The default is 6).
  • isSrv( isSrv :String|Boolean ) : ConnOpts - Tells if the server uses SRV (MongoDB Atlas for example)__(The default is false).

Static methods

  • ConnOpts.fromEnv( ) - Builds a new ConnOpts from the environment variables.
    • MONGO_USER - The username
    • MONGO_PASS - The password
    • MONGO_HOST - The hostname
    • MONGO_PORT - The port
    • MONGO_DB_NAME - The database name
    • MONGO_AUTH_SOURCE - The authentication collection name
    • MONGO_WITHOUT_AUTH_ON_URL - If it should not use authentication on the connection string
    • MONGO_POOL_SIZE - The size of the pool
    • MONGO_IS_SRV - If it is SRV

MongoPool

Instances of this class connects to the database, and holds the connection pool reference for later use.

Instance properties

  • isConnected : Boolean - Tells if there is a connection present.
  • pool : mongoose.Connection - The reference to the mongoose connection.

Instance methods

  • connect( opts : ConnOpts, [promiseLibrary] : Function|* ) : Promisse<mongoose.Connection> - Connects to the database using the opts. (promiseLibrary defaults to the standard JavaScript Promise implementation).
  • disconnect( ) : Promise<void> - Disconnects from the database.

License

MIT