共用方式為


Database class

讀取或刪除現有資料庫的作業。

請參閱 資料庫 來建立新的資料庫,以及讀取/查詢所有資料庫;使用 client.databases

注意:所有這些作業都會針對固定預算進行呼叫。 您應該設計您的系統,讓這些呼叫以子線性方式調整您的應用程式。 例如,請勿在每個單一 database.read() 呼叫之前呼叫 item.read(),以確保資料庫存在;在應用程式啟動時執行此動作。

屬性

client
containers

用於建立新的容器,或查詢/讀取所有容器。

使用 .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 { body: containerDefinition, container } = await client
  .database("<db id>")
  .containers.create({ id: "<container id>" });
id
url

傳回資源的參考 URL。 用於在許可權中連結。

範例

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 { database } = await client.databases.createIfNotExists({ id: "Test Database" });

const url = database.url;
users

用於建立新的使用者,或查詢/讀取所有使用者。

使用 .user(id) 依標識碼讀取、取代或刪除特定的現有 使用者

方法

container(string)

用來依標識碼讀取、取代或刪除特定的現有 資料庫

使用 .containers 建立新的容器,或查詢/讀取所有容器。

範例

刪除容器

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("<db id>").container("<container id>").delete();
createClientEncryptionKey(string, AEAD_AES_256_CBC_HMAC_SHA256, EncryptionKeyWrapMetadata)

為資料庫帳戶創建加密金鑰

範例

import { ClientSecretCredential } from "@azure/identity";
import {
  AzureKeyVaultEncryptionKeyResolver,
  CosmosClient,
  EncryptionKeyWrapMetadata,
  EncryptionKeyResolverName,
  KeyEncryptionAlgorithm,
  EncryptionAlgorithm,
} from "@azure/cosmos";

const endpoint = "https://your-account.documents.azure.com";
const key = "<database account masterkey>";
const credentials = new ClientSecretCredential("<tenant-id>", "<client-id>", "<app-secret>");
const keyResolver = new AzureKeyVaultEncryptionKeyResolver(credentials);
const client = new CosmosClient({
  endpoint,
  key,
  clientEncryptionOptions: {
    keyEncryptionKeyResolver: keyResolver,
  },
});
const { database } = await client.databases.createIfNotExists({ id: "<db id>" });
const metadata: EncryptionKeyWrapMetadata = {
  type: EncryptionKeyResolverName.AzureKeyVault,
  name: "<key-name>",
  value: "<key-vault-url>",
  algorithm: KeyEncryptionAlgorithm.RSA_OAEP,
};

await database.createClientEncryptionKey(
  "<cek-id>",
  EncryptionAlgorithm.AEAD_AES_256_CBC_HMAC_SHA256,
  metadata,
);
delete(RequestOptions)

刪除指定的資料庫。

範例

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();
read(RequestOptions)

讀取指定資料庫的定義。

範例

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: database } = await client.database("<db id>").read();
readClientEncryptionKey(string)

讀取資料庫帳戶的加密金鑰

範例

import { ClientSecretCredential } from "@azure/identity";
import { AzureKeyVaultEncryptionKeyResolver, CosmosClient } from "@azure/cosmos";

const endpoint = "https://your-account.documents.azure.com";
const key = "<database account masterkey>";
const credentials = new ClientSecretCredential("<tenant-id>", "<client-id>", "<app-secret>");
const keyResolver = new AzureKeyVaultEncryptionKeyResolver(credentials);
const client = new CosmosClient({
  endpoint,
  key,
  clientEncryptionOptions: {
    keyEncryptionKeyResolver: keyResolver,
  },
});
const { database } = await client.databases.createIfNotExists({ id: "<db id>" });

const { resource: clientEncryptionKey } = await database.readClientEncryptionKey("<cek-id>");
readOffer(RequestOptions)

取得資料庫上的供應專案。 如果不存在,則會傳回未定義的 OfferResponse。

範例

在資料庫中閱讀報價

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: offer } = await client.database("<db id>").readOffer();
rewrapClientEncryptionKey(string, EncryptionKeyWrapMetadata)

使用新的金鑰加密金鑰重新包裝用戶端加密金鑰

