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 * Constructor
* *
* @param {object} config - connection parameters * @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')({ * @example let nodeQuery = require('ci-node-query')({
* driver: 'mysql', * driver: 'mysql',
* connection: { * 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'; 'use strict';
const Pg = require('./Pg'); const Pg = require('./Pg');
const PgNative = require('./PgNative')
module.exports = config => { 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'; 'use strict';
module.exports = config => { module.exports = config => {
const Implementation = (config.adapter && config.adapter === 'dblite') const Implementation = (config.native)
? require('./dblite') ? require('./sqlite3')
: require('./sqlite3'); : require('./dblite');
return new Implementation(config.connection); return new Implementation(config.connection);
}; };

View File

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

View File

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

View File

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