Freigeben über


Database class

Vorgänge zum Lesen oder Löschen einer vorhandenen Datenbank.

Siehe Datenbanken zum Erstellen neuer Datenbanken und lesen/Abfragen aller Datenbanken; verwenden Sie client.databases.

Hinweis: Alle diese Vorgänge führen Aufrufe für ein festes Budget aus. Sie sollten Ihr System so entwerfen, dass diese Aufrufe mit Ihrer Anwendung sublinear skaliert werden. Rufen Sie z. B. vor jedem einzelnen database.read() Aufruf nicht item.read() auf, um sicherzustellen, dass die Datenbank vorhanden ist; Führen Sie dies einmal beim Starten der Anwendung aus.

Eigenschaften

client
containers

Wird zum Erstellen neuer Container oder zum Abfragen/Lesen aller Container verwendet.

Verwenden Sie .database(id), um ein bestimmtes, vorhandenes Datenbank- nach ID zu lesen, zu ersetzen oder zu löschen.

Beispiel

Erstellen eines neuen 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 });

const { body: containerDefinition, container } = await client
  .database("<db id>")
  .containers.create({ id: "<container id>" });
id
url

Gibt eine Verweis-URL für die Ressource zurück. Wird zum Verknüpfen in Berechtigungen verwendet.

Beispiel

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

Wird zum Erstellen neuer Benutzer oder zum Abfragen/Lesen aller Benutzer verwendet.

Verwenden Sie .user(id), um eine bestimmte, vorhandene Benutzer- nach ID zu lesen, zu ersetzen oder zu löschen.

Methoden

container(string)

Wird verwendet, um eine bestimmte, vorhandene Datenbank nach ID zu lesen, zu ersetzen oder zu löschen.

Verwenden Sie .containers das Erstellen neuer Container oder das Abfragen/Lesen aller Container.

Beispiel

Löschen eines 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)

Erstellen eines Verschlüsselungsschlüssels für das Datenbankkonto

Beispiel

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)

Löschen Sie die angegebene Datenbank.

Beispiel

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)

Lesen Sie die Definition der angegebenen Datenbank.

Beispiel

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)

Lesen des Verschlüsselungsschlüssels für das Datenbankkonto

Beispiel

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)

Ruft Angebot für Datenbank ab. Wenn keine vorhanden ist, wird ein OfferResponse mit undefiniert zurückgegeben.

Beispiel

Lesen Sie das Angebot in der Datenbank

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)

Umschließt einen Client-Verschlüsselungsschlüssel mit dem neuen Schlüssel-Verschlüsselungsschlüssel

Beispiel

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)

Wird verwendet, um eine bestimmte, vorhandene Benutzer- nach ID zu lesen, zu ersetzen oder zu löschen.

Verwenden Sie .users zum Erstellen neuer Benutzer oder zum Abfragen/Lesen aller Benutzer.

Beispiel

Löschen eines Benutzers

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

Details zur Eigenschaft

client

client: CosmosClient

Eigenschaftswert

containers

Wird zum Erstellen neuer Container oder zum Abfragen/Lesen aller Container verwendet.

Verwenden Sie .database(id), um ein bestimmtes, vorhandenes Datenbank- nach ID zu lesen, zu ersetzen oder zu löschen.

Beispiel

Erstellen eines neuen 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 });

const { body: containerDefinition, container } = await client
  .database("<db id>")
  .containers.create({ id: "<container id>" });
containers: Containers

Eigenschaftswert

id

id: string

Eigenschaftswert

string

url

Gibt eine Verweis-URL für die Ressource zurück. Wird zum Verknüpfen in Berechtigungen verwendet.

Beispiel

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

Eigenschaftswert

string

users

Wird zum Erstellen neuer Benutzer oder zum Abfragen/Lesen aller Benutzer verwendet.

Verwenden Sie .user(id), um eine bestimmte, vorhandene Benutzer- nach ID zu lesen, zu ersetzen oder zu löschen.

users: Users

Eigenschaftswert

Details zur Methode

container(string)

Wird verwendet, um eine bestimmte, vorhandene Datenbank nach ID zu lesen, zu ersetzen oder zu löschen.

Verwenden Sie .containers das Erstellen neuer Container oder das Abfragen/Lesen aller Container.

Beispiel

Löschen eines 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

Parameter

id

string

Gibt zurück

createClientEncryptionKey(string, AEAD_AES_256_CBC_HMAC_SHA256, EncryptionKeyWrapMetadata)

Erstellen eines Verschlüsselungsschlüssels für das Datenbankkonto

Beispiel

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>

Parameter

clientEncryptionKeyId

string

encryptionAlgorithm
AEAD_AES_256_CBC_HMAC_SHA256
keyWrapMetadata
EncryptionKeyWrapMetadata

Gibt zurück

delete(RequestOptions)

Löschen Sie die angegebene Datenbank.

Beispiel

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>

Parameter

options
RequestOptions

Gibt zurück

Promise<DatabaseResponse>

read(RequestOptions)

Lesen Sie die Definition der angegebenen Datenbank.

Beispiel

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>

Parameter

options
RequestOptions

Gibt zurück

Promise<DatabaseResponse>

readClientEncryptionKey(string)

Lesen des Verschlüsselungsschlüssels für das Datenbankkonto

Beispiel

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>

Parameter

clientEncryptionKeyId

string

Gibt zurück

readOffer(RequestOptions)

Ruft Angebot für Datenbank ab. Wenn keine vorhanden ist, wird ein OfferResponse mit undefiniert zurückgegeben.

Beispiel

Lesen Sie das Angebot in der Datenbank

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>

Parameter

options
RequestOptions

Gibt zurück

Promise<OfferResponse>

rewrapClientEncryptionKey(string, EncryptionKeyWrapMetadata)

Umschließt einen Client-Verschlüsselungsschlüssel mit dem neuen Schlüssel-Verschlüsselungsschlüssel

Beispiel

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>

Parameter

clientEncryptionKeyId

string

newKeyWrapMetadata
EncryptionKeyWrapMetadata

Neue Metadaten zum Umschließen von Verschlüsselungsschlüsseln

Gibt zurück

Neu verpackter Client-Verschlüsselungsschlüssel mit einem neuen kundenseitig verwalteten Schlüssel

user(string)

Wird verwendet, um eine bestimmte, vorhandene Benutzer- nach ID zu lesen, zu ersetzen oder zu löschen.

Verwenden Sie .users zum Erstellen neuer Benutzer oder zum Abfragen/Lesen aller Benutzer.

Beispiel

Löschen eines Benutzers

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

Parameter

id

string

Gibt zurück