Skip to content
This repository was archived by the owner on Apr 7, 2023. It is now read-only.

Commit 01f8ff1

Browse files
committed
Allow overriding of more options
The original implementation did not allow configuration of some of the connection options, but there is a reasonable case for making more of them configurable to accomodate different circumstances.
1 parent 9fe285f commit 01f8ff1

File tree

2 files changed

+62
-10
lines changed

2 files changed

+62
-10
lines changed

src/index.ts

Lines changed: 37 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,19 @@ import { Logger, OracleDBCredentials, OracleDBOptions } from './types';
99

1010
oracledb.outFormat = oracledb.OUT_FORMAT_OBJECT;
1111

12+
/**
13+
* The default options for the Oracle Wrapper
14+
*/
15+
16+
const defaultOptions: OracleDBOptions = {
17+
logger: console,
18+
poolMin: 0,
19+
poolMax: 2,
20+
poolIncrement: 1,
21+
poolCloseTimeout: 60,
22+
prefetchRowCount: 1000,
23+
};
24+
1225
/**
1326
* Interface to the Oracle database
1427
*/
@@ -36,26 +49,42 @@ export default class OracleWrapper {
3649
*/
3750
private poolCredentials: PoolAttributes;
3851

52+
/**
53+
* The amount of time to wait for the connection pool to close
54+
*/
55+
private poolCloseTimeout: number;
56+
57+
/**
58+
* the number of rows that should be fetched at one time
59+
*/
60+
private prefetchRowCount: number;
61+
3962
/**
4063
* Create a new oracle interface
4164
*/
4265
public constructor(
4366
credentials: OracleDBCredentials,
44-
options: OracleDBOptions = {}
67+
userOptions: OracleDBOptions = {}
4568
) {
4669
const {
4770
host, port, sid, user, password,
4871
} = credentials;
72+
const options = {
73+
...defaultOptions,
74+
...userOptions,
75+
};
4976
this.alias = options.alias || sid;
50-
this.logger = options.logger || console;
77+
this.logger = options.logger;
78+
this.poolCloseTimeout = options.poolCloseTimeout;
79+
this.prefetchRowCount = options.prefetchRowCount;
5180
this.poolCredentials = {
5281
user,
5382
password,
5483
poolAlias: this.alias,
5584
connectionString: `${host}:${port}/${sid}`,
56-
poolMin: 0,
57-
poolMax: 2,
58-
poolIncrement: 1,
85+
poolMin: options.poolMin,
86+
poolMax: options.poolMax,
87+
poolIncrement: options.poolIncrement,
5988
};
6089
}
6190

@@ -87,7 +116,7 @@ export default class OracleWrapper {
87116
public async releasePool(): Promise<void> {
88117
if (this.connectionPool) {
89118
try {
90-
await this.connectionPool.close(60);
119+
await this.connectionPool.close(this.poolCloseTimeout);
91120
this.connectionPool = null;
92121
this.logger.info('Connection pool released');
93122
} catch (err) {
@@ -106,10 +135,9 @@ export default class OracleWrapper {
106135
query: string,
107136
variables: Record<string, unknown> = {}
108137
): Promise<T[]> {
109-
const ROW_COUNT = 1000;
110138
const options = {
111139
resultSet: true,
112-
prefetchRows: ROW_COUNT,
140+
prefetchRows: this.prefetchRowCount,
113141
};
114142
const conn = await this.getConnection();
115143
// Execute Query
@@ -127,7 +155,7 @@ export default class OracleWrapper {
127155
// https://jsao.io/2015/07/an-overview-of-result-sets-in-the-nodejs-driver/
128156
try {
129157
const fetchAndConcat = async (prevRows: T[]): Promise<T[]> => {
130-
const rows = await result.resultSet.getRows(ROW_COUNT);
158+
const rows = await result.resultSet.getRows(this.prefetchRowCount);
131159
if (rows.length) {
132160
return fetchAndConcat([...prevRows, ...rows]);
133161
}

src/types.ts

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,5 +39,29 @@ export interface OracleDBOptions {
3939
* An instance of a logger. Uses the console by default.
4040
*/
4141
logger?: Logger;
42-
42+
/**
43+
* The minimum number of pool connections to make.
44+
* Defaults to 0
45+
*/
46+
poolMin?: number;
47+
/**
48+
* The maximum number of pool connections to make.
49+
* Defaults to 2
50+
*/
51+
poolMax?: number;
52+
/**
53+
* The number of additional pool connections that should be opened at one time
54+
* Defaults to 1
55+
*/
56+
poolIncrement?: number;
57+
/**
58+
* The amount of time to wait for a pool connection to close
59+
* Defaults to 60
60+
*/
61+
poolCloseTimeout?: number;
62+
/**
63+
* The number of rows that should be prefetched when querying the database
64+
* Defaults to 1000
65+
*/
66+
prefetchRowCount?: number;
4367
}

0 commit comments

Comments
 (0)