node-query/lib/NodeQuery.js

64 lines
1.3 KiB
JavaScript
Executable File

'use strict';
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'],
]);
/**
* Class for connection management
*/
class NodeQuery {
/**
* Constructor
*
* @param {object} config - connection paramaters
* @constructor
*/
constructor(config) {
this.instance = null;
if (config != null) {
let drivername = dbDriverMap.get(config.driver);
if (! drivername) {
throw new Error(`Selected driver (${config.driver}) does not exist!`);
}
let driver = require(`./drivers/${drivername}`);
let $adapter = require(`./adapters/${drivername}`);
let adapter = new $adapter(config.connection);
this.instance = new QueryBuilder(driver, adapter);
}
}
/**
* Return an existing query builder instance
*
* @return {QueryBuilder} - The Query Builder object
*/
getQuery() {
if (this.instance == null) {
throw new Error('No Query Builder instance to return');
}
return this.instance;
}
}
module.exports = (config => new NodeQuery(config));