A workflow is a good start if you want to build an application. However, sometimes it’s still necessary to add control elements. In this section, you’ll learn to add buttons and to write server side code to make your application even more powerful.
Starting with v1.9.0
CYPEX offers an approach to create database functions directly within its interface.
The REST API behind the scenes executes these functions, providing a unified experience for managing both data and logic.
Let’s start with a basic data model:
BEGIN;
CREATE SCHEMA inventory;
CREATE TABLE inventory.t_inventory (
id serial PRIMARY KEY,
name text NOT NULL,
assigned_to_id int8 NULL,
hardware_details text NULL,
software_details text NULL
);
CREATE TABLE inventory.t_users (
id serial PRIMARY KEY,
first_name text NOT NULL,
last_name text NOT NULL,
salutation text NOT NULL,
email text NULL,
phone_number text NULL
);
COMMIT;
Navigate to the CYPEX Admin Panel to find the new Functions Tab next to Entities and Queries on the Database page.
By clicking the “Create New” button function editor will be opened.
Create a new function using SQL syntax. CYPEX supports a variety of function types.
The function must be defined in the cypex_generated schema - because it’s the only schema exposed via the REST API which is generally available, NO other schema will be taken into consideration.
A sample function definition is provided below:
Before saving the function, configure role-based permissions to regulate access and execution. This involves assigning specific roles that are permitted to utilize the function, enhancing security and data integrity.
Once the function code is complete and permissions are set, click the “SAVE” button. The newly created function will be visible in the Function tab. By clicking on the corresponding menu item, a detailed view of the function, including its definition and assigned permissions, can be accessed.
After creating the module, an entity, the query and permissions, you can generate the application. The result once this is done is a basic application showing nothing more than tables.
Now the goal is to add a button calling the assign_inventory_to_user()
function on the SQL side.
After these preparations have been completed, you can enter edit-mode, select the “Call Button” element, this specific element is designed for directly executing pre-created database functions. and add it to your app.
Note the name of the function. Make sure that you choose the right function. Configure the button to call the inventory assignment function.
CYPEX provides various elements for gathering user input, such as text inputs and fields, dropdowns, date pickers, tables, etc. This input can be passed as arguments to the database function. The usage of built-in JavaScript expressions provides smooth functional interaction between elements.
For instance, by using the selected rows from the Users and Inventory tables, we can assign inventory items to users:
Finally, add a label and select a nice icon. Voilà, you have just created your first button and your first server-side business logic.
Upon saving these changes, clicking the “Call Button” will execute the function, assigning the specified inventory to the designated user:
Buttons are useful to trigger server-side business logic such as aggregations. But you can also directly impact your workflows. Sometimes, more complex operations are needed. Triggers are the best and most appropriate way to make that happen.