Skip to content

Pipeline Terminator - Write Back

In this section, we’ll explore how to use write back 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 }
]);

You can also update/replace the data in the model using the commit terminator.

Commit Array of Objects Object

Section titled “Commit ”

The commit terminator commits the pipeline result back to the model.

var items_pl = items.pipe().isolate()
// let's say, we want to update the `price` and `is_locked` fields
// for items with `price` greater than or equal to 8
.filter(function(data, index) { // include only items with `price` greater than or equal to 8
return data.price >= 8;
})
.pluck(["id", "price", "is_locked"]) // pick the `id`, `price`, and `is_locked` fields
.map(function(data, index) { // map the result with the new values
return { id: data.id, price: data.price * 0.8, is_locked: true };
});
// result:
// [
// { id: 3, price: 12.0, is_locked: true },
// { id: 4, price: 10.0, is_locked: true },
// { id: 5, price: 6.4, is_locked: true },
// { id: 6, price: 8.0, is_locked: true },
// { id: 8, price: 7.2, is_locked: true }
// ]
// print `items` model first, so we can see the changes
items.print();
// (1) "patch" mode, update the existing data
items_pl.commit(); // commit the result back to the model
items.print(); // print all the data in the model
// (2) "replace" mode, clear the existing data and insert the new data
items_pl.commit({ mode: "replace" });
items.print(); // print all the data in the model

Executes the pipeline and commits the result back to the model.

pipeline.d.ts
class NimbusDBPipeline {
// ... other methods and properties ...
static commit(
_options?: NimbusDBPipelineCommitOptions
): void;
}
  • Type: NimbusDBPipelineCommitOptions
  • Default: undefined
  • An optional object that allows you to customize the behavior of the commit operation.
  • Type: void

Optional configurations for the commit terminator.

pipeline.d.ts
type NimbusDBPipelineCommitOptions = Partial<{
mode: "patch" | "replace"; // patch = `.upsert`, replace = `.clear()` then `.insert()` (default = patch)
}> & NimbusDBPipelineTerminalOptions
& (NimbusDBInsertOptions | NimbusDBUpsertOptions)
;