In this article, you'll learn how to:
Import files into your Betty Blocks application
Getting started
The use case for this example is to import a CSV file containing 'end-users'. The records include first name, last name, email, and an external identifier for each end-user. A link to a sample CSV file with end-users is provided below.
If you want to learn how to import other files and create a relation with another model based on a common field like the 'external identifier' used here, make sure to read our Indexed collections article.
Make sure to have a data model present with the following properties:
With the data model ready let's get started.
Uploading your file
We'll begin by uploading the end-user data into the application. Download the CSV file provided below and upload it to your public files (accessible in the Tools section).
We'll use the link to the file later, the next step is to install the required action steps. We'll need these two action steps from the block store: 'Mass mutate' and 'Parse data to array'.
Creating the import functionality
After installing the steps, you can create a new action as follows:
Drag the Mass mutate step on the canvas and the Parse data to array step within it. The Mass mutate step doesn't need any configuration, the Parse data to array step needs a text variable with the link to the public file like this:
Press Save to save the variable inside the Parse data to array step, before we continue with the action we need to create a schema model. A schema model is used by the Parse data to array step to create a 'blueprint' to write the data towards, we can then use the blueprint in another action step like a Create record step later on.
Create a schema model called 'End-user Schema'. The properties within this model should exactly match the names of the columns in your source CSV file. In our case, add these properties:
With the schema model in place, let's navigate back to the Import end-users action and configure the Parse data to array step further with our new schema model:
After configuring the step above drag a Loop step underneath it and configure it like this:
To create individual records for each entry in the CSV file, we'll use a Loop step to iterate through the array from the Parse data to array step. Within the Loop, a Create record step will be added and configured as shown in the image below.
Notice that we can click the right arrow in the yielded section to select the properties of our 'End-user Schema' model. This allows us to map these selected properties to the corresponding properties in the model we created earlier.
Here's what the completed action will look like:
In essence, we've used the Mass Mutate step to efficiently process data in batches. This avoids the inefficiency of looping and creating individual records, which would trigger a database query for each iteration.
The Parse data to array step facilitates this process by allowing us to define a schema model and iterate through the parsed data. Within the loop, a new record is created for each entry. Finally, the Mass mutate step efficiently processes the accumulated data once the loop completes.
Run the action
Run the action by hitting the Test run button in the top-right.
To verify the success of the action, create a new user management page using the back office template. Configure the template to display the properties we've added (first name, last name, email, and external identifier). Once created, navigate to the newly created page to confirm that the imported end-users, including their first names, last names, emails, and external identifiers, are displayed correctly.
This use case can be applied to other CSV or JSON files as well. You can use the knowledge from this use case in two ways:
Recreate the action with adjustments: Build a new action with a different model, schema model, and public file URL specific to a different file that you want to import.
Modify the existing action: Update the current action by changing the model, schema model, and public file URL specific to a different file that you want to import.
As stated at the beginning of this article, if you want to learn how to import other files and create a relation with another model based on a value like the external identifier in this example, make sure to read our Indexed collections article.