Skip to content

Instantly share code, notes, and snippets.

@jimmiehansson
Created February 13, 2018 16:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jimmiehansson/d7d36c646403bd46162a40fe979dc218 to your computer and use it in GitHub Desktop.
Save jimmiehansson/d7d36c646403bd46162a40fe979dc218 to your computer and use it in GitHub Desktop.
Example of a simple functional file
'use strict';
/**
* This notation was generated by templates.
* // -------------------------------------------------
* GLOBAL FILE NOTATIONS
* Project of: Rocketdesk Auth
* Filename: Connector.js by jimmie
* Created: 2017-07-17 @ 23:00
* Product of: Rocketdesk.net
* // -------------------------------------------------
* Make sure this file is part of its proper namespace
* and project before moving on.
* // -------------------------------------------------
* Code-tags conventionally should be used (See below) :
* TODO - Something that someone need to do.
* DOING - Self remind for what you are doing.
* CONSIDER - Reminder to consider a change or addition.
* BUG - The below section of a code cause a bug.
* FIXME - The below section of code need to be fixed.
* HACK - The below section of code is a workaround.
* XXX - Any notation important enough to consider implementing.
* CLARIFY - Very incomprehensible section of code below.
*
* Created by jimmie on (2017-07-17).
*
* Repository link: https://github.com/jimmiehansson/rocket.git
*/
/**
* Require node specific modules and libraries
* here to separate from the rest of the code.
*/
const { Pool, Client } = require('pg');
/**
* Import universal/associated libraries
* here to separate from the rest of the code.
*/
import {
scheduleConnection,
eventDispatch,
} from './PoolHandler';
import {
PRE_ENV
} from './Constants/Environment';
import {
populate
} from './Config/Db';
import {
PGSQL_POOL_CONNECTION_TIMEOUT_MILLIS,
PGSQL_POOL_IDLE_TIMEOUT_MILLIS,
PGSQL_POOL_MIN_CLIENTS,
PGSQL_POOL_MAX_CLIENTS,
PGSQL_POOL_EVENT_CONNECT,
PGSQL_POOL_EVENT_ACQUIRE,
PGSQL_POOL_EVENT_ERROR,
PGSQL_POOL_SSL,
PGSQL_CLIENT_SSL,
PGSQL_CLIENT_TYPES
} from './Constants/Common';
import {
} from './Constants/Language/English';
/**
* DOING: Get the environment
* before initiating the connections.
* Production should be set explicitly.
* @param PRE_ENV
*/
const loadEnvironment = PRE_ENV||'local';
/**
* DOING: Should create a connection instance
* with the populate config object and return
* a pool reference.
* @param {string} configurationType
* @returns {PG.Pool}
*/
const initPoolConnector = configurationType => new Pool(
populate.default()[configurationType],
{
connectionTimeoutMillis : PGSQL_POOL_CONNECTION_TIMEOUT_MILLIS,
idleTimeoutMillis : PGSQL_POOL_IDLE_TIMEOUT_MILLIS,
min : PGSQL_POOL_MIN_CLIENTS,
max : PGSQL_POOL_MAX_CLIENTS,
ssl : PGSQL_POOL_SSL
});
/**
* DOING: Should create a connection instance
* with the populate config object and return
* a client reference.
* @param {string} configurationType
* @returns {PG.Client}
*/
const initClientConnector = configurationType => new Client(
populate.default()[configurationType],
{
ssl : PGSQL_CLIENT_SSL,
types : PGSQL_CLIENT_TYPES
});
/**
* DOING: Should register the events
* from the connection being registered.
* @param db
*/
export const registerConnection = db => {
db
.on(PGSQL_POOL_EVENT_CONNECT, client => registerClient(db, client))
.on(PGSQL_POOL_EVENT_ACQUIRE, client => registerAcquire(db, client))
.on(PGSQL_POOL_EVENT_ERROR, error => registerError(db, error));
};
/**
* DOING: Should delegate the connection
* event from the pool handler.
* @param db
* @param client
*/
const registerClient = (db, client) => eventDispatch(PGSQL_POOL_EVENT_CONNECT, db, client);
/**
* DOING: Should delegate the acquire
* event from the pool handler.
* @param db
* @param client
*/
const registerAcquire = (db, client) => eventDispatch(PGSQL_POOL_EVENT_ACQUIRE, db, client);
/**
* DOING: Should delegate the error
* event from the pool handler.
* @param db
* @param client
*/
const registerError = (db, client) => eventDispatch(PGSQL_POOL_EVENT_ERROR, db, client);
/**
* DOING: Should return a reference object
* to the client cursor with the current environment.
* @param {boolean} instanceType
* @returns {Object}
*/
export const dbConnector = instanceType => (instanceType) ? scheduleConnection(initPoolConnector(loadEnvironment)) : scheduleConnection(initClientConnector(loadEnvironment));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment