Database class
读取或删除现有数据库的作。
请参阅 数据库,了解如何创建新数据库,以及读取/查询所有数据库;使用 client.databases
。
注意:所有这些作都针对固定预算进行调用。
应设计系统,以便这些调用与应用程序进行子线性缩放。
例如,在每次调用 database.read()
之前不要调用 item.read()
,以确保数据库存在;在应用程序启动时执行此作。
属性
client | |
containers | 用于创建新容器或查询/读取所有容器。 使用 示例 创建新容器
|
id | |
url | 返回资源的引用 URL。 用于在权限中链接。 示例
|
users | 用于创建新用户或查询/读取所有用户。 使用 |
方法
container(string) | 用于按 ID 读取、替换或删除特定现有 数据库。 使用 示例 删除容器
|
create |
为数据库帐户创建加密密钥 示例
|
delete(Request |
删除给定的数据库。 示例
|
read(Request |
读取给定数据库的定义。 示例
|
read |
读取数据库帐户的加密密钥 示例
|
read |
获取数据库上的产品/服务。 如果不存在,则返回未定义的 OfferResponse。 示例 在数据库中阅读报价
|
rewrap |
使用新的密钥加密密钥重新包装客户端加密密钥 示例
|
user(string) | 用于按 ID 读取、替换或删除特定现有 用户。 使用 示例 删除用户
|
属性详细信息
client
containers
用于创建新容器或查询/读取所有容器。
使用 .database(id)
按 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
方法详细信息
container(string)
用于按 ID 读取、替换或删除特定现有 数据库。
使用 .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
返回
Promise<ClientEncryptionKeyResponse>
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
返回
Promise<ClientEncryptionKeyResponse>
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
新的加密密钥包装元数据
返回
Promise<ClientEncryptionKeyResponse>
使用新的客户管理式密钥重新包装客户端加密密钥
user(string)
用于按 ID 读取、替换或删除特定现有 用户。
使用 .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