
Security News
Crates.io Implements Trusted Publishing Support
Crates.io adds Trusted Publishing support, enabling secure GitHub Actions-based crate releases without long-lived API tokens.
@zarcobox/oracle-object
Advanced tools
Turns oracledb execute result into javascript objects
Oracle's npm module oracledb
returns an atypical response in which columns are displayed as a meta-data object separate from the rows. This module helps programmers by turning those into more natural key-valued javascript objects.
The latest version is available at: https://www.npmjs.com/package/@zarcobox/oracle-object
Use your favorite package manager to install. For instance:
yarn add @zarcobox/oracle-object
Then import it:
import oracle-object from '@zarcobox/oracle-object'
Or, for commonjs:
const oracle-object = require('@zarcobox/oracle-object')
And you're good to go!
oracleObject(result, fn, options)
The function receives the result of an execution as the first parameter. This is the only required parameter.
Here's an exemple of an SQL execution with oracledb:
// ...
const result = await connection.execute(
`SELECT manager_id, department_id, department_name
FROM departments
WHERE manager_id = :id`,
[23],
)
This is an example of a normal result from oracledb
:
const result = {
metaData: [
{ name: 'manager_id' },
{ name: 'department_id'},
{ name: 'department_name'}
],
rows: [
['23', '3842', 'Finances'],
['23', '3984', 'Sales'],
// ...
]
}
The simplest usage of oracle-object
would be:
oracleObject(result)
Which returns:
[
{
manager_id: '23',
department_id: '3842',
department_name: 'Finances'
},
{
manager_id: '23',
department_id: '3984',
department_name: 'Sales'
}
]
The second argument is an optional function or an object which receives each column and modifies it. In the example below the function makes the object keys have the same name as the columns but lower cased.
const registers = oracleObject(result, col => col.toLowerCase())
Or, given an example result where CD_PATIENT and DS_NAME are actual database columns, if you want to modify the name of each column you could do:
const patients = oracleObject(result, (col) => {
switch(col) {
case 'CD_PATIENT': return 'code'
case 'DS_NAME': return 'name'
default: return col
}
}, { allowNull: true })
An equivalent would be:
const patients = oracleObject(result, {
'CD_PATIENT': 'code'
'DS_NAME': 'name'
}
}, { allowNull: true, defaultFn: col => col })
And the returned object will look like this:
patients = [
{
code: '129831',
name: 'Luiz Felipe'
// ...
},
{
code: '129831',
name: 'Larissa',
// ...
},
]
Note: the default return ensures the function does not limit the object keys to the ones in the cases.
const options = { defaultFn: col => 'not-defined-'+col }
const patients = oracleObject(result, {
'CD_PATIENT': 'code',
'DS_NAME': 'name'
}, options)
The third parameter of oracle-object
receives an options object.
The option allowNull: false
is set by default. If you want all props do not forget to add that option.
The option defaultFn
can be set when using objects instead of functions to apply that function to not specified (or default) column names.
Run the test suit with yarn test
.
If you want to contribute in any of theses ways:
You can (and should) open an issue or even a pull request!
Thanks for your interest in contributing to this repo!
Luiz Felipe Zarco (felipezarco@hotmail.com)
This code is licensed under the MIT License. See the LICENSE.md file for more info.
FAQs
Turns oracledb execute result into javascript objects
The npm package @zarcobox/oracle-object receives a total of 1 weekly downloads. As such, @zarcobox/oracle-object popularity was classified as not popular.
We found that @zarcobox/oracle-object demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Crates.io adds Trusted Publishing support, enabling secure GitHub Actions-based crate releases without long-lived API tokens.
Research
/Security News
Undocumented protestware found in 28 npm packages disrupts UI for Russian-language users visiting Russian and Belarusian domains.
Research
/Security News
North Korean threat actors deploy 67 malicious npm packages using the newly discovered XORIndex malware loader.