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

@agrippa-io/node-connection-manager

v1.0.8

Published

A Singleton Class used to register and distribute database connections

Downloads

15

Readme

@agrippa-io/node-connection-manager

What is this repository for?

This module is a Singleton designed to manage Database Connections within applications.

How do I get set up?

Simply run the following command in the desired Application: npm install @agrippa-io/node-connection-manager

Usage

Initializing the ConnectionStore

// Importing the package will automatically instantiate the Connection Manager
const ConnectionStore = require('@agrippa-io/connection-manager');

Creating Connections

// RECOMMENDATION
// For Security Reasons, use ENVIRONMENT VARIABLES for your connection URIs
const {
    MONGO_URI_PROD,
    MONGO_URI_MEDIA,
} = process.env;

// Mongoose
const mongoose = require('mongoose');
const opts = { useNewUrlParser: true };
// Mongoose connections
const connection_test = mongooose.createConnection('mongodb://localhost:27017/test', opts);
const connection_prod = mongooose.createConnection(MONGO_URI_PROD, opts);
const connection_media_prod = mongooose.createConnection(MONGO_URI_MEDIA, opts);

// Sequalize
const { Sequelize } = require('sequelize');
// Sequalize Connections
const sequalize_prod = new Sequelize(MYSQL_URI_PROD);

Key Concepts

connection-manager groups connections by Store to help Client Users conceptualize what type of connection they are accessing.

Stores

A TYPE_STORE Map has been provided to help promote grouping connections by popular Persistence Technologies, including:

  • MONGO
  • MYSQL

The storeName is just a string, so you are free to create your own groups but we recommend sticking to the conventions.

// Import STORE constants
const STORE = require('@agrippa-io/connection-manager/stores.constants';
// Access
console.log(STORE.MONGO); // prints 'mongo'
console.log(STORE.MYSQL); // prints 'mysql'
Named Connections

Named Connections to simplify the access and registration of connections within the ConnectionStore.

Named Connections are used to quickly register/retrieve connections. This helps create self-documented coding patterns and reduces the learning-curve for using the module and provide a consistent means of map/reduce/filter when working with the ConnectionStore getter/setter methods.

Named Connections are JSON Objects with the following properties:

  • storeName {String} - Name of the Store
  • connectionName {String} - Unique name for connection
  • connection {Any} - The connection instance

Example - Named Connection:

const namedConnection_test = {
  storeName: STORE.MONGO,
  connectionName: 'test',
  connection: connection_test
};

Registering Connections

Continued based on Create Connections examples above:

// Register a single connection
ConnectionStore.addNamedConnection(
  STORE.MONGO,
  'mongo-test',
  connection_test
);
// Create Many Named Connections of mixed Stores
const namedConnections = [
    {
      storeName: STORE.MONGO,
      connectionName: 'test',
      connection: connection_test
    },
    {
      storeName: STORE.MONGO,
      connectionName: 'production',
      connection: connection_prod
    },
    {
      storeName: STORE.MYSQL,
      connectionName: 'mysql-production',
      connection: sequalize_prod
    },
];

// Register Many Connections in a single method call
ConnectionStore.addNamedConnections(namedConnections);

Accessing Connections

There are 3 methods for retrieving Named Connections from the ConnectionStore:

  • getNamedConnections() - Returns all Named Connections inside the ConnectionStore
  • getStoreNamedConnections(storeName) - Returns all Named Connections for the provided Store
  • getNamedConnection(storeName, connectionName) - Returns a single Named Connection or null if no matching connection
// Get an array of ALL connections in the ConnectionStore
// Returns an Array of Named Connections
const namedConnetions = ConnectionStore.getNamedConnections();

// Get an array of ALL connections for a particular STORE
// Returns an Array of Named Connections
const mongoNamedConnections = ConnectionStore.getStoreNamedConnections
(STORE.MONGO);
const mysqlNamedConnections = ConnectionStore.getStoreNamedConnections
(STORE.MYSQL);

// Get a specific Named Connection
const productionNamedConnection = ConnectionStore.getNamedConnection
(STORE.MONGO, 'production');

Access the Connection instance from a Named Connection:

// Access the connection using dot syntax, index/key or destructuring
const dot_connection = productionNamedConnection.connection;
const index_connection = productionNamedConnection['connection'];
const { 
  connection : destructured_connection
} = productionNamedConnection;

Removing Connections

There are 3 methods for removing Named Connections from the ConnectionStore:

  • removeNamedConnection(storeName, connectionName) - Returns removed Named Connections
  • removeNamedConnections(namedConnections) - Returns all removed Named Connections from ConnectionStore
  • clearNamedConnections() - Returns all removed Named Connection from ConnectionStore
// Remove a specific Named Connection
const testNamedConnection = ConnectionStore.removeNamedConnection(STORE.MONGO, 'test');

// Remove an array of Named Connections of mixed Stores
// The named connections can omit the connection itself
// Returns an Array of removed Named Connections
const removedNamedConnections = ConnectionStore.removeNamedConnections([
    { storeName: STORE.MONGO, connectionName: 'production' },
    { storeName: STORE.MYSQL, connectionName: 'production' },
]);

// Remove ALL connections in the ConnectionStore
// Returns an Array of Named Connections
const removedRemainingNamedConnetions = ConnectionStore.clearNamedConnections();

READ-ONLY Class Properties

You can access a JSON Object of the ConnectionStore current state:

console.log(ConnectionStore.namedConnections);

DEBUGGING

We have provided ConnectionStore.debug() that will log the state of the ConnectionStore at the time of invocation:

// Prints the State of the ConnectionStore
ConnectionStore.debug();

Testing

Execute the following command within the repository: npm test