npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

oracledbexec

v1.6.0

Published

Running Oracle queries made easier

Downloads

138

Readme

oracledbexec

npm license

Running Oracle queries made easier.

Install

npm install oracledbexec --save

Variables

This module will read seven environment variables. If it doesn't find the related environment variable it will read the default value. Or you can pass database configuration parameters when initializing the module.

  • ORA_USR: the database user name. (default: hr)
  • ORA_PWD: the password of the database user. (default: hr)
  • ORA_CONSTR: connection string <host>:<port>/<service name>. (default: localhost:1521/XEPDB1)
  • POOL_MIN: the number of connections initially created. (default: 10)
  • POOL_MAX: the maximum number of connections. (default: 10)
  • POOL_INCREMENT: the number of connections that are opened whenever a connection request exceeds the number of currently open connections. (default: 0)
  • POOL_ALIAS: is used to explicitly add pools to the connection pool cache. (default: default)
  • POOL_PING_INTERVAL: check aliveness of connection if idle in the pool in seconds. (default: 60)
  • QUEUE_MAX: the maximum getConnection() calls in the pool queue. (default: 500)
  • QUEUE_TIMEOUT: terminate getConnection() calls queued for longer than 60000 milliseconds (default: 60000)

Usage

Initialize database in index.js/app.js file, to create connection pool and cache it.

const oracledbexec = require('oracledbexec')

oracledbexec.initialize()

Or pass database configuration parameters.

const oracledbexec = require('oracledbexec')

let dbconfig = {
    user: 'hr',
    password: 'hr',
    connectString: 'localhost:1521/XEPDB1',
    poolMin: 10,
    poolMax: 10,
    poolIncrement: 0,
    poolAlias: 'default',
    poolPingInterval: 60,
    queueMax: 500,
    queueTimeout: 60000,
}
oracledbexec.initialize(dbconfig)

Once initialized, you can use the main function of this module. The following is an example of executing a query statement:

const { oraexec, oraexectrans } = require('oracledbexec')

try {
    let sql = `SELECT * FROM countries WHERE country_id = :country_id`
    let param = {country_id: 'JP'}
    let result = await oraexec(sql, param)
    console.log(result.rows)
} catch (err) {
    console.log(err.message)
}

If you want to call a specific pool, you can pass the pool alias parameter behind.

let result = await oraexec(sql, param, 'hrpool')

For many sql statements, use the transaction function oraexectrans, so that if one sql statement fails, it will rollback.

const { oraexec, oraexectrans } = require('oracledbexec')

try {
    let queries = []
    queries.push({query: `INSERT INTO countries VALUES (:country_id, :country_name)`, parameters: {country_id: 'ID', country_name: 'Indonesia'}})
    queries.push({query: `INSERT INTO countries VALUES (:country_id, :country_name)`, parameters: {country_id: 'JP', country_name: 'Japan'}})
    queries.push({query: `INSERT INTO countries VALUES (:country_id, :country_name)`, parameters: {country_id: 'CN', country_name: 'China'}})
    await oraexectrans(queries)
} catch (err) {
    console.log(err.message)
}

Same as oraexec, you can pass the pool alias parameter behind.

let result = await oraexectrans(queries, 'hrpool')

In the above explanation, all sessions are created automatically. we simply call the method for query execution. If you want to handle separate sessions, follow these instructions.

If you to handle handle transactions that require a pause to retrieve variables from other tables. So, we have to manually create a new session for execution of transaction queries.

Be careful, if you use this method, don't forget to close the session under any circumstances or an orphan session occurs.

The sequence is to create a session, execute the query, and commit the session. here's an example:

const { begintrans, exectrans, committrans, rollbacktrans } = require('oracledbexec')

try {
    let session = begintrans()

    let sql = `SELECT country_name FROM countries WHERE country_id=:country_id`
    let param = {country_id: 'ID'}
    let result = await exectrans(session, sql, param)

    sql = `INSERT INTO sometable VALUES (:name, :country_name)`
    param = {name: 'Some Name', country_name: results.rows[0].country_name}
    await exectrans(session, sql, param)

    await committrans(session)
} catch (err) {
    await rollbacktrans(session)
    console.log(err.message)
}

Of course, you can pass the pool alias parameter when create a session.

let result = await begintrans('hrpool')

That's all.

If you find this useful, please ⭐ the repository. Any feedback is welcome.

You can contribute or you want to, feel free to Buy me a coffee! :coffee:, I will be really thankfull for anything even if it is a coffee or just a kind comment towards my work, because that helps me a lot.

License

MIT