CosmosClient class

提供 Azure Cosmos DB 資料庫帳戶的客戶端邏輯表示法。 此客戶端可用來設定和執行 Azure Cosmos DB 資料庫服務中的要求。

範例

具現化用戶端並建立新的資料庫

const client = new CosmosClient({endpoint: "<URL HERE>", auth: {masterKey: "<KEY HERE>"}});
await client.databases.create({id: "<datbase name here>"});

範例

使用自定義連線原則具現化用戶端

const connectionPolicy = new ConnectionPolicy();
connectionPolicy.RequestTimeout = 10000;
const client = new CosmosClient({
   endpoint: "<URL HERE>",
   auth: {masterKey: "<KEY HERE>"},
   connectionPolicy
});

建構函式

CosmosClient(CosmosClientOptions)

建立新的 CosmosClient 物件。 如需您可以使用哪些選項的詳細資訊,請參閱 CosmosClientOptions

CosmosClient(string)

從 連接字串 建立新的 CosmosClient 物件。 您可以在 Azure 入口網站中找到您的資料庫 連接字串

屬性

databases

用於建立新的資料庫,或查詢/讀取所有資料庫。

用來 .database(id) 依標識碼讀取、取代或刪除特定的現有資料庫。

範例

建立新的資料庫

const {resource: databaseDefinition, database} = await client.databases.create({id: "<name here>"});
offers

用於查詢 & 讀取所有供應專案。

用來 .offer(id) 讀取或取代現有的供應專案。

方法

database(string)

用於依標識符讀取、更新或刪除現有資料庫,或存取屬於該資料庫的容器。

這不會進行網路呼叫。 使用 .read 取得 Database 對象之後,取得資料庫的相關信息。

範例

從現有的資料庫建立新的容器

const container = client.database("<database id>").containers.create("<container id>");

範例

刪除現有的資料庫

await client.database("<id here>").delete();
dispose()

清除背景端點重新整理程式。 在另一個進程中終結 CosmosClient 時,請使用 client.dispose () 。

getDatabaseAccount(RequestOptions)

取得目前 DatabaseAccount (的相關信息,包括支援哪些區域等等。)

getReadEndpoint()

取得目前使用的讀取端點。 適用於疑難解答目的。

url 可能包含區域後綴 (例如“-eastus”) ,如果我們使用位置特定端點。

getReadEndpoints()

取得目前使用的讀取端點。 適用於疑難解答目的。

url 可能包含區域後綴 (例如“-eastus”) ,如果我們使用位置特定端點。

getWriteEndpoint()

取得目前使用的寫入端點 URL。 適用於疑難解答目的。

url 可能包含區域後綴 (例如“-eastus”) ,如果我們使用位置特定端點。

getWriteEndpoints()

取得已知的寫入端點。 適用於疑難解答目的。

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) 依標識碼讀取、取代或刪除特定的現有資料庫。

範例

建立新的資料庫

const {resource: databaseDefinition, database} = await client.databases.create({id: "<name here>"});
databases: Databases

屬性值

offers

用於查詢 & 讀取所有供應專案。

用來 .offer(id) 讀取或取代現有的供應專案。

offers: Offers

屬性值

方法詳細資料

database(string)

用於依標識符讀取、更新或刪除現有資料庫,或存取屬於該資料庫的容器。

這不會進行網路呼叫。 使用 .read 取得 Database 對象之後,取得資料庫的相關信息。

範例

從現有的資料庫建立新的容器

const container = client.database("<database id>").containers.create("<container id>");

範例

刪除現有的資料庫

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

傳回

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

offer(string)

用於依標識碼讀取或更新現有的供應專案。

function offer(id: string): Offer

參數

id

string

供應項目的標識碼。

傳回