Security News
tea.xyz Spam Plagues npm and RubyGems Package Registries
Tea.xyz, a crypto project aimed at rewarding open source contributions, is once again facing backlash due to an influx of spam packages flooding public package registries.
@azure/data-tables
Advanced tools
Readme
Azure Tables is a cloud-based service that stores structured NoSQL data, providing a key/attribute store with a schemaless design. Tables storage gives developers flexibility and scalability with all the best parts of Azure cloud.
Use the client library to:
Azure Cosmos DB provides a Table API for applications that are written for Azure Table storage and that need premium capabilities like:
Key links:
Currently supported environments:
You must have an Azure subscription and a Storage Account or an Azure CosmosDB database to use this package.
@azure/data-tables
packageThe preferred way to install the Azure Tables client library for JavaScript is to use the npm package manager. Type the following into a terminal window:
npm install @azure/data-tables
TableServiceClient
Azure Tables supports several ways to authenticate. In order to interact with the Azure Tables service you'll need to create an instance of a Tables client - TableServiceClient
or TableClient
for example. See samples for creating the TableServiceClient
to learn more about authentication.
Note: Azure Active Directory (AAD) is only supported for Azure Storage accounts.
AzureNamedKeyCredential
To use this client library in the browser, first you need to use a bundler. For details on how to do this, please refer to our bundling documentation.
You need to set up Cross-Origin Resource Sharing (CORS) rules for your storage account if you need to develop for browsers. Go to Azure portal and Azure Storage Explorer, find your storage account, create new CORS rules for blob/queue/file/table service(s).
For example, you can create following CORS settings for debugging. But please customize the settings carefully according to your requirements in production environment.
TableServiceClient
- Client that provides functions to interact at a Table Service level such as create, list and delete tables
TableClient
- Client that provides functions to interact at an entity level such as create, list and delete entities within a table.
Table
- Tables store data as collections of entities.
Entity
- Entities are similar to rows. An entity has a primary key and a set of properties. A property is a name, typed-value pair, similar to a column.
Common uses of the Table service include:
To use the clients, import the package in your file:
const AzureTables = require("@azure/data-tables");
Alternatively, selectively import only the types you need:
const { TableServiceClient, AzureNamedKeyCredential } = require("@azure/data-tables");
The TableServiceClient
requires a URL to the table service and an access credential. It also optionally accepts some settings in the options
parameter.
TableServiceClient
with AzureNamedKeyCredentialYou can instantiate a TableServiceClient
with a AzureNamedKeyCredential
by passing account-name and account-key as arguments. (The account-name and account-key can be obtained from the azure portal.)
[ONLY AVAILABLE IN NODE.JS RUNTIME]
const { TableServiceClient, AzureNamedKeyCredential } = require("@azure/data-tables");
const account = "<account>";
const accountKey = "<accountkey>";
const credential = new AzureNamedKeyCredential(account, accountKey);
const serviceClient = new TableServiceClient(
`https://${account}.table.core.windows.net`,
credential
);
TableServiceClient
with TokenCredential (AAD)Azure Tables provides integration with Azure Active Directory (Azure AD) for identity-based authentication of requests to the Table service when targeting a Storage endpoint. With Azure AD, you can use role-based access control (RBAC) to grant access to your Azure Table resources to users, groups, or applications.
To access a table resource with a TokenCredential
, the authenticated identity should have either the "Storage Table Data Contributor" or "Storage Table Data Reader" role.
With the @azure/identity
package, you can seamlessly authorize requests in both development and production environments.
To learn more about Azure AD integration in Azure Storage, see the Azure.Identity README
const { TableServiceClient } = require("@azure/data-tables");
const { DefaultAzureCredential } = require("@azure/identity");
// DefaultAzureCredential expects the following three environment variables:
// - AZURE_TENANT_ID: The tenant ID in Azure Active Directory
// - AZURE_CLIENT_ID: The application (client) ID registered in the AAD tenant
// - AZURE_CLIENT_SECRET: The client secret for the registered application
const credential = new DefaultAzureCredential();
const account = "<account name>";
const clientWithAAD = new TableServiceClient(
`https://${account}.table.core.windows.net`,
credential
);
TableServiceClient
with SAS TokenAlso, You can instantiate a TableServiceClient
with a shared access signatures (SAS). You can get the SAS token from the Azure Portal.
const { TableServiceClient, AzureSASCredential } = require("@azure/data-tables");
const account = "<account name>";
const sas = "<service Shared Access Signature Token>";
const serviceClientWithSAS = new TableServiceClient(
`https://${account}.table.core.windows.net`,
new AzureSASCredential(sas)
);
You can list tables within an account through a TableServiceClient
instance calling the listTables
function. This function returns a PageableAsyncIterator
that you can consume using for-await-of
const { TableServiceClient, AzureNamedKeyCredential } = require("@azure/data-tables");
const account = "<account>";
const accountKey = "<accountkey>";
const credential = new AzureNamedKeyCredential(account, accountKey);
const serviceClient = new TableServiceClient(
`https://${account}.table.core.windows.net`,
credential
);
async function main() {
let tablesIter = serviceClient.listTables();
let i = 1;
for await (const table of tablesIter) {
console.log(`Table${i}: ${table.name}`);
i++;
// Output:
// Table1: testTable1
// Table1: testTable2
// Table1: testTable3
// Table1: testTable4
// Table1: testTable5
}
}
main();
You can create a table through a TableServiceClient
instance calling the createTable
function. This function takes the name of the table to create as a parameter.
Note that createTable
won't throw an error when the table already exists.
const { TableServiceClient, AzureNamedKeyCredential } = require("@azure/data-tables");
const account = "<account>";
const accountKey = "<accountkey>";
const credential = new AzureNamedKeyCredential(account, accountKey);
const serviceClient = new TableServiceClient(
`https://${account}.table.core.windows.net`,
credential
);
async function main() {
const tableName = `newtable`;
// If the table 'newTable' already exists, createTable doesn't throw
await serviceClient.createTable(tableName);
}
main();
Here is a sample that demonstrates how to test if the table already exists when attempting to create it:
const { TableServiceClient, AzureNamedKeyCredential } = require("@azure/data-tables");
const account = "<account>";
const accountKey = "<accountkey>";
const credential = new AzureNamedKeyCredential(account, accountKey);
const serviceClient = new TableServiceClient(
`https://${account}.table.core.windows.net`,
credential
);
async function main() {
const tableName = `newtable${new Date().getTime()}`;
await serviceClient.createTable(tableName, {
onResponse: (response) => {
if (response.status === 409) {
console.log(`Table ${tableName} already exists`);
}
}
});
}
main();
The TableClient
is created in a similar way as the TableServiceClient
with the difference that TableClient
takes a table name as a parameter
TableClient
with AzureNamedKeyCredential
You can instantiate a TableClient
with a AzureNamedKeyCredential
by passing account-name and account-key as arguments. (The account-name and account-key can be obtained from the azure portal.)
[ONLY AVAILABLE IN NODE.JS RUNTIME]
const { TableClient, AzureNamedKeyCredential } = require("@azure/data-tables");
// Enter your storage account name and shared key
const account = "<account>";
const accountKey = "<accountkey>";
const tableName = "<tableName>";
// Use AzureNamedKeyCredential with storage account and account key
// AzureNamedKeyCredential is only available in Node.js runtime, not in browsers
const credential = new AzureNamedKeyCredential(account, accountKey);
const client = new TableClient(`https://${account}.table.core.windows.net`, tableName, credential);
TableClient
with TokenCredential
(Azure Active Directory)Azure Tables provides integration with Azure Active Directory (Azure AD) for identity-based authentication of requests to the Table service when targeting a Storage endpoint. With Azure AD, you can use role-based access control (RBAC) to grant access to your Azure Table resources to users, groups, or applications.
To access a table resource with a TokenCredential
, the authenticated identity should have either the "Storage Table Data Contributor" or "Storage Table Data Reader" role.
With the @azure/identity
package, you can seamlessly authorize requests in both development and production environments.
To learn more about Azure AD integration in Azure Storage, see the Azure.Identity README
const { TableClient } = require("@azure/data-tables");
const { DefaultAzureCredential } = require("@azure/identity");
// DefaultAzureCredential expects the following three environment variables:
// - AZURE_TENANT_ID: The tenant ID in Azure Active Directory
// - AZURE_CLIENT_ID: The application (client) ID registered in the AAD tenant
// - AZURE_CLIENT_SECRET: The client secret for the registered application
const credential = new DefaultAzureCredential();
const account = "<account name>";
const tableName = "<tableName>";
const clientWithAAD = new TableClient(
`https://${account}.table.core.windows.net`,
tableName,
credential
);
TableClient
with SAS TokenYou can instantiate a TableClient
with a shared access signatures (SAS). You can get the SAS token from the Azure Portal.
const { TableClient, AzureSASCredential } = require("@azure/data-tables");
const account = "<account name>";
const sas = "<service Shared Access Signature Token>";
const tableName = "<tableName>";
const clientWithSAS = new TableClient(
`https://${account}.table.core.windows.net`,
tableName,
new AzureSASCredential(sas)
);
TableClient
with TokenCredential (AAD)Azure Tables provides integration with Azure Active Directory (Azure AD) for identity-based authentication of requests to the Table service when targeting a Storage endpoint. With Azure AD, you can use role-based access control (RBAC) to grant access to your Azure Table resources to users, groups, or applications.
To access a table resource with a TokenCredential
, the authenticated identity should have either the "Storage Table Data Contributor" or "Storage Table Data Reader" role.
With the @azure/identity
package, you can seamlessly authorize requests in both development and production environments.
To learn more about Azure AD integration in Azure Storage, see the Azure.Identity README
const { TableClient } = require("@azure/data-tables");
const { DefaultAzureCredential } = require("@azure/identity");
// DefaultAzureCredential expects the following three environment variables:
// - AZURE_TENANT_ID: The tenant ID in Azure Active Directory
// - AZURE_CLIENT_ID: The application (client) ID registered in the AAD tenant
// - AZURE_CLIENT_SECRET: The client secret for the registered application
const credential = new DefaultAzureCredential();
const account = "<account name>";
const tableName = "<tableName>";
const clientWithAAD = new TableClient(
`https://${account}.table.core.windows.net`,
tableName,
credential
);
You can list entities within a table by through a TableClient
instance calling the listEntities
function. This function returns a PageableAsyncIterator
that you can consume using for-await-of
const { TableClient, AzureNamedKeyCredential } = require("@azure/data-tables");
const account = "<account>";
const accountKey = "<accountkey>";
const tableName = "<tableName>";
const credential = new AzureNamedKeyCredential(account, accountKey);
const client = new TableClient(`https://${account}.table.core.windows.net`, tableName, credential);
async function main() {
let entitiesIter = client.listEntities();
let i = 1;
for await (const entity of entitiesIter) {
console.log(`Entity${i}: PartitionKey: ${entity.partitionKey} RowKey: ${entity.rowKey}`);
i++;
// Output:
// Entity1: PartitionKey: P1 RowKey: R1
// Entity2: PartitionKey: P2 RowKey: R2
// Entity3: PartitionKey: P3 RowKey: R3
// Entity4: PartitionKey: P4 RowKey: R4
}
}
main();
You can create a new Entity in a table by through a TableClient
instance calling the createEntity
function. This function takes the entity to insert as a parameter. The entity must contain partitionKey
and rowKey
.
const { TableClient, AzureNamedKeyCredential } = require("@azure/data-tables");
const account = "<account>";
const accountKey = "<accountkey>";
const tableName = "<tableName>";
const credential = new AzureNamedKeyCredential(account, accountKey);
const client = new TableClient(`https://${account}.table.core.windows.net`, tableName, credential);
async function main() {
const testEntity = {
partitionKey: "P1",
rowKey: "R1",
foo: "foo",
bar: 123
};
await client.createEntity(testEntity);
}
main();
The Azure Tables Client SDK also works with Azurite, an Azure Storage and Tables API compatible server emulator. Please refer to the (Azurite repository) on how to get started using it.
The easiest way to connect to Azurite from your application is to configure a connection string that references the shortcut UseDevelopmentStorage=true
. The shortcut is equivalent to the full connection string for the emulator, which specifies the account name, the account key, and the emulator endpoints for each of the Azure Storage services: (see more). Using this shortcut, the Azure Tables Client SDK would setup the default connection string and allowInsecureConnection
in the client options.
import { TableClient } from "@azure/data-tables";
const connectionString = "UseDevelopmentStorage=true";
const client = TableClient.fromConnectionString(connectionString, "myTable");
You can connect to azurite manually without using the connection string shortcut by specifying the service URL and AzureNamedKeyCredential
or a custom connection string. However, allowInsecureConnection
will need to be set manually in case Azurite runs in an http
endpoint.
import { TableClient, AzureNamedKeyCredential } from "@azure/data-tables";
const client = new TableClient(
"<Azurite-http-table-endpoint>",
"myTable",
new AzureNamedKeyCredential("<Azurite-account-name>", "<Azurite-account-key>"),
{ allowInsecureConnection: true }
);
When you interact with Tables service using the Javascript/Typescript SDK, errors returned by the service correspond to the same HTTP status codes returned for REST API requests: Storage Table Service Error Codes
Enabling logging may help uncover useful information about failures. In order to see a log of HTTP requests and responses, set the AZURE_LOG_LEVEL
environment variable to info
. Alternatively, logging can be enabled at runtime by calling setLogLevel
in the @azure/logger
:
const { setLogLevel } = require("@azure/logger");
setLogLevel("info");
More code samples coming soon Issue#10531
This project welcomes contributions and suggestions. Most contributions require you to agree to a Contributor License Agreement (CLA) declaring that you have the right to, and actually do, grant us the rights to use your contribution. For details, visit https://cla.microsoft.com.
When you submit a pull request, a CLA-bot will automatically determine whether you need to provide a CLA and decorate the PR appropriately (e.g., label, comment). Simply follow the instructions provided by the bot. You will only need to do this once across all repos using our CLA.
This project has adopted the Microsoft Open Source Code of Conduct. For more information see the Code of Conduct FAQ or contact opencode@microsoft.com with any additional questions or comments.
If you'd like to contribute to this library, please read the contributing guide to learn more about how to build and test the code.
FAQs
An isomorphic client library for the Azure Tables service.
The npm package @azure/data-tables receives a total of 36,251 weekly downloads. As such, @azure/data-tables popularity was classified as popular.
We found that @azure/data-tables demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Tea.xyz, a crypto project aimed at rewarding open source contributions, is once again facing backlash due to an influx of spam packages flooding public package registries.
Security News
As cyber threats become more autonomous, AI-powered defenses are crucial for businesses to stay ahead of attackers who can exploit software vulnerabilities at scale.
Security News
UnitedHealth Group disclosed that the ransomware attack on Change Healthcare compromised protected health information for millions in the U.S., with estimated costs to the company expected to reach $1 billion.