Save To Database Flow Development
We will now develop a Form Action Flow to save the Computer Inspection form data to a SQL database, For this tutorial we will provision a free PostgreSQL database on Supabase. If you want to run this tutorial on a PostgreSQL database and do not have access to one please create an account on Supabase and provision one. Alternatively you can continue to build this tutorial on a MySQL or MSSQL database also. The only difference is in creating the system which is explained later on.
Prerequisite - SQL Database
Section titled “Prerequisite - SQL Database”Recall that the Inspection Form has the following components and API property name.
| Component/Field | API Property Name | SQL Type | Allow Null |
|---|---|---|---|
| Date / Time | inspectDateTime | text | No |
| Equipment | equipment | text | No |
| Condition | condition | text | No |
| Comment | comment | text | Yes |
| Inspected By | inspectedBy | text | No |
| Signature | signature | text | No |
Note that we have left out the equipmentImage by design. We will revisit this later.
For this tutorial, we will be connecting to a PostgreSQL database on Supabase for free. Login to your Supabase account online and create a database / project. If you want to proceed with MySQL or MSSQL please follow the database documentation to create a database, table and the columns (use varchar instead of text where appropriate) and keep the connection credentials handy.
Supabase PostgreSQL
Section titled “Supabase PostgreSQL”- Create a project or choose an existing one and click on the Table Editor in your project. Make a note of the password for reference
- Create a new table called
computerinspection - Now add new columns to the table using the API Property Name as the column name with the corresponding SQL types from above
- Remember only comment is a Nullable field as it may not be filled if the condition of the equipment is is good, all others are mandatory fields
- Once the 6 additional columns are added click on
Save - Note that the table has 8 columns in total including a primary key
idand acreated_atdate time - Now create a RLS policy to allow to ALL access for Authenticated users. For MySQL and MSSQL refer the documentation to create the required access for reading and writing the table
- From the Database settings screen, copy the DB host, port, database name and user name
- To confirm that all works well (and to later verify if our data is being saved correctly) use a PostgreSQL GUI tool or install one and connect to the database server and confirm
We are now good to go on to the developing the flow and save our Inspection data when completed.
Flow Development
Section titled “Flow Development”- In the Forms list, click on the
Flowsicon on the Computer Inspection row - A list of Flows will be displayed. Now click on the
Createbutton and choose theForm Actionflow- Title - Enter Save to Database
- Description - Enter description as Save to PGSQL database on Supabase or similar
- Select the
On Completeoption - Click on
Saveto create the flow
- You will now be navigated to the Flow Builder
Understanding the steps
Section titled “Understanding the steps”Before we develop the flow, let us draw a flow chart of the different steps we need to perform to save the data:
flowchart TD
A([Start]) -->|Form Complete Action| B[Extract JSON Data]
B --> C{Error}
C --> |No| D[(Save to Database)]
C --> |Yes| E([ReturnError])
D --> F{Error}
F --> |No| G([Return Success])
F --> |Yes| E
Developing the Flow
Section titled “Developing the Flow”- In the Flow Builder, the
Add Nodedialog should already be open. If not, click on theForm Action Flowstart node and then click on the :fontawesome-solid-circle-plus: icon that is displayed below the node - Search for the
JSON Parsernode by typing in json and clicking on it - The
JSON Parsernode is now opened and you can configure it.- Change the title to
Extract Formand remember to connect theForm Action Flowstart nodeSuccessconnector to this node (else the expression builder will not display all the possible fields and nodes) - Since we want to work with the Form Data, click on the
Form Fieldssection - For
Map Field NameselectForm Data - Under
Expressionclick on the :fontawesome-solid-wrench: icon to open the Expression Builder - To extract the data we need to know what format the data is going to be submitted. In the Expression Builder windows, click on the :fontawesome-solid-rectangle-list: icon to open the data format screen.
- Since this is the first time we are checking this there is no data. Click on the
Add Form Databutton and the form is now displayed. Fill in the form and submit it. Remember this is just test data for us to work with the flow and is not data that is submitted by an end user and hence does not affect the system status. - Now the data format is displayed in the JSON editor. Click on the editor, select the entire JSON and copy it. Note that the condition is set to Warning so that we can also enter a comment in the form and get it in the format.
{"inspectDateTime": "2024-03-07T03:45:09.000Z","equipment": "cpu","condition": "warning","equipmentImage": [],"inspectedBy": 1,"comment":"Warning comment","signature": "data:image/png;base64,iVBORw0K ......... AAEl==","branding": {"data": {},"metadata": {}}}
- Paste the data into the
JSONwindow on the left - The expression builder we use for JSON parsing is a tool called Jsonata
- We now need to extract only the fields we want to persist. Note that if the column names do not match the API Property Names, the same JSONata expression can be used to rename the attributes in the output JSON.
- In our example since the column names and API Property Names match, we will write an expression to extract the required data as is
- :fontawesome-solid-triangle-exclamation: Important: The JSON data needs to be an object with the table name as attribute and the value the actual row of data. This allows the No-Code SQL Engine to determine the data for each of the tables specified
- Since comment is an optional field, we will use the inbuilt $exists() function to check if a comment is present and if not found add comment with a null value to the output JSON
{"computerinspection":$.{"inspectDateTime":inspectDateTime,"equipment":equipment,"condition":condition,"inspectedBy":inspectedBy,"comment":$exists(comment) ? comment:null,"signature":signature}}
- Paste the expression from the step above into the
Mapping Expressionwindow on the right. The extracted data is now displayed at the bottom. Confirm that what is displayed is correct. - Now Click on
Saveand close the builder, - Now Click on the :fontawesome-solid-floppy-disk: icon and save the node
- Change the title to
- Now we need to save the data to the PostgreSQL database. Select the
Extract Formnode and then click on the :fontawesome-solid-circle-plus: icon - Search for the
SQL Querynode by typing in sql and clicking on it - The
SQL Querynode is now opened and you can configure it.- Change the title to
Save Form Dataand remember to connect theExtract FormnodeSuccessconnector to this node (else the expression builder will not display all the possible fields and nodes) - First we need to configure a new database server. Click on :fontawesome-solid-plus: icon next to the Database Server dropdown.
- The
Add Systemdialog is displayed. Enter the following data:- Description: Supabase (or anything else you desire)
- Identifier: This is a unique identifier for the system, enter supabase-pgsql
- Type: PostgreSQL (or any other database of your choice)
- Server: Enter the host name from the Supabase config, for e.g. aws-0-us-west-1.pooler.supabase.com
- Port: 5432 (for PostgreSQL)
- Database Name: postgres
- User: Copy the user name from Supabase database settings and paste
- Password: Enter the password you configured when you created the project
- Save the system
- Now choose the newly added
supabase-pgsqlsystem (or the name that you entered above) - Select
Execution TypeasOperationsince we are going to directly insert the data into the database - Select
SQL OperationasInsert - For
Keys, click on the :fontawesome-solid-wrench: icon and choose the tablecomputerinspectionand click Save - Under
Data To Process, click on the :fontawesome-solid-wrench: icon and entersuccessin the searh box. The previously addedextractformnode with Success is now shown. Click on Success and Save to accept it. - Now click on Save to save the node.
- Change the title to
- Connect the
Save Form Datanode to Success - Now click on Publish button to save and compile the flow. If the compilation is successful it will also be published and we can run it to test.
Testing the Flow
Section titled “Testing the Flow”- Now that we have published the flow we are ready to test it. The Flow Builder has an integrated Run and Debug tool to help us troubleshoot the flow in case of errors.
- Now click on the Run Flow button. The Run dialog is now displayed. You can enter Form (test) data now by clicking on the
Form Databutton. - Run the flow by clicking on the
Runbutton. The flow should run successfully. - If there are errors it could be due to some mistakes while configuring the system or copy/pasting the expression or similar. Check each step carefully with the documentation above.
- Now login to the external Database GUI tool and check the
computerinspectiontable. Voila, the Form (test) data that you entered must now be inserted to the table. - Go ahead and make some changes to the Form (test) data and
Runthe flow again. Refresh the table in the external Database GUI and confirm if a second row is now inserted.
Login to Turbo Forms App
Section titled “Login to Turbo Forms App”Now login to the Turbo Forms App on web or mobile as the same user and click on the + button to create a new form to fill and choose the Computer Inspection form. Fill in data, choose the user, take a picture and after filling all fields click on Complete. This will ensure that the form is finalized and the On Complete flow we developed above will be executed. Check in the external Database GUI tool if the form data you just submitted is inserted into the table.
Topics Covered
Section titled “Topics Covered”- Developing a flow to extract required fields from the submitted form data
- Saving form data to a PostgreSQL (or similar) database when a form is completed
- Creating Form (test) data
- Running the flow and testing it