共用方式為


Item class

用來在特定項目上執行作業。

如需所有項目的作業,請參閱 專案;請參閱 container.items

屬性

container
id
url

傳回資源的參考 URL。 用於在許可權中連結。

方法

delete<T>(RequestOptions)

刪除專案。

SDK 不一定強制執行任何提供的型別 T。 您可以取得或多或少的屬性,而且由您的邏輯強制執行。

範例

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)

在專案上執行 JSONPatch。

SDK 不一定強制執行任何提供的型別 T。 您可以取得或多或少的屬性,而且由您的邏輯強制執行。

範例

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)

讀取項目的定義。

SDK 不一定強制執行任何提供的型別 T。 您可以取得或多或少的屬性,而且由您的邏輯強制執行。 如果類型 T 是類別,則不會傳遞 typeof 比較,因為它不會有相符的原型。 建議只使用介面。

JSON 項目沒有設定的架構。 它們可能包含任意數目的自定義屬性。

範例

使用自定義類型進行回應

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)

取代項目的定義。

JSON 項目沒有設定的架構。 它們可能包含任意數目的自定義屬性。

範例

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)

取代項目的定義。

SDK 不一定強制執行任何提供的型別 T。 您可以取得或多或少的屬性,而且由您的邏輯強制執行。

JSON 項目沒有設定的架構。 它們可能包含任意數目的自定義屬性。

範例

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

屬性詳細資料

container

container: Container

屬性值

id

id: string

屬性值

string

url

傳回資源的參考 URL。 用於在許可權中連結。

string url

屬性值

string

方法詳細資料

delete<T>(RequestOptions)

刪除專案。

SDK 不一定強制執行任何提供的型別 T。 您可以取得或多或少的屬性,而且由您的邏輯強制執行。

範例

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>>

參數

options
RequestOptions

要求的其他選項

傳回

Promise<ItemResponse<T>>

patch<T>(PatchRequestBody, RequestOptions)

在專案上執行 JSONPatch。

SDK 不一定強制執行任何提供的型別 T。 您可以取得或多或少的屬性,而且由您的邏輯強制執行。

範例

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>>

參數

options
RequestOptions

要求的其他選項

傳回

Promise<ItemResponse<T>>

read<T>(RequestOptions)

讀取項目的定義。

SDK 不一定強制執行任何提供的型別 T。 您可以取得或多或少的屬性,而且由您的邏輯強制執行。 如果類型 T 是類別,則不會傳遞 typeof 比較,因為它不會有相符的原型。 建議只使用介面。

JSON 項目沒有設定的架構。 它們可能包含任意數目的自定義屬性。

範例

使用自定義類型進行回應

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>>

參數

options
RequestOptions

要求的其他選項

傳回

Promise<ItemResponse<T>>

replace(ItemDefinition, RequestOptions)

取代項目的定義。

JSON 項目沒有設定的架構。 它們可能包含任意數目的自定義屬性。

範例

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>>

參數

body
ItemDefinition

要取代現有 Item定義的定義。

options
RequestOptions

要求的其他選項

傳回

replace<T>(T, RequestOptions)

取代項目的定義。

SDK 不一定強制執行任何提供的型別 T。 您可以取得或多或少的屬性,而且由您的邏輯強制執行。

JSON 項目沒有設定的架構。 它們可能包含任意數目的自定義屬性。

範例

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>>

參數

body

T

要取代現有 Item定義的定義。

options
RequestOptions

要求的其他選項

傳回

Promise<ItemResponse<T>>