This topic describes how to use the Node.js SDK of Table Store to create, query, or delete a global secondary index.

Create a global secondary index

  • Create a global secondary index when you create a primary table
    Example:
    var client = require('./client');
    var TableStore = require('../index.js');
    
    var params = {
      tableMeta: {
        tableName: 'sdkGlobalTest',
        primaryKey: [
          {
            name: 'pk1',
            type: TableStore.PrimaryKeyType.INTEGER
          },
          {
            name: 'pk2',
            type: TableStore.PrimaryKeyType.INTEGER
          }
        ],
        definedColumn: [
          {
            "name":"col4",
            "type": TableStore.DefinedColumnType.DCT_INTEGER
          },
          {
            "name": "col2",
            "type": TableStore.DefinedColumnType.DCT_INTEGER
          }
        ],
      },
      reservedThroughput: {
        capacityUnit: {
          read: 0,
          write: 0
        }
      },
      tableOptions: {
        timeToLive: -1,// Specify the validity period of data in seconds. The value of -1 indicates that the data never expires. To specify the validity period to one year, set timeToLive to the product of 365, 24 ,and 3600.
        maxVersions: 1// Specify the maximum number of versions that can be saved in each column. The value of 1 indicates that only the latest version is saved in each column.
      },
      streamSpecification: {
        enableStream: false, // You cannot enable Stream for global secondary index.
      },
      indexMetas: [
        {
          name: "sdkIndex1",
          primaryKey: ["pk2"],
          definedColumn: ["col1", "col2"]
        },
        {
          name: "sdkIndex2",
          primaryKey: ["col1"],
          definedColumn: ["col2"]
        }
      ]
    };
    
    client.createTable(params, function (err, data) {
      if (err) {
        console.log('error:', err);
        return;
      }
      console.log('success:', data);
    });
  • Create a global secondary index for an existing table
    You can create a global secondary index for an existing primary table. Example:
    var client = require('./client');
    var TableStore = require('../index.js');
    
    client.createIndex({
        mainTableName: "sdkGlobalTest",
        indexMeta: {
            name: "sdkIndex2",
            primaryKey: ["col1"],
            definedColumn: ["col2"],
            includeBaseData: false,
            indexUpdateMode: TableStore.IndexUpdateMode.IUM_ASYNC_INDEX,// Synchronize incremental data by default.
            indexType: TableStore.IndexType.IT_GLOBAL_INDEX,//IT_GLOBAL_INDEX
        }
    }, function (err, data) {
        if (err) {
            console.log('error:', err);
            return;
        }
        console.log('success:', JSON.stringify(data, null, 2));
    });

Delete a global secondary index

You can delete a global secondary index from a primary table. Example:
var client = require('./client');

client.dropIndex({
  mainTableName: "sdkGlobalTest",
  indexName: "sdkIndex1"
}, function (err, data) {
  if (err) {
    console.log('error:', err);
    return;
  }
  console.log('success:', JSON.stringify(data, null, 2));
});

Query a global secondary index

Examples:
  • Read a row of data from a global secondary index
    var TableStore = require('../index.js');
    var Long = TableStore.Long;
    var client = require('./client');
    
    var params = {
      tableName: "index1",
      primaryKey: [ {'pk2': Long.fromNumber(2)}, {'pk1': Long.fromNumber(1)}]
    };
    
    client.getRow(params, function (err, data) {
      if (err) {
        console.log('error:', err);
        return;
      }
      console.log('success:', JSON.stringify(data, null, 2));
    });
  • Read data within the specified range from a global secondary index
    var TableStore = require('../index.js');
    var Long = TableStore.Long;
    var client = require('./client');
    
    var params = {
      tableName: "sdkIndex1",
      direction: TableStore.Direction.FORWARD,
      maxVersions: 10,
      inclusiveStartPrimaryKey: [{ "pk2": TableStore.INF_MIN }, { "pk1": TableStore.INF_MIN }],
      exclusiveEndPrimaryKey: [{ "pk2": TableStore.INF_MAX }, { "pk1": TableStore.INF_MAX }],
      limit: 2
    };
    
    var resultRows = []
    
    var getRange = function () {
      client.getRange(params, function (err, data) {
        if (err) {
          console.log('error:', err);
          return;
        }
        resultRows = resultRows.concat(data.rows)
    
        if (data.nextStartPrimaryKey) {
          params.inclusiveStartPrimaryKey = [
            { "pk2": data.nextStartPrimaryKey[0].value },
            { "pk1": data.nextStartPrimaryKey[1].value }
          ];
          getRange()
        } else {
          console.log(JSON.stringify(resultRows));
        }
      });
    }
    
    getRange()