Bulk Row Operations
This guide explains how to perform bulk operations on table rows using row selection, JavaScript queries, and SQL with a configured SQL data source. PostgreSQL is used in the examples, but the same approach applies to other SQL databases.
Prerequisites
- A connected SQL data source
- A Table component added to the canvas
- Basic familiarity with:
- Table row selection
- JavaScript queries
- SQL queries
Setup
Step 1: Fetch Data from the Database
- Create an SQL query named getRecords.
- Use:
SELECT * FROM <table_name> - Enable Run query on application load.
Step 2: Populate the Table
- Add a Table component.
- Set the Data property:
{{queries.getRecords.data}}
Step 3: Enable Row Selection
In Table properties, navigate to Row Selection and enable:
- Allow selection
- Highlight selected row
- Bulk selection
Bulk Delete Rows
Step 1: Create a JavaScript Query to Generate Delete SQL Query
-
Create a RunJS Query named
generateDeleteSQLQueryto read selected rows from table and generate a DELETE query. -
Enter the following code:
const uniqueIdentifier = "id";
const idsToDelete = Object.values(components.ordersTable.selectedRows).map(dataUpdate => dataUpdate[uniqueIdentifier]);
const idsString = idsToDelete.map(id => `'${id}'`).join(', ');
const SQL = `DELETE FROM orders WHERE ${uniqueIdentifier} IN (${idsString});`;
return SQL; -
Click on the Preview button to see the SQL statement generated by the query.
Step 2: Create a New Postgres Query to Delete the Data
-
Create a Postgres Query named
deleteRecordsto run the query returned from generateDeleteSQLQuery and delete the data. -
Select SQL Mode.
-
Enter the following code:
{{queries.generateDeleteSQLQuery.data}}
-
Go to Settings. Add a new Event handler. Select Query Success as Event, Run Query as Action and getRecords as Query. This ensures the data is refreshed upon deletion of data.
Step 3: Link the JavaScript Query to the Delete Query
-
Open Settings for the generateDeleteSQLQuery.
-
Add a new Event handler. Select Query Success as Event, Run Query as Action and deleteRecords as Query.
This ensures that once the SQL is generated successfully, the delete query runs automatically.
Step 4: Add a Button to Delete the Selected Rows
-
Edit its properties and set the Label property to "Delete selected".
-
Add a new Event event handler to the button.
-
Select On click as the Event, Run Query as the Action, and generateDeleteSQLQuery as the Query.
-
Optionally, we can add a Loading state and Visibility as well.