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
Method description
createTable: function createTable(params, callback)
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);
});