Data Pagination in Pipelines
In this section, we’ll explore how to paginate your data in Pipeline. We’ll use items model for this example.
var schema = { id: { type: NIMBUSDB_DATA_TYPE.INTEGER, const: NIMBUSDB_CONSTRAINT.PRIMARY_KEY }, name: NIMBUSDB_DATA_TYPE.STRING, price: { type: NIMBUSDB_DATA_TYPE.NUMBER, validator: function(data, value) { return value >= 0; }, default_value: 0 }, is_locked: { type: NIMBUSDB_DATA_TYPE.BOOLEAN, const: NIMBUSDB_CONSTRAINT.OPTIONAL, default_value: false }};
items = new NimbusDBModel("global", "items", schema, [ { id: 1, name: "Apple", price: 5 }, { id: 2, name: "Banana", price: 7.2 }, { id: 3, name: "Cherry", price: 15 }, { id: 4, name: "Date", price: 12.5 }, { id: 5, name: "Elderberry", price: 8 }, { id: 6, name: "Fig", price: 10 }, { id: 7, name: "Grape", price: 6 }, { id: 8, name: "Honeydew", price: 9 }, { id: 9, name: "Kiwi", price: 4 }, { id: 10, name: "Lemon", price: 3 }]);Paginating Data
Section titled “Paginating Data”Limit Array
Section titled “Limit ”The limit operation is used to limit the data to the first _count elements. A negative value takes from the end.
// (1) limit to the first 3 elementsvar items_pl = items.pipe() // create an `items` pipeline // let's say you have this data in your pipeline: // [ 5, 7.2, 15, 12.5, 8, 10, 6, 9, 4, 3 ] .limit(3); // limit to the first 3 elements
// result:// [ 5, 7.2, 15 ]
// (2) limit to the last 3 elementsvar items_pl = items.pipe() // create an `items` pipeline // let's say you have this data in your pipeline: // [ 5, 7.2, 15, 12.5, 8, 10, 6, 9, 4, 3 ] .limit(-3); // limit to the last 3 elements
// result:// [ 3, 4, 9 ]Offset Array
Section titled “Offset ”The offset operation is used to skip the first _count elements. A negative value skips from the end.
// (1) skip the first 3 elementsvar items_pl = items.pipe() // create an `items` pipeline // let's say you have this data in your pipeline: // [ 5, 7.2, 15, 12.5, 8, 10, 6, 9, 4, 3 ] .offset(3); // skip the first 3 elements
// result:// [ 12.5, 8, 10, 6, 9, 4, 3 ]
// (2) skip the last 3 elementsvar items_pl = items.pipe() // create an `items` pipeline // let's say you have this data in your pipeline: // [ 5, 7.2, 15, 12.5, 8, 10, 6, 9, 4, 3 ] .offset(-3); // skip the last 2 elements (because `-1` is the last element)
// result:// [ 9, 6, 10, 8, 12.5, 15, 7.2, 5 ]Slice Array
Section titled “Slice ”The slice operation is used to extract a slice of the data starting at _start for _length elements.
// (1) extract a slice of the data starting at index 2var items_pl = items.pipe() // create an `items` pipeline // let's say you have this data in your pipeline: // [ 5, 7.2, 15, 12.5, 8, 10, 6, 9, 4, 3 ] .slice(2); // start at index 2 (the third element) and extract remaining elements
// result:// [ 15, 12.5, 8, 10, 6, 9, 4, 3 ]
// (2) extract a slice of the data starting at index 2 with a length of 2var items_pl = items.pipe() // create an `items` pipeline // let's say you have this data in your pipeline: // [ 5, 7.2, 15, 12.5, 8, 10, 6, 9, 4, 3 ] .slice(2, 3); // start at index 2 (the third element) and extract 3 elements
// result:// [ 15, 12.5, 8 ]
// (3) extract a slice of the data from the 3 last elementsvar items_pl = items.pipe() // create an `items` pipeline // let's say you have this data in your pipeline: // [ 5, 7.2, 15, 12.5, 8, 10, 6, 9, 4, 3 ] .slice(-3); // start at the third-last element and extract remaining elements
// result:// [ 9, 4, 3 ]
// (4) extract a slice of the data from the 3 last elements with a length of 2var items_pl = items.pipe() // create an `items` pipeline // let's say you have this data in your pipeline: // [ 5, 7.2, 15, 12.5, 8, 10, 6, 9, 4, 3 ] .slice(-3, 2); // start at the third-last element and extract 2 elements
// result:// [ 9, 4 ]
// (5) extract a slice of the data from the 3 last elements with a length of 4 (reversed)var items_pl = items.pipe() // create an `items` pipeline // let's say you have this data in your pipeline: // [ 5, 7.2, 15, 12.5, 8, 10, 6, 9, 4, 3 ] .slice(-3, -4); // start at the third-last element and extract 4 elements (reversed)
// result:// [ 9, 6, 10, 8 ]References
Section titled “References”Pipeline.limit()
Section titled “Pipeline.limit()”Limits the pipeline data to the first _count elements. A negative value takes from the end.
Signature
Section titled “Signature”class NimbusDBPipeline { // ... other methods and properties ... static limit( _count: int ): NimbusDBPipeline;}Parameters
Section titled “Parameters”_count
Section titled “_count”- Type:
int - The number of elements to keep. Negative values take from the end.
Returns
Section titled “Returns”- Type:
NimbusDBPipeline - A new
NimbusDBPipelineinstance (mutable = false) or the current pipeline instance (mutable = true).
Pipeline.offset()
Section titled “Pipeline.offset()”Skips the first _count elements. A negative value skips from the end.
Signature
Section titled “Signature”class NimbusDBPipeline { // ... other methods and properties ... static offset( _count: int ): NimbusDBPipeline;}Parameters
Section titled “Parameters”_count
Section titled “_count”- Type:
int - The number of elements to skip.
Returns
Section titled “Returns”- Type:
NimbusDBPipeline - A new
NimbusDBPipelineinstance (mutable = false) or the current pipeline instance (mutable = true).
Pipeline.slice()
Section titled “Pipeline.slice()”Extracts a slice of the pipeline data starting at _start for _length elements.
Signature
Section titled “Signature”class NimbusDBPipeline { // ... other methods and properties ... static slice( _start: int, _length?: int ): NimbusDBPipeline;}Parameters
Section titled “Parameters”_start
Section titled “_start”- Type:
int - The starting index (negative = from the end).
_length
Section titled “_length”- Type:
int - Default:
undefined(data.length - start) - The number of elements to include.
Returns
Section titled “Returns”- Type:
NimbusDBPipeline - A new
NimbusDBPipelineinstance (mutable = false) or the current pipeline instance (mutable = true).