Get Rows

You can retrieve single row or multiple rows of data from a table in the Catalyst Data Store. The table object used in these code snippets is defined in the component instance page.

Get a Row Object

Before you fetch a row from a table in the Data Store, you must first create a row object for it using the unique Row ID of the row as shown below. This will not fire a server-side call.

Copied//Create a row object using the Row ID
var datastore = catalyst.table;
var table = datastore.tableId('ShipmentTracking');
var row = table.rowId(12781121212121);

Get a Single Row

You can now fetch a single row from a table using the get() method. You must pass the unique Row ID of the row to the row object that you created in the previous section, as shown below.

The promise returned here will be resolved to a JSON row object, in which the content key contains a row object.

Copied//Fetch the row details by passing the row ID to the row object
var datastore = catalyst.table;
var table = datastore.tableId('ShipmentTracking');
var row = table.rowId(12781121212121);
var rowPromise = row.get();
rowPromise
     .then((response) => {
             console.log(response.content);
 })
      .catch((err) => {
             console.log(err);
});

A sample response that you will receive is shown below. The response is the same for all versions of Web SDK.

Web SDK

Copied{
  CREATORID: "2136000000006003",
  MODIFIEDTIME: "2021-08-17 13:02:11:184",
  CREATEDTIME: "2021-08-16 16:29:10:499",
  CityName: "Pune",
  ROWID: "2136000000011011"
}

Get All Rows Through Pagination

You can retrieve all the rows of data from a table in the Data Store by incorporating pagination in your code using the getPagedRows() function. Pagination allows you to fetch the rows of a table in batches or pages through iterations.

This iteration is executed until all the rows fetched, which is validated by hasNext, as shown in the sample code below. You can refer to the table by its unique Table ID or name.

For example, if you require the rows to be fetched in batches of 100 as individual pages, you can specify the count of the max_rows parameter as 100, as shown below.

Note: The max_rows parameter is optional. The SDK call will return 200 rows in a single page by default if this value is not specified.

Additionally, after each execution of the loop, you will receive a token string in the response data that authorizes the subsequent fetching of data. You must pass this token through the next_token parameter during the subsequent iteration, as shown below. During the first execution of the loop, the value for the next_token string is assigned as undefined. The next set of records are fetched through more_records in the response data.

Note: Pagination has been made available from the Web SDK v3.1.0 update. This will not be available in the older versions of the Web SDK.
Copiedfunction getMyPagedRows(hasNext = true, next_token = undefined) {
    if (!hasNext) {
        return;
    } 
    catalyst.table
        .tableId('userDetails') //Specify the Table ID or Table name of the table to fetch the records from
        .getPagedRows({ next_token, max_rows: 100 }) //Define the maximum rows to be fetched in a single page and pass it along with nextToken
        .then(resp => {
            console.log('rows : ', resp.content); //Fetch the rows from the table
            return getMyPagedRows(resp.more_records, resp.next_token); //Fetch the next set of records and the token string for the next iteration
        })
        .catch((err) => {
            console.log(err.toString());
        }); 
}

A sample response that you will receive if there are more records available is shown below. The more_records parameter will be set to true in this case.

Web SDK v3.1.0

Copied{
    "status": 200,
    "content": [
        {
            "CREATORID": "3359000000006003",
            "MODIFIEDTIME": "2022-01-11 18:18:24:855",
            "name": "raj",
            "CREATEDTIME": "2022-01-11 18:18:24:855",
            "ROWID": "3359000000108111"
        },
        {
            "CREATORID": "3359000000006003",
            "MODIFIEDTIME": "2022-01-11 18:18:25:117",
            "name": "raj",
            "CREATEDTIME": "2022-01-11 18:18:25:117",
            "ROWID": "3359000000108114"
        },
        {
            "CREATORID": "3359000000006003",
            "MODIFIEDTIME": "2022-01-11 18:18:25:120",
            "name": "raj",
            "CREATEDTIME": "2022-01-11 18:18:25:120",
            "ROWID": "3359000000108117"
        }
    ],
    "message": "OK",
    "more_records": true,
    "next_token": "{{token}}"
}

A sample response that you will receive if there are no more records available is shown below. The more_records parameter will be set to false in this case.

Web SDK v3.1.0

Copied{
    "status": 200,
    "content": [
        {
            "CREATORID": "3359000000006003",
            "MODIFIEDTIME": "2022-01-11 18:18:43:556",
            "name": "raj99",
            "CREATEDTIME": "2022-01-11 18:18:43:556",
            "ROWID": "3359000000108410"
        },
        {
            "CREATORID": "3359000000006003",
            "MODIFIEDTIME": "2022-01-11 18:18:43:557",
            "name": "raj98",
            "CREATEDTIME": "2022-01-11 18:18:43:557",
            "ROWID": "3359000000108413"
        },
        {
            "CREATORID": "3359000000006003",
            "MODIFIEDTIME": "2022-01-11 18:18:43:568",
            "name": "raj96",
            "CREATEDTIME": "2022-01-11 18:18:43:568",
            "ROWID": "3359000000108417"
        }
    ],
    "message": "OK",
    "more_records": false
}
Note: We have deprecated support for the getAllRows() method that was available in the earlier versions of the Web SDK to fetch multiple rows of data from a table. Pagination is now available as an enhancement that enables you to fetch all rows, without any limitations on the number of rows fetched. The getAllRows() method will be removed from all future SDK versions. Please ensure that you upgrade your code accordingly.