ネストされた表の反復使用

表を反復して使用する場合、または複数の取得操作を実行している場合、デフォルトでは操作中の表から行のみが取得されます。ただし、取得方法にOptionsオブジェクトを使用すると、その親表および子表も取得するように指定できます。

これを実行する場合、まず親表が取得され、操作中の表が取得された後、子表が取得されます。つまり、表の階層的順序が順守されます。

取得された親表および子表はテキスト配列を使用して識別され、続いてoptionsオブジェクトのincludedTablesプロパティに指定されます。

複数の表から取得された行を操作する場合、その行が属する表を決定するのはユーザーです。

たとえば、次のような子および孫表を含む表を作成するとします。

CREATE TABLE prodTable (
    prodType STRING,
    typeDescription STRING,
    PRIMARY KEY (prodType)
) 
CREATE TABLE prodTable.prodCategory (
    categoryName STRING,
    categoryDescription STRING,
    PRIMARY KEY (categoryName)
) 
CREATE TABLE prodTable.prodCategory.item (
    itemSKU STRING,
    itemDescription STRING,
    itemPrice FLOAT,
    vendorUID STRING,
    inventoryCount INTEGER,
    PRIMARY KEY (itemSKU)
) 

次のようなデータを含む表:

  • 行1:

    • prodType: Hardware

    • typeDescription: Equipment、toolsおよびparts

    • 行1.1:

      • categoryName: Bolts

      • categoryDescription: Metric & US Sizes

      • 行1.1.1:

        • itemSKU: 1392610

        • itemDescription: 1/4-20 x 1/2 Grade 8 Hex

        • itemPrice: 11.99

        • vendorUID: A8LN99

        • inventoryCount: 1457

  • 行2:

    • prodType: Tools

    • typeDescription: Handおよびpower tools

    • 行2.1:

      • categoryName: Handtools

      • categoryDescription: Hammers、screwdrivers、saws

      • 行2.1.1:

        • itemSKU: 1582178

        • itemDescription: Acme 20 ounce claw hammer

        • itemPrice: 24.98

        • vendorUID: D6BQ27

        • inventoryCount: 249

次の例では、ReturnRowtableプロパティを確認することで、表示する表を識別します。

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

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

   // Using an empty primary key for
   // this iteration
   var key = {};

   // Identify the child tables to include
   // in the iteration.
   var includeTables = ['prodTable.prodCategory',
                        'prodTable.prodCategory.item'];

   store.tableIterator('prodTable', key, {
                includedTables: includeTables
           },
   function (err, iterator) {
        if (err)
            throw err;
        else {

            // Configure Iterator event done
            iterator.on('done', function() {
                store.close();
            });

            console.log("Retrieved rows:");

            iterator.forEach(function (err, retRow) {
                if (err)
                    throw err;
                else {
                    // It is necessary for our code to
                    // identify which table we are
                    // displaying.
                    if (retRow.table == 'prodTable') {
                        console.log('\n');
                        console.log('Type: ' +
                            retRow.row.prodType);
                        console.log('Description: ' +
                            retRow.row.typeDescription);

                    } else if (retRow.table ==
                            'prodTable.prodCategory') {
                        console.log('\tCategory: ' +
                                    retRow.row.categoryName);
                        console.log('\tDescription:  ' +
                                    retRow.row.categoryDescription);
                    } else {
                        console.log('\t\tSKU: ' +
                            retRow.row.itemSKU);
                        console.log('\t\tDescription: ' +
                            retRow.row.itemDescription);
                        console.log('\t\tPrice: ' +
                            retRow.row.itemPrice);
                        console.log('\t\tVendor UID: ' +
                            retRow.row.vendorUID);
                        console.log('\t\tInventory Count: ' +
                            retRow.row.inventoryCount);
                        console.log('\n');
                    }
                }
            });
        }
   });
}).on('close', function() {
    console.log('Store closed.');
}).on('error', function(error) {
    console.log(error);
});
store.open();