Skip to main content

1 - Connect with the System Database

Last updated 12/06/2023

In certain scenarios you may find you need to connect with the system database. This documentation is to explain how to do this.

In the last section, we created roles and imported the users for the Book Club application so that we have some data to play with. At the moment, none of our members have a role assigned because the Member role we created is stored in the system database while the users are stored in the application database. So before we can log in as a member, we need to connect with the system database to assign each member the Member role. We can do this by using Five's getDatabaseConnectionByID().


Tips
  • Your application schema is stored in the system database!
  • Your data is stored in the application database!


The diagram below gives you a good interpretation of how your schema and data are stored.


Schema and Data Storage
Figure 1 - Schema and data storage


1. Select Logic in the menu.

2. Select Code Editor in the sub-menu.


Code Editor Menu Item
Figure 2 - Code Editor menu item


Add the MigrateUsers Function

1. Click the Add New Code button.


Add New Code Button
Figure 3 - Add New Code button


2. Type MigrateRoles in the Function ID field.

3. Click the lookup icon in the Language field and select JavaScript.

4. Click the OKAY button.


Add the MigrateRoles Function
Figure 4 - Add the MigrateRoles function


5. Highlight and delete the template in the Code Editor.


Clean the Code Editor
Figure 5 - Clean the Code Editor


6. Click the Copy button on the code block below.


JavaScript
MigrateRoles()
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');
}


7. Paste the code in the Code Editor.

8. Click the Save Current Tab button.


Save the MigrateRoles Function
Figure 6 - Save the MigrateRoles function


Add the Migrate User Roles Process

The Migrate User Roles process needs to be created so the MigrateRoles function can be attached to the Do Run event. We will need to open the Migrate User Roles process in the Book Club application and click the Run button. Once the Run button is clicked, the code will be executed on the server and migrate the users imported with the Member role created for the Book Club application.

Application roles are unique globally and this is the reason why we have to migrate our users to the Member role in the Book Club application.

1. Click Tasks in the menu.

2. Click Processes in the sub-menu.


Processes Menu Item
Figure 7 - Processes menu item


3. Click the Add Item button.

4. Type Migrate User Roles in the Title field.


Add the Migrate User Roles Process
Figure 8 - Add the Migrate User Roles process


5. Click the Events tab.


Events Tab
Figure 9 - Events tab


6. Click the lookup icon in the Do Run field and select MigrateRoles.


Attach the MigrateRoles Function
Figure 10 - Attach the MigrateRoles function


tip
If you click the VIEW button located next to an Event field, it will open the code attached in the Code Editor for you!


7. Click the Save button in the form app bar.


Save the Migrate User Roles Process
Figure 11 - Save the Migrate User Roles process


Create the Migrate User Roles Menu

Because we need to open and run the process in the Book Club application we will need to create a menu item for the process to have access to it. We will only ever need to run this process once, as any new users created for the Book Club application can simply have their role attached when adding the user to the system.


tip
For a process that needs to run frequently, you create a job that will run in the background and this way the process will not need a menu item!


1. Select Visual in the menu.

2. Select Menus in the sub-menu.


Menus Menu Item
Figure 12 - Menus menu item


3. Click the Add Item button.

4. Type Migrate User Roles in the Caption field.

5. Type 100300 before MigrateUserRoles in the Menu ID field.

6. Type 100300 in the Menu Order field.

7. Click the lookup icon in the Action field and select MigrateUserRoles (Process).

8. Click the lookup icon in the Parent Menu field and select 100000AdministrationMenu (AdministrationMenu).


Add the Migrate User Role Menu
Figure 13 - Add the Migrate User Roles menu


9. Click the Save button in the form app bar.


Save the Migrate User Role Menu
Figure 14 - Save the Migrate User Roles menu


Run the Book Club Application

This is a great time to run the Book Club application so we can run the Migrate User Roles process and check out the user logins.

1. Click the Run button in the toolbar.


Run Button
Figure 15 - Run button


info
We will need to sign in with the Administrator role to begin with because we have positioned the Migrate User Roles process in the Administration Menu.


2. Type admin in the Username field.

3. Type your password in the Password field.

4. Click the Sign In button.


Sign In With the Administrator Role
Figure 16 - Sign in with the Administrator role


5. Click Administration Menu in the menu.


Administration Menu item
Figure 17 - Administration Menu menu item


6. Click Migrate User Roles in the menu.


Migrate User Roles Menu Item
Figure 18 - Migrate User Roles menu item


7. Click the Run button.


info
The process will run and migrate the Member role to the Member users.


Run the Process
Figure 19 - Run the process


8. Click the OK button.


OK Button
Figure 20 - OK button


9. Click Users in the menu.


Users Menu Item
Figure 21 - Users menu item


10. Select the Anne Clifford record in the list.


info
You can see now in the Role field Anne has the role of Member. If you click through the other records you will see they are assigned the Member role with the exception of admin and public as we specified in the function for them not to be assigned the Member role.


Anne Clifford Record
Figure 22 - Anne Clifford record


11. Click Members Menu in the menu.


Members Menu Item
Figure 23 - Members Menu menu item


12. Click Member Books in the menu.


Member Books Menu Item<
Figure 24 - Member Books menu item


info
All the books that belong to all members in the system will be listed here as you are signed in with the Administrator role. Selecting a record will display the details for the book.


13. Select the first record in the list.


Book List
Figure 25 - Book list


info
If you take a look at the list you can see that Anne has four books in the system that she can lend out. We'll sign in as Anne and we'll be able to see what she sees with her Member role.


Anne's Books
Figure 26 - Anne's books


14. Click the Avatar button.

15. Click the Logout button.


Logout as Administrator
Figure 27 - Logout as Administrator


16. Type anne in the Username field.

17. Type Demo123! in the Password field.

18. Click the Sign In button.


Sign In With the User Anne
Figure 28 - Sign in with the user Anne


info
When Anne signs in with her Member role, she lands straight on the Member Books menu item and the list is filtered so she only sees her books in the system. Feel free to log in with any of the members in the system, just use their username on the Users form and the password Demo123! The same password is set for each user for you for ease of use.


Anne's Member Account
Figure 29 - Anne's member account


19. Click the Avatar button.

20. Click the Logout button.


Logout as Anne
Figure 30 - Logout as the user Anne


21. Click the Close button and return to Five.


Close Button
Figure 31 - Close button