TableClient class
TableClient 代表 Azure 資料表服務的用戶端,可讓您在單一數據表上執行作業。
建構函式
| Table |
建立 TableClient 類別的新實例。 |
| Table |
建立 TableClient 類別的新實例。 |
| Table |
建立 TableClient 的實例。 |
| Table |
建立 TableClient 類別的新實例。 |
方法
| create |
在數據表中插入實體。 |
| create |
使用傳遞至用戶端建構函式的 tableName 建立數據表 |
| delete |
刪除數據表中指定的實體。 |
| delete |
永久刪除具有其所有實體的目前數據表。 |
| from |
從連接字串建立 TableClient 的實例。 |
| get |
擷取數據表上指定之任何可搭配共用存取簽章使用之預存存取原則的詳細數據。 |
| get |
傳回數據表中的單一實體。 |
| list |
查詢數據表中的實體。 |
| set |
設定可與共用存取簽章搭配使用的數據表預存存取原則。 |
| submit |
提交由一組動作所組成的交易。 您可以提供動作做為清單,也可以使用 tableTransaction 來協助建置交易。 範例用法:
TableTransaction 的範例用法:
|
| update |
更新數據表中的實體。 |
| upsert |
將數據表中的實體向上插入。 |
建構函式詳細資料
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 用來驗證要求。 僅支持節點
- 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
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);
傳回
Promise<TableInsertEntityHeaders>
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>");
傳回
Promise<TableDeleteEntityHeaders>
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
選項參數。
傳回
Promise<GetAccessPolicyResponse>
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}`);
傳回
Promise<GetTableEntityResponse<TableEntityResult<T>>>
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
數據表的訪問控制清單。
- options
- OperationOptions
選項參數。
傳回
Promise<TableSetAccessPolicyHeaders>
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
Tuple,其中包含要執行的動作,以及用來執行動作的實體
- options
- OperationOptions
請求的選項。
傳回
Promise<TableTransactionResponse>
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");
傳回
Promise<TableUpdateEntityHeaders>
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");
傳回
Promise<TableMergeEntityHeaders>