Comparing version 2.7.0 to 2.8.0
@@ -5,2 +5,3 @@ var optionalRequire = require('./optionalRequire'); | ||
var _ = require('underscore'); | ||
var urlUtils = require('url'); | ||
@@ -10,2 +11,3 @@ module.exports = function () { | ||
return { | ||
@@ -40,18 +42,25 @@ query: function (query, params, options) { | ||
connect: function (config) { | ||
connect: function (_config) { | ||
var self = this; | ||
var config = _config.url? parseUrl(_config.url): _config.config; | ||
if (config.config.options) { | ||
Object.keys(config.config.options).forEach(function (key) { | ||
oracledb[key] = config.config.options[key]; | ||
if (config.options) { | ||
Object.keys(config.options).forEach(function (key) { | ||
oracledb[key] = config.options[key]; | ||
}); | ||
} | ||
return promisify(function (cb) { | ||
if (config.config.pool) { | ||
config.config.pool.getConnection(cb); | ||
function makeConnection() { | ||
if (config.pool === true) { | ||
return self.connectionPool(config).then(pool => { | ||
return promisify(cb => pool.getConnection(cb)); | ||
}); | ||
} else if (config.pool) { | ||
return promisify(cb => config.pool.getConnection(cb)); | ||
} else { | ||
oracledb.getConnection(config.config, cb); | ||
return promisify(cb => oracledb.getConnection(config, cb)); | ||
} | ||
}).then(function (connection) { | ||
} | ||
return makeConnection().then(function (connection) { | ||
self.connection = connection; | ||
@@ -61,2 +70,18 @@ }); | ||
connectionPoolCache: {}, | ||
connectionPool: function(config) { | ||
var key = JSON.stringify(config); | ||
var value = this.connectionPoolCache[key]; | ||
if (!value) { | ||
value = this.connectionPoolCache[key] = promisify(function (cb) { | ||
oracledb.createPool(config, cb); | ||
}); | ||
} | ||
return value; | ||
}, | ||
close: function () { | ||
@@ -118,1 +143,42 @@ var self = this; | ||
} | ||
function parseValue(value) { | ||
var number = Number(value); | ||
if (!isNaN(number)) { | ||
return number; | ||
} | ||
if (value == 'true' || value == 'false') { | ||
return value == 'true'; | ||
} | ||
return value; | ||
} | ||
function parseOptions(options) { | ||
var result = {}; | ||
Object.keys(options).forEach(key => { | ||
result[key] = parseValue(options[key]); | ||
}); | ||
return result; | ||
} | ||
function parseUrl(url) { | ||
var u = urlUtils.parse(url, true); | ||
var auth = u.auth? u.auth.split(':'): []; | ||
var options = parseOptions(u.query); | ||
var pool = options.pool; | ||
delete options.pool; | ||
return { | ||
user: auth[0], | ||
password: auth[1], | ||
connectString: u.host + u.pathname, | ||
pool: pool, | ||
options: options | ||
}; | ||
} |
{ | ||
"name": "sworm", | ||
"version": "2.7.0", | ||
"version": "2.8.0", | ||
"description": "a lightweight write-only ORM for MSSQL, MySQL, PostgreSQL, Oracle, Sqlite 3", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -239,3 +239,5 @@ # SWORM [![npm version](https://img.shields.io/npm/v/sworm.svg)](https://www.npmjs.com/package/sworm) [![npm](https://img.shields.io/npm/dm/sworm.svg)](https://www.npmjs.com/package/sworm) [![Build Status](https://travis-ci.org/featurist/sworm.svg?branch=master)](https://travis-ci.org/featurist/sworm) | ||
* `url` a connection URL passed to the postgres driver. See the [`pg` url format](https://github.com/brianc/node-postgres/wiki/pg#connectstring-connectionstring-function-callback). | ||
* `url` a connection URL, the following are supported | ||
* `pg` - `postgres://user:password@host:5432/database`. See the [`pg` url format](https://github.com/brianc/node-postgres/wiki/pg#connectstring-connectionstring-function-callback). | ||
* `oracle` - `oracle://user:password@host:1521/sid` | ||
* `setupSession` a function that is passed the `db` to setup the session before any queries are run. | ||
@@ -242,0 +244,0 @@ |
var urlUtils = require('url'); | ||
var os = require('os'); | ||
@@ -7,3 +8,7 @@ if (process.env.DOCKER_HOST) { | ||
} else { | ||
module.exports = 'localhost'; | ||
if (/darwin/i.test(os.type())) { | ||
module.exports = '192.168.64.2'; | ||
} else { | ||
module.exports = 'localhost'; | ||
} | ||
} |
@@ -7,2 +7,4 @@ if (!process.env.TRAVIS) { | ||
var expect = require('chai').expect; | ||
var _ = require('underscore'); | ||
var addUrlParams = require('./addUrlParams'); | ||
@@ -101,11 +103,79 @@ var database = { | ||
var config = { | ||
driver: "oracle", | ||
config: { user: "system", password: "oracle", connectString: `${dockerHostname}/XE` } | ||
}; | ||
function urlConfig(options) { | ||
return { | ||
driver: 'oracle', | ||
url: addUrlParams(`oracle://system:oracle@${dockerHostname}:1521/XE`, options) | ||
}; | ||
} | ||
describeDatabase("oracle", config, database, function () { | ||
function config(options) { | ||
return { | ||
driver: "oracle", | ||
config: _.extend({ | ||
user: "system", | ||
password: "oracle", | ||
connectString: `${dockerHostname}:1521/XE` | ||
}, options) | ||
}; | ||
} | ||
describeDatabase("oracle", config(), database, function () { | ||
describe('connection pooling', function () { | ||
var db; | ||
afterEach(function () { | ||
return db.close(); | ||
}); | ||
function numberOfPools() { | ||
return db.driver? Object.keys(db.driver.connectionPoolCache).length: 0; | ||
} | ||
it("doesn't pool connections normally", function () { | ||
db = sworm.db(urlConfig()); | ||
var poolsBefore = numberOfPools(); | ||
return db.query('select * from people').then((rows) => { | ||
expect(rows).to.eql([]); | ||
expect(Object.keys(numberOfPools()).length).to.equal(poolsBefore); | ||
}); | ||
}); | ||
it("pools connections when pool: true", function () { | ||
db = sworm.db(config({pool: true})); | ||
var poolsBefore = numberOfPools(); | ||
return db.query('select * from people').then((rows) => { | ||
expect(rows).to.eql([]); | ||
expect(numberOfPools()).to.equal(poolsBefore + 1); | ||
}); | ||
}); | ||
it("pools connections when &pool=true", function () { | ||
db = sworm.db(urlConfig({pool: true})); | ||
var poolsBefore = numberOfPools(); | ||
return db.query('select * from people').then((rows) => { | ||
expect(rows).to.eql([]); | ||
expect(numberOfPools()).to.equal(poolsBefore + 1); | ||
}); | ||
}); | ||
}); | ||
describe('connection options', function () { | ||
var db; | ||
it('can pass connection options', function () { | ||
oracledb.maxRows = 100; | ||
db = sworm.db(urlConfig({maxRows: 100000})); | ||
return db.connect().then(() => { | ||
expect(oracledb.maxRows).to.equal(100000); | ||
}); | ||
}); | ||
afterEach(function () { | ||
return db.close(); | ||
}); | ||
}); | ||
describe('options', function () { | ||
it('can pass options to query', function () { | ||
var db = sworm.db(config); | ||
var db = sworm.db(config()); | ||
var person = db.model({table: 'people'}); | ||
@@ -112,0 +182,0 @@ |
@@ -6,11 +6,5 @@ var dockerHostname = require('./dockerHostname'); | ||
var expect = require('chai').expect; | ||
var urlUtils = require('url'); | ||
var pg = require('pg'); | ||
var addUrlParams = require('./addUrlParams'); | ||
function addExtras(url, extras) { | ||
var parsedUrl = urlUtils.parse(url, true); | ||
_.extend(parsedUrl.query, extras); | ||
return urlUtils.format(parsedUrl); | ||
} | ||
function urlConfig(name, extras) { | ||
@@ -21,3 +15,3 @@ name = name || ''; | ||
if (extras) { | ||
url = addExtras(url, extras); | ||
url = addUrlParams(url, extras); | ||
} | ||
@@ -24,0 +18,0 @@ |
91710
25
2245
613