diff --git a/lib/NodeQuery.js b/lib/NodeQuery.js index e64d696..8af3938 100755 --- a/lib/NodeQuery.js +++ b/lib/NodeQuery.js @@ -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: { diff --git a/lib/adapters/Pg/PgNative.js b/lib/adapters/Pg/PgNative.js new file mode 100644 index 0000000..c341d26 --- /dev/null +++ b/lib/adapters/Pg/PgNative.js @@ -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; diff --git a/lib/adapters/Pg/index.js b/lib/adapters/Pg/index.js index c20cdb3..5a940e7 100644 --- a/lib/adapters/Pg/index.js +++ b/lib/adapters/Pg/index.js @@ -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); }; diff --git a/lib/adapters/Sqlite/index.js b/lib/adapters/Sqlite/index.js index b4c891c..b59bcf2 100644 --- a/lib/adapters/Sqlite/index.js +++ b/lib/adapters/Sqlite/index.js @@ -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); }; diff --git a/package.json b/package.json index 531487a..4336160 100644 --- a/package.json +++ b/package.json @@ -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\"", diff --git a/test/adapters/dblite_test.js b/test/adapters/dblite_test.js index 690fc3e..6a53f4e 100644 --- a/test/adapters/dblite_test.js +++ b/test/adapters/dblite_test.js @@ -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); diff --git a/test/adapters/sqlite3_test.js b/test/adapters/sqlite3_test.js index e86f1fc..a7cb8dd 100644 --- a/test/adapters/sqlite3_test.js +++ b/test/adapters/sqlite3_test.js @@ -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);