Get Rows

You can retrieve single row or multiple rows of data from a table in the Catalyst Data Store. The table reference used in these code snippets can either be a table instance or the table meta.

Get A Single Row

You can fetch a single row from a table using the getRow() method. You must pass the unique Row ID of the row to this method as shown in the sample code below.

The promise returned here will be resolved to a JSON row object.

    
copy
//Use the table instance or the table meta object to fetch a row by passing the Row ID let rowPromise = table.getRow(1510000000109476);

A sample response that you will receive is shown below. The response is the same for both versions of Node.js.

    
copy
{ 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 getMyPagedRows() 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 code below. You can refer to the table by its unique Table ID.

For example, if you require the rows to be fetched in batches of 100 as individual pages, you can define a variable for the maximum rows to be fetched in each page and specify the count. The sample code below assigns maxRows as 100.

Note: The maxRows 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 can fetch this token through next_token, and pass it as the value for nextToken during the subsequent iteration, as shown in the code below. During the first execution of the loop, the value for the nextToken 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 Node.js SDK v2.1.0 update. This will not be available in the older versions of the Node.js SDK.
    
copy
//Fetch rows through pagination and declare the value for nextToken as undefined for the first iteration function getMyPagedRows(hasNext = true, nextToken = undefined) { if (!hasNext) { return; } dataStore.table(195000000042025) //Specify the Table ID of the table to fetch the records from .getPagedRows({ nextToken, maxRows: 100 }) //Define the maximum rows to be fetched in a single page and pass it along with nextToken .then(({ data, next_token, more_records }) => { console.log('rows : ', data); //Fetch the rows from the table return getMyPagedRows(more_records, 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.

Node.js v2.1.0

    
copy
{ "status": 200, "data": [ { "CREATORID": "3359000000006003", "MODIFIEDTIME": "2022-01-11 18:18:24:855", "name": "Alex Jones", "CREATEDTIME": "2022-01-11 18:18:24:855", "ROWID": "3359000000108111" }, { "CREATORID": "3359000000006003", "MODIFIEDTIME": "2022-01-11 18:18:25:117", "name": "Robert Neal", "CREATEDTIME": "2022-01-11 18:18:25:117", "ROWID": "3359000000108114" }, { "CREATORID": "3359000000006003", "MODIFIEDTIME": "2022-01-11 18:18:25:120", "name": "Roslyn Gunn", "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.

Node.js v2.1.0

    
copy
{ "status": 200, "data": [ { "CREATORID": "3359000000006003", "MODIFIEDTIME": "2022-01-11 18:18:43:556", "name": "Alex Jones", "CREATEDTIME": "2022-01-11 18:18:43:556", "ROWID": "3359000000108410" }, { "CREATORID": "3359000000006003", "MODIFIEDTIME": "2022-01-11 18:18:43:557", "name": "Robert Neal", "CREATEDTIME": "2022-01-11 18:18:43:557", "ROWID": "3359000000108413" }, { "CREATORID": "3359000000006003", "MODIFIEDTIME": "2022-01-11 18:18:43:568", "name": "Roslyn Gunn", "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 earlier 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.

Last Updated 2024-04-03 14:37:15 +0530 +0530