@potentii/mongo-connection

1.0.1 • Public • Published

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

Package Sidebar

Install

npm i @potentii/mongo-connection

Weekly Downloads

0

Version

1.0.1

License

MIT

Unpacked Size

11 kB

Total Files

8

Last publish

Collaborators

  • potentii