Last active
April 16, 2020 05:12
-
-
Save mxriverlynn/26eaf4e144eea851af01 to your computer and use it in GitHub Desktop.
node-oracledb cursor handling
This file contains 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
// set up your oracle stuff ... | |
function runIt(cb){ | |
// connection and other setup here | |
var bindvars = { | |
o_cursor: { | |
type: oracledb.CURSOR, | |
dir : oracledb.BIND_OUT | |
} | |
}; | |
connection.execute("call schema.cool_proc", bindvars, function(err, out){ | |
// use your standard error handling, here | |
if (err) { throw err; } | |
// collection of data | |
var myCoolStuff = []; | |
// set up the cursor | |
var cursor = new Cursor(out.o_cursor); | |
// handle errors | |
cursor.on("error", (err) => {/* standard error handler here*/}); | |
// handle data coming through | |
// each data item is a single record | |
cursor.on("data", (row) => { | |
var coolThing = new CoolThing(row); | |
myCoolThings.push(coolThing); | |
}) | |
// read it and handle the end of the cursor | |
cursor.read(() => { | |
// all done so do stuff with myCoolThings array | |
cb(myCoolThings); | |
}); | |
}); | |
} | |
// do the whole thing and get the rows as an array of my object type | |
runIt((things) => { | |
// just loop and log the items for now | |
things.forEach((thing) => {console.log(thing); }); | |
}) |
This file contains 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
// this code will turn an OUT cursor from the node-oracledb library | |
// and turn the data into an array objects that has the structure of | |
// [{NAME: value, NAME_2: value, NAME_N: value}, ...] | |
// where the NAME is the column name returned through the cursor | |
// | |
// Distributed freely under MIT License <http://mutedsolutions.mit-license.org> | |
// Copyright 2016 Muted Solutions, LLC. All Rights Reserved. | |
const EventEmitter = require("events"); | |
const util = require("util"); | |
const _ = require("underscore"); | |
// Config | |
// ------ | |
const defaultNumRows = 500; | |
// Cursor | |
// ------ | |
function Cursor(o_cursor, options){ | |
EventEmitter.call(this); | |
this.options = _.defaults({}, options, { | |
numRows: defaultNumRows | |
}); | |
this.o_cursor = o_cursor; | |
this._configure(o_cursor.metaData); | |
} | |
util.inherits(Cursor, EventEmitter); | |
// Methods | |
// ------- | |
Cursor.prototype.read = function(cb){ | |
var numRows = this.options.numRows; | |
this._fetchRows(numRows, cb); | |
}; | |
Cursor.prototype.close = function(cb){ | |
this.o_cursor.close((err) => { | |
if (err) { return cb(err); } | |
return cb(); | |
}); | |
} | |
// Private API | |
// ----------- | |
Cursor.prototype._fetchRows = function(numRows, done){ | |
this.o_cursor.getRows(numRows, (err, rows) => { | |
if (err) { return this.emit("error", err); } | |
if (rows.length === 0) { | |
return this.close(done); | |
} | |
rows.forEach((row) => { | |
var rowData = this._getRowData(row); | |
this.emit("data", rowData); | |
}); | |
this._fetchRows(numRows, done); | |
}); | |
} | |
Cursor.prototype._configure = function(metaData){ | |
this._columnConfig = metaData; | |
}; | |
Cursor.prototype._getRowData = function(row){ | |
var rowData = {}; | |
row.forEach((dataPoint, index) => { | |
var rowConfig = this._columnConfig[index]; | |
rowData[rowConfig.name] = dataPoint; | |
}); | |
return rowData; | |
}; | |
// Exports | |
// ------- | |
module.exports = Cursor; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment