Data Bag in OutSystems

A Data Bag is a simple and convenient way to store structured information in OutSystems. In general, one can say that a Data Bag is a global variable that is stored as JSON data. And JSON is great for data exchange, small storage and generically defined structures.

You can use Data Bags to:

  • Backup your database to local files
  • Move data from a screen to another
  • Store temporary data
  • Move data from one data source to another
  • Store an app’s source URL
  • Search Bar
  • Store configuration data
  • Save arbitrary key/value user preferences

And certainly, there are other ways you can use a Data Bag.

How to develop the Data Bag

1. Create Data Bag entity

To create a Data Bag, you must first create an entity. It can be a simple entity. Only with the “Id”, a “Data” attribute which is where the JSON data will be stored (with a length of 2500 to 5000), and audit attributes like “CreatedOn” (that can be used for sanitization) and “CreatedBy” (that can be used for security).

2. Create the structures that will be stored

In this step, it is necessary to create the different structures you will need to store the data in your Data Bag. Here a sample structure is created.

3. Store data in the Data Bag

To store data in the Data Bag, it is necessary to create a specific action for each structure that you have. It is important to note that you can have different structures in a single Data Bag. To store the data, it is necessary to serialize the JSON, encodes objects in a string.

4. Retrieve data from the Bag Data

To retrieve the data, one can go through two approaches. Via the DataBagId or via index search. After retrieving the data, it is necessary to deserialize the JSON into the correct data type.

  • Via Data Bag identifier

Have an action that through DataBagId obtains the data. And then deserialize the data to the structure data type needed.

  • Via index search

Using Aggregate to search for the desired text. But to have this approach, it is necessary that the structure is prepared to have a Keyword that identifies the element that needs to be obtained.
To know if one text/string is contained in another text/string, use the Text Built-in Function Index. After retrieving the data use the deserializer with the structure data type needed.

Index(text,keyword) > -1

5. Data Bag sanitization

In order for the Data Bag to avoid data accumulation that is no longer needed, it is necessary to remove unnecessary data. For this, a Timer is created to sanitize. The Timer must be scheduled to remove the data with the best time spacing.

This is the action that the Timer runs. In here it is scheduled to remove data that is 2 or more days old.

where {DataBag}.[CreatedOn] < @DaysAgo 
-- Or you can use DATEDIFF
--DELETE FROM {DataBag} WHERE DATEDIFF(DAY, {DataBag}.[CreatedOn], @CurrDate) > 2;

In conclusion we can see that Data Bags are a powerful feature that lets you store global information. The flexibility allowed by the Data Bag is one of its greatest assets. Used in the right context, it can bring great benefits to the project.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s