Comparing version 4.1.0 to 4.2.0
48
index.js
@@ -19,3 +19,25 @@ var InfluxRequest = require('./lib/InfluxRequest.js') | ||
function parseOptionsUrl (url_) { | ||
var parsed = url.parse(url_) | ||
var options = { | ||
host: parsed.hostname, | ||
port: parsed.port, | ||
protocol: parsed.protocol | ||
} | ||
if (parsed.auth) { | ||
var authSplit = parsed.auth.split(':') | ||
if (authSplit.length !== 2) throw new Error('Invalid authentication: ' + parsed.auth) | ||
options.username = authSplit[0] | ||
options.password = authSplit[1] | ||
} | ||
if (parsed.pathname.length > 1) options.database = parsed.pathname.slice(1) | ||
return options | ||
} | ||
var InfluxDB = function (options) { | ||
if (typeof options === 'string') options = parseOptionsUrl(options) | ||
this.options = _.extend(_.clone(defaultOptions), options) | ||
@@ -35,2 +57,3 @@ | ||
_.each(this.options.hosts, function (host) { | ||
if (typeof host === 'string') host = parseOptionsUrl(host) | ||
var port = host.port || self.options.port | ||
@@ -252,22 +275,27 @@ var protocol = host.protocol || self.options.protocol | ||
InfluxDB.prototype._createKeyValueString = function (object) { | ||
return _.map(object, function (value, key) { | ||
var output = [] | ||
_.forOwn(object, function (value, key) { | ||
if (typeof value === 'string') { | ||
return key + '="' + value + '"' | ||
output.push(key + '="' + value + '"') | ||
} else { | ||
return key + '=' + value | ||
output.push(key + '=' + value) | ||
} | ||
}).join(',') | ||
}) | ||
return output.join(',') | ||
} | ||
InfluxDB.prototype._createKeyTagString = function (object) { | ||
return _.map(object, function (value, key) { | ||
var output = [] | ||
_.forOwn(object, function (value, key) { | ||
if (typeof value === 'string') { | ||
return key + '=' + value.replace(/ /g, '\\ ').replace(/,/g, '\\,') | ||
output.push(key + '=' + value.replace(/ /g, '\\ ').replace(/,/g, '\\,').replace(/=/g, '\\=')) | ||
} else { | ||
return key + '=' + value | ||
output.push(key + '=' + value) | ||
} | ||
}).join(',') | ||
}) | ||
return output.join(',') | ||
} | ||
InfluxDB.prototype._prepareValues = function (series) { | ||
var self = this | ||
var output = [] | ||
@@ -278,3 +306,3 @@ _.forEach(series, function (values, seriesName) { | ||
if (points[1] && _.isObject(points[1]) && _.keys(points[1]).length > 0) { | ||
line += ',' + this._createKeyTagString(points[1]) | ||
line += ',' + self._createKeyTagString(points[1]) | ||
} | ||
@@ -288,3 +316,3 @@ | ||
} | ||
line += ' ' + this._createKeyValueString(points[0]) | ||
line += ' ' + self._createKeyValueString(points[0]) | ||
if (timestamp) { | ||
@@ -291,0 +319,0 @@ if (timestamp instanceof Date) { |
{ | ||
"name": "influx", | ||
"version": "4.1.0", | ||
"version": "4.2.0", | ||
"description": "InfluxDB Client", | ||
@@ -29,3 +29,3 @@ "main": "index.js", | ||
"dependencies": { | ||
"lodash": "^3.10.0", | ||
"lodash": "^4.11.1", | ||
"request": "2.x" | ||
@@ -32,0 +32,0 @@ }, |
@@ -64,3 +64,18 @@ # node-influx | ||
You can also pass an URL or an array of URLs: | ||
```js | ||
var influx = require('influx') | ||
var client = influx('http://dbuser:f4ncyp4ass@localhost:8060/my_database') | ||
// or | ||
client = influx({ | ||
hosts: ['http://127.0.0.1', 'https://127.0.0.2'], | ||
username: 'dbuser', | ||
password: 'f4ncyp4ass', | ||
database: 'my_database' | ||
}) | ||
``` | ||
### Configuration options | ||
@@ -67,0 +82,0 @@ |
35
test.js
@@ -52,2 +52,33 @@ /* eslint-env mocha */ | ||
}) | ||
describe('configured using URLs', function () { | ||
it('should parse it when passed as `options`', function () { | ||
var urlClient = influx( | ||
'http://admin:fancierpassword@influx.foobar.com:1337/mydatabase' | ||
) | ||
assert.equal(urlClient.options.host, 'influx.foobar.com') | ||
assert.equal(urlClient.options.port, 1337) | ||
assert.equal(urlClient.options.username, 'admin') | ||
assert.equal(urlClient.options.password, 'fancierpassword') | ||
assert.equal(urlClient.options.database, 'mydatabase') | ||
}) | ||
it('should parse them when passed in `hosts`', function () { | ||
var urlClient = influx({ | ||
hosts: [ | ||
'http://127.0.0.1:1337', | ||
'https://127.0.0.2:1338' | ||
] | ||
}) | ||
var hostsParsed = urlClient.getHostsAvailable() | ||
assert.equal(hostsParsed[0].name, '127.0.0.1') | ||
assert.equal(hostsParsed[0].port, 1337) | ||
assert.equal(hostsParsed[0].protocol, 'http:') | ||
assert.equal(hostsParsed[1].name, '127.0.0.2') | ||
assert.equal(hostsParsed[1].port, 1338) | ||
assert.equal(hostsParsed[1].protocol, 'https:') | ||
}) | ||
}) | ||
}) | ||
@@ -317,2 +348,6 @@ | ||
}) | ||
it('should write a point that has "length" in its keys', function (done) { | ||
dbClient.writePoint(info.series.strName, {length: 3}, {length: '5'}, done) | ||
}) | ||
}) | ||
@@ -319,0 +354,0 @@ |
73248
15
1450
451
+ Addedlodash@4.17.21(transitive)
- Removedlodash@3.10.1(transitive)
Updatedlodash@^4.11.1