2015-12-02 13:01:31 -05:00
|
|
|
"use strict";
|
|
|
|
|
2015-12-03 20:43:42 -05:00
|
|
|
let instance = null;
|
|
|
|
let fs = require('fs'),
|
|
|
|
helpers = require('./helpers'),
|
|
|
|
QueryBuilder = require('./QueryBuilder');
|
2015-12-02 13:01:31 -05:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @module NodeQuery
|
|
|
|
*/
|
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
|
|
|
|
*
|
|
|
|
* @return {void}
|
|
|
|
*/
|
|
|
|
constructor() {
|
2015-12-02 13:01:31 -05:00
|
|
|
this.instance = null;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2015-12-03 20:43:42 -05:00
|
|
|
* Create a query builder object
|
|
|
|
*
|
|
|
|
* @memberOf NodeQuery
|
|
|
|
* @param {String} driverType - The name of the database type, eg. mysql or pg
|
|
|
|
* @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 drivername
|
|
|
|
* @return {QueryBuilder} - The Query Builder object
|
|
|
|
*/
|
|
|
|
init(driverType, connObject, connLib) {
|
|
|
|
connLib = connLib || driverType;
|
|
|
|
|
|
|
|
let paths = {
|
|
|
|
driver: `${__dirname}/drivers/` + helpers.upperCaseFirst(driverType),
|
|
|
|
adapter: `${__dirname}/adapters/${connLib}`
|
|
|
|
};
|
|
|
|
|
|
|
|
Object.keys(paths).forEach(type => {
|
|
|
|
if ( ! fs.existsSync(paths[type]))
|
|
|
|
{
|
|
|
|
throw new Error(
|
|
|
|
`Selected ${type} (` +
|
|
|
|
helpers.upperCaseFirst(driverType) +
|
|
|
|
`) does not exist!`
|
|
|
|
);
|
|
|
|
}
|
|
|
|
});
|
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() {
|
|
|
|
if ( ! this.instance) {
|
|
|
|
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
|
|
|
|
2015-12-03 20:43:42 -05:00
|
|
|
}
|
2015-12-02 13:01:31 -05:00
|
|
|
|
2015-12-03 20:43:42 -05:00
|
|
|
module.exports = new NodeQuery();
|