2014-10-08 19:44:14 -04:00
|
|
|
"use strict";
|
|
|
|
|
2014-10-23 10:53:16 -04:00
|
|
|
/** @module node-query */
|
2014-11-05 17:08:56 -05:00
|
|
|
var NodeQuery = function() {
|
|
|
|
|
|
|
|
var instance = null;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Create a query builder object
|
|
|
|
*
|
|
|
|
* @alias module:node-query
|
|
|
|
* @param {String} drivername - The name of the database type, eg. mysql or pg
|
|
|
|
* @param {Object} connObject - A connection object from the database library you are connecting with
|
2014-12-01 14:08:10 -05:00
|
|
|
* @param {String} [connLib] - The name of the db connection library you are using, eg. mysql or mysql2. Optional if the same as drivername
|
2014-11-05 17:08:56 -05:00
|
|
|
* @return {queryBuilder}
|
|
|
|
*/
|
|
|
|
this.init = function (driverType, connObject, connLib) {
|
2014-12-01 14:08:10 -05:00
|
|
|
connLib = connLib || driverType;
|
|
|
|
|
2014-11-05 17:08:56 -05:00
|
|
|
var fs = require('fs'),
|
|
|
|
qb = require('./query-builder');
|
|
|
|
|
|
|
|
var paths = {
|
|
|
|
driver: __dirname + '/drivers/' + driverType + '.js',
|
|
|
|
adapter: __dirname + '/adapters/' + connLib + '.js'
|
|
|
|
};
|
|
|
|
|
|
|
|
Object.keys(paths).forEach(function(type) {
|
|
|
|
if ( ! fs.existsSync(paths[type]))
|
|
|
|
{
|
|
|
|
console.log(paths[type]);
|
|
|
|
throw new Error('Selected ' + type + ' does not exist!');
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
instance = qb(require(paths.driver), require(paths.adapter)(connObject));
|
|
|
|
|
|
|
|
return instance;
|
2014-10-20 16:56:45 -04:00
|
|
|
};
|
2014-10-23 10:53:16 -04:00
|
|
|
|
2014-11-05 17:08:56 -05:00
|
|
|
/**
|
|
|
|
* Return an existing query builder instance
|
|
|
|
*
|
|
|
|
* @return {queryBuilder}
|
|
|
|
*/
|
|
|
|
this.getQuery = function () {
|
|
|
|
if ( ! instance) {
|
|
|
|
throw new Error("No Query Builder instance to return");
|
2014-10-23 10:53:16 -04:00
|
|
|
}
|
|
|
|
|
2014-11-05 17:08:56 -05:00
|
|
|
return instance;
|
|
|
|
};
|
2014-11-03 10:59:18 -05:00
|
|
|
|
2014-10-23 10:53:16 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
|
2014-11-05 17:08:56 -05:00
|
|
|
|
|
|
|
module.exports = new NodeQuery();
|