getDatabaseConnectionByID()
Last updated 12/09/2024
Example
The following code checks if a user called Member exists in the main database, if so delete its related record in the external database.
getDatabaseConnectionByID() to delete a member user if it exists
function DeleteUserMemberIfExist(five: Five, context: any, result: FiveResult) : FiveResult {
const db = five.getDatabaseConnectionByID('MembersDB');
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(`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 Members WHERE MemberUsersKey = ?`, 0, iUserKey);
if (deleteResults.isOk() === false) {
return five.createError(deleteResults);
}
tx.commit();
return five.success(result, 'Member user has been deleted.');
}
getDatabaseConnectionByID() to delete a member user if it exists
function DeleteUserMemberIfExist(five, context, result) {
const db = five.getDatabaseConnectionByID('MembersDB');
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(`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 Members WHERE MemberUsersKey = ?`, 0, iUserKey);
if (deleteResults.isOk() === false) {
return five.createError(deleteResults);
}
tx.commit();
return five.success(result, 'Member user has been deleted.');
}