var Classes = Object.create(null); /** * Create a new Connection instance. * @param {object} config * @public */ exports.createConnection = function createConnection(config) { var Connection = loadClass('Connection'); var ConnectionConfig = loadClass('ConnectionConfig'); return new Connection({config: new ConnectionConfig(config)}); }; /** * Create a new Pool instance. * @param {object} config * @public */ exports.createPool = function createPool(config) { var Pool = loadClass('Pool'); var PoolConfig = loadClass('PoolConfig'); return new Pool({config: new PoolConfig(config)}); }; /** * Create a new PoolCluster instance. * @param {object} config * @public */ exports.createPoolCluster = function createPoolCluster(config) { var PoolCluster = loadClass('PoolCluster'); return new PoolCluster(config); }; /** * Create a new Query instance. * @public */ exports.createQuery = function createQuery(sql, values, callback) { var Connection = loadClass('Connection'); return Connection.createQuery(sql, values, callback); }; /** * Escape a value for SQL. * @param {*} value * @param {boolean} [stringifyObjects=false] * @param {string} [timeZone=local] * @public */ exports.escape = function escape(value, stringifyObjects, timeZone) { var SqlString = loadClass('SqlString'); return SqlString.escape(value, stringifyObjects, timeZone); }; /** * Escape an identifier for SQL. * @param {*} value * @param {boolean} [forbidQualified] * @public */ exports.escapeId = function escapeId(value, forbidQualified) { var SqlString = loadClass('SqlString'); return SqlString.escapeId(value, forbidQualified); }; /** * Format SQL and replacement values into a SQL string. * @param {string} sql * @param {array} [values] * @param {boolean} [stringifyObjects=false] * @param {string} [timeZone=local] * @public */ exports.format = function format(sql, values, stringifyObjects, timeZone) { var SqlString = loadClass('SqlString'); return SqlString.format(sql, values, stringifyObjects, timeZone); }; /** * The type constants. * @public */ Object.defineProperty(exports, 'Types', { get: loadClass.bind(null, 'Types') }); /** * Load the given class. * @private */ function loadClass(className) { var Class = Classes[className]; if (Class !== undefined) { return Class; } // This uses a switch for static require analysis switch (className) { case 'Connection': Class = require('./lib/Connection'); break; case 'ConnectionConfig': Class = require('./lib/ConnectionConfig'); break; case 'Pool': Class = require('./lib/Pool'); break; case 'PoolCluster': Class = require('./lib/PoolCluster'); break; case 'PoolConfig': Class = require('./lib/PoolConfig'); break; case 'SqlString': Class = require('./lib/protocol/SqlString'); break; case 'Types': Class = require('./lib/protocol/constants/types'); break; default: throw new Error('Cannot find class \'' + className + '\''); } // Store to prevent invoking require() Classes[className] = Class; return Class; }