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

TableClient class

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

构造函数

TableClient(string, string, NamedKeyCredential, TableServiceClientOptions)

创建 TableClient 类的新实例。

TableClient(string, string, SASCredential, TableServiceClientOptions)

创建 TableClient 类的新实例。

TableClient(string, string, TableServiceClientOptions)

创建 TableClient 的实例。

TableClient(string, string, TokenCredential, TableServiceClientOptions)

创建 TableClient 类的新实例。

属性

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[])

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

用法示例:

const { TableClient } = require("@azure/data-tables");
const connectionString = "<connection-string>"
const tableName = "<tableName>"
const client = TableClient.fromConnectionString(connectionString, tableName);
const actions = [
   ["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 的示例用法:

const { TableClient } = require("@azure/data-tables");
const connectionString = "<connection-string>"
const tableName = "<tableName>"
const client = TableClient.fromConnectionString(connectionString, tableName);
const transaction = new TableTransaction();
// Call the available action in the TableTransaction object
transaction.create({partitionKey: "p1", rowKey: "1", data: "test1"});
transaction.delete("p1", "2");
transaction.update({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 管道的选项。

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

const { AzureNamedKeyCredential, TableClient } = require("@azure/data-tables");
const account = "<storage account name>";
const accountKey = "<account key>"
const tableName = "<table name>";
const sharedKeyCredential = new AzureNamedKeyCredential(account, accountKey);

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

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 令牌的示例:

const { AzureSASCredential, TableClient } = require("@azure/data-tables");
const account = "<storage account name>";
const sasToken = "<sas-token>";
const tableName = "<table name>";
const sasCredential = new AzureSASCredential(sasToken);

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

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 令牌的示例:

const { TableClient } = require("@azure/data-tables");
const account = "<storage account name>";
const sasToken = "<SAS token>";
const tableName = "<table name>";

const client = 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 凭据的示例:

cons { DefaultAzureCredential } = require("@azure/identity");
const { AzureSASCredential, TableClient } = require("@azure/data-tables");
const account = "<storage account name>";
const sasToken = "<sas-token>";
const tableName = "<table name>";
const credential = new DefaultAzureCredential();

const client = 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

选项参数。

创建实体的示例

const { AzureNamedKeyCredential, TableClient } = require("@azure/data-tables")
const account = "<storage account name>";
const accountKey = "<account key>"
const tableName = "<table name>";
const sharedKeyCredential = new AzureNamedKeyCredential(account, accountKey);

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

// partitionKey and rowKey are required properties of the entity to create
// and accepts any other properties
await client.createEntity({partitionKey: "p1", rowKey: "r1", foo: "Hello!"});

返回

createTable(OperationOptions)

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

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

参数

options
OperationOptions

选项参数。

创建表的示例

const { AzureNamedKeyCredential, TableClient } = require("@azure/data-tables")
const account = "<storage account name>";
const accountKey = "<account key>"
const tableName = "<table name>";
const sharedKeyCredential = new AzureNamedKeyCredential(account, accountKey);

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

// calling create table will create the table used
// to instantiate the TableClient.
// Note: If the table already
// exists this function 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

选项参数。

删除实体的示例

const { AzureNamedKeyCredential, TableClient } = require("@azure/data-tables")
const account = "<storage account name>";
const accountKey = "<account key>"
const tableName = "<table name>";
const sharedKeyCredential = new AzureNamedKeyCredential(account, accountKey);

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

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

返回

deleteTable(OperationOptions)

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

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

参数

options
OperationOptions

选项参数。

删除表的示例

const { AzureNamedKeyCredential, TableClient } = require("@azure/data-tables")
const account = "<storage account name>";
const accountKey = "<account key>"
const tableName = "<table name>";
const sharedKeyCredential = new AzureNamedKeyCredential(account, accountKey);

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

// calling deleteTable will delete the table used
// to instantiate the TableClient.
// Note: If the table doesn't exist this function doesn't fail.
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

选项参数。

获取实体的示例

const { AzureNamedKeyCredential, TableClient } = require("@azure/data-tables")
const account = "<storage account name>";
const accountKey = "<account key>"
const tableName = "<table name>";
const sharedKeyCredential = new AzureNamedKeyCredential(account, accountKey);

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

// getEntity will get a single entity stored in the service that
// matches exactly the partitionKey and rowKey used as parameters
// to the method.
const entity = await client.getEntity("<partitionKey>", "<rowKey>");
console.log(entity);

返回

listEntities<T>(ListTableEntitiesOptions)

查询表中的实体。

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

参数

options
ListTableEntitiesOptions

选项参数。

列出实体的示例

const { AzureNamedKeyCredential, TableClient } = require("@azure/data-tables")
const account = "<storage account name>";
const accountKey = "<account key>"
const tableName = "<table name>";
const sharedKeyCredential = new AzureNamedKeyCredential(account, accountKey);

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

// list entities returns a AsyncIterableIterator
// this helps consuming paginated responses by
// automatically handling getting the next pages
const entities = client.listEntities();

// this loop will get all the entities from all the pages
// returned by the service
for await (const entity of entities) {
   console.log(entity);
}

返回

setAccessPolicy(SignedIdentifier[], OperationOptions)

为可与共享访问签名一起使用的表设置存储的访问策略。

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

参数

tableAcl

SignedIdentifier[]

表的访问控制列表。

options
OperationOptions

选项参数。

返回

submitTransaction(TransactionAction[])

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

用法示例:

const { TableClient } = require("@azure/data-tables");
const connectionString = "<connection-string>"
const tableName = "<tableName>"
const client = TableClient.fromConnectionString(connectionString, tableName);
const actions = [
   ["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 的示例用法:

const { TableClient } = require("@azure/data-tables");
const connectionString = "<connection-string>"
const tableName = "<tableName>"
const client = TableClient.fromConnectionString(connectionString, tableName);
const transaction = new TableTransaction();
// Call the available action in the TableTransaction object
transaction.create({partitionKey: "p1", rowKey: "1", data: "test1"});
transaction.delete("p1", "2");
transaction.update({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[]): Promise<TableTransactionResponse>

参数

actions

TransactionAction[]

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

返回

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

选项参数。

更新实体的示例

const { AzureNamedKeyCredential, TableClient } = require("@azure/data-tables")
const account = "<storage account name>";
const accountKey = "<account key>"
const tableName = "<table name>";
const sharedKeyCredential = new AzureNamedKeyCredential(account, accountKey);

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

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

选项参数。

更新插入实体的示例

const { AzureNamedKeyCredential, TableClient } = require("@azure/data-tables")
const account = "<storage account name>";
const accountKey = "<account key>"
const tableName = "<table name>";
const sharedKeyCredential = new AzureNamedKeyCredential(account, accountKey);

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

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")

返回