- API Documentation
- Class: Connection
- Constructor: Connection(database)
- Connection.connect(url, username, password)
- Connection.getStatement()
- Connection.close()
- Connection.disconn()
- Connection.debug(choice)
- Connection.getConnAttr(attribute)
- Connection.isConnected()
- Connection.setConnAttr(attribute, value)
- Connection.validStmt(sql)
- Connection.setLibraryList(list)
- Class: Statement
- Constructor: Statement(connection)
- Statement.bindParam(params)
- Statement.bind(params)
- Statement.bindParameters(params)
- Statement.close()
- Statement.closeCursor()
- Statement.commit()
- Statement.enableNumericTypeConversion(flag)
- Statement.exec(sql)
- Statement.execute()
- Statement.fetch()
- Statement.fetchAll()
- Statement.fieldName(index)
- Statement.fieldNullable(index)
- Statement.fieldPrecise(index)
- Statement.fieldScale(index)
- Statement.fieldType(index)
- Statement.fieldWidth(index)
- Statement.getStmtAttr(attribute)
- Statement.nextResult()
- Statement.numFields()
- Statement.numRows()
- Statement.prepare(sql)
- Statement.rollback()
- Statement.setStmtAttr(attribute, value)
- Statement.stmtError(hType, recno)
- Class: DBPool
The Connection constructor accepts an optional database parameter which can be used to connect to the database.
If database is not provided make sure to use the connect() before performing any other methods.
Parameters:
- database:
Objectincludes the properties:-
url
Stringlocation of the database, use*LOCALfor a local database. -
username
Stringfor the database user. -
password
Stringfor the database user.
-
NOTE: if connecting using *LOCAL, the username & password can be undefined but ensure that database contains:
url: '*LOCAL'
Establishes a Connection to the database.
Parameters:
-
url:
Stringthe url of the database to connect to. If a url is not specified, it defaults to*LOCAL. -
username:
Stringthe username for the database user. -
password:
Stringthe password for the database user.
Returns: Object the Connection object with an established connection.
Returns a Statement Object initialized to the current Connection. Ensure that the Connection object is connected first before attempting to get a Statement.
The isConnected method can be used to check if the Connection object is currently connected.
Returns: Object a new Statement object initialized with the current Connection.
Closes the Connection to the DB and frees the connection object.
Returns: Promise when resolved will return true indicating successful closure, or the promise will be rejected.
Disconnects an existing connection to the database.
Returns: Promise when resolved will return true indicating successful disconnection, or the promise will be rejected.
Prints verbose detailed info to the console if choice is set true. Can be turned off by setting choice = false.
Parameters:
- choice:
booleanthe option either true or false to turn debug on/off.
Returns: Promise when resolved will return true | false indicating the current state of debug, or the promise will be rejected.
Returns the current value of a connection attribute, if the attribute exists.
Parameters:
- attribute:
Numberthe attribute to retrieve the current value from.
Returns: Promise when resolved will return the specified connection attribute settings either Number | String, or the promise will be rejected.
Link: Connection Attributes
Checks if the Connection object is currently connected to the database.
Returns: true or false indicating if the Connection object is currently connected.
Sets a connection attribute to a provided valid value.
Parameters:
-
attribute:
Numberthe attribute to be set. -
value:
string | numberthe value to set the attribute to.
Returns: Promise when resolved will return true indicating success, or the promise will be rejected.
Checks if the given SQL is valid and interprets vendor escape clauses.
Parameters:
- sql:
String, the sql string to be validated.
Returns: Promise when resolved will return the transformed sql string that is seen by the data source, or the promise will be rejected.
Change to system naming and set the library list (using CHGLIBL) of the connection.
Parameters:
- list:
Arrayof strings where each element in the array is a library.
Parameters:
- connection: optional
Connection Objectfor the Statement to use.
NOTE: If you don't pass a Connection Object one will be implicitly created and will attempt to connect to *LOCAL.
Deprecated: use Statement.bindParameters(params) instead.
Associates parameter markers in an sql statement to application variables.
Parameters:
- params:
Arraythe parameter list in order corresponding to the parameter markers. Each parameter element will also be an Array with 3 values [value,io,indicator].
io can be:
- IN
- OUT
- INOUT
indicator can be:
- CHAR
- INT
- NUMERIC
- BINARY
- BLOB
- CLOB
- BOOLEAN
- NULL
These values are constants which are attached to object returned when you const idbp = require('idb-pconnector').
You can access the constants like so : idbp.IN
Returns: Promise when resolved there is no return value but if an error occurred the promise will be rejected.
Shorthand equivalent of bindParam(params) above.
Associates parameter markers in an sql statement to application variables.
Parameters:
- params:
Arraythe parameter list of values to bind. The values are ordered based on the parameter markers in the sql statement.
Returns: Promise when resolved there is no return value but if an error occurred the promise will be rejected.
Example: Here
Ends and frees the statement object.
Returns: Promise when resolved will return true indicating successful closure, or the promise will be rejected.
Closes the cursor associated with the Statement object and discards any pending results.
Returns: Promise when resolved will return true indicating successful closure, or the promise will be rejected.
Adds all changes to the database that have been made on the connection since connect time.
Returns: Promise when resolved will return true indicating successful commit, or the promise will be rejected.
Directly executes a given sql String. The exec() method does not work with stored procedure use execute() method instead.
Parameters:
- sql:
Stringthe sql command to execute.
Returns:
Promisewhen resolved if available will return the result set as anArray.- If no result set was available the
Promisewill resolve tonull - If the
sqlstatement could not be directly executed thePromisewill be rejected.
Example: Here
Runs a statement that was successfully prepared using prepare(). Used to call stored procedure calls.
Returns: `
- Promise
when resolved if available will return output parameters as anArray`. - If no output parameters were available the
Promisewill resolve tonull. - If the prepared statement could not be executed the
Promisewill be rejected.
Example: Here
NOTE: that execute() will return output parameters and not a result set.
If available you can retrieve the result set by either running fetch() or fetchAll() after execute() has been called.
If a result set exists, fetch() will retrieve a row from the result set. The row is an Object.
Fetch can be continuously run until there is no data. If there is no data to be fetched null will be returned indicating the end of the result set.
Returns:
Promisewhen resolved will return anObjectrepresenting the row that was retrieved.- If there is no data remaining to be fetched in the result set
nullwill be returned. - If there was never a result set to be fetched the
Promisewill be rejected.
Example Fetching a result set until there is no more data to fetch:
const {Connection} = require('idb-pconnector');
async function fetch(){
try {
let sql = 'SELECT * FROM QIWS.QCUSTCDT',
connection = new Connection();
connection.debug(true);
let statement = connection.connect().getStatement();
await statement.prepare(sql);
await statement.execute();
let result = await statement.fetch();
while (result !== null ){
console.log(`Fetch result:\n ${JSON.stringify(result)}`);
result = await statement.fetch();
}
} catch(error){
console.log(error.stack);
}
}
fetch();If a result set exists , fetchAll() retrieves all the rows from the result set.
Returns: Promise when resolved will return an Array of Objects representing the result set or the promise will be rejected.
If a valid index is provided, fieldName() returns the name of the indicated field.
Parameters:
- index:
Numberthe position of the field within the table. It is 0 based.
Returns: Promise when resolved will return String name of the field or the promise will be rejected.
If a valid index is provided, fieldNullable returns true | false indicating if field can be set to null.
Parameters:
- index:
Numberthe position of the field within the table. It is 0 based.
Returns: Promise when resolved will return true | false or the promise will be rejected.
If a valid index is provided, fieldPrecise returns the precision of the indicated field.
Parameters:
- index:
Numberthe position of the field within the table. It is 0 based.
Returns: Promise when resolved will return Number or the promise will be rejected.
If a valid index is provided, fieldScale returns the scale of the indicated column.
Parameters:
- index:
Numberthe position of the field within the table. It is 0 based.
Returns: Promise when resolved will return a Number or the promise will be rejected.
If a valid index is provided, fieldType returns the data type of the indicated field.
Parameters:
- index:
Numberthe position of the field within the table. It is 0 based.
Returns: Promise when resolved will return a Number or the promise will be rejected.
If a valid index is provided, fieldWidth returns the field width of the indicated field
Parameters:
- index:
Numberthe position of the field within the table. It is 0 based.
Returns: Promise, when resolved will return a Number or the promise will be rejected.
If a valid Statement attribute is provided , getStmtAttr returns the current settings for the specified attribute.
Parameters:
- attribute:
Numberthe attribute to retrieve the current value from.
Returns: Promise, when resolved will return the specified connection attribute settings as a Number | String, or the promise will be rejected.
Refer to the list below for valid Statement Attributes.
Link: Statement Attributes
Determines whether there is more information available on the statement handle that has been associated with a stored procedure that is returning result sets.
After completely processing the first result set, the application can call nextResult() to determine if another result set is available.
If the current result set has unfetched rows, nextResult() discards them by closing the cursor.
Returns:
Promisewhen resolvetruewill be returned indicating there is another result set ornullis returned indicating there was not another result set.- If an error occurred while processing the promise is rejected.
If a result set is available , numFields retrieves number of fields contained in the result set.
Returns: Promise when resolved Number is returned or the promise is rejected.
If a query was performed, numRowsretrieves the number of rows that were affected by a query.
Returns: Promise when resolved will return a Number or the promise is rejected.
If valid sql is provided , prepare prepares the sql statement and sends it to the DBMS.
Parameters:
- sql:
Stringthe SQL string to be prepared.
Returns: Promise when resolved no value is returned but if an error occurred the promise is rejected.
Example: Here
Reverts changes to the database that have been made on the connection since connect time or the previous call to commit().
Returns: Promise when resolved true is returned or promise is rejected.
Sets a statement attribute to a provided valid value.
Parameters:
-
attribute:
Numberthe attribute to be set. -
value:
string | numberthe value to set the attribute to. -
Returns:
Promisewhen resolved will returntrueindicating success or the promise will be rejected.
Returns the diagnostic information associated with the most recently called function for a particular statement, connection, or environment handler.
Parameters:
-
hType:
Number, indicates the handler type of diagnostic information.-
SQL_HANDLE_ENV: Retrieve the environment diagnostic information -
SQL_HANDLE_DBC: Retrieve the connection diagnostic information -
SQL_HANDLE_STMT: Retrieve the statement diagnostic information
-
-
recno:
Number, indicates which error should be retrieved. The first error record is number 1.
Returns: Promise when resolved returns String or the promise is rejected.
Enables or disables automatic numeric conversion.
Parameters:
-
flag:
booleanto turn automatic data conversion mode on or off. Default value isfalse.-
true: SQL numeric types (INTEGER,DECIMAL,NUMERIC) are converted to JavascriptNumberobjects instead of strings. Due to the data type limitations of JavascriptNumbers, precision may be lost. If the SQL value is outside the bounds of what aNumbercan represent, it will be returned as a string instead. -
false: The result data is returned as strings.
-
Returns: boolean the current state of the flag otherwise an error is thrown.
Manages a list of DBPoolConnection instances.
Constructor to instantiate a new instance of a DBPool class given the database and config.
Parameters:
-
database:
Objectwith the properties:url:Stringlocation of the database, use*LOCALfor a local database.username:Stringfor the database user.password:Stringfor the database user.
-
config:
Objectwith the properties:incrementSize:Numberis an integer that sets the desired size of theDBPool, defaults to 8 connections.debug:booleansetting it to true will display verbose output to the console, defaults to false.enableNumericTypeConversion:booleanEnabling this option will convert numerics within the result set returned from runSql and prepareExecute calls as JS Numbers instead of strings when it is safe to do so. This option defaults to false.
Example: Here
Instantiates a new instance of DBPoolConnection with an index and appends it to the pool.
Assumes the database of the pool when establishing the connection.
Parameters:
index: Number an optional identifier to id the connection for debug purposes.
Frees all connections in the pool (Sets availability back to true for all) and cleans up statements on the connection.
Returns:Promise when resolved returns true if all were detached successfully, or the promise is rejected.
Retires (Removes) all connections from being used again
Returns: Promise when resolved returns true if all were retired successfully, or the promise is rejected.
Frees a connection (Sets availability back to true) cleans up statements on the connection.
Parameters:
- connection:
DBPoolConnection, Frees a connection (Returns the connection "Available" back to true) closes any statements and gets a new statement.
Returns: Promise when resolved returns true if detached successfully, or the promise is rejected.
Retires a connection from being used and removes it from the pool.
Parameters:
- connection:
DBPoolConnection, Retires a connection from being used and removes it from the pool
Returns: Promise when resolved returns true if retired successfully, or the promise is rejected.
Finds and returns the first available Connection.
If no available connection is found, connections will be incremented by the incrementSize, which defaults to 8.
Returns: DBPoolConnection connection from the DBPool.
An aggregate to run an sql statement, just provide the sql to run.
NOTE: that Stored Procedures should use the prepareExecute() aggregate instead.
Parameters:
- sql:
Stringthe sql statement to run.
Returns:
-
Arrayif the sql returns a result set it is returned as anArrayofObjects. -
If no result set is available
nullis returned.
NOTE: Caller should check if null is returned.
Example: Here
Aggregate to prepare, bind, and execute. Just provide the sql and the optional params as an array.
options object can now be used for global configuration. This is used to set options on all the parameters within the params Array.
Currently, the input output indicator io is the only available option to set. This will override the default which is 'both'.
Example: prepareExecute(sql, parrams, {io: 'in'})
Also parameters can be customized at an individual level by passing an object within the params Array.
The object format:
value:String | Number | Buffer | boolean | nullio:in | out | bothasClob:true | false
value is the only required property others will fall back to defaults if undefined.
If you want to bind a string as a clob you can add asClob: true property to the object.
Example: {value: 'your string', asClob: true, io: 'in'}
Parameters:
-
sql:
stringthe sql to prepare , include parameter markers (?, ?, ...) -
params:
Arrayan optional array of values to bind. order of the values in the array must match the order of the desired parameter marker in the sql string. -
options:
Objectwith config options to set for all parameters. The Object can contain: -
io:
Stringset to either the'in','out', or'both'. Indicating that the parameter is an input, output, or inout parameter.
Returns:
-
Objectin the format:{resultSet: [], outputParams: []}if the Prepared SQL returns result set & output parameters. -
Objectin the format:{resultSet: []}if the Prepared SQL returns result set but no output parameters. -
Objectin the format:{outputParams: []}if the Prepared SQL output parameters but no result set. -
If neither were available
nullwill be returned indicating that there is no result set or output parameters.
NOTE: Caller should check if null is returned.
Example: Here
Sets the connection attribute for each Connection in the pool.
Parameters:
- attribute:
Objectthat contains:- attribute:
Numberthe attribute to set. - value:
Number | Stringthe value to set the attribute to
- attribute: