Share via


Database class

Operations for reading or deleting an existing database.

See Databases for creating new databases, and reading/querying all databases; use client.databases.

Note: all these operations make calls against a fixed budget. You should design your system such that these calls scale sublinearly with your application. For instance, do not call database.read() before every single item.read() call, to ensure the database exists; do this once on application start up.

Properties

client
containers

Used for creating new containers, or querying/reading all containers.

Use .database(id) to read, replace, or delete a specific, existing Database by id.

Example

Create a new container

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

Returns a reference URL to the resource. Used for linking in Permissions.

Example

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

Used for creating new users, or querying/reading all users.

Use .user(id) to read, replace, or delete a specific, existing User by id.

Methods

container(string)

Used to read, replace, or delete a specific, existing Database by id.

Use .containers creating new containers, or querying/reading all containers.

Example

Delete a container

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)

Create Encryption key for database account

Example

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)

Delete the given Database.

Example

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)

Read the definition of the given Database.

Example

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)

Read Encryption key for database account

Example

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)

Gets offer on database. If none exists, returns an OfferResponse with undefined.

Example

Read the offer on the 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)

rewraps a client encryption key with new key encryption key

Example

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)

Used to read, replace, or delete a specific, existing User by id.

Use .users for creating new users, or querying/reading all users.

Example

Delete a user

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

Property Details

client

client: CosmosClient

Property Value

containers

Used for creating new containers, or querying/reading all containers.

Use .database(id) to read, replace, or delete a specific, existing Database by id.

Example

Create a new container

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

Property Value

id

id: string

Property Value

string

url

Returns a reference URL to the resource. Used for linking in Permissions.

Example

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

Property Value

string

users

Used for creating new users, or querying/reading all users.

Use .user(id) to read, replace, or delete a specific, existing User by id.

users: Users

Property Value

Method Details

container(string)

Used to read, replace, or delete a specific, existing Database by id.

Use .containers creating new containers, or querying/reading all containers.

Example

Delete a container

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

Parameters

id

string

Returns

createClientEncryptionKey(string, AEAD_AES_256_CBC_HMAC_SHA256, EncryptionKeyWrapMetadata)

Create Encryption key for database account

Example

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>

Parameters

clientEncryptionKeyId

string

encryptionAlgorithm
AEAD_AES_256_CBC_HMAC_SHA256
keyWrapMetadata
EncryptionKeyWrapMetadata

Returns

delete(RequestOptions)

Delete the given Database.

Example

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>

Parameters

options
RequestOptions

Returns

Promise<DatabaseResponse>

read(RequestOptions)

Read the definition of the given Database.

Example

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>

Parameters

options
RequestOptions

Returns

Promise<DatabaseResponse>

readClientEncryptionKey(string)

Read Encryption key for database account

Example

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>

Parameters

clientEncryptionKeyId

string

Returns

readOffer(RequestOptions)

Gets offer on database. If none exists, returns an OfferResponse with undefined.

Example

Read the offer on the 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>

Parameters

options
RequestOptions

Returns

Promise<OfferResponse>

rewrapClientEncryptionKey(string, EncryptionKeyWrapMetadata)

rewraps a client encryption key with new key encryption key

Example

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>

Parameters

clientEncryptionKeyId

string

newKeyWrapMetadata
EncryptionKeyWrapMetadata

new encryption key wrap metadata

Returns

rewrapped client encryption key with new customer managed key

user(string)

Used to read, replace, or delete a specific, existing User by id.

Use .users for creating new users, or querying/reading all users.

Example

Delete a user

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

Parameters

id

string

Returns