Bulk Insert Records into Database - SQL

This article discusses how you can insert multiple records of data into your SQL database. For this tutorial, we will be working with PostgreSQL using Supabase. With the help of JS Code Block, data from the controls e.g. Tablegrid can be transformed upon which the bulk insert query syntax can be built and returned from the function.

Create Insert Query

Once you are done configuring PostgreSQL connector, create an Insert query as shown below. Write the insert query for your table with appropriate field names and declare a variable, in our case it is “records” which will hold the values that needs to be inserted into the table. The “record” variable will capture the input values as passed by the JS Code Block.

App Builder

For this use case, we have taken a TableGrid binded to a PostgreSQL table and a button with the label “Insert Records”. The button’s click action will trigger the bulk insert logic.

Insert Button - Click Action

Add a JS Code Block and create variables for each Tablegrid field respectively.


let records = [];
for(var i = 0; i < ids.length; i++) {
  records.push(`(${ids[i]}, '${firstnames[i]}', 
                '${lastnames[i]}', '${addresses[i]}', 
                '${emails[i]}', '${designations[i]}')`);
output = records.join(', ');


Code Explanation

An empty array variable “records” is created, and for each loop iteration, a value string is created which is pushed onto the “records” array. The value string takes the form of (value1, value2, …, valueN). The array values are then joined with a comma and assigned to the output variable.

Now click on continue and add a variable that will hold the JSCode Block output, in our case, it is records.

Now add the database connector and select the Insert Query we created earlier. In the “records” field, select the JSCode output by clicking on the Use Keywords option.

Finally, run the app and select any of the rows from the TableGrid and click on the “Insert Records” button.

Selected records from tablegrid appear in the PostgreSQL table, once the “Insert Records” button is clicked.