Data Mutation
In this section, we will discuss how to mutate/modify NimbusDBData instances. We’ll use this schema and the items model as an 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 }]);Adding/Removing Fields
Section titled “Adding/Removing Fields”We can add or remove fields from a record regardless of the schema.
// let's use `apple` record as an examplevar apple = items.get_by_primary_linked(1);
// you can also use `.find()` to get the linked data// var apple = items.find(1);
// add a new fieldapple.add({ color: "red" });apple.print();
// add multiple fieldsapple.add({ weight: 150, unit: "grams"});apple.print();
// remove a fieldapple.remove("color");apple.print();
// remove multiple fieldsapple.remove(["id", "name"]);apple.print();Value Increment/Decrement
Section titled “Value Increment/Decrement”// let's use `apple` record as an examplevar apple = items.get_by_primary_linked(1);
// you can also use `.find()` to get the linked data// var apple = items.find(1);
// increment `price` by 1apple.inc("price");apple.print();
// increment `price` by 2.5apple.inc("price", 2.5);apple.print();
// increment `price` and `owned` by 1apple.inc(["price", "owned"]);// but only `price` will be incremented, because `owned` doesn't exist in the recordapple.print();
// increment `price` and `owned` by 2, and create all columns if it doesn't existapple.inc(["price", "owned"], [2, 2], { create_if_missing: true});apple.print(); // now "owned" column exist in the record
// decrement `price` by 1apple.dec("price");apple.print();
// decrement `price` by 2.5apple.dec("price", 2.5);apple.print();
// decrement `price` by 2.5 and `owned` by 1apple.dec(["price", "owned"], [2.5, 1]);apple.print();Modification to the Original Record
Section titled “Modification to the Original Record”You can use write_through: true or affect_original: true option to sync the changes back to the original data.
// let's use `apple` record for non-write-through example// and `banana` for write-through examplevar apple = items.find(1);var banana = items.find(2);
// add a new fieldapple.add({ color: "red" });banana.add({ color: "yellow" }, { write_through: true});
// now print to see the differencesapple.print();banana.print();items.print({ pick: ["id", "name", "price", "color"]});
// remove a fieldapple.remove("color");banana.remove("color", { write_through: true});
// increment/decrement priceapple.inc("price");banana.inc("price", 2, { write_through: true});
// check the differences againapple.print(); // `price` = 6banana.print(); // `price` = 9.2items.print({ // apple `price` = 5, banana `price` = 9.2 pick: ["id", "name", "price", "color"]});Isolating Records
Section titled “Isolating Records”Use .isolate() method to get an isolated copy of a record, which can be used to modify the record without affecting the original data.
// both are linked copiesvar apple = items.find(1);var banana = items.find(2);
// isolate `banana` recordbanana = banana.isolate();// now `banana` is isolated copy, and `apple` is still linked copy// you can't do any data-scope operation on `banana`
// these will throw an error// banana.inc("price");// banana.dec("price");// banana.remove("color");// banana.add("color", "yellow");
// any modification on `banana` won't affect the original recordbanana.name = "strawberry";show_debug_message($"Modified name: {banana.name}."); // "strawberry"items.find(2).print(); // `name` is still "banana"References
Section titled “References”Data.add()
Section titled “Data.add()”Adds extra properties or update existing properties of this data instance, bypassing schema rules, type checks, and validators.
Signature
Section titled “Signature”class NimbusDBData { // ... other methods and properties ... static add( _data: Struct, _options?: NimbusDBDataOpsOptions ): void;}Parameters
Section titled “Parameters”- Type:
Struct - The extra data object/struct to merge in.
_options
Section titled “_options”- Type:
NimbusDBDataOpsOptions - Default:
undefined - Optional configurations for the operation.
Data.dec() | Data.decrement()
Section titled “Data.dec() | Data.decrement()”Signature
Section titled “Signature”class NimbusDBData { // ... other methods and properties ... static dec( _column: string | string[], _by?: number | number[], _options?: NimbusDBDataNumopsOptions ): void;}Parameters
Section titled “Parameters”_column
Section titled “_column”- Type:
string|string[] - The column name or array of column names to decrement.
- Type:
number|number[] - Default:
1|[1, ..., 1](array length equals to_columnlength) - Amount or array of amounts to decrement by.
_options
Section titled “_options”- Type:
NimbusDBDataNumopsOptions - Default:
undefined - Optional configurations for the operation.
Data.inc() | Data.increment()
Section titled “Data.inc() | Data.increment()”Signature
Section titled “Signature”class NimbusDBData { // ... other methods and properties ... static inc( _column: string | string[], _by?: number | number[], _options?: NimbusDBDataNumopsOptions ): void;}Parameters
Section titled “Parameters”_column
Section titled “_column”- Type:
string|string[] - The column name or array of column names to increment.
- Type:
number|number[] - Default:
1|[1, ..., 1](array length equals to_columnlength) - Amount or array of amounts to increment by.
_options
Section titled “_options”- Type:
NimbusDBDataNumopsOptions - Default:
undefined - Optional configurations for the operation.
Data.isolate()
Section titled “Data.isolate()”Signature
Section titled “Signature”class NimbusDBData { // ... other methods and properties ... static isolate(): Struct;}Returns
Section titled “Returns”- Type:
Struct - Returns an isolated copy of the data.
Data.print()
Section titled “Data.print()”Signature
Section titled “Signature”class NimbusDBData { // ... other methods and properties ... static print(): void;}Data.remove()
Section titled “Data.remove()”Signature
Section titled “Signature”class NimbusDBData { // ... other methods and properties ... static remove( _prop: string | string[], _options?: NimbusDBDataOpsOptions ): void;}Parameters
Section titled “Parameters”- Type:
string|string[] - The column name or array of column names to remove.
_options
Section titled “_options”- Type:
NimbusDBDataOpsOptions - Default:
undefined - Optional configurations for the operation.
NimbusDBDataOpsOptions
Section titled “NimbusDBDataOpsOptions”Optional configurations for NimbusDBData operations.
type NimbusDBDataOpsOptions = Partial<{ write_through: boolean; // sync the change to the original data (default = false) affect_original: boolean; // alias for `write_through`}>;NimbusDBDataNumopsOptions
Section titled “NimbusDBDataNumopsOptions”Optional configurations for numeric operations on NimbusDBData instances.
type NimbusDBDataNumopsOptions = Partial<{ create_if_missing: boolean; // create the column if it's missing (default = false)}> & NimbusDBDataOpsOptions;