Share via


Item class

Used to perform operations on a specific item.

See Items for operations on all items; see container.items.

Properties

container
id
url

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

Methods

delete<T>(RequestOptions)

Delete the item.

Any provided type, T, is not necessarily enforced by the SDK. You may get more or less properties and it's up to your logic to enforce it.

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 { container } = await database.containers.createIfNotExists({ id: "Test Container" });

interface TodoItem {
  title: string;
  done: boolean;
  id: string;
}

const { resource: item } = await container.item("id", "<pkValue>").read<TodoItem>();

await container.item("id").delete<TodoItem>();
patch<T>(PatchRequestBody, RequestOptions)

Perform a JSONPatch on the item.

Any provided type, T, is not necessarily enforced by the SDK. You may get more or less properties and it's up to your logic to enforce it.

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

interface TodoItem {
  title: string;
  done: boolean;
  id: string;
}

const { database } = await client.databases.createIfNotExists({ id: "Test Database" });

const { container } = await database.containers.createIfNotExists({ id: "Test Container" });

const { resource: item } = await container.item("id", "<pkValue>").read<TodoItem>();

const { resource: patchedItem } = await container.item("id").patch<TodoItem>([
  {
    op: "replace", // Operation type (can be replace, add, remove, set, incr)
    path: "/title", // The path to the property to update
    value: "new-title", // New value for the property
  },
  {
    op: "remove",
    path: "/done",
  },
]);
read<T>(RequestOptions)

Read the item's definition.

Any provided type, T, is not necessarily enforced by the SDK. You may get more or less properties and it's up to your logic to enforce it. If the type, T, is a class, it won't pass typeof comparisons, because it won't have a match prototype. It's recommended to only use interfaces.

There is no set schema for JSON items. They may contain any number of custom properties.

Example

Using custom type for response

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 { container } = await database.containers.createIfNotExists({ id: "Test Container" });

interface TodoItem {
  title: string;
  done: boolean;
  id: string;
}

const { resource: item } = await container.item("id", "<pkValue>").read<TodoItem>();
replace(ItemDefinition, RequestOptions)

Replace the item's definition.

There is no set schema for JSON items. They may contain any number of custom properties.

Example

import { CosmosClient, ItemDefinition } 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 { container } = await database.containers.createIfNotExists({ id: "Test Container" });

const item: ItemDefinition = {
  id: "id",
  title: "new_title",
};

const { resource: replacedItem } = await container.item("id").replace(item);
replace<T>(T, RequestOptions)

Replace the item's definition.

Any provided type, T, is not necessarily enforced by the SDK. You may get more or less properties and it's up to your logic to enforce it.

There is no set schema for JSON items. They may contain any number of custom properties.

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 { container } = await database.containers.createIfNotExists({ id: "Test Container" });

interface TodoItem {
  title: string;
  done: boolean;
  id: string;
}

const { resource: item } = await container.item("id", "<pkValue>").read<TodoItem>();

item.done = true;
const { resource: replacedItem } = await container.item("id").replace<TodoItem>(item);

Property Details

container

container: Container

Property Value

id

id: string

Property Value

string

url

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

string url

Property Value

string

Method Details

delete<T>(RequestOptions)

Delete the item.

Any provided type, T, is not necessarily enforced by the SDK. You may get more or less properties and it's up to your logic to enforce it.

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 { container } = await database.containers.createIfNotExists({ id: "Test Container" });

interface TodoItem {
  title: string;
  done: boolean;
  id: string;
}

const { resource: item } = await container.item("id", "<pkValue>").read<TodoItem>();

await container.item("id").delete<TodoItem>();
function delete<T>(options?: RequestOptions): Promise<ItemResponse<T>>

Parameters

options
RequestOptions

Additional options for the request

Returns

Promise<ItemResponse<T>>

patch<T>(PatchRequestBody, RequestOptions)

Perform a JSONPatch on the item.

Any provided type, T, is not necessarily enforced by the SDK. You may get more or less properties and it's up to your logic to enforce it.

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

interface TodoItem {
  title: string;
  done: boolean;
  id: string;
}

const { database } = await client.databases.createIfNotExists({ id: "Test Database" });

const { container } = await database.containers.createIfNotExists({ id: "Test Container" });

const { resource: item } = await container.item("id", "<pkValue>").read<TodoItem>();

const { resource: patchedItem } = await container.item("id").patch<TodoItem>([
  {
    op: "replace", // Operation type (can be replace, add, remove, set, incr)
    path: "/title", // The path to the property to update
    value: "new-title", // New value for the property
  },
  {
    op: "remove",
    path: "/done",
  },
]);
function patch<T>(body: PatchRequestBody, options?: RequestOptions): Promise<ItemResponse<T>>

Parameters

options
RequestOptions

Additional options for the request

Returns

Promise<ItemResponse<T>>

read<T>(RequestOptions)

Read the item's definition.

Any provided type, T, is not necessarily enforced by the SDK. You may get more or less properties and it's up to your logic to enforce it. If the type, T, is a class, it won't pass typeof comparisons, because it won't have a match prototype. It's recommended to only use interfaces.

There is no set schema for JSON items. They may contain any number of custom properties.

Example

Using custom type for response

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 { container } = await database.containers.createIfNotExists({ id: "Test Container" });

interface TodoItem {
  title: string;
  done: boolean;
  id: string;
}

const { resource: item } = await container.item("id", "<pkValue>").read<TodoItem>();
function read<T>(options?: RequestOptions): Promise<ItemResponse<T>>

Parameters

options
RequestOptions

Additional options for the request

Returns

Promise<ItemResponse<T>>

replace(ItemDefinition, RequestOptions)

Replace the item's definition.

There is no set schema for JSON items. They may contain any number of custom properties.

Example

import { CosmosClient, ItemDefinition } 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 { container } = await database.containers.createIfNotExists({ id: "Test Container" });

const item: ItemDefinition = {
  id: "id",
  title: "new_title",
};

const { resource: replacedItem } = await container.item("id").replace(item);
function replace(body: ItemDefinition, options?: RequestOptions): Promise<ItemResponse<ItemDefinition>>

Parameters

body
ItemDefinition

The definition to replace the existing Item's definition with.

options
RequestOptions

Additional options for the request

Returns

replace<T>(T, RequestOptions)

Replace the item's definition.

Any provided type, T, is not necessarily enforced by the SDK. You may get more or less properties and it's up to your logic to enforce it.

There is no set schema for JSON items. They may contain any number of custom properties.

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 { container } = await database.containers.createIfNotExists({ id: "Test Container" });

interface TodoItem {
  title: string;
  done: boolean;
  id: string;
}

const { resource: item } = await container.item("id", "<pkValue>").read<TodoItem>();

item.done = true;
const { resource: replacedItem } = await container.item("id").replace<TodoItem>(item);
function replace<T>(body: T, options?: RequestOptions): Promise<ItemResponse<T>>

Parameters

body

T

The definition to replace the existing Item's definition with.

options
RequestOptions

Additional options for the request

Returns

Promise<ItemResponse<T>>