2016-03-11 10:41:04 -05:00
|
|
|
const QueryBuilder = require('./QueryBuilder');
|
|
|
|
|
|
|
|
// Map config driver name to code class name
|
|
|
|
const dbDriverMap = new Map([
|
|
|
|
['my', 'Mysql'],
|
|
|
|
['mysql', 'Mysql'],
|
|
|
|
['maria', 'Mysql'],
|
|
|
|
['mariadb', 'Mysql'],
|
|
|
|
['firebird', 'Firebird'],
|
|
|
|
['postgresql', 'Pg'],
|
|
|
|
['postgres', 'Pg'],
|
|
|
|
['pg', 'Pg'],
|
|
|
|
['sqlite3', 'Sqlite'],
|
2016-11-17 21:30:41 -05:00
|
|
|
['sqlite', 'Sqlite'],
|
|
|
|
['sqlserver', 'MSSQLServer'],
|
|
|
|
['mssql', 'MSSQLServer']
|
2016-03-11 10:41:04 -05:00
|
|
|
]);
|
2015-12-02 13:01:31 -05:00
|
|
|
|
|
|
|
/**
|
2016-01-26 19:29:12 -05:00
|
|
|
* Class for connection management
|
2016-03-15 15:37:24 -04:00
|
|
|
*
|
|
|
|
* @param {object} config - connection parameters
|
2015-12-02 13:01:31 -05:00
|
|
|
*/
|
2015-12-03 20:43:42 -05:00
|
|
|
class NodeQuery {
|
2015-12-02 13:01:31 -05:00
|
|
|
/**
|
2015-12-03 20:43:42 -05:00
|
|
|
* Constructor
|
|
|
|
*
|
2016-03-15 15:37:24 -04:00
|
|
|
* @param {object} config - connection parameters
|
2016-11-22 18:26:43 -05:00
|
|
|
* @param {string} config.driver - the database driver to use, such as mysql, sqlite, mssql, or pgsql
|
|
|
|
* @param {object|string} config.connection - the connection options for the database
|
|
|
|
* @param {string} config.connection.host - the ip or hostname of the database server
|
|
|
|
* @param {string} config.connection.user - the user to log in as
|
|
|
|
* @param {string} config.connection.password - the password to log in with
|
|
|
|
* @param {string} config.connection.database - the name of the database to connect to
|
2016-03-15 15:37:24 -04:00
|
|
|
* @example let nodeQuery = require('ci-node-query')({
|
|
|
|
* driver: 'mysql',
|
|
|
|
* connection: {
|
|
|
|
* host: 'localhost',
|
|
|
|
* user: 'root',
|
|
|
|
* password: '',
|
|
|
|
* database: 'mysql'
|
|
|
|
* }
|
|
|
|
* });
|
|
|
|
* @example let nodeQuery = require('ci-node-query')({
|
|
|
|
* driver: 'sqlite',
|
|
|
|
* connection: ':memory:'
|
|
|
|
* });
|
2015-12-03 20:43:42 -05:00
|
|
|
*/
|
2016-09-14 16:50:32 -04:00
|
|
|
constructor (config) {
|
2015-12-02 13:01:31 -05:00
|
|
|
this.instance = null;
|
2016-03-11 10:41:04 -05:00
|
|
|
|
2016-11-17 21:30:41 -05:00
|
|
|
if (config !== undefined) {
|
2018-02-09 17:29:26 -05:00
|
|
|
const driverName = dbDriverMap.get(config.driver);
|
2016-03-11 16:32:38 -05:00
|
|
|
|
2018-02-09 17:29:26 -05:00
|
|
|
if (!driverName) {
|
2016-03-11 16:32:38 -05:00
|
|
|
throw new Error(`Selected driver (${config.driver}) does not exist!`);
|
|
|
|
}
|
|
|
|
|
2018-02-09 17:29:26 -05:00
|
|
|
const driver = require(`./drivers/${driverName}`);
|
|
|
|
const Adapter = require(`./adapters/${driverName}`);
|
2016-03-11 10:41:04 -05:00
|
|
|
|
2018-02-09 17:29:26 -05:00
|
|
|
this.instance = new QueryBuilder(driver, Adapter(config));
|
2016-03-11 10:41:04 -05:00
|
|
|
}
|
2015-12-02 13:01:31 -05:00
|
|
|
}
|
|
|
|
|
2015-12-03 20:43:42 -05:00
|
|
|
/**
|
|
|
|
* Return an existing query builder instance
|
|
|
|
*
|
|
|
|
* @return {QueryBuilder} - The Query Builder object
|
|
|
|
*/
|
2016-09-14 16:50:32 -04:00
|
|
|
getQuery () {
|
2015-12-07 12:03:42 -05:00
|
|
|
if (this.instance == null) {
|
2015-12-07 15:58:31 -05:00
|
|
|
throw new Error('No Query Builder instance to return');
|
2015-12-02 13:01:31 -05:00
|
|
|
}
|
|
|
|
|
2015-12-03 20:43:42 -05:00
|
|
|
return this.instance;
|
|
|
|
}
|
|
|
|
}
|
2015-12-02 13:01:31 -05:00
|
|
|
|
2016-09-14 16:50:32 -04:00
|
|
|
module.exports = config => new NodeQuery(config);
|