Docs / Language & functions
a9script authoring pack — pagination functions
The pagination functions, with their arguments and a line of real script each. Called by bare name; nothing is imported. A call marked pauses does real work outside the script: the run is saved, and continues on the next line with the result.
fetchPage(functionName, arguments, cursor?) — pauses
Reads ONE page from a configured function that was set up to page, and tells you how to ask for the next one. The hand-written way through a collection — reach for dsFetchInto when the pages are only going into a dataset anyway.
functionName(string) — The configured function to call. It must have a pagination block: without one it answers a single result, and paging it is refused rather than guessed at.arguments(object) — The arguments that function declares, exactly as a bare call passes them. They are sent again for every page — a page is the same call, asked for a different slice.cursor(any, optional) — Where to continue: what the previous page answered asnext. Left out, the walk starts at the first page.
Answers: { items, next, delta }. items is this page’s records as a list. next is what to pass as the cursor for the page after this one — ABSENT when this was the last page, which is how the walk ends. delta is the provider’s sync token when the function’s block names where it lives, usually only on the last page; store it with setState if the next sync should ask for changes only.
const page = fetchPage("MyPagedFunction", {});
log(page.items.length + " records on the first page");
if (page.next === undefined) { log("that was all of them"); }
dsFetchInto(functionName, arguments, dataset, options?) — pauses
Pulls EVERY page of a configured function into a dataset — one call instead of a cursor loop. The platform fetches a page, stages it, and pauses the run between pages, so a pull of a hundred pages costs a hundred short segments instead of one run holding a worker; if the far end says it is being called too often, the run waits and continues from the page it was on rather than starting over.
functionName(string) — The configured function to walk. It must have a pagination block.arguments(object) — The arguments that function declares — sent again for every page.dataset(string) — Where the records are staged. Arun:name is private to this run and cleaned up after it —dsRenameit to a durable name to keep it, which is how a pulled set becomes the next sync’s baseline.options(object, optional) —{ keyPath }— which field of a record is its key in the dataset. Left out, the record’s own id is used:id,IdorID, whichever it has. A record with none of them, and nokeyPathto say otherwise, is refused rather than given an invented key.
Answers: { pages, rows, delta } once the whole collection is in: how many pages were fetched, how many records were staged, and the provider’s sync token when its block names one. A collection with more pages than the function’s configured page limit is refused as PULL_INCOMPLETE, naming the limit and what was staged: a partial pull never passes for a whole one, and the staged records stay.
const pull = dsFetchInto("MyPagedFunction", {}, "run:contacts");
log("staged " + pull.rows + " records from " + pull.pages + " pages");
dsRename("run:contacts", "contacts");