@vimla/oracle-client-http
v0.14.0
Published
Library for exposing / consuming Oracle DB over http
Downloads
2
Keywords
Readme
oracle-http-client
Install
npm install @vimla/oracle-client-http
Run in server mode
npm install oracledb
import Server, { IServerAttributes } from '@vimla/oracle-client-http/Server'
import { AddressInfo } from 'net'
process.env.NODE_ENV = 'production'
const config: IServerAttributes = {
pool: {
user: 'some-db-user',
password: 'some-db-password',
connectString: 'dbhost:1521/schema'
poolMax: 50 // maximum amount of simultaneous connections
},
healthCheckTable: 'users',
credentials: [
{ keyId: 'api', secret: 'api-secret' },
{ keyId: 'filter', secret: 'filter-secret' }
]
}
const server = new Server(config)
server.listen(3000, (err, server) => {
const address = server.address() as AddressInfo
console.log('Listening on port', address.port)
})
Run in client mode
import { Client, IClientAttributes } from '@vimla/oracle-client-http'
const config: IClientAttributes = {
baseURL: 'http://oracle-http-server:3000',
credentials: { keyId: 'api', secret: 'api-secret' },
maxConcurrent: 50 // maximum amount of concurrent http connections
}
const client = new Client(config)
console.log(await client.health())
/*
{
'health": "ok",
"pool": {
"connectionsOpen": 1,
"connectionsInUse": 0
},
"time": 9
}
*/
const statements: any[][] = [
['SELECT * FROM users'],
['SELECT * FROM users', , { maxRows: 10 }],
['SELECT * FROM users WHERE ACTIVE = :active', { active: true }, { maxRows: 10 } ]
]
console.log(await client.execute(statements))
/*
{
results: [
{
metaData: [{name: 'ID'}, {name: 'FIRST_NAME'}, {name: 'LAST_NAME'}, {name: 'ACTIVE'}],
rows: [
{ID: 12345, FIRST_NAME: 'Johan', LAST_NAME: 'Obrink', ACTIVE: true}
...
],
time: 25
},
{...}, // results for second statement
{...} // results for third statement
],
time: 175
}
*/
Options
Rows can be camelCased by passing in options with execute or set as default on client
import { Client, ClientAttributes } from '@vimla/oracle-client-http'
// Options in execute call
const config1: IClientAttributes = {
baseURL: 'http://oracle-http-server:3000',
credentials: { keyId: 'api', secret: 'api-secret' },
maxConcurrent: 50
}
const client1 = new Client(config1)
console.log(await client1.execute([['SELECT * FROM users']], {camelCase: true}))
// Default options in client
const config2: IClientAttributes = {
baseURL: 'http://oracle-http-server:3000',
credentials: { keyId: 'api', secret: 'api-secret' },
defaultOptions: { camelCase: true },
maxConcurrent: 50
}
const client2 = new Client(config2)
]
console.log(await client2.execute([['SELECT * FROM users']]))
// Both yield:
/*
{
results: [
{
metaData: [{name: 'ID'}, {name: 'FIRST_NAME'}, {name: 'LAST_NAME'}, {name: 'ACTIVE'}],
rows: [
{id: 12345, firstName: 'Johan', lastName: 'Obrink', active: true}
...
],
time: 25
}
],
time: 27
}
*/
Legacy Client
In order to facilitate usage in old code base, use the legacy client.
import { Client, IClientAttributes, LegacyClient } from '@vimla/oracle-client-http'
const config: IClientAttributes = {
baseURL: 'http://oracle-http-server:3000',
credentials: { keyId: 'api', secret: 'api-secret' },
maxConcurrent: 50
}
const client = new Client(config)
const legacyClient = new LegacyClient(client)
console.log(await legacyClient.execute(conn => conn.execute('SELECT * FROM users')))
/*
[
{id: 12345, firstName: 'Johan', lastName: 'Obrink', active: true}
...
]
*/
console.log(await legacyClient.executeMultiple([
conn => conn.execute('SELECT * FROM users'),
conn => conn.execute('SELECT * FROM products'),
]))
/*
[
[
{id: 12345, firstName: 'Johan', lastName: 'Obrink', active: true}
...
],
[
{id: 56789, name: 'Tesla', model: 'S', version: 'P100D'}
...
]
]
*/