Bagikan melalui


Item class

Digunakan untuk melakukan operasi pada item tertentu.

Lihat Item untuk operasi pada semua item; lihat container.items.

Properti

container
id
url

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

Metode

delete<T>(RequestOptions)

Hapus item.

Jenis apa pun yang disediakan, T, belum tentu diberlakukan oleh SDK. Anda mungkin mendapatkan lebih banyak atau kurang properti dan terserah logika Anda untuk memberlakukannya.

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

Lakukan JSONPatch pada item.

Jenis apa pun yang disediakan, T, belum tentu diberlakukan oleh SDK. Anda mungkin mendapatkan lebih banyak atau kurang properti dan terserah logika Anda untuk memberlakukannya.

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

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)

Baca definisi item.

Jenis apa pun yang disediakan, T, belum tentu diberlakukan oleh SDK. Anda mungkin mendapatkan lebih banyak atau kurang properti dan terserah logika Anda untuk memberlakukannya. Jika jenis, T, adalah kelas, itu tidak akan melewati perbandingan typeof, karena tidak akan memiliki prototipe yang cocok. Disarankan untuk hanya menggunakan antarmuka.

Tidak ada skema yang ditetapkan untuk item JSON. Mereka mungkin berisi sejumlah properti kustom.

Contoh

Menggunakan jenis kustom untuk respons

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)

Ganti definisi item.

Tidak ada skema yang ditetapkan untuk item JSON. Mereka mungkin berisi sejumlah properti kustom.

Contoh

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)

Ganti definisi item.

Jenis apa pun yang disediakan, T, belum tentu diberlakukan oleh SDK. Anda mungkin mendapatkan lebih banyak atau kurang properti dan terserah logika Anda untuk memberlakukannya.

Tidak ada skema yang ditetapkan untuk item JSON. Mereka mungkin berisi sejumlah properti kustom.

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

Detail Properti

container

container: Container

Nilai Properti

id

id: string

Nilai Properti

string

url

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

string url

Nilai Properti

string

Detail Metode

delete<T>(RequestOptions)

Hapus item.

Jenis apa pun yang disediakan, T, belum tentu diberlakukan oleh SDK. Anda mungkin mendapatkan lebih banyak atau kurang properti dan terserah logika Anda untuk memberlakukannya.

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

Parameter

options
RequestOptions

Opsi tambahan untuk permintaan

Mengembalikan

Promise<ItemResponse<T>>

patch<T>(PatchRequestBody, RequestOptions)

Lakukan JSONPatch pada item.

Jenis apa pun yang disediakan, T, belum tentu diberlakukan oleh SDK. Anda mungkin mendapatkan lebih banyak atau kurang properti dan terserah logika Anda untuk memberlakukannya.

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

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

Parameter

options
RequestOptions

Opsi tambahan untuk permintaan

Mengembalikan

Promise<ItemResponse<T>>

read<T>(RequestOptions)

Baca definisi item.

Jenis apa pun yang disediakan, T, belum tentu diberlakukan oleh SDK. Anda mungkin mendapatkan lebih banyak atau kurang properti dan terserah logika Anda untuk memberlakukannya. Jika jenis, T, adalah kelas, itu tidak akan melewati perbandingan typeof, karena tidak akan memiliki prototipe yang cocok. Disarankan untuk hanya menggunakan antarmuka.

Tidak ada skema yang ditetapkan untuk item JSON. Mereka mungkin berisi sejumlah properti kustom.

Contoh

Menggunakan jenis kustom untuk respons

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

Parameter

options
RequestOptions

Opsi tambahan untuk permintaan

Mengembalikan

Promise<ItemResponse<T>>

replace(ItemDefinition, RequestOptions)

Ganti definisi item.

Tidak ada skema yang ditetapkan untuk item JSON. Mereka mungkin berisi sejumlah properti kustom.

Contoh

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

Parameter

body
ItemDefinition

Definisi untuk mengganti definisi item yang ada.

options
RequestOptions

Opsi tambahan untuk permintaan

Mengembalikan

replace<T>(T, RequestOptions)

Ganti definisi item.

Jenis apa pun yang disediakan, T, belum tentu diberlakukan oleh SDK. Anda mungkin mendapatkan lebih banyak atau kurang properti dan terserah logika Anda untuk memberlakukannya.

Tidak ada skema yang ditetapkan untuk item JSON. Mereka mungkin berisi sejumlah properti kustom.

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

Parameter

body

T

Definisi untuk mengganti definisi item yang ada.

options
RequestOptions

Opsi tambahan untuk permintaan

Mengembalikan

Promise<ItemResponse<T>>