This topic describes how to use the Node.js SDK of Table Store to add an auto-increment primary key column and to write data when you create a table.

Note
  • If you set a primary key column of a table as an auto-increment column, you do not need to enter values in this column when writing data in a row. Instead, Table Store automatically generates primary key values.
  • The values of the auto-increment column are unique and increase sequentially.
  • Only one primary key column can be set as the auto-increment column in each table.
  • Partition keys and attribute columns cannot be set as auto-increment columns.

Create a table

You can add an auto-increment primary key column when you create a table. Example:

var TableStore = require('../index.js');
var Long = TableStore.Long;
var client = require('./client');

var tableName = "autoIncTable";
var pk1 = "stringPK";
var pk2 = "autoIncPK";

function createTableWithAutoIncrementPk() {
    var createParams = {
        tableMeta: {
            tableName: tableName,
            primaryKey: [
                {
                    name: pk1,
                    type: 'STRING'
                },
                {
                    name: pk2,
                    type: 'INTEGER',
                    option: 'AUTO_INCREMENT'// Specify the auto-increment column by setting option to AUTO_INCREMENT.
                },
            ]
        },
        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.
        },
    };

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

Write data

Example:

var TableStore = require('../index.js');
var Long = TableStore.Long;
var client = require('./client');

var tableName = "autoIncTable";
var pk1 = "stringPK";
var pk2 = "autoIncPK";

function putRow() {
    var putParams = {
        tableName: tableName,
        condition: new TableStore.Condition(TableStore.RowExistenceExpectation.IGNORE, null),
        primaryKey: [
            { stringPK: 'pk1' },
            { autoIncPK: TableStore.PK_AUTO_INCR }
        ],
        attributeColumns: [
            { 'col1': 'col1val' }
        ],
        returnContent: { returnType: TableStore.ReturnType.Primarykey }
    };

    client.putRow(putParams, function (err, data) {
        if (err) {
            console.log('error:', err);
            return;
        }

        console.log('put row success,autoIncrement pk value:' + JSON.stringify(data.row.primaryKey));
    });

}