Use Tablestore SDK for Node.js to add an auto-increment primary key column to a data table, write rows to that column, and retrieve the generated values.
Prerequisites
Before you begin, ensure that you have:
An initialized client. For more information, see Initialize a Tablestore client.
Understand auto-increment column behavior
Auto-increment primary key column values are unique and monotonically increasing, but not necessarily sequential within a partition that shares the same partition key value.
Create a data table with an auto-increment primary key column
When creating a data table, designate one non-partition-key primary key column as auto-increment by setting its option to AUTO_INCREMENT. The column must use the INTEGER data type. A table can have at most one auto-increment primary key column, and you cannot add one to an existing table.
Auto-increment primary key column values are 64-bit signed long integers. Only non-partition-key columns support the AUTO_INCREMENT option.
Example
The following example creates test_table with two primary key columns: id (partition key, STRING) and incr (auto-increment, INTEGER).
function createTableSample() {
var createParams = {
tableMeta: {
tableName: 'test_table',
primaryKey: [
{
name: 'id',
type: 'STRING' // Partition key — you provide this value on every write
},
{
name: 'incr',
type: 'INTEGER',
option: 'AUTO_INCREMENT' // Tablestore generates this value automatically
},
]
},
tableOptions: {
timeToLive: -1, // -1 means data never expires
maxVersions: 1
},
reservedThroughput: {
capacityUnit: {
read: 0,
write: 0
}
},
};
client.createTable(createParams, function (err, data) {
if (err) {
console.error('error:', err);
return;
}
console.log('success:', data);
});
}
Write data
When writing a row, set the auto-increment column value to TableStore.PK_AUTO_INCR — a placeholder that tells Tablestore to generate the value. To retrieve the generated value (for example, to use it in a subsequent read or update), set returnType to TableStore.ReturnType.Primarykey in returnContent.
Example
The following example writes a row to test_table and prints the generated primary key.
function putRowSample() {
var putParams = {
tableName: 'test_table',
condition: new TableStore.Condition(TableStore.RowExistenceExpectation.IGNORE, null),
primaryKey: [
{ id: 'row1' },
// Use PK_AUTO_INCR as a placeholder — Tablestore replaces it with the generated value
{ incr: TableStore.PK_AUTO_INCR }
],
attributeColumns: [
{ 'col1': 'val1' }
],
// Return the full primary key so you can use the generated incr value in later queries
returnContent: { returnType: TableStore.ReturnType.Primarykey }
};
client.putRow(putParams, function (err, data) {
if (err) {
console.error('error:', err);
return;
}
// Request metadata and capacity units consumed
console.log("RequestId: ", data.RequestId);
console.log("Read CU Cost: ", data.consumed.capacityUnit.read);
console.log("Write CU Cost: ", data.consumed.capacityUnit.write);
// Print the generated primary key, including the auto-increment value
// This is only returned when returnType is set to Primarykey
if (data.row.primaryKey) {
console.log(JSON.stringify(data.row.primaryKey));
}
});
}