Udostępnij za pomocą


Database class

Operacje odczytu lub usuwania istniejącej bazy danych.

Zobacz Databases na potrzeby tworzenia nowych baz danych oraz odczytywania/wykonywania zapytań dotyczących wszystkich baz danych; użyj client.databases.

Uwaga: wszystkie te operacje tworzą wywołania względem stałego budżetu. Należy zaprojektować system tak, aby te wywołania były skalowane podlinearnie z aplikacją. Na przykład nie należy wywoływać database.read() przed każdym wywołaniem item.read(), aby upewnić się, że baza danych istnieje; zrób to raz podczas uruchamiania aplikacji.

Właściwości

client
containers

Służy do tworzenia nowych kontenerów lub wykonywania zapytań/odczytywania wszystkich kontenerów.

Użyj .database(id), aby odczytywać, zastępować lub usuwać określony istniejący Database według identyfikatora.

Przykład

Tworzenie nowego kontenera

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

Zwraca adres URL odwołania do zasobu. Służy do łączenia w obszarze Uprawnienia.

Przykład

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

Służy do tworzenia nowych użytkowników lub wykonywania zapytań/odczytywania wszystkich użytkowników.

Użyj .user(id) do odczytywania, zastępowania lub usuwania określonego istniejącego użytkownika według identyfikatora.

Metody

container(string)

Służy do odczytywania, zastępowania lub usuwania określonego istniejącego Database według identyfikatora.

Użyj .containers tworzenia nowych kontenerów lub wykonywania zapytań/odczytywania wszystkich kontenerów.

Przykład

Usuwanie kontenera

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)

Tworzenie klucza szyfrowania dla konta bazy danych

Przykład

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)

Usuń daną bazę danych.

Przykład

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)

Przeczytaj definicję danej bazy danych.

Przykład

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)

Odczytywanie klucza szyfrowania dla konta bazy danych

Przykład

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)

Pobiera ofertę w bazie danych. Jeśli żadna z nich nie istnieje, zwraca element OfferResponse z niezdefiniowanym.

Przykład

Zapoznaj się z ofertą w bazie danych

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)

Ponownie opakowuje klucz szyfrowania klienta przy użyciu nowego klucza szyfrowania

Przykład

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)

Służy do odczytywania, zastępowania lub usuwania określonego, istniejącego użytkownika według identyfikatora.

Użyj .users do tworzenia nowych użytkowników lub wykonywania zapytań/odczytywania wszystkich użytkowników.

Przykład

Usuwanie użytkownika

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

Szczegóły właściwości

client

client: CosmosClient

Wartość właściwości

containers

Służy do tworzenia nowych kontenerów lub wykonywania zapytań/odczytywania wszystkich kontenerów.

Użyj .database(id), aby odczytywać, zastępować lub usuwać określony istniejący Database według identyfikatora.

Przykład

Tworzenie nowego kontenera

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

Wartość właściwości

id

id: string

Wartość właściwości

string

url

Zwraca adres URL odwołania do zasobu. Służy do łączenia w obszarze Uprawnienia.

Przykład

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

Wartość właściwości

string

users

Służy do tworzenia nowych użytkowników lub wykonywania zapytań/odczytywania wszystkich użytkowników.

Użyj .user(id) do odczytywania, zastępowania lub usuwania określonego istniejącego użytkownika według identyfikatora.

users: Users

Wartość właściwości

Szczegóły metody

container(string)

Służy do odczytywania, zastępowania lub usuwania określonego istniejącego Database według identyfikatora.

Użyj .containers tworzenia nowych kontenerów lub wykonywania zapytań/odczytywania wszystkich kontenerów.

Przykład

Usuwanie kontenera

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

Parametry

id

string

Zwraca

createClientEncryptionKey(string, AEAD_AES_256_CBC_HMAC_SHA256, EncryptionKeyWrapMetadata)

Tworzenie klucza szyfrowania dla konta bazy danych

Przykład

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>

Parametry

clientEncryptionKeyId

string

encryptionAlgorithm
AEAD_AES_256_CBC_HMAC_SHA256
keyWrapMetadata
EncryptionKeyWrapMetadata

Zwraca

delete(RequestOptions)

Usuń daną bazę danych.

Przykład

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>

Parametry

options
RequestOptions

Zwraca

Promise<DatabaseResponse>

read(RequestOptions)

Przeczytaj definicję danej bazy danych.

Przykład

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>

Parametry

options
RequestOptions

Zwraca

Promise<DatabaseResponse>

readClientEncryptionKey(string)

Odczytywanie klucza szyfrowania dla konta bazy danych

Przykład

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>

Parametry

clientEncryptionKeyId

string

Zwraca

readOffer(RequestOptions)

Pobiera ofertę w bazie danych. Jeśli żadna z nich nie istnieje, zwraca element OfferResponse z niezdefiniowanym.

Przykład

Zapoznaj się z ofertą w bazie danych

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>

Parametry

options
RequestOptions

Zwraca

Promise<OfferResponse>

rewrapClientEncryptionKey(string, EncryptionKeyWrapMetadata)

Ponownie opakowuje klucz szyfrowania klienta przy użyciu nowego klucza szyfrowania

Przykład

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>

Parametry

clientEncryptionKeyId

string

newKeyWrapMetadata
EncryptionKeyWrapMetadata

Nowe metadane zawijania klucza szyfrowania

Zwraca

Ponownie opakowany klucz szyfrowania klienta przy użyciu nowego klucza zarządzanego przez klienta

user(string)

Służy do odczytywania, zastępowania lub usuwania określonego, istniejącego użytkownika według identyfikatora.

Użyj .users do tworzenia nowych użytkowników lub wykonywania zapytań/odczytywania wszystkich użytkowników.

Przykład

Usuwanie użytkownika

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

Parametry

id

string

Zwraca