Bagikan melalui


Database class

Operasi untuk membaca atau menghapus database yang sudah ada.

Lihat Database untuk membuat database baru, dan membaca/mengkueri semua database; gunakan client.databases.

Catatan: semua operasi ini melakukan panggilan terhadap anggaran tetap. Anda harus merancang sistem Anda sih sehingga panggilan ini menskalakan secara sublinear dengan aplikasi Anda. Misalnya, jangan memanggil database.read() sebelum setiap panggilan item.read(), untuk memastikan database ada; lakukan ini setelah aplikasi dimulai.

Properti

client
containers

Digunakan untuk membuat kontainer baru, atau mengkueri/membaca semua kontainer.

Gunakan .database(id) untuk membaca, mengganti, atau menghapus Database tertentu yang sudah ada menurut id.

Contoh

Membuat kontainer baru

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

Mengembalikan URL referensi ke sumber daya. Digunakan untuk menautkan di Izin.

Contoh

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

Digunakan untuk membuat pengguna baru, atau mengkueri/membaca semua pengguna.

Gunakan .user(id) untuk membaca, mengganti, atau menghapus pengguna tertentu yang sudah ada menurut id.

Metode

container(string)

Digunakan untuk membaca, mengganti, atau menghapus Database tertentu yang sudah ada menurut id.

Gunakan .containers membuat kontainer baru, atau mengkueri/membaca semua kontainer.

Contoh

Menghapus kontainer

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)

Membuat kunci Enkripsi untuk akun database

Contoh

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)

Hapus Database yang diberikan.

Contoh

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)

Baca definisi Database yang diberikan.

Contoh

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)

Baca kunci enkripsi untuk akun database

Contoh

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)

Mendapatkan penawaran pada database. Jika tidak ada, mengembalikan OfferResponse dengan tidak terdefinisi.

Contoh

Baca penawaran di database

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)

Membungkus ulang kunci enkripsi klien dengan kunci enkripsi kunci baru

Contoh

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)

Digunakan untuk membaca, mengganti, atau menghapus Pengguna tertentu yang ada berdasarkan id.

Gunakan .users untuk membuat pengguna baru, atau mengkueri/membaca semua pengguna.

Contoh

Menghapus pengguna

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

Detail Properti

client

client: CosmosClient

Nilai Properti

containers

Digunakan untuk membuat kontainer baru, atau mengkueri/membaca semua kontainer.

Gunakan .database(id) untuk membaca, mengganti, atau menghapus Database tertentu yang sudah ada menurut id.

Contoh

Membuat kontainer baru

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

Nilai Properti

id

id: string

Nilai Properti

string

url

Mengembalikan URL referensi ke sumber daya. Digunakan untuk menautkan di Izin.

Contoh

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

Nilai Properti

string

users

Digunakan untuk membuat pengguna baru, atau mengkueri/membaca semua pengguna.

Gunakan .user(id) untuk membaca, mengganti, atau menghapus pengguna tertentu yang sudah ada menurut id.

users: Users

Nilai Properti

Detail Metode

container(string)

Digunakan untuk membaca, mengganti, atau menghapus Database tertentu yang sudah ada menurut id.

Gunakan .containers membuat kontainer baru, atau mengkueri/membaca semua kontainer.

Contoh

Menghapus kontainer

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

Mengembalikan

createClientEncryptionKey(string, AEAD_AES_256_CBC_HMAC_SHA256, EncryptionKeyWrapMetadata)

Membuat kunci Enkripsi untuk akun database

Contoh

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

Mengembalikan

delete(RequestOptions)

Hapus Database yang diberikan.

Contoh

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

Mengembalikan

Promise<DatabaseResponse>

read(RequestOptions)

Baca definisi Database yang diberikan.

Contoh

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

Mengembalikan

Promise<DatabaseResponse>

readClientEncryptionKey(string)

Baca kunci enkripsi untuk akun database

Contoh

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

Mengembalikan

readOffer(RequestOptions)

Mendapatkan penawaran pada database. Jika tidak ada, mengembalikan OfferResponse dengan tidak terdefinisi.

Contoh

Baca penawaran di database

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

Mengembalikan

Promise<OfferResponse>

rewrapClientEncryptionKey(string, EncryptionKeyWrapMetadata)

Membungkus ulang kunci enkripsi klien dengan kunci enkripsi kunci baru

Contoh

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

Metadata pembungkus kunci enkripsi baru

Mengembalikan

Kunci enkripsi klien yang dibungkus ulang dengan kunci yang dikelola pelanggan baru

user(string)

Digunakan untuk membaca, mengganti, atau menghapus Pengguna tertentu yang ada berdasarkan id.

Gunakan .users untuk membuat pengguna baru, atau mengkueri/membaca semua pengguna.

Contoh

Menghapus pengguna

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

Mengembalikan