Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
async function dostuff() {
let connection;
try {
// Get a connection from the default pool
connection = await oracledb.getConnection();
const sql = `SELECT sysdate FROM dual WHERE :b = 1`;
const binds = [1];
const options = { outFormat: oracledb.OUT_FORMAT_OBJECT };
const result = await connection.execute(sql, binds, options);
console.log(result);
} catch (err) {
console.error(err);
} finally {
if (connection) {
try {
// Put the connection back in the pool
await connection.close();
} catch (err) {
console.error(err);
}
}
}
}
}
}
);
//
// Fetch an object back
//
console.log('\nQuerying:');
result = await connection.execute(
`SELECT id, farm FROM no_farmtab WHERE id = 1 `,
[],
// outFormat determines whether rows will be in arrays or JavaScript objects.
// It does not affect how the FARM column itself is represented.
{ outFormat: oracledb.OUT_FORMAT_OBJECT }
);
for (const row of result.rows) {
const farm = row.FARM; // a DbObject for the named Oracle type
console.log('\nFarm is:', farm); // the whole object
console.log('JSON', JSON.stringify(farm)); // Objects can be stringified
console.log('\nFarmer name', farm.FARMERNAME); // an attribute of the object
console.log('Harvest is:'); // iterate over the collection
for (const crop of farm.HARVEST) {
console.log(crop);
}
}
{ SHIRTNUMBER: 10, NAME: 'Alison' },
{ SHIRTNUMBER: 20, NAME: 'Bob' },
{ SHIRTNUMBER: 30, NAME: 'Charlie' },
{ SHIRTNUMBER: 40, NAME: 'Doug' }
],
type: TeamTypeClass // could also use 'type: "TEAMTYPE"'
}
});
// Query the new data back
let result = await connection.execute(
`SELECT sportname, team FROM no_sports`,
[],
{
outFormat: oracledb.OUT_FORMAT_OBJECT
}
);
for (const row of result.rows) {
console.log("The " + row.SPORTNAME + " team players are:");
for (const player of row.TEAM) {
console.log(" " + player.NAME);
}
// console.log(JSON.stringify(row.TEAM)); // can easily stringify
}
} catch (err) {
console.error(err);
} finally {
if (connection) {
try {
await connection.close();
`SELECT farmer, picked, ripeness
FROM no_banana_farmer
ORDER BY id`;
let result;
// Default Array Output Format
result = await connection.execute(sql);
console.log("----- Banana Farmers (default ARRAY output format) --------");
console.log(result.rows);
// Optional Object Output Format
result = await connection.execute(
sql,
{}, // A bind parameter is needed to disambiguate the following options parameter and avoid ORA-01036
{ outFormat: oracledb.OUT_FORMAT_OBJECT }); // outFormat can be OBJECT or ARRAY. The default is ARRAY
console.log("----- Banana Farmers (default OBJECT output format) --------");
console.log(result.rows);
} catch (err) {
console.error(err);
} finally {
if (connection) {
try {
// Connections should always be released when not needed
await connection.close();
} catch (err) {
console.error(err);
}
}
}
}
it('12.4.3 data in resultSet is object when setting outFormat OBJECT', function(done) {
connection.should.be.ok();
var accessCount = 0;
connection.execute(
"SELECT employees_name FROM nodb_rs1_emp",
[],
{ resultSet: true, fetchArraySize: 100, outFormat: oracledb.OUT_FORMAT_OBJECT },
function(err, result) {
should.not.exist(err);
fetchRowFromRS(result.resultSet);
}
);
function fetchRowFromRS(rs) {
rs.getRow(function(err, row) {
should.not.exist(err);
if(row) {
accessCount++;
row.EMPLOYEES_NAME.should.eql('staff ' + accessCount);
row.should.be.an.Object;
return fetchRowFromRS(rs);
} else {
function getResultSet(cb) {
connection.execute(
"SELECT * FROM " + tab + " ORDER BY num",
[],
{ resultSet: true, outFormat: oracledb.OUT_FORMAT_OBJECT },
function(err, result) {
should.not.exist(err);
rs2 = result.resultSet;
cb();
}
);
},
function test(cb) {
it('13.1.2 stream results for oracle connection (outFormat: oracledb.OUT_FORMAT_OBJECT)', function (done) {
var stream = connection.queryStream(
'SELECT employee_name FROM nodb_stream1 ORDER BY employee_name',
{},
{ outFormat: oracledb.OUT_FORMAT_OBJECT }
);
stream.on('error', function (error) {
should.fail(error, null, 'Error event should not be triggered');
});
var counter = 0;
stream.on('data', function (data) {
should.exist(data);
counter++;
});
stream.on('end', function () {
should.equal(counter, rowsAmount);
done();
});
function(cb) {
connection.execute(
"SELECT ID, B AS B1, B AS B2 from nodb_blob1 WHERE ID = " + id,
{ },
{
outFormat : oracledb.OUT_FORMAT_OBJECT,
resultSet : true
},
function(err, result) {
should.not.exist(err);
result.resultSet.getRow(
function(err, row) {
should.not.exist(err);
var resultVal = row.B1;
compareClientFetchResult(err, resultVal, specialStr, content, contentLength);
resultVal = row.B2;
compareClientFetchResult(err, resultVal, specialStr, content, contentLength);
result.resultSet.close(function(err) {
should.not.exist(err);
cb();
});
}
function(cb) {
connection.execute(
"SELECT ID, C from nodb_clob1 WHERE ID = :id",
{ id : id },
{
outFormat : oracledb.OUT_FORMAT_OBJECT,
resultSet : true
},
function(err, result) {
should.not.exist(err);
result.resultSet.getRow(
function(err, row) {
var resultVal;
resultVal = row.C;
if(specialStr === null) {
should.equal(resultVal, null);
} else {
compareClientFetchResult(err, resultVal, specialStr, insertContent, insertContentLength);
}
result.resultSet.close(function(err) {
should.not.exist(err);
cb();
describe('116.2 works with fetchInfo and outFormat = OBJECT', function() {
var maxRowBak = oracledb.maxRows;
var option = {
outFormat: oracledb.OUT_FORMAT_OBJECT,
fetchInfo: { "CONTENT": { type: oracledb.STRING } }
};
before(function(done) {
async.series([
function makeTable(callback) {
connection.execute(
proc_create_table,
function(err) {
should.not.exist(err);
callback();
});
},
function insertRow(callback) {
insertData(connection, tableName, callback);
},
function fillRowid(callback) {