Skip to content

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 }
]);

It means to check if the pipeline result meets the specified condition.

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: false

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: true

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: true

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: 1

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: false

Returns true if every element at the given cursor position satisfies the condition.

pipeline.d.ts
class NimbusDBPipeline {
// ... other methods and properties ...
static every(
_func: (element: any, index: int) => boolean,
_options?: NimbusDBPipelineTerminalOptions
): boolean;
}
  • Type: (element: any, index: int) => boolean
    • Parameters:
      • element: The current element.
      • index: The current index.
    • Returns: true if the condition is met, false otherwise.
  • The condition function to use for the every operation.
  • Type: NimbusDBPipelineTerminalOptions
  • Default: undefined
  • An optional object that allows you to customize the behavior of the every operation.
  • Type: boolean
  • The result of the every operation.

Checks if the pipeline result contains the specified element.

pipeline.d.ts
class NimbusDBPipeline {
// ... other methods and properties ...
static includes(
_value: any,
_options?: NimbusDBPipelineTerminalOptions
): boolean;
}
  • Type: any
  • The value to search for.
  • Type: NimbusDBPipelineTerminalOptions
  • Default: undefined
  • An optional object that allows you to customize the behavior of the includes operation.
  • Type: boolean
  • The result of the includes operation.

Finds the index of an element matching a value or predicate function.

pipeline.d.ts
class NimbusDBPipeline {
// ... other methods and properties ...
static index_of(
_value: any,
_options?: NimbusDBPipelineTerminalOptions
): int;
}
  • Type: any
  • The value to search for.
  • Type: NimbusDBPipelineTerminalOptions
  • Default: undefined
  • An optional object that allows you to customize the behavior of the index_of operation.
  • Type: int
  • The result of the index_of operation.

Checks whether the pipeline data at the given cursor position is empty.

pipeline.d.ts
class NimbusDBPipeline {
// ... other methods and properties ...
static is_empty(
_pos?: int,
_options?: NimbusDBPipelineTerminalOptions
): boolean;
}
  • Type: int
  • Default: -1 (the last step)
  • The cursor position used as the end of the pipeline operations.
  • Type: NimbusDBPipelineTerminalOptions
  • Default: undefined
  • An optional object that allows you to customize the behavior of the is_empty operation.
  • Type: boolean
  • The result of the is_empty operation.

Returns true if at least one element satisfies the condition.

pipeline.d.ts
class NimbusDBPipeline {
// ... other methods and properties ...
static some(
_func: (element: any, index: int) => boolean,
_options?: NimbusDBPipelineTerminalOptions
): boolean;
}
  • Type: (element: any, index: int) => boolean
    • Parameters:
      • element: The current element.
      • index: The current index.
    • Returns: true if the condition is met, false otherwise.
  • The condition function to use for the some operation.
  • Type: NimbusDBPipelineTerminalOptions
  • Default: undefined
  • An optional object that allows you to customize the behavior of the some operation.
  • Type: boolean
  • The result of the some operation.