Use Tablestore SDK for Node.js to perform wide column model operations.
Quick start
Set up the environment, install the SDK, and initialize the client.
Prerequisites
-
Node.js 4.0 or later.
ImportantDo not use Node.js 12.0 to 12.14 due to compatibility issues.
-
A Node.js development tool.
Install the SDK
Run the following command to install the SDK:
npm install tablestore
Sample programs for common wide column model operations:
-
Download the Node.js SDK and check the sample folder.
-
View samples on GitHub.
Configure access credentials
Create an AccessKey for your Alibaba Cloud account or RAM user, then configure it as an environment variable to avoid hard-coding credentials.
Restart your IDE, terminal, other desktop applications, and background services after configuration to load the updated environment variables. For more information about other types of access credentials, see Configure access credentials.
Linux
-
Append the environment variables to
~/.bashrc:echo "export TABLESTORE_ACCESS_KEY_ID='YOUR_ACCESS_KEY_ID'" >> ~/.bashrc echo "export TABLESTORE_ACCESS_KEY_SECRET='YOUR_ACCESS_KEY_SECRET'" >> ~/.bashrc -
Apply the changes:
source ~/.bashrc -
Verify the environment variables:
echo $TABLESTORE_ACCESS_KEY_ID echo $TABLESTORE_ACCESS_KEY_SECRET
macOS
-
Check your default shell:
echo $SHELL -
Configure based on your shell type:
Zsh
-
Append the environment variables to
~/.zshrc:echo "export TABLESTORE_ACCESS_KEY_ID='YOUR_ACCESS_KEY_ID'" >> ~/.zshrc echo "export TABLESTORE_ACCESS_KEY_SECRET='YOUR_ACCESS_KEY_SECRET'" >> ~/.zshrc -
Apply the changes:
source ~/.zshrc -
Verify the environment variables:
echo $TABLESTORE_ACCESS_KEY_ID echo $TABLESTORE_ACCESS_KEY_SECRET
Bash
-
Append the environment variables to
~/.bash_profile:echo "export TABLESTORE_ACCESS_KEY_ID='YOUR_ACCESS_KEY_ID'" >> ~/.bash_profile echo "export TABLESTORE_ACCESS_KEY_SECRET='YOUR_ACCESS_KEY_SECRET'" >> ~/.bash_profile -
Apply the changes:
source ~/.bash_profile -
Verify the environment variables:
echo $TABLESTORE_ACCESS_KEY_ID echo $TABLESTORE_ACCESS_KEY_SECRET
-
Windows
CMD
-
Set the environment variables in CMD:
setx TABLESTORE_ACCESS_KEY_ID "YOUR_ACCESS_KEY_ID" setx TABLESTORE_ACCESS_KEY_SECRET "YOUR_ACCESS_KEY_SECRET" -
Restart CMD and verify:
echo %TABLESTORE_ACCESS_KEY_ID% echo %TABLESTORE_ACCESS_KEY_SECRET%
PowerShell
-
Run in PowerShell:
[Environment]::SetEnvironmentVariable("TABLESTORE_ACCESS_KEY_ID", "YOUR_ACCESS_KEY_ID", [EnvironmentVariableTarget]::User) [Environment]::SetEnvironmentVariable("TABLESTORE_ACCESS_KEY_SECRET", "YOUR_ACCESS_KEY_SECRET", [EnvironmentVariableTarget]::User) -
Verify the environment variables:
[Environment]::GetEnvironmentVariable("TABLESTORE_ACCESS_KEY_ID", [EnvironmentVariableTarget]::User) [Environment]::GetEnvironmentVariable("TABLESTORE_ACCESS_KEY_SECRET", [EnvironmentVariableTarget]::User)
Initialize the client
Initialize the client and list tables to verify the connection.
Public network access is disabled by default for new instances. To use public network access, enable it in the Network Management.
var TableStore = require('tablestore');
// Replace yourInstanceName with your instance name
var instancename = 'yourInstanceName';
// Replace yourEndpoint with your instance endpoint
var endpoint = 'yourEndpoint';
// Obtain the AccessKey ID and AccessKey Secret from environment variables
var accessKeyId = process.env.TABLESTORE_ACCESS_KEY_ID;
var secretAccessKey = process.env.TABLESTORE_ACCESS_KEY_SECRET;
// Initialize the Tablestore client
var client = new TableStore.Client({
accessKeyId: accessKeyId,
secretAccessKey: secretAccessKey,
endpoint: endpoint,
instancename: instancename
});
// List all tables in the instance and print to the console
client.listTable({}, function (err, data) {
if (err) {
console.log('error:', err);
return;
}
console.log('success:', data);
});
Data types
Tablestore data types map to Node.js SDK types as follows.
|
Tablestore data type |
Node.js SDK data type |
Description |
|
String |
string |
A JavaScript primitive type. |
|
Integer |
int64 |
JavaScript lacks a native 64-bit integer type. The SDK provides |
|
Double |
number |
A JavaScript primitive type. |
|
Boolean |
boolean |
A JavaScript primitive type. |
|
Binary |
Buffer |
A Node.js |
TableStore.Long converts between int64 and JavaScript number or string types:
// Construct int64 from number or string
const valueA = TableStore.Long.fromNumber(1000);
const valueB = TableStore.Long.fromString('2000');
// Convert int64 back to native JavaScript types
const numA = valueA.toNumber(); // 1000
const strA = valueA.toString(); // '1000'
const numB = valueB.toNumber(); // 2000
const strB = valueB.toString(); // '2000'
FAQ
References
For information about error handling in Tablestore, see Error handling.