CosmosClient class
提供 Azure Cosmos DB 資料庫帳戶的客戶端邏輯表示法。 此客戶端可用來在 Azure Cosmos DB 資料庫服務中設定和執行要求。
範例
具現化用戶端並建立新的資料庫
import { CosmosClient } from "@azure/cosmos";
const endpoint = "https://your-account.documents.azure.com";
const key = "<database account masterkey>";
const client = new CosmosClient({ endpoint, key });
範例
使用自定義連線原則具現化用戶端
import { CosmosClient } from "@azure/cosmos";
const endpoint = "https://your-account.documents.azure.com";
const key = "<database account masterkey>";
const client = new CosmosClient({
endpoint,
key,
connectionPolicy: {
requestTimeout: 10000,
},
});
範例
具現化具有 AAD 驗證和自訂範圍的用戶端
import { DefaultAzureCredential } from "@azure/identity";
import { CosmosClient } from "@azure/cosmos";
const endpoint = "https://your-account.documents.azure.com";
const aadCredentials = new DefaultAzureCredential();
const client = new CosmosClient({
endpoint,
aadCredentials,
aadScope: "https://cosmos.azure.com/.default", // Optional custom scope
});
建構函式
| Cosmos |
建立新的 CosmosClient 物件。 如需您可以使用哪些選項的詳細資訊,請參閱 CosmosClientOptions。 |
| Cosmos |
從連接字串建立新的 CosmosClient 物件。 您可以在 Azure 入口網站中找到您的資料庫連接字串 |
屬性
| databases | 用於建立新的資料庫,或查詢/讀取所有資料庫。 使用 範例 建立新的資料庫
|
| offers | 用於查詢 & 讀取所有供應專案。 使用 |
方法
| database(string) | 用於讀取、更新或刪除現有資料庫,方法是標識符或存取屬於該資料庫的容器。 這不會進行網路呼叫。 在取得資料庫 範例 從現有的資料庫建立新的容器
範例 刪除現有的資料庫
|
| dispose() | 清除背景端點重新整理器。 在另一個進程中終結 CosmosClient 時,請使用 client.dispose()。 |
| get |
取得目前 DatabaseAccount 的相關信息(包括支援哪些區域等等) |
| get |
取得目前使用的讀取端點。 適用於疑難解答目的。 如果我們使用位置特定端點,URL 可能會包含區域後綴(例如 “-eastus”。 |
| get |
取得目前使用的讀取端點。 適用於疑難解答目的。 如果我們使用位置特定端點,URL 可能會包含區域後綴(例如 “-eastus”。 |
| get |
取得目前使用的寫入端點 URL。 適用於疑難解答目的。 如果我們使用位置特定端點,URL 可能會包含區域後綴(例如 “-eastus”。 |
| get |
取得已知的寫入端點。 適用於疑難解答目的。 如果我們使用位置特定端點,URL 可能會包含區域後綴(例如 “-eastus”。 |
| offer(string) | 用於依標識碼讀取或更新現有的供應專案。 |
建構函式詳細資料
CosmosClient(CosmosClientOptions)
建立新的 CosmosClient 物件。 如需您可以使用哪些選項的詳細資訊,請參閱 CosmosClientOptions。
new CosmosClient(options: CosmosClientOptions)
參數
- options
- CosmosClientOptions
選項包;至少需要設定端點和驗證
CosmosClient(string)
從連接字串建立新的 CosmosClient 物件。 您可以在 Azure 入口網站中找到您的資料庫連接字串
new CosmosClient(connectionString: string)
參數
- connectionString
-
string
屬性詳細資料
databases
用於建立新的資料庫,或查詢/讀取所有資料庫。
使用 .database(id) 依標識碼讀取、取代或刪除特定的現有資料庫。
範例
建立新的資料庫
import { CosmosClient } from "@azure/cosmos";
const endpoint = "https://your-account.documents.azure.com";
const key = "<database account masterkey>";
const client = new CosmosClient({ endpoint, key });
const { resource: databaseDefinition, database } = await client.databases.create({
id: "<name here>",
});
databases: Databases
屬性值
offers
方法詳細資料
database(string)
用於讀取、更新或刪除現有資料庫,方法是標識符或存取屬於該資料庫的容器。
這不會進行網路呼叫。 在取得資料庫 .read 對象之後,請使用 取得資料庫的相關信息。
範例
從現有的資料庫建立新的容器
import { CosmosClient } from "@azure/cosmos";
const endpoint = "https://your-account.documents.azure.com";
const key = "<database account masterkey>";
const client = new CosmosClient({ endpoint, key });
const container = client.database("<database id>").containers.create({
id: "<name here>",
});
範例
刪除現有的資料庫
import { CosmosClient } from "@azure/cosmos";
const endpoint = "https://your-account.documents.azure.com";
const key = "<database account masterkey>";
const client = new CosmosClient({ endpoint, key });
await client.database("<id here>").delete();
function database(id: string): Database
參數
- id
-
string
資料庫的標識碼。
傳回
dispose()
清除背景端點重新整理器。 在另一個進程中終結 CosmosClient 時,請使用 client.dispose()。
function dispose()
getDatabaseAccount(RequestOptions)
取得目前 DatabaseAccount 的相關信息(包括支援哪些區域等等)
function getDatabaseAccount(options?: RequestOptions): Promise<ResourceResponse<DatabaseAccount>>
參數
- options
- RequestOptions
傳回
Promise<ResourceResponse<DatabaseAccount>>
getReadEndpoint()
取得目前使用的讀取端點。 適用於疑難解答目的。
如果我們使用位置特定端點,URL 可能會包含區域後綴(例如 “-eastus”。
function getReadEndpoint(): Promise<string>
傳回
Promise<string>
getReadEndpoints()
取得目前使用的讀取端點。 適用於疑難解答目的。
如果我們使用位置特定端點,URL 可能會包含區域後綴(例如 “-eastus”。
function getReadEndpoints(): Promise<readonly string[]>
傳回
Promise<readonly string[]>
getWriteEndpoint()
取得目前使用的寫入端點 URL。 適用於疑難解答目的。
如果我們使用位置特定端點,URL 可能會包含區域後綴(例如 “-eastus”。
function getWriteEndpoint(): Promise<string>
傳回
Promise<string>
getWriteEndpoints()
取得已知的寫入端點。 適用於疑難解答目的。
如果我們使用位置特定端點,URL 可能會包含區域後綴(例如 “-eastus”。
function getWriteEndpoints(): Promise<readonly string[]>
傳回
Promise<readonly string[]>