cypher-stream
Advanced tools
Comparing version 0.1.9 to 0.1.10
40
index.js
var oboe = require('oboe'); | ||
var Readable = require('stream').Readable; | ||
var util = require('util'); | ||
var urlParser = require('url'); | ||
@@ -27,21 +28,35 @@ util.inherits(CypherStream, Readable); | ||
function CypherStream (url, query, params) { | ||
function CypherStream (url, statement, parameters) { | ||
Readable.call(this, { objectMode: true }); | ||
var columns; | ||
var stream = this; | ||
var headers = { | ||
"X-Stream": true, | ||
"Accept": "application/json", | ||
}; | ||
var parsedUrl = urlParser.parse(url); | ||
//add HTTP basic auth if needed | ||
if(parsedUrl.auth) { | ||
headers['Authorization'] = 'Basic ' + | ||
new Buffer(parsedUrl.auth).toString('base64'); | ||
} | ||
if (url[url.length - 1] !== '/') { | ||
url += '/'; // ensure trailing slash | ||
} | ||
oboe({ | ||
url : url+'db/data/cypher', | ||
url : url+'db/data/transaction/commit', | ||
method : 'POST', | ||
headers : headers, | ||
headers : { "X-Stream": true, "Accept": "application/json" }, | ||
body : { query: query, params: params } | ||
body : { statements: [ { statement: statement, parameters: parameters} ] } | ||
}) | ||
.node('!columns', function CypherStreamNodeColumns(c) { | ||
.node('!results[*].columns', function CypherStreamNodeColumns(c) { | ||
stream.emit('columns', c); | ||
columns = c; | ||
this.forget(); | ||
}) | ||
.node('!data[*]', function CypherStreamNodeData(result, path, ancestors) { | ||
.node('!results[*].data[*].row', function CypherStreamNodeData(result, path, ancestors) { | ||
var data = {}; | ||
@@ -56,2 +71,11 @@ columns.forEach(function (column, i) { | ||
}) | ||
.node('!errors[*]', function (error, path, ancestors){ | ||
var message = "Query Failure"; | ||
if(error.message) { | ||
message += ": " + error.message; | ||
} | ||
var err = new Error(message); | ||
err.code = error.code; | ||
stream.emit('error', err); | ||
}) | ||
.fail(function CypherStreamHandleError(error) { | ||
@@ -62,4 +86,4 @@ // handle non-neo4j errors | ||
var err = error.thrown || new Error('Neo4j ' + error.statusCode); | ||
err.statusCode = error.statusCode | ||
err.body = error.body | ||
err.statusCode = error.statusCode; | ||
err.body = error.body; | ||
err.jsonBody = error.jsonBody; | ||
@@ -66,0 +90,0 @@ stream.emit('error', err); |
{ | ||
"name": "cypher-stream", | ||
"version": "0.1.9", | ||
"version": "0.1.10", | ||
"description": "Streams cypher query results in a clean format", | ||
@@ -25,3 +25,3 @@ "main": "index.js", | ||
"dependencies": { | ||
"oboe": "~1.15.0" | ||
"oboe": "~2.0.2" | ||
}, | ||
@@ -28,0 +28,0 @@ "devDependencies": { |
@@ -0,0 +0,0 @@ # cypher-stream [](https://travis-ci.org/brian-gates/cypher-stream) [](http://badge.fury.io/js/cypher-stream) [](https://david-dm.org/brian-gates/cypher-stream.png#info=devDependencies) |
@@ -26,3 +26,3 @@ var should = require('should'); | ||
cypher('match (n:Test) return n limit 10') | ||
.on('data', function (result){ | ||
.on('data', function (result) { | ||
results++; | ||
@@ -45,6 +45,7 @@ result.should.eql({ n: { test: true } }); | ||
errored = true; | ||
String(error).should.equal('Error: Query failure: Invalid input \'i\': expected <init> (line 1, column 1)\n"invalid query"\n ^'); | ||
error.neo4j.exception.should.equal('SyntaxException'); | ||
error.neo4j.stacktrace.should.be.an.array; | ||
error.neo4j.statusCode.should.equal(400); | ||
String(error).should.equal('Error: Query Failure: Invalid input \'i\': expected <init> (line 1, column 1)\n"invalid query"\n ^'); | ||
// these aren't returned the same in transaction mode. | ||
// error.neo4j.exception.should.equal('SyntaxException'); | ||
// error.neo4j.stacktrace.should.be.an.array; | ||
// error.neo4j.statusCode.should.equal(400); | ||
}) | ||
@@ -138,2 +139,11 @@ .on('end', function() { | ||
it('works with basic http auth', function (done){ | ||
var cyp = require('../index')('http://neo:cypher@localhost:7474/'); | ||
cyp('match (n:Test) return n limit 1') | ||
.on('error', shouldNotError) | ||
.on('end', done) | ||
.resume() | ||
; | ||
}); | ||
}); |
Sorry, the diff of this file is not supported yet
21449
237
+ Addedoboe@2.0.3(transitive)
- Removedoboe@1.15.2(transitive)
Updatedoboe@~2.0.2