0.0.2 • Published 4 years ago

@zarcobox/oracle-object v0.0.2

Weekly downloads
-
License
MIT
Repository
github
Last release
4 years ago

Oracle Object

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.

License: MIT npm version Build Status Coverage Status Downloads

npm

Installation

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!

Usage

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.

Testing

Run the test suit with yarn test.

Contributing

If you want to contribute in any of theses ways:

  • Give your ideas or feedback
  • Question something
  • Point out a problem or issue
  • Enhance the code or its documentation
  • Help in any other way

You can (and should) open an issue or even a pull request!

Thanks for your interest in contributing to this repo!

Author

Luiz Felipe Zarco (felipezarco@hotmail.com)

License

This code is licensed under the MIT License. See the LICENSE.md file for more info.