TableClient class
TableClient 代表 Azure Tables 服務的用戶端,可讓您在單一資料表上執行作業。
建構函式
Table |
建立 TableClient 類別的新實例。 |
Table |
建立 TableClient 類別的新實例。 |
Table |
建立 TableClient 的實例。 |
Table |
建立 TableClient 類別的新實例。 |
屬性
pipeline | 表示對 URL 提出 HTTP 要求的管線。 管線可以有多個原則來管理對伺服器進行之前和之後操作每個要求。 |
table |
要執行作業的資料表名稱。 |
url | 資料表帳戶 URL |
方法
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 管線的選項。
使用帳戶名稱/金鑰的範例:
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
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!"});
傳回
Promise<TableInsertEntityHeaders>
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>")
傳回
Promise<TableDeleteEntityHeaders>
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
選項參數。
傳回
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
選項參數。
取得實體的範例
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);
傳回
Promise<GetTableEntityResponse<TableEntityResult<T>>>
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
資料表的存取控制清單。
- options
- OperationOptions
選項參數。
傳回
Promise<TableSetAccessPolicyHeaders>
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
Tuple,其中包含要執行的動作,以及執行動作的實體
傳回
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
選項參數。
更新實體的範例
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")
傳回
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
選項參數。
插入實體的範例
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")
傳回
Promise<TableMergeEntityHeaders>