Nav

Example: To Store and Retrieve Information in an Object Store

The Object Store connector provides Store (<os:store/>) and Retrieve (<os:retrieve/>) operations.

Storing Information from the Object Store

You can use an Object Store to store values like this:


         
      
1
2
3
4
5
6
7
8
9
10
11
12
13
<os:store key="state">
  <os:value>
    <![CDATA[#[
      output application/json
       ---
       {
          "id": attributes.queryParams.id,
          "timestamp": now(),
          "name": payload.name
        }
      ]]]>
  </os:value>
</os:store>

You can use the Store operation to store new values or update existing ones, depending on the value of the failIfPresent parameter. When this parameter is set to false (the default), any pre-existing value associated to that key is overwritten. If the parameter is set to true, then an OS:KEY_ALREADY_EXISTS error is thrown instead.

Note that it is not possible to store null values. However, it is common to obtain a value by evaluating an expression or transformation:

  • Testing that the value is not null.

  • Storing the value if it is present.

  • Otherwise, doing nothing.

The failOnNullValue parameter simplifies this use case. By default (true), a OS:NULL_VALUE error is thrown if a null value is supplied. However, when the parameter is set to false, a null value causes this operation to do nothing: No error is raised, and no value is altered.

Retrieving Information from the Object Store

The Retrieve operations retrieves the value of given Object Store key.

You can use the key to retrieve single values:

<os:retrieve key="userId" />

You can use an operation like this one to provide default values when the key is not present in the store:


         
      
1
2
3
<os:retrieve key="timestamp">
  <os:default-value>#[now()]</os:default-value>
</os:retrieve>

You can have default, complex structures:


         
      
1
2
3
4
5
6
7
8
9
10
11
12
13
<os:retrieve key="state">
  <os:default-value>
    <![CDATA[#[
     output application/json
     ---
     {
        "id": attributes.queryParams.id,
        "timestamp": now(),
        "name": payload.name
      }
    ]]]>
  </os:default-value>
</os:retrieve>

Note that the defaultValue parameter handles cases in which no value exists for a given key. If you do not provide the parameter and the operation resolves to a null value, an OS:KEY_NOT_FOUND error is thrown. Otherwise, the defaultValue is returned but not stored.

All Object Store operations are synchronized at the key level. No other operation is able to access the same key on the same Object Store while the operation is running. If the runtime is running in cluster mode, this synchronization is also guaranteed across nodes.