你当前正在访问 Microsoft Azure Global Edition 技术文档网站。 如果需要访问由世纪互联运营的 Microsoft Azure 中国技术文档网站,请访问 https://docs.azure.cn

TableClient class

TableClient 表示 Azure 表服务的客户端,允许对单个表执行作。

属性

pipeline

表示向 URL 发出 HTTP 请求的管道。 管道可以有多个策略来管理对服务器之前和之后每个请求的作。

tableName

要在其中执行作的表的名称。

url

表帐户 URL

方法

createEntity<T>(TableEntity<T>, OperationOptions)

在表中插入实体。

createTable(OperationOptions)

使用传递给客户端构造函数的 tableName 创建表

deleteEntity(string, string, DeleteTableEntityOptions)

删除表中的指定实体。

deleteTable(OperationOptions)

永久删除包含其所有实体的当前表。

fromConnectionString(string, string, TableServiceClientOptions)

从连接字符串创建 TableClient 的实例。

getAccessPolicy(OperationOptions)

检索有关可用于共享访问签名的表上指定的任何存储访问策略的详细信息。

getEntity<T>(string, string, GetTableEntityOptions)

返回表中的单个实体。

listEntities<T>(ListTableEntitiesOptions)

查询表中的实体。

setAccessPolicy(SignedIdentifier[], OperationOptions)

设置可用于共享访问签名的表的存储访问策略。

submitTransaction(TransactionAction[], OperationOptions)

提交由一组作组成的事务。 可以将作作为列表提供,也可以使用 TableTransaction 来帮助生成事务。

示例用法:

import { DefaultAzureCredential } from "@azure/identity";
import { TableClient, TransactionAction } from "@azure/data-tables";

const account = "<account>";
const accountKey = "<accountkey>";
const tableName = "<tableName>";

const credential = new DefaultAzureCredential();
const client = new TableClient(`https://${account}.table.core.windows.net`, tableName, credential);

const actions: TransactionAction[] = [
  ["create", { partitionKey: "p1", rowKey: "1", data: "test1" }],
  ["delete", { partitionKey: "p1", rowKey: "2" }],
  ["update", { partitionKey: "p1", rowKey: "3", data: "newTest" }, "Merge"],
];
const result = await client.submitTransaction(actions);

TableTransaction 的示例用法:

import { DefaultAzureCredential } from "@azure/identity";
import { TableClient, TableTransaction } from "@azure/data-tables";

const account = "<account>";
const accountKey = "<accountkey>";
const tableName = "<tableName>";

const credential = new DefaultAzureCredential();
const client = new TableClient(`https://${account}.table.core.windows.net`, tableName, credential);

const transaction = new TableTransaction();

// Call the available action in the TableTransaction object
transaction.createEntity({ partitionKey: "p1", rowKey: "1", data: "test1" });
transaction.deleteEntity("p1", "2");
transaction.updateEntity({ partitionKey: "p1", rowKey: "3", data: "newTest" }, "Merge");

// submitTransaction with the actions list on the transaction.
const result = await client.submitTransaction(transaction.actions);
updateEntity<T>(TableEntity<T>, UpdateMode, UpdateTableEntityOptions)

更新表中的实体。

upsertEntity<T>(TableEntity<T>, UpdateMode, OperationOptions)

向上插入表中的实体。

构造函数详细信息

TableClient(string, string, NamedKeyCredential, TableServiceClientOptions)

创建 TableClient 类的新实例。

new TableClient(url: string, tableName: string, credential: NamedKeyCredential, options?: TableServiceClientOptions)

参数

url

string

作为所需作目标的服务帐户的 URL,例如“https://myaccount.table.core.windows.net"。

tableName

string

表的名称

credential
NamedKeyCredential

NamedKeyCredential 用于对请求进行身份验证。 仅支持 Node

options
TableServiceClientOptions

自选。 用于配置 HTTP 管道的选项。

使用帐户名称/密钥的示例:

import { AzureNamedKeyCredential, TableClient } from "@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(string, string, SASCredential, TableServiceClientOptions)

创建 TableClient 类的新实例。

new TableClient(url: string, tableName: string, credential: SASCredential, options?: TableServiceClientOptions)

参数

url

string

作为所需作目标的服务帐户的 URL,例如“https://myaccount.table.core.windows.net"。

tableName

string

表的名称

credential
SASCredential

用于对请求进行身份验证的 SASCredential

options
TableServiceClientOptions

自选。 用于配置 HTTP 管道的选项。

使用 SAS 令牌的示例:

import { TableClient, AzureSASCredential } from "@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(string, string, TableServiceClientOptions)

创建 TableClient 的实例。

new TableClient(url: string, tableName: string, options?: TableServiceClientOptions)

参数

url

string

指向 Azure 存储表服务的客户端字符串,例如“https://myaccount.table.core.windows.net"。 可以追加 SAS,例如“https://myaccount.table.core.windows.net?sasString"。

tableName

string

表的名称

options
TableServiceClientOptions

用于配置 HTTP 管道的选项。

追加 SAS 令牌的示例:

import { TableClient } from "@azure/data-tables";

const account = "<account name>";
const sasToken = "<SAS token>";
const tableName = "<tableName>";

const clientWithSAS = new TableClient(
  `https://${account}.table.core.windows.net?${sasToken}`,
  tableName,
);

TableClient(string, string, TokenCredential, TableServiceClientOptions)

创建 TableClient 类的新实例。

new TableClient(url: string, tableName: string, credential: TokenCredential, options?: TableServiceClientOptions)

参数

url

string

作为所需作目标的服务帐户的 URL,例如“https://myaccount.table.core.windows.net"。

tableName

string

表的名称

credential
TokenCredential

用于对请求进行身份验证的 Azure Active Directory 凭据

options
TableServiceClientOptions

自选。 用于配置 HTTP 管道的选项。

使用 Azure Active Directory 凭据的示例:

import { DefaultAzureCredential } from "@azure/identity";
import { TableClient } from "@azure/data-tables";

const credential = new DefaultAzureCredential();
const account = "<account name>";
const tableName = "<tableName>";

const clientWithAAD = new TableClient(
  `https://${account}.table.core.windows.net`,
  tableName,
  credential,
);

属性详细信息

pipeline

表示向 URL 发出 HTTP 请求的管道。 管道可以有多个策略来管理对服务器之前和之后每个请求的作。

pipeline: Pipeline

属性值

tableName

要在其中执行作的表的名称。

tableName: string

属性值

string

url

表帐户 URL

url: string

属性值

string

方法详细信息

createEntity<T>(TableEntity<T>, OperationOptions)

在表中插入实体。

function createEntity<T>(entity: TableEntity<T>, options?: OperationOptions): Promise<TableInsertEntityHeaders>

参数

entity

TableEntity<T>

表实体的属性。

options
OperationOptions

选项参数。

创建实体的示例

import { DefaultAzureCredential } from "@azure/identity";
import { TableClient } from "@azure/data-tables";

const account = "<account>";
const accountKey = "<accountkey>";
const tableName = "<tableName>";

const credential = new DefaultAzureCredential();
const client = new TableClient(`https://${account}.table.core.windows.net`, tableName, credential);

const testEntity = {
  partitionKey: "P1",
  rowKey: "R1",
  foo: "foo",
  bar: 123,
};
await client.createEntity(testEntity);

返回

createTable(OperationOptions)

使用传递给客户端构造函数的 tableName 创建表

function createTable(options?: OperationOptions): Promise<void>

参数

options
OperationOptions

选项参数。

创建表的示例

import { DefaultAzureCredential } from "@azure/identity";
import { TableClient } from "@azure/data-tables";

const account = "<account>";
const accountKey = "<accountkey>";
const tableName = "<tableName>";

const credential = new DefaultAzureCredential();
const client = new TableClient(`https://${account}.table.core.windows.net`, tableName, credential);

// If the table 'newTable' already exists, createTable doesn't throw
await client.createTable();

返回

Promise<void>

deleteEntity(string, string, DeleteTableEntityOptions)

删除表中的指定实体。

function deleteEntity(partitionKey: string, rowKey: string, options?: DeleteTableEntityOptions): Promise<TableDeleteEntityHeaders>

参数

partitionKey

string

实体的分区键。

rowKey

string

实体的行键。

options
DeleteTableEntityOptions

选项参数。

删除实体的示例

import { DefaultAzureCredential } from "@azure/identity";
import { TableClient } from "@azure/data-tables";

const account = "<account>";
const accountKey = "<accountkey>";
const tableName = "<tableName>";

const credential = new DefaultAzureCredential();
const client = new TableClient(`https://${account}.table.core.windows.net`, tableName, credential);

// deleteEntity deletes the entity that matches exactly the partitionKey and rowKey
await client.deleteEntity("<partitionKey>", "<rowKey>");

返回

deleteTable(OperationOptions)

永久删除包含其所有实体的当前表。

function deleteTable(options?: OperationOptions): Promise<void>

参数

options
OperationOptions

选项参数。

删除表的示例

import { DefaultAzureCredential } from "@azure/identity";
import { TableClient } from "@azure/data-tables";

const account = "<account>";
const accountKey = "<accountkey>";
const tableName = "<tableName>";

const credential = new DefaultAzureCredential();
const client = new TableClient(`https://${account}.table.core.windows.net`, tableName, credential);

await client.deleteTable();

返回

Promise<void>

fromConnectionString(string, string, TableServiceClientOptions)

从连接字符串创建 TableClient 的实例。

static function fromConnectionString(connectionString: string, tableName: string, options?: TableServiceClientOptions): TableClient

参数

connectionString

string

帐户连接字符串或 Azure 存储帐户的 SAS 连接字符串。 [ 注意 - 帐户连接字符串只能在NODE.JS运行时使用。 ] 帐户连接字符串示例 - DefaultEndpointsProtocol=https;AccountName=myaccount;AccountKey=accountKey;EndpointSuffix=core.windows.net SAS 连接字符串示例 - BlobEndpoint=https://myaccount.table.core.windows.net/;QueueEndpoint=https://myaccount.queue.core.windows.net/;FileEndpoint=https://myaccount.file.core.windows.net/;TableEndpoint=https://myaccount.table.core.windows.net/;SharedAccessSignature=sasString

tableName

string

options
TableServiceClientOptions

用于配置 HTTP 管道的选项。

返回

给定连接字符串中的新 TableClient。

getAccessPolicy(OperationOptions)

检索有关可用于共享访问签名的表上指定的任何存储访问策略的详细信息。

function getAccessPolicy(options?: OperationOptions): Promise<GetAccessPolicyResponse>

参数

options
OperationOptions

选项参数。

返回

getEntity<T>(string, string, GetTableEntityOptions)

返回表中的单个实体。

function getEntity<T>(partitionKey: string, rowKey: string, options?: GetTableEntityOptions): Promise<GetTableEntityResponse<TableEntityResult<T>>>

参数

partitionKey

string

实体的分区键。

rowKey

string

实体的行键。

options
GetTableEntityOptions

选项参数。

获取实体的示例

import { DefaultAzureCredential } from "@azure/identity";
import { TableClient } from "@azure/data-tables";

const account = "<account>";
const accountKey = "<accountkey>";
const tableName = "<tableName>";

const credential = new DefaultAzureCredential();
const client = new TableClient(`https://${account}.table.core.windows.net`, tableName, credential);

const entity = await client.getEntity("<partitionKey>", "<rowKey>");
console.log(`Entity: PartitionKey: ${entity.partitionKey} RowKey: ${entity.rowKey}`);

返回

listEntities<T>(ListTableEntitiesOptions)

查询表中的实体。

function listEntities<T>(options?: ListTableEntitiesOptions): PagedAsyncIterableIterator<TableEntityResult<T>, TableEntityResultPage<T>, PageSettings>

参数

options
ListTableEntitiesOptions

选项参数。

列出实体的示例

import { DefaultAzureCredential } from "@azure/identity";
import { TableClient } from "@azure/data-tables";

const account = "<account>";
const accountKey = "<accountkey>";
const tableName = "<tableName>";

const credential = new DefaultAzureCredential();
const client = new TableClient(`https://${account}.table.core.windows.net`, tableName, credential);

let i = 0;
const entities = client.listEntities();
for await (const entity of entities) {
  console.log(`Entity${++i}: PartitionKey: ${entity.partitionKey} RowKey: ${entity.rowKey}`);
}

返回

setAccessPolicy(SignedIdentifier[], OperationOptions)

设置可用于共享访问签名的表的存储访问策略。

function setAccessPolicy(tableAcl: SignedIdentifier[], options?: OperationOptions): Promise<TableSetAccessPolicyHeaders>

参数

tableAcl

SignedIdentifier[]

表的访问控制列表。

options
OperationOptions

选项参数。

返回

submitTransaction(TransactionAction[], OperationOptions)

提交由一组作组成的事务。 可以将作作为列表提供,也可以使用 TableTransaction 来帮助生成事务。

示例用法:

import { DefaultAzureCredential } from "@azure/identity";
import { TableClient, TransactionAction } from "@azure/data-tables";

const account = "<account>";
const accountKey = "<accountkey>";
const tableName = "<tableName>";

const credential = new DefaultAzureCredential();
const client = new TableClient(`https://${account}.table.core.windows.net`, tableName, credential);

const actions: TransactionAction[] = [
  ["create", { partitionKey: "p1", rowKey: "1", data: "test1" }],
  ["delete", { partitionKey: "p1", rowKey: "2" }],
  ["update", { partitionKey: "p1", rowKey: "3", data: "newTest" }, "Merge"],
];
const result = await client.submitTransaction(actions);

TableTransaction 的示例用法:

import { DefaultAzureCredential } from "@azure/identity";
import { TableClient, TableTransaction } from "@azure/data-tables";

const account = "<account>";
const accountKey = "<accountkey>";
const tableName = "<tableName>";

const credential = new DefaultAzureCredential();
const client = new TableClient(`https://${account}.table.core.windows.net`, tableName, credential);

const transaction = new TableTransaction();

// Call the available action in the TableTransaction object
transaction.createEntity({ partitionKey: "p1", rowKey: "1", data: "test1" });
transaction.deleteEntity("p1", "2");
transaction.updateEntity({ partitionKey: "p1", rowKey: "3", data: "newTest" }, "Merge");

// submitTransaction with the actions list on the transaction.
const result = await client.submitTransaction(transaction.actions);
function submitTransaction(actions: TransactionAction[], options?: OperationOptions): Promise<TableTransactionResponse>

参数

actions

TransactionAction[]

包含要执行的作的元组以及用于执行作的实体

options
OperationOptions

请求的选项。

返回

updateEntity<T>(TableEntity<T>, UpdateMode, UpdateTableEntityOptions)

更新表中的实体。

function updateEntity<T>(entity: TableEntity<T>, mode?: UpdateMode, options?: UpdateTableEntityOptions): Promise<TableUpdateEntityHeaders>

参数

entity

TableEntity<T>

要更新的实体的属性。

mode
UpdateMode

更新实体的不同模式:- 合并:通过更新实体的属性来更新实体,而无需替换现有实体。 - 替换:通过替换整个实体来更新现有实体。

options
UpdateTableEntityOptions

选项参数。

更新实体的示例

import { DefaultAzureCredential } from "@azure/identity";
import { TableClient } from "@azure/data-tables";

const account = "<account>";
const accountKey = "<accountkey>";
const tableName = "<tableName>";

const credential = new DefaultAzureCredential();
const client = new TableClient(`https://${account}.table.core.windows.net`, tableName, credential);

const entity = { partitionKey: "p1", rowKey: "r1", bar: "updatedBar" };

// Update uses update mode "Merge" as default
// merge means that update will match a stored entity
// that has the same partitionKey and rowKey as the entity
// passed to the method and then will only update the properties present in it.
// Any other properties that are not defined in the entity passed to updateEntity
// will remain as they are in the service
await client.updateEntity(entity);

// We can also set the update mode to Replace, which will match the entity passed
// to updateEntity with one stored in the service and replace with the new one.
// If there are any missing properties in the entity passed to updateEntity, they
// will be removed from the entity stored in the service
await client.updateEntity(entity, "Replace");

返回

upsertEntity<T>(TableEntity<T>, UpdateMode, OperationOptions)

向上插入表中的实体。

function upsertEntity<T>(entity: TableEntity<T>, mode?: UpdateMode, options?: OperationOptions): Promise<TableMergeEntityHeaders>

参数

entity

TableEntity<T>

表实体的属性。

mode
UpdateMode

更新实体的不同模式:- 合并:通过更新实体的属性来更新实体,而无需替换现有实体。 - 替换:通过替换整个实体来更新现有实体。

options
OperationOptions

选项参数。

更新插入实体的示例

import { DefaultAzureCredential } from "@azure/identity";
import { TableClient } from "@azure/data-tables";

const account = "<account>";
const accountKey = "<accountkey>";
const tableName = "<tableName>";

const credential = new DefaultAzureCredential();
const client = new TableClient(`https://${account}.table.core.windows.net`, tableName, credential);

const entity = { partitionKey: "p1", rowKey: "r1", bar: "updatedBar" };

// Upsert uses update mode "Merge" as default.
// This behaves similarly to update but creates the entity
// if it doesn't exist in the service
await client.upsertEntity(entity);

// We can also set the update mode to Replace.
// This behaves similarly to update but creates the entity
// if it doesn't exist in the service
await client.upsertEntity(entity, "Replace");

返回