Indexing Embedded Records

You can create an index on an embedded record field so long as the record field contains scalar data. To create the index, define the record as normal. To index the field, you specify the name of the embedded record and the name of the field using dot notation.

To create the index, first create the table:

CREATE Table myRecordTable (
    uid INTEGER,
    myRecord RECORD (firstField STRING, secondField INTEGER),
    PRIMARY KEY (uid)
) 

Once the table has been added to the store, create the index:

CREATE INDEX recordFieldIndex on myRecordTable (myRecord.secondField) 

Data is retrieved if the table row contains the identified record field with the specified value. So, for example, if you create a series of table rows like this:

function writeRow(store, uid, record) {

   var row = {
       uid: uid,
       myRecord: record
   };
   console.log("Putting row");
   store.put('myRecordTable', row,
           function (err) {
                if (err)
                    throw err;
                else {
                    console.log("Row inserted.");
                }
           });
   console.log("Row put");
}

...
// Store handle configuration and open skipped for brevity
...

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


   writeRow(store, 12345, {firstField: "String field for 12345",
                           secondField: 3388});
   writeRow(store, 345, {firstField: "String field for 345",
                           secondField: 3388});
   writeRow(store, 111, {firstField: "String field for 111",
                          secondField: 12});
   store.close();
}).on('close', function() {
    console.log('Store closed.');
}).on('error', function(error) {
    console.log(error);
});
store.open(); 

then you can retrieve any table rows that contain the embedded record where "secondField" is set to a specified value. (The embedded record index that we specified, above, indexed myRecord.secondField.)

You retrieve the matching table rows, and iterate over them in the same way you would any other index type. For example:

...
// Store handle configuration and open skipped for brevity
...

store.on('open', function () {
   console.log('Store opened');
   var indexKey1 = {myRecord: {secondField: 3388} }

   store.indexIterator('myRecordTable', 'recordFieldIndex',
            {indexKey: indexKey1},
            function (err, iterator) {
                  iterator.forEach(function (err, currentRow) {
                     console.log(currentRow.row);
                  });
                  store.close();
            }
   );

}).on('close', function() {
    console.log('Store closed.');
}).on('error', function(error) {
    console.log(error);
});
store.open();