Introduction
This section discusse how to paginate through a cas table with a single restaflib method called casFetchData
casFetchData method
casFetchData is a method in restaflib for paginating through a CAS table. The arguments to this method are:
- store - the restaf store object
- session - the cas session object obtained via casSetup
- payload - the payload to be processed on the cas server. The schema of the payload is
- table - table of interest ex: {caslib: 'public', name: 'cars'}
- from - the starting record no.
- count - no of rows to be returned
- format - if true the results are formatted
The response has the following schema:
{
pagination: {
prev: {
table: { caslib: caslib, name: name},
from: first record,( -1 if currently at top)
count: no of records,
format: If true data is formatted
},
next: {
table: { caslib: caslib, name: name},
from: first record,(-1 if currently at the bottom)
count: no of records,
format: If true data is formatted
}
},
data: {
schema: [Array of schema conforming to CAS standards],
rows: [ Array of rows of data]
}
Example
As in the previous two blogs casSetup is used to obtain the cas session. This example shows how to paginate thru the table either forward or backward.
async function example () {
let {session} = await restaflib.casSetup(store);
let payload = {
from : 1,
count : 20,
format: true,
table : {caslib: 'Public', name: 'cars'}
}
// Get the first 20 records
let result = await restaflib.casFetchData(store, session, payload);
.. process the data ...
// Scroll forward to the end
while (result.pagination.next.from !== -1) {
result = await restaflib.casFetchData(store, session, result.pagination.next);
... process the data ...
}
console.log('--------------------------------------- scroll backwards');
// use the last prev to start scrolling backwards to the top
while (result.pagination.prev.from !== -1) {
result = await restaflib.casFetchData(store, session, result.pagination.prev);
... process the data...
}
await store.apiCall(session.links('delete'));
}
Please see here for a working example Below is a sample output for the our favorite cars table.
{
"pagination": {
"next": {
"count": 20,
"format": true,
"from": 41,
"table": {
"caslib": "Public",
"name": "cars"
}
},
"prev": {
"count": 20,
"format": true,
"from": 21,
"table": {
"caslib": "Public",
"name": "cars"
}
}
},
"data": {
"rows": [
[
1,
"Acura",
"MDX ",
"SUV ",
"Asia ",
"All ",
" 36945",
" 33337",
" 3.5",
" 6",
" 265",
" 17",
" 23",
" 4451",
" 106",
" 189"
],
[...], [...], ...
],
"schema": [
{
"Column": "_Index_",
"FormattedLength": 5,
"ID": 0,
"NFD": 0,
"NFL": 0,
"RawLength": 5,
"Type": "double"
},
{...}, {..}, ...
]
}
}
}
Notes
The casFetchData method uses the caslRun method and casl statements. The casl statements for this method are here
Setup
Web Applications
Include the following two script tags.
<script src="https://unpkg.com/@sassoftware/restaf/dist/restaf.min.js"></script>
<script src="https://unpkg.com/@sassoftware/restaflib/dist/restaflib.min.js"></script>
Two globals restaf and restaflib will be available for use in your script tags.
Nodejs application
Install restaf and restaflib using the following command
npm install @sassoftware/restaf @sassoftware/restaflib