Pipeline Terminator - Assertions
In this section, we’ll explore how to use assertion terminators 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 }]);Assertions
Section titled “Assertions”It means to check if the pipeline result meets the specified condition.
Every Array
Section titled “Every ”The every method checks if all elements in the pipeline result meet the specified condition.
var items_pl = items.pipe().isolate() .filter(function(data, index) { // include only items with `price` greater than or equal to 8 return data.price >= 8; }) .limit(3) // limit the result to 3 items .map(function(data, index) { // map the result to an array of strings return data.name; });
var result = items_pl.every(function(data, index) { return string_length(data) >= 5;});show_debug_message($"Result: {result}");
// Result: falseSome Array
Section titled “Some ”The some method checks if at least one element in the pipeline result meets the specified condition.
var items_pl = items.pipe().isolate() .filter(function(data, index) { // include only items with `price` greater than or equal to 8 return data.price >= 8; }) .limit(3) // limit the result to 3 items .map(function(data, index) { // map the result to an array of strings return data.name; });
var result = items_pl.some(function(data, index) { return string_length(data) >= 5;});show_debug_message($"Result: {result}");
// Result: trueIncludes Array
Section titled “Includes ”The includes method checks if the pipeline result contains the specified element.
var items_pl = items.pipe().isolate() .filter(function(data, index) { // include only items with `price` greater than or equal to 8 return data.price >= 8; }) .limit(3) // limit the result to 3 items .map(function(data, index) { // map the result to an array of strings return data.name; });
var result = items_pl.includes("Cherry");show_debug_message($"Result: {result}");
// Result: trueIndex of Array
Section titled “Index of ”The index_of method returns the index of the first occurrence of the specified element in the pipeline result.
var items_pl = items.pipe().isolate() .filter(function(data, index) { // include only items with `price` greater than or equal to 8 return data.price >= 8; }) .limit(3) // limit the result to 3 items .map(function(data, index) { // map the result to an array of strings return data.name; });
var result = items_pl.index_of("Date");show_debug_message($"Result: {result}");
// Result: 1Is Empty Array
Section titled “Is Empty ”The is_empty method checks if the pipeline result is empty.
var items_pl = items.pipe().isolate() .filter(function(data, index) { // include only items with `price` greater than or equal to 8 return data.price >= 8; }) .limit(3) // limit the result to 3 items .map(function(data, index) { // map the result to an array of strings return data.name; });
var result = items_pl.is_empty();show_debug_message($"Result: {result}");
// Result: falseReferences
Section titled “References”Pipeline.every()
Section titled “Pipeline.every()”Returns true if every element at the given cursor position satisfies the condition.
Signature
Section titled “Signature”class NimbusDBPipeline { // ... other methods and properties ... static every( _func: (element: any, index: int) => boolean, _options?: NimbusDBPipelineTerminalOptions ): boolean;}Parameters
Section titled “Parameters”- Type:
(element: any, index: int) => boolean- Parameters:
element: The current element.index: The current index.
- Returns:
trueif the condition is met,falseotherwise.
- Parameters:
- The condition function to use for the every operation.
_options
Section titled “_options”- Type:
NimbusDBPipelineTerminalOptions - Default:
undefined - An optional object that allows you to customize the behavior of the every operation.
Returns
Section titled “Returns”- Type:
boolean - The result of the every operation.
Signature
Section titled “Signature”class NimbusDBPipeline { // ... other methods and properties ... static every( _pos: int, _func: (element: any, index: int) => boolean, _options?: NimbusDBPipelineTerminalOptions ): boolean;}Parameters
Section titled “Parameters”- Type:
int - The cursor position used as the end of the pipeline operations.
- Type:
(element: any, index: int) => boolean- Parameters:
element: The current element.index: The current index.
- Returns:
trueif the condition is met,falseotherwise.
- Parameters:
- The condition function to use for the every operation.
_options
Section titled “_options”- Type:
NimbusDBPipelineTerminalOptions - Default:
undefined - An optional object that allows you to customize the behavior of the every operation.
Returns
Section titled “Returns”- Type:
boolean - The result of the every operation.
Pipeline.includes()
Section titled “Pipeline.includes()”Checks if the pipeline result contains the specified element.
Signature
Section titled “Signature”class NimbusDBPipeline { // ... other methods and properties ... static includes( _value: any, _options?: NimbusDBPipelineTerminalOptions ): boolean;}Parameters
Section titled “Parameters”_value
Section titled “_value”- Type:
any - The value to search for.
_options
Section titled “_options”- Type:
NimbusDBPipelineTerminalOptions - Default:
undefined - An optional object that allows you to customize the behavior of the includes operation.
Returns
Section titled “Returns”- Type:
boolean - The result of the includes operation.
Signature
Section titled “Signature”class NimbusDBPipeline { // ... other methods and properties ... static includes( _value: any, _pos?: int, _options?: NimbusDBPipelineTerminalOptions ): boolean;}Parameters
Section titled “Parameters”_value
Section titled “_value”- Type:
any - The value to search for.
- Type:
int - The cursor position used as the end of the pipeline operations.
_options
Section titled “_options”- Type:
NimbusDBPipelineTerminalOptions - Default:
undefined - An optional object that allows you to customize the behavior of the includes operation.
Returns
Section titled “Returns”- Type:
boolean - The result of the includes operation.
Pipeline.index_of()
Section titled “Pipeline.index_of()”Finds the index of an element matching a value or predicate function.
Signature
Section titled “Signature”class NimbusDBPipeline { // ... other methods and properties ... static index_of( _value: any, _options?: NimbusDBPipelineTerminalOptions ): int;}Parameters
Section titled “Parameters”_value
Section titled “_value”- Type:
any - The value to search for.
_options
Section titled “_options”- Type:
NimbusDBPipelineTerminalOptions - Default:
undefined - An optional object that allows you to customize the behavior of the index_of operation.
Returns
Section titled “Returns”- Type:
int - The result of the index_of operation.
Signature
Section titled “Signature”class NimbusDBPipeline { // ... other methods and properties ... static index_of( _value: any, _pos?: int, _options?: NimbusDBPipelineTerminalOptions ): int;}Parameters
Section titled “Parameters”_value
Section titled “_value”- Type:
any - The value to search for.
- Type:
int - The cursor position used as the end of the pipeline operations.
_options
Section titled “_options”- Type:
NimbusDBPipelineTerminalOptions - Default:
undefined - An optional object that allows you to customize the behavior of the index_of operation.
Returns
Section titled “Returns”- Type:
int - The result of the index_of operation.
Pipeline.is_empty()
Section titled “Pipeline.is_empty()”Checks whether the pipeline data at the given cursor position is empty.
Signature
Section titled “Signature”class NimbusDBPipeline { // ... other methods and properties ... static is_empty( _pos?: int, _options?: NimbusDBPipelineTerminalOptions ): boolean;}Parameters
Section titled “Parameters”- Type:
int - Default:
-1(the last step) - The cursor position used as the end of the pipeline operations.
_options
Section titled “_options”- Type:
NimbusDBPipelineTerminalOptions - Default:
undefined - An optional object that allows you to customize the behavior of the is_empty operation.
Returns
Section titled “Returns”- Type:
boolean - The result of the is_empty operation.
Pipeline.some()
Section titled “Pipeline.some()”Returns true if at least one element satisfies the condition.
Signature
Section titled “Signature”class NimbusDBPipeline { // ... other methods and properties ... static some( _func: (element: any, index: int) => boolean, _options?: NimbusDBPipelineTerminalOptions ): boolean;}Parameters
Section titled “Parameters”- Type:
(element: any, index: int) => boolean- Parameters:
element: The current element.index: The current index.
- Returns:
trueif the condition is met,falseotherwise.
- Parameters:
- The condition function to use for the some operation.
_options
Section titled “_options”- Type:
NimbusDBPipelineTerminalOptions - Default:
undefined - An optional object that allows you to customize the behavior of the some operation.
Returns
Section titled “Returns”- Type:
boolean - The result of the some operation.
Signature
Section titled “Signature”class NimbusDBPipeline { // ... other methods and properties ... static some( _pos: int, _func: (element: any, index: int) => boolean, _options?: NimbusDBPipelineTerminalOptions ): boolean;}Parameters
Section titled “Parameters”- Type:
int - The cursor position used as the end of the pipeline operations.
- Type:
(element: any, index: int) => boolean- Parameters:
element: The current element.index: The current index.
- Returns:
trueif the condition is met,falseotherwise.
- Parameters:
- The condition function to use for the some operation.
_options
Section titled “_options”- Type:
NimbusDBPipelineTerminalOptions - Default:
undefined - An optional object that allows you to customize the behavior of the some operation.
Returns
Section titled “Returns”- Type:
boolean - The result of the some operation.