範例

import { ClientSecretCredential } from "@azure/identity";
import {
  AzureKeyVaultEncryptionKeyResolver,
  CosmosClient,
  EncryptionKeyWrapMetadata,
  EncryptionKeyResolverName,
  KeyEncryptionAlgorithm,
} from "@azure/cosmos";

const endpoint = "https://your-account.documents.azure.com";
const key = "<database account masterkey>";
const credentials = new ClientSecretCredential("<tenant-id>", "<client-id>", "<app-secret>");
const keyResolver = new AzureKeyVaultEncryptionKeyResolver(credentials);
const client = new CosmosClient({
  endpoint,
  key,
  clientEncryptionOptions: {
    keyEncryptionKeyResolver: keyResolver,
  },
});
const { database } = await client.databases.createIfNotExists({ id: "<db id>" });
const newMetadata: EncryptionKeyWrapMetadata = {
  type: EncryptionKeyResolverName.AzureKeyVault,
  name: "<key-name>",
  value: "<key-vault-url>",
  algorithm: KeyEncryptionAlgorithm.RSA_OAEP,
};

await database.rewrapClientEncryptionKey("<new-cek-id>", newMetadata);
user(string)

用來依標識碼讀取、取代或刪除特定的現有 使用者

使用 .users 來建立新的使用者,或查詢/讀取所有使用者。

範例

刪除使用者

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("<db id>").user("<user id>").delete();

屬性詳細資料

client

client: CosmosClient

屬性值

containers

用於建立新的容器,或查詢/讀取所有容器。

使用 .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 { body: containerDefinition, container } = await client
  .database("<db id>")
  .containers.create({ id: "<container id>" });
containers: Containers

屬性值

id

id: string

屬性值

string

url

傳回資源的參考 URL。 用於在許可權中連結。

範例

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 { database } = await client.databases.createIfNotExists({ id: "Test Database" });

const url = database.url;
string url

屬性值

string

users

用於建立新的使用者,或查詢/讀取所有使用者。

使用 .user(id) 依標識碼讀取、取代或刪除特定的現有 使用者

users: Users

屬性值

方法詳細資料

container(string)

用來依標識碼讀取、取代或刪除特定的現有 資料庫

使用 .containers 建立新的容器,或查詢/讀取所有容器。

範例

刪除容器

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("<db id>").container("<container id>").delete();
function container(id: string): Container

參數

id

string

傳回

createClientEncryptionKey(string, AEAD_AES_256_CBC_HMAC_SHA256, EncryptionKeyWrapMetadata)

為資料庫帳戶創建加密金鑰

範例

import { ClientSecretCredential } from "@azure/identity";
import {
  AzureKeyVaultEncryptionKeyResolver,
  CosmosClient,
  EncryptionKeyWrapMetadata,
  EncryptionKeyResolverName,
  KeyEncryptionAlgorithm,
  EncryptionAlgorithm,
} from "@azure/cosmos";

const endpoint = "https://your-account.documents.azure.com";
const key = "<database account masterkey>";
const credentials = new ClientSecretCredential("<tenant-id>", "<client-id>", "<app-secret>");
const keyResolver = new AzureKeyVaultEncryptionKeyResolver(credentials);
const client = new CosmosClient({
  endpoint,
  key,
  clientEncryptionOptions: {
    keyEncryptionKeyResolver: keyResolver,
  },
});
const { database } = await client.databases.createIfNotExists({ id: "<db id>" });
const metadata: EncryptionKeyWrapMetadata = {
  type: EncryptionKeyResolverName.AzureKeyVault,
  name: "<key-name>",
  value: "<key-vault-url>",
  algorithm: KeyEncryptionAlgorithm.RSA_OAEP,
};

await database.createClientEncryptionKey(
  "<cek-id>",
  EncryptionAlgorithm.AEAD_AES_256_CBC_HMAC_SHA256,
  metadata,
);
function createClientEncryptionKey(clientEncryptionKeyId: string, encryptionAlgorithm: AEAD_AES_256_CBC_HMAC_SHA256, keyWrapMetadata: EncryptionKeyWrapMetadata): Promise<ClientEncryptionKeyResponse>

參數

clientEncryptionKeyId

string

encryptionAlgorithm
AEAD_AES_256_CBC_HMAC_SHA256
keyWrapMetadata
EncryptionKeyWrapMetadata

傳回

delete(RequestOptions)

刪除指定的資料庫。

範例

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 delete(options?: RequestOptions): Promise<DatabaseResponse>

參數

options
RequestOptions

傳回

Promise<DatabaseResponse>

read(RequestOptions)

讀取指定資料庫的定義。

範例

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: database } = await client.database("<db id>").read();
function read(options?: RequestOptions): Promise<DatabaseResponse>

參數

options
RequestOptions

傳回

Promise<DatabaseResponse>

readClientEncryptionKey(string)

讀取資料庫帳戶的加密金鑰

範例

import { ClientSecretCredential } from "@azure/identity";
import { AzureKeyVaultEncryptionKeyResolver, CosmosClient } from "@azure/cosmos";

const endpoint = "https://your-account.documents.azure.com";
const key = "<database account masterkey>";
const credentials = new ClientSecretCredential("<tenant-id>", "<client-id>", "<app-secret>");
const keyResolver = new AzureKeyVaultEncryptionKeyResolver(credentials);
const client = new CosmosClient({
  endpoint,
  key,
  clientEncryptionOptions: {
    keyEncryptionKeyResolver: keyResolver,
  },
});
const { database } = await client.databases.createIfNotExists({ id: "<db id>" });

const { resource: clientEncryptionKey } = await database.readClientEncryptionKey("<cek-id>");
function readClientEncryptionKey(clientEncryptionKeyId: string): Promise<ClientEncryptionKeyResponse>

參數

clientEncryptionKeyId

string

傳回

readOffer(RequestOptions)

取得資料庫上的供應專案。 如果不存在,則會傳回未定義的 OfferResponse。

範例

在資料庫中閱讀報價

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: offer } = await client.database("<db id>").readOffer();
function readOffer(options?: RequestOptions): Promise<OfferResponse>

參數

options
RequestOptions

傳回

Promise<OfferResponse>

rewrapClientEncryptionKey(string, EncryptionKeyWrapMetadata)

使用新的金鑰加密金鑰重新包裝用戶端加密金鑰

範例

import { ClientSecretCredential } from "@azure/identity";
import {
  AzureKeyVaultEncryptionKeyResolver,
  CosmosClient,
  EncryptionKeyWrapMetadata,
  EncryptionKeyResolverName,
  KeyEncryptionAlgorithm,
} from "@azure/cosmos";

const endpoint = "https://your-account.documents.azure.com";
const key = "<database account masterkey>";
const credentials = new ClientSecretCredential("<tenant-id>", "<client-id>", "<app-secret>");
const keyResolver = new AzureKeyVaultEncryptionKeyResolver(credentials);
const client = new CosmosClient({
  endpoint,
  key,
  clientEncryptionOptions: {
    keyEncryptionKeyResolver: keyResolver,
  },
});
const { database } = await client.databases.createIfNotExists({ id: "<db id>" });
const newMetadata: EncryptionKeyWrapMetadata = {
  type: EncryptionKeyResolverName.AzureKeyVault,
  name: "<key-name>",
  value: "<key-vault-url>",
  algorithm: KeyEncryptionAlgorithm.RSA_OAEP,
};

await database.rewrapClientEncryptionKey("<new-cek-id>", newMetadata);
function rewrapClientEncryptionKey(clientEncryptionKeyId: string, newKeyWrapMetadata: EncryptionKeyWrapMetadata): Promise<ClientEncryptionKeyResponse>

參數

clientEncryptionKeyId

string

newKeyWrapMetadata
EncryptionKeyWrapMetadata

新的加密金鑰包裝元數據

傳回

使用新的客戶管理式金鑰重新包裝用戶端加密金鑰

user(string)

用來依標識碼讀取、取代或刪除特定的現有 使用者

使用 .users 來建立新的使用者,或查詢/讀取所有使用者。

範例

刪除使用者

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("<db id>").user("<user id>").delete();
function user(id: string): User

參數

id

string

傳回