getDatabaseConnectionByID()
Last updated 10/05/2023
Example One
The code first gets a database connection using the Five
object and checks if it is successful. If not, it returns an error using the createError()
on the Five
object.
Next, it starts a transaction using the database connection and checks if it is successful. If not, it returns an error using the createError()
on the Transaction
object.
Then, it executes an SQL query to select the iRoleKey
from the iRole
table where the Name
column is equal to Member
. If the query is not successful, it returns an
error using the createError()
on the Five
object. If the query is successful, it retrieves the iRoleKey
value from the query results and stores it in the variable iRoleKey
.
Next, it executes another SQL query to update the iRoleKey
value in the iUser
table for all users except for admin
and public
. It sets the iRoleKey
value to the
value stored in the iRoleKey
variable. If the query is not successful, it returns an error using the createError()
on the Five
object. Finally, if everything is
successful, it returns a success message using the success()
on the Five
object.
function MigrateRoles(five: Five, context: any, result: FiveResult) : FiveResult {
const db = five.getDatabaseConnectionByID(five.SYSTEM_DATABASE_NAME);
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 iRoleKey FROM iRole WHERE Name = 'Member'`, 0);
if (results.isOk() === false) {
return five.createError(results);
}
const iRoleKey = results.values[0].iRoleKey;
const updateResults = five.executeQuery(`UPDATE iUser SET iRoleKey = ? WHERE UserID <> 'admin' AND UserID <> 'public'`, 0, iRoleKey);
if (updateResults.isOk() === false) {
return five.createError(updateResults);
}
return five.success(result, 'Migration completed');
}
function MigrateRoles(five, context, result) {
const db = five.getDatabaseConnectionByID(five.SYSTEM_DATABASE_NAME);
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 iRoleKey FROM iRole WHERE Name = 'Member'`, 0);
if (results.isOk() === false) {
return five.createError(results);
}
const iRoleKey = results.values[0].iRoleKey;
const updateResults = five.executeQuery(`UPDATE iUser SET iRoleKey = ? WHERE UserID <> 'admin' AND UserID <> 'public'`, 0, iRoleKey);
if (updateResults.isOk() === false) {
return five.createError(updateResults);
}
return five.success(result, 'Migration completed');
}