Writing Rows to a Table in the Store

Writing Rows to a Child Table
Other put Operations

Writing a new row to a table in the store, and updating an existing row are usually identical operations (although methods exist that work only if the row is being updated, or only if it is being created — these are described a little later in this section).

Remember that you can only write data to a table after it has been added to the store. See Introducing Oracle NoSQL Database Tables and Indexes for details.

To write a row to a table in the store:

  1. Create a store handle and open it.

  2. Construct a Row node.js object. Each name in the object's name:value pairs must correspond to field name that has been declared for the table to which you will write the row.

  3. Use the Store.put() method to write the row to the store. This method has two required arguments. The first identifies the table to which you want to write the row. The second accepts the Row object you constructed in the previous step.

For example:

var nosqldb = require('nosqldb-oraclejs');

// Create a configuration object
var configuration = new nosqldb.Configuration();
configuration.proxy.startProxy = false;
configuration.proxy.host = 'localhost:7010';
configuration.storeHelperHosts = ['localhost:5000'];
configuration.storeName = 'kvstore';

// Open the store with the specified configuration
var store = nosqldb.createStore(configuration);

store.on('open', function () {
   console.log('Store opened');

   var row = {item: "Bolts", 
              description: "Hex head, stainless",
              count: 5,
              percentage: 0.2173913};

   store.put('myTable', row,
           function (err) {
                if (err)
                    throw err;
                else {
                    console.log("Row inserted.");
                    store.close();
                }
           });
}).on('close', function() {
    console.log('Store closed.');
}).on('error', function(error) {
    console.log(error);
});
store.open(); 

Writing Rows to a Child Table

To write to a child table, first create the row in the parent table to which the child belongs. You do this by populating the parent row with data. Then you write the child table's row(s). When you do, you must specify the primary key used by the parent table, as well as the primary key used by the child table's rows.

For example, in Defining Child Tables we showed how to create a child table. To write data to that table, do this:

var nosqldb = require('nosqldb-oraclejs');

// Create a configuration object
var configuration = new nosqldb.Configuration();
configuration.proxy.startProxy = false;
configuration.proxy.host = 'localhost:7010';
configuration.storeHelperHosts = ['localhost:5000'];
configuration.storeName = 'kvstore';

// Create a store with the specified configuration
var store = nosqldb.createStore(configuration);

store.on('open', function () {
    console.log('Store opened');

    var parentRow = {itemCategory:'Bolts',
        description:'Metric & US sizes'};

    store.put('myInventory', parentRow,
              function (err) {
              if (err)
                   throw err;
              else
                   console.log("Parent row inserted.");
              });

    var childRow = {itemCategory:'Bolts',
        itemSKU:'1392610',
        itemDescription:"1/4-20 x 1/2 Grade 8 Hex",
        price:11.99,
        inventoryCount:1457};

    store.put('myInventory.itemDetails', childRow,
              function (err) {
              if (err)
                   throw err;
              else
                   console.log("Child row inserted.");
                   store.close();
              });
}).on('close', function() {
    console.log('Store closed.');
}).on('error', function(error) {
        console.log(error);
    });
store.open(); 

Other put Operations

Beyond the very simple usage of the Store.put() method illustrated above, there are three other put operations that you can use:

  • Store.putIfAbsent()

    This method will only put the row if the row's primary key value DOES NOT currently exist in the table. That is, this method is successful only if it results in a create operation.

  • Store.putIfPresent()

    This method will only put the row if the row's primary key value already exists in the table. That is, this method is only successful if it results in an update operation.

  • Store.putIfVersion()

    This method will put the row only if the value matches the supplied version information. For more information, see Using Row Versions.