Created
January 17, 2021 18:40
-
-
Save gx0r/33a39853aad2777fd1924e4ffe6c4d00 to your computer and use it in GitHub Desktop.
Oracle query string example
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var oracle = require('strong-oracle')(); | |
// Or use the other Oracle driver | |
// var oracle = require('oracle'); | |
var OracleQueryStream = require('oracle-query-stream'); | |
var JSONStream = require('JSONStream'); | |
var db = require('./db'); | |
function pipeQueryToResponse(res, query, queryargs) { | |
var queryargs = queryargs || []; | |
oracle.connect(db, function(err, connection) { | |
if (err) { | |
res.status(500).send("" + err); | |
console.error(err); | |
return; | |
} | |
var reader = connection.reader(query, queryargs); | |
connection.setPrefetchRowCount(100); | |
var stream = new OracleQueryStream(reader); | |
stream.pipe(JSONStream.stringify('[', | |
',\n\n', | |
']')).pipe(res); | |
stream.on('end', function() { | |
console.log('closing connection'); | |
connection.close(); | |
}) | |
}); | |
} | |
var app = require('express')(); | |
app.get('/', function(req, res) { | |
res.setHeader("Content-Type", "application/json"); | |
var query = | |
"SELECT * FROM (SELECT * FROM USERS ORDER BY NAME ) WHERE ROWNUM <= 2000"; | |
pipeQueryToResponse(res, query); | |
}); | |
app.listen(3000); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment