2015-12-07 15:58:31 -05:00
|
|
|
'use strict';
|
2015-12-02 13:01:31 -05:00
|
|
|
|
2016-03-11 10:41:04 -05:00
|
|
|
const fs = require('fs');
|
|
|
|
const helpers = require('./helpers');
|
|
|
|
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'],
|
|
|
|
['sqlite', 'Sqlite'],
|
|
|
|
]);
|
2015-12-02 13:01:31 -05:00
|
|
|
|
|
|
|
/**
|
2016-01-26 19:29:12 -05:00
|
|
|
* Class for connection management
|
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-11 10:41:04 -05:00
|
|
|
* @param {object} config - connection paramaters
|
2016-01-26 19:29:12 -05:00
|
|
|
* @constructor
|
2015-12-03 20:43:42 -05:00
|
|
|
*/
|
2016-03-11 10:41:04 -05:00
|
|
|
constructor(config) {
|
2015-12-02 13:01:31 -05:00
|
|
|
this.instance = null;
|
2016-03-11 10:41:04 -05:00
|
|
|
|
|
|
|
if (config != null) {
|
|
|
|
let drivername = dbDriverMap.get(config.driver);
|
|
|
|
let driver = require(`./drivers/${drivername}`);
|
|
|
|
let $adapter = require(`./adapters/${drivername}`);
|
|
|
|
|
|
|
|
let adapter = new $adapter(config.connection);
|
|
|
|
this.instance = new QueryBuilder(driver, adapter);
|
|
|
|
}
|
2015-12-02 13:01:31 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2015-12-03 20:43:42 -05:00
|
|
|
* Create a query builder object
|
|
|
|
*
|
|
|
|
* @param {String} driverType - The name of the database type, eg. mysql or pg
|
2016-03-09 15:05:38 -05:00
|
|
|
* @param {Object} connObject - A connection object from the database library
|
|
|
|
* you are connecting with
|
|
|
|
* @param {String} [connLib] - The name of the db connection library you are
|
|
|
|
* using, eg. mysql or mysql2. Optional if the same as driverType
|
2015-12-03 20:43:42 -05:00
|
|
|
* @return {QueryBuilder} - The Query Builder object
|
|
|
|
*/
|
|
|
|
init(driverType, connObject, connLib) {
|
|
|
|
connLib = connLib || driverType;
|
|
|
|
|
|
|
|
let paths = {
|
2015-12-07 12:03:42 -05:00
|
|
|
driver: `${__dirname}/drivers/${helpers.upperCaseFirst(driverType)}`,
|
2015-12-07 15:58:31 -05:00
|
|
|
adapter: `${__dirname}/adapters/${connLib}`,
|
2015-12-03 20:43:42 -05:00
|
|
|
};
|
|
|
|
|
|
|
|
Object.keys(paths).forEach(type => {
|
2015-12-07 12:03:42 -05:00
|
|
|
try {
|
|
|
|
fs.statSync(`${paths[type]}.js`);
|
2015-12-07 15:58:31 -05:00
|
|
|
} catch (e) {
|
2015-12-03 20:43:42 -05:00
|
|
|
throw new Error(
|
2015-12-07 12:03:42 -05:00
|
|
|
`Selected ${type} (${helpers.upperCaseFirst(driverType)}) does not exist!`
|
2015-12-03 20:43:42 -05:00
|
|
|
);
|
|
|
|
}
|
|
|
|
});
|
2015-12-02 13:01:31 -05:00
|
|
|
|
2015-12-03 20:43:42 -05:00
|
|
|
let driver = require(paths.driver);
|
|
|
|
let $adapter = require(paths.adapter);
|
|
|
|
let adapter = new $adapter(connObject);
|
2015-12-02 13:01:31 -05:00
|
|
|
|
2015-12-03 20:43:42 -05:00
|
|
|
this.instance = new QueryBuilder(driver, adapter);
|
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
|
|
|
|
2015-12-03 20:43:42 -05:00
|
|
|
/**
|
|
|
|
* Return an existing query builder instance
|
|
|
|
*
|
|
|
|
* @return {QueryBuilder} - The Query Builder object
|
|
|
|
*/
|
|
|
|
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-03-11 10:41:04 -05:00
|
|
|
module.exports = (config => {
|
|
|
|
return new NodeQuery(config);
|
|
|
|
});
|