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 🙏

© 2025 – Pkg Stats / Ryan Hefner

@janiscommerce/database-dispatcher

v2.0.1

Published

Database dispatcher

Downloads

318

Readme

database-dispatcher

Build Status Coverage Status

DatabaseDispatcher is a package that returns the necessary DB driver from a received model. Access to the databases configuration then returns the driver instance with the connection. It caches the driver per config properties.

Installation

npm install @janiscommerce/database-dispatcher

# Then install all the drivers that you need.
npm install --save @janiscommerce/mysql
npm install --save @janiscommerce/mongodb
npm install --save @janiscommerce/elasticsearch
# or any other DBDriver if it's exists

Settings

Config file with Settings package, with settings in JSON file /path/to/root/.janiscommercerc.json into the key database, clients, databaseWriteType, databaseReadType:

{
	"database": {
		"core": {
			"type": "mysql",
			"host": "http://my-host-name.com",
			"database": "my-database"
			// ...
		},
		"other": {
			"type": "mongodb",
			"host": "http://other-host-name.com",
			"database": "my-database"
			// ...
		},
		"another": {
			"type": "elasticsearch",
			"host": "http://another-host-name.com",
			// ...
		}
	},
	"databaseWriteType": "mongodb",
	"databaseReadType": "mysql",
	"clients": {
		"database": {
			"fields": {
				"write": {
					"dbHost": "host",
					"database": "collection"
				},
				"read": {
					"url": "host",
					"dbDatabase": "database"
				}
			}
		}
	}
}

Database Settings

  • Keys validations depends on the DBDriver.
  • type: if it's not exist, databaseWriteType will be use as default.

Client Settings

To assign Read and Write types of DBDrivers.

  • databaseWriteType: Required, Type of Write Database. It's the default DBDriver for every database.
  • databaseReadType: Optional, Type of Read Database.

Mapped Aliases of Clients Objects

  • clients.database.fields.write: For Write DB
  • clients.database.fields.read: For Read DB

Structure:

  • key: Field present in Client object
  • value: Field needed in DBDriver

Example

In .janiscommercerc.json

{
	"clients": {
		"database": {
			"fields": {
				"write": {
					"url": "host",
					"index": "database"
				}
			}
		}
	},
	"databaseWriteType": "elasticsearch"
}

Client Object getted from Client DB

client = {
	id: '123'
	name: 'Company',
	url: 'http://company-host-name.com',
	index: 'company.index'
}

Config will be

config = {
	host: 'http://company-host-name.com',
	databse: 'company.index'
}

API

  • getDatabaseByKey(databaseKey) Receives the database key [String] and returns the database driver instance associeted to a config. If the databaseKey dosen't exists in any config source will throw a DatabaseDispatcherError. The default value of databaseKey parameter is "_default".

  • getDatabaseByConfig(config) Receives the config [Object] and returns the database driver instance.

  • getDatabaseByClient(clientObject, useReadDB) Receives the client object [Object] and returns the database driver instance associeted to a config. If useReadDB ([Boolean]) doesn't exists or it's FALSE will try to get Write DBDriver Type by default, if it's TRUE will try to get Read DBDriver Type. If the databaseReadType doesn't exists will use databaseWriteType. If the databaseWriteType doesn't exists will be throw a DatabaseDispatcherError.

  • clearCache() Clear the internal cache, including config and DB connections.

Errors

The errors are informed with a DatabaseDispatcherError. This object has a code that can be useful for a correct error handling. The codes are the following:

| Code | Description | |------|------------------------------------------------------| | 1 | Settings not found | | 2 | Invalid settings | | 3 | ConfigDB not found for databaseKey in Settings | | 4 | Invald ConfigDB found in Settings for a databaseKey | | 5 | Invalid Client Object | | 6 | DB Driver not installed | | 7 | Invalid DB Driver (not a valid Class) |

Usage

const DatabaseDispatcher = require('@janiscommerce/database-dispatcher');

/*
	/path/to/.jannsicommercerc.json
    database: {
		core: {
        	type: 'mysql',
        	host: 'foo',
        	...
		},
		...
	},
	databaseWriteType: 'mongodb',
	databaseReadType: 'elasticsearch',
	clients: {
		database: {
			fields: {
				write: {
					dbHost: 'host'
				},
				read: {
					url: 'host',
					index: 'database'
				}
			}
		}
	},
	...
*/

// Get by Key
const myDBConnection = DatabaseDispatcher.getDatabaseByKey('core'); // A new DBDriver instance is returned.

console.log(myDBConnection); // expected output: DBDriver


// Get Write DB by Client
const client = model.getClientById('123') // Or Any method you use to get client's objects

const myDBWriteConnection = DatabaseDispatcher.getDatabaseByClient(client); // A new DBDriver instance is returned.

console.log(myDBWriteConnection.constructor.name); // expected output: MongoDB


// Get Read DB by Client
const otherClient = model.getClientById('1234') // Or Any method you use to get client's objects

const myDBWriteConnection = DatabaseDispatcher.getDatabaseByClient(otherClient, true); // A new DBDriver instance is returned.

console.log(myDBWriteConnection.constructor.name); // expected output: ElasticSearch

DatabaseDispatcher.clearCache(); // cached connections and configs cleared.