Skip to main content

getDatabaseConnectionByID()

Last updated 5/02/2024

Example

The following code uses the getDatabaseConnectionByID() function on the Five object and checks if the connection is successful.

An error will be returned using the createError() function if the connection could not be made.

The executeQuery() function is used to select a user by the name of member.

An error will be returned using the createError() function if the SQL could not be executed.

The recordCount() function returns the number of records returned from the query.

The executeQuery() function is used to delete a user by it's iUserKey retrieved from the previous executeQuery() function.

An error will be returned using the createError() function if the SQL could not be executed.

A FiveError object will be returned indicating success by calling the success() function with an optional message to display to the user.

getDatabaseConnectionByID() to delete a member user if it exists
function DeleteUserMemberIfExist(five: Five, context: any, result: FiveResult) : FiveResult {
const db = five.getDatabaseConnectionByID('ExampleDB');
if (db.isOk() === false) {
return five.createError(db);
}

const tx = five.startTransaction(db);
if (tx.isOk() === false) {
return five.createError(tx);
}

const results = five.executeQuery(tx, `SELECT iUserKey FROM iUser WHERE Name = 'Member'`, 0);
if (results.isOk() === false) {
return five.createError(results);
}

if (results.recordCount() === 0) {
return five.success(result, 'Member does not exist.')
}

const iUserKey = results.records[0].iUserKey;
const deleteResults = five.executeQuery(tx,`DELETE FROM iUser WHERE iUserKey = ?`, 0, iUserKey);
if (deleteResults.isOk() === false) {
return five.createError(deleteResults);
}

tx.commit();

return five.success(result, 'Member user has been deleted.');
}