Make 'navtive' a connection option to use a driver with native bindings

This commit is contained in:
Timothy Warren 2016-11-22 18:26:43 -05:00
parent 7f22eee84d
commit b54e69570f
7 changed files with 53 additions and 23 deletions

View File

@ -28,6 +28,12 @@ class NodeQuery {
* Constructor
*
* @param {object} config - connection parameters
* @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
* @example let nodeQuery = require('ci-node-query')({
* driver: 'mysql',
* connection: {

View File

@ -0,0 +1,28 @@
'use strict';
const Pg = require('./Pg');
const Result = require('../../Result');
const pg = require('pg').native;
const url = require('url');
class PgNative extends Pg {
constructor (config) {
super(config);
let instance = null;
let connectionString = Pg._formatConnectionString(config);
if (connectionString !== '') {
let conn = new pg.Client(connectionString);
conn.connect(err => {
if (err) {
throw new Error(err);
}
});
instance = Promise.resolve(conn);
}
super(instance);
}
}
module.exports = PgNative;

View File

@ -1,7 +1,10 @@
'use strict';
const Pg = require('./Pg');
const PgNative = require('./PgNative')
module.exports = config => {
return new Pg(config.connection);
return (config.native)
? new PgNative(config.connection)
: new Pg(config.connection);
};

View File

@ -1,9 +1,9 @@
'use strict';
module.exports = config => {
const Implementation = (config.adapter && config.adapter === 'dblite')
? require('./dblite')
: require('./sqlite3');
const Implementation = (config.native)
? require('./sqlite3')
: require('./dblite');
return new Implementation(config.connection);
};

View File

@ -23,12 +23,14 @@
"codeigniter",
"mariadb",
"mysql",
"mssql",
"query builder",
"postgres",
"postgresql",
"sql",
"sqlite",
"sqlite3"
"sqlite3",
"sqlserver"
],
"bugs": {
"url": "https://git.timshomepage.net/timw4mail/node-query/issues"
@ -43,6 +45,7 @@
"pg": "^6.0.0",
"require-reload": "~0.2.2",
"sqlite3": "^3.1.8",
"tedious": "^1.14.0",
"xregexp": "^3.0.0"
},
"devDependencies": {
@ -53,6 +56,7 @@
"globstar": "^1.0.0",
"happiness": "^7.1.2",
"istanbul": "~0.4.2",
"jsdoc": "^3.4.3",
"mocha": "^3.0.0",
"npm-run-all": "^3.0.0",
"nsp": "^2.2.1"
@ -65,6 +69,7 @@
"default": "npm-run-all --parallel audit lint:src lint:tests && npm run test",
"predocs": "globstar -- documentation build -f md -o API.md \"lib/*.js\"",
"docs": "globstar -- documentation build -f html -o docs \"lib/*.js\"",
"postdocs": "jsdoc lib -r -d documentation",
"happy": "happiness \"lib/**/*.js\" \"test/**/*.js\"",
"happy:src": "happiness \"lib/**/*.js\"",
"happy:tests": "happiness \"test/**/*.js\"",

View File

@ -17,15 +17,9 @@ let qb = nodeQuery.getQuery();
suite('Dblite adapter tests -', () => {
suiteSetup(done => {
// Set up the sqlite database
const createTest = 'CREATE TABLE IF NOT EXISTS "create_test" ("id" INTEGER PRIMARY KEY, "key" TEXT, "val" TEXT);';
const createJoin = 'CREATE TABLE IF NOT EXISTS "create_join" ("id" INTEGER PRIMARY KEY, "key" TEXT, "val" TEXT);';
qb.query(createTest)
.then(() => qb.query(createJoin))
.then(() => {
return done();
});
qb.queryFile(`${__dirname}/../sql/sqlite.sql`)
.then(() => done())
.catch(e => done(e));
});
testRunner(qb);

View File

@ -17,15 +17,9 @@ let qb = nodeQuery.getQuery();
suite('Sqlite3 adapter tests -', () => {
suiteSetup(done => {
// Set up the sqlite database
const createTest = 'CREATE TABLE IF NOT EXISTS "create_test" ("id" INTEGER PRIMARY KEY, "key" TEXT, "val" TEXT);';
const createJoin = 'CREATE TABLE IF NOT EXISTS "create_join" ("id" INTEGER PRIMARY KEY, "key" TEXT, "val" TEXT);';
qb.query(createTest)
.then(() => qb.query(createJoin))
.then(() => {
return done();
});
qb.queryFile(`${__dirname}/../sql/sqlite.sql`)
.then(() => done())
.catch(e => done(e));
});
testRunner(qb);