All Products
Search
Document Center

Tablestore:Create a data table

Last Updated:Jun 19, 2025

This topic describes how to create a data table in Tablestore by using the Node.js SDK.

Usage notes

After you create a data table, wait until the data table is loaded before you perform operations on data. Otherwise, the operations will fail. This process typically takes several seconds.

Prerequisites

Initialize a Tablestore client

Method description

 createTable: function createTable(params, callback)

params parameter description

  • tableMeta (required) object: The schema information of the table, including the following parameters.

    Name

    Type

    Description

    tableName (required)

    string

    The name of the data table.

    primaryKey (required)

    Array

    The information about the primary key.

    • You can set 1 to 4 primary key columns, which are sorted in ascending order by default. The first primary key column serves as the partition key.

    • The primary key data types include STRING, INTEGER, and BINARY. Integer-type primary key columns that are not partition keys can be set as auto-increment primary key columns.

    definedColumn (optional)

    Array

    The information about predefined columns.

    • Predefined columns are attribute columns that are predefined. They can be used to create secondary indexes and search indexes.

    • The predefined column data types include STRING, INTEGER, DOUBLE, and BOOLEAN.

  • tableOptions (required) object: The configuration information of the table, including the following parameters.

    Name

    Type

    Description

    timeToLive (required)

    number

    The time to live (TTL) of data in the data table. Unit: seconds.

    • When set to -1, data never expires. Otherwise, the minimum value is 86400 (1 day). Data that exceeds the TTL will be automatically purged.

    • If you want to use search indexes or secondary indexes, you must set this parameter to -1 or set the allowUpdate parameter to false.

    maxVersions (required)

    number

    The maximum number of versions.

    • If you want to use search indexes or secondary indexes, the maximum number of versions must be set to 1.

    maxTimeDeviation (optional)

    number

    The maximum version drift, in seconds. The default value is 86400 (1 day).

    • The difference between the timestamp of written data and the current system time must be within the maximum version drift range. Otherwise, the data write operation will fail.

    • The valid version range for attribute column data is [max(data write time - maximum version drift, data write time - TTL), data write time + maximum version drift).

    allowUpdate (optional)

    boolean

    Specifies whether to allow updates. The default value is true.

    • When set to false, you cannot update data by using the updateRow() method.

  • indexMetas (optional) Array: The list of secondary indexes. Each index includes the following parameters.

    Name

    Type

    Description

    name (required)

    string

    The name of the index.

    primaryKey (required)

    Array

    The primary key columns of the index.

    • The primary key columns of an index are a combination of primary key columns and predefined columns of the data table.

    • If you want to create a local secondary index, the first primary key column of the index must be the first primary key column of the data table.

    definedColumn (optional)

    Array

    The predefined columns of the index.

    • These are composed of predefined columns from the data table.

    indexType (optional)

    number

    The type of the index. Valid values:

    • IT_GLOBAL_INDEX (default): global secondary index.

    • IT_LOCAL_INDEX: local secondary index.

    indexUpdateMode (optional)

    number

    The update mode of the index. Valid values:

    • IUM_ASYNC_INDEX (default): asynchronous update. The update mode of a global secondary index must be set to asynchronous update.

    • IUM_SYNC_INDEX: synchronous update. The update mode of a local secondary index must be set to synchronous update.

  • streamSpecification (optional) object: The Stream configuration information, including the following parameters.

    Name

    Type

    Description

    enableStream (optional)

    boolean

    Specifies whether to enable Stream. The default value is false.

    expirationTime (optional)

    number

    The expiration time of the Stream, which indicates the retention period of incremental logs. Unit: hours. Maximum value: 168 (7 days).

    • When enableStream is set to true, expirationTime must be set.

  • reservedThroughput (required) object: The reserved read and write throughput, in CU. Only high-performance instances in CU mode can be set to non-zero values that are effective.

Sample code

Create a data table

The following sample code creates a test_table table that contains one primary key column of the String type.

var params = {
  // The schema information of the data table
  tableMeta: {
    tableName: 'test_table',
    // At least one primary key column is required to create a data table
    primaryKey: [
      {
        name: 'id',
        type: 'STRING'
      }
    ],
    // (Optional) Add predefined columns
    definedColumn: [
      {
        name: 'name',
        type: TableStore.DefinedColumnType.DCT_STRING
      }
    ]
  },
  // The configuration information of the data table
  tableOptions: {
    // When you create a data table, you must specify the TTL. A value of -1 indicates that data never expires
    timeToLive: -1,
    // When you create a data table, you must specify the maximum number of versions
    maxVersions: 1,
    // (Optional) Set the maximum version drift
    maxTimeDeviation: 86400,
    // (Optional) Specify whether to allow updates
    allowUpdate: true
  },
  // When you create a data table, you must set the reserved read and write throughput (only high-performance instances in CU mode support setting non-zero values for reserved read and write throughput)
  reservedThroughput: {
    capacityUnit: {
      read: 0,
      write: 0
    }
  },
  // (Optional) Configure Stream information
  streamSpecification: {
    enableStream: true,
    expirationTime: 168
  }
};

client.createTable(params, function (err, data) {
  if (err) {
    console.error('error:', err);
    return;
  }
  console.log('success:', data);
});

Create a data table with secondary indexes

The following sample code creates a data table and configures secondary indexes.

var params = {
    // The schema information of the data table
    tableMeta: {
        tableName: 'test_table',
        // Primary key
        primaryKey: [
            {
                name: 'id',
                type: 'STRING'
            }
        ],
        // Predefined columns
        definedColumn: [
            {
                name: 'name',
                type: TableStore.DefinedColumnType.DCT_STRING
            }
        ]
    },
    // The configuration information of the data table
    tableOptions: {
        // TTL
        timeToLive: -1,
        // Maximum number of versions
        maxVersions: 1
    },
    // Reserved read and write throughput (only high-performance instances in CU mode support setting non-zero values for reserved read and write throughput)
    reservedThroughput: {
        capacityUnit: {
            read: 0,
            write: 0
        }
    },
    // Secondary index information
    indexMetas: [
        {
            name: 'test_table_index',
            // Index primary key
            primaryKey: ['id'],
            // Index predefined columns
            definedColumn: ['name'],
            // Index type
            indexType: TableStore.IndexType.IT_LOCAL_INDEX,
            // Index update mode
            indexUpdateMode: TableStore.IndexUpdateMode.IUM_SYNC_INDEX
        }
    ]
};

client.createTable(params, function (err, data) {
    if (err) {
        console.error('error:', err);
        return;
    }
    console.log('success:', data);
});

References