Items class
建立新項目的作業,以及讀取/查詢所有專案
請參閱讀取、替換或刪除現有容器的 專案 ;使用 .item(id)。
屬性
| container |
方法
| batch(Operation |
對專案執行交易式批次作業。 Batch 會根據作業的用途,取得輸入作業的作業陣列。 Batch 是交易式的,如果一個作業失敗,則會復原所有作業。 選項包括:Create、Upsert、Read、Replace 和 Delete 使用範例:
|
| bulk(Operation |
對專案執行大量作業。 |
| change |
建立 |
| change |
建立 Example 從變更摘要的開頭讀取。
|
| change |
建立 |
| change |
建立 |
| create<T>(T, Request |
建立專案。 SDK 不一定強制執行任何提供的型別 T。 您可以取得或多或少的屬性,而且由您的邏輯強制執行。 JSON 項目沒有設定的架構。 它們可能包含任意數目的自定義屬性。 Example 建立專案。
|
| execute |
對專案執行大量作業。 Example
|
| get |
傳回反覆運算器,逐一查看變更的頁面。 傳回的反覆運算器可用來擷取單一分割區索引鍵、摘要範圍或整個容器的變更。 Example
|
| get |
查詢加密容器中的所有專案。 Example 讀取所有專案至陣列。
|
| query(string | Sql |
查詢所有專案。 Example 讀取所有專案至陣列。
|
| query<T>(string | Sql |
查詢所有專案。 Example 讀取所有專案至陣列。
|
| read |
讀取所有專案。 JSON 項目沒有設定的架構。 它們可能包含任意數目的自定義屬性。 Example 讀取所有專案至陣列。
|
| read |
讀取所有專案。 SDK 不一定強制執行任何提供的型別 T。 您可以取得或多或少的屬性,而且由您的邏輯強制執行。 JSON 項目沒有設定的架構。 它們可能包含任意數目的自定義屬性。 Example 讀取所有專案至陣列。
|
| read |
建立 |
| read |
建立 Example 從變更摘要的開頭讀取。
|
| read |
建立 |
| read |
建立 |
| upsert(unknown, Request |
向上插入專案。 JSON 項目沒有設定的架構。 它們可能包含任意數目的自定義屬性。 |
| upsert<T>(T, Request |
向上插入專案。 SDK 不一定強制執行任何提供的型別 T。 您可以取得或多或少的屬性,而且由您的邏輯強制執行。 JSON 項目沒有設定的架構。 它們可能包含任意數目的自定義屬性。 Example 向上插入專案。
|
屬性詳細資料
container
方法詳細資料
batch(OperationInput[], PartitionKey, RequestOptions)
對專案執行交易式批次作業。
Batch 會根據作業的用途,取得輸入作業的作業陣列。 Batch 是交易式的,如果一個作業失敗,則會復原所有作業。 選項包括:Create、Upsert、Read、Replace 和 Delete
使用範例:
import { CosmosClient, OperationInput } 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" });
// The partitionKey is a required second argument. If it’s undefined, it defaults to the expected partition key format.
const operations: OperationInput[] = [
{
operationType: "Create",
resourceBody: { id: "doc1", name: "sample", key: "A" },
},
{
operationType: "Upsert",
resourceBody: { id: "doc2", name: "other", key: "A" },
},
];
await container.items.batch(operations, "A");
function batch(operations: OperationInput[], partitionKey?: PartitionKey, options?: RequestOptions): Promise<Response<OperationResponse[]>>
參數
- operations
作業清單。 限制 100
- partitionKey
- PartitionKey
- options
- RequestOptions
用於修改要求
傳回
Promise<Response<OperationResponse[]>>
bulk(OperationInput[], BulkOptions, RequestOptions)
警告
此 API 現已淘汰。
Use executeBulkOperations instead.
Bulk takes an array of Operations which are typed based on what the operation does. The choices are: Create, Upsert, Read, Replace, and Delete
Usage example:
import { CosmosClient, OperationInput } 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" });
// partitionKey is optional at the top level if present in the resourceBody
const operations: OperationInput[] = [
{
operationType: "Create",
resourceBody: { id: "doc1", name: "sample", key: "A" },
},
{
operationType: "Upsert",
partitionKey: "A",
resourceBody: { id: "doc2", name: "other", key: "A" },
},
];
await container.items.bulk(operations);
對專案執行大量作業。
function bulk(operations: OperationInput[], bulkOptions?: BulkOptions, options?: RequestOptions): Promise<BulkOperationResponse>
參數
- operations
作業清單。 限制 100
- bulkOptions
- BulkOptions
選擇性選項物件,可修改大量行為。 傳遞 { continueOnError: false } 以在失敗時停止執行作業。 (預設值為 true)
- options
- RequestOptions
用於修改要求。
傳回
Promise<BulkOperationResponse>
changeFeed(ChangeFeedOptions)
警告
此 API 現已淘汰。
Use getChangeFeedIterator instead.
建立 ChangeFeedIterator 逐一查看變更的頁面
function changeFeed(changeFeedOptions?: ChangeFeedOptions): ChangeFeedIterator<any>
參數
- changeFeedOptions
- ChangeFeedOptions
傳回
ChangeFeedIterator<any>
changeFeed(PartitionKey, ChangeFeedOptions)
警告
此 API 現已淘汰。
Use getChangeFeedIterator instead.
建立 ChangeFeedIterator 逐一查看變更的頁面
Example
從變更摘要的開頭讀取。
const iterator = items.readChangeFeed({ startFromBeginning: true });
const firstPage = await iterator.fetchNext();
const firstPageResults = firstPage.result
const secondPage = await iterator.fetchNext();
function changeFeed(partitionKey: PartitionKey, changeFeedOptions?: ChangeFeedOptions): ChangeFeedIterator<any>
參數
- partitionKey
- PartitionKey
- changeFeedOptions
- ChangeFeedOptions
傳回
ChangeFeedIterator<any>
changeFeed<T>(ChangeFeedOptions)
警告
此 API 現已淘汰。
Use getChangeFeedIterator instead.
建立 ChangeFeedIterator 逐一查看變更的頁面
function changeFeed<T>(changeFeedOptions?: ChangeFeedOptions): ChangeFeedIterator<T>
參數
- changeFeedOptions
- ChangeFeedOptions
傳回
changeFeed<T>(PartitionKey, ChangeFeedOptions)
警告
此 API 現已淘汰。
Use getChangeFeedIterator instead.
建立 ChangeFeedIterator 逐一查看變更的頁面
function changeFeed<T>(partitionKey: PartitionKey, changeFeedOptions?: ChangeFeedOptions): ChangeFeedIterator<T>
參數
- partitionKey
- PartitionKey
- changeFeedOptions
- ChangeFeedOptions
傳回
create<T>(T, RequestOptions)
建立專案。
SDK 不一定強制執行任何提供的型別 T。 您可以取得或多或少的屬性,而且由您的邏輯強制執行。
JSON 項目沒有設定的架構。 它們可能包含任意數目的自定義屬性。
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" });
const { resource: createdItem } = await container.items.create({
id: "<item id>",
properties: {},
});
function create<T>(body: T, options?: RequestOptions): Promise<ItemResponse<T>>
參數
- body
-
T
表示項目的主體。 可以包含任意數目的使用者定義屬性。
- options
- RequestOptions
用於修改要求(例如,指定分割區索引鍵)。
傳回
Promise<ItemResponse<T>>
executeBulkOperations(OperationInput[], RequestOptions)
對專案執行大量作業。
Example
import { CosmosClient, OperationInput } 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 operations: OperationInput[] = [
{
operationType: "Create",
resourceBody: { id: "doc1", name: "sample", key: "A" },
},
{
operationType: "Upsert",
partitionKey: "A",
resourceBody: { id: "doc2", name: "other", key: "A" },
},
];
await container.items.executeBulkOperations(operations);
function executeBulkOperations(operations: OperationInput[], options?: RequestOptions): Promise<BulkOperationResult[]>
參數
- operations
作業清單
- options
- RequestOptions
用於修改請求
傳回
Promise<BulkOperationResult[]>
作對應的作結果清單
getChangeFeedIterator<T>(ChangeFeedIteratorOptions)
傳回反覆運算器,逐一查看變更的頁面。 傳回的反覆運算器可用來擷取單一分割區索引鍵、摘要範圍或整個容器的變更。
Example
import {
CosmosClient,
PartitionKeyDefinitionVersion,
PartitionKeyKind,
ChangeFeedStartFrom,
} 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 containerDefinition = {
id: "Test Database",
partitionKey: {
paths: ["/name", "/address/zip"],
version: PartitionKeyDefinitionVersion.V2,
kind: PartitionKeyKind.MultiHash,
},
};
const { container } = await database.containers.createIfNotExists(containerDefinition);
const partitionKey = "some-partition-Key-value";
const options = {
changeFeedStartFrom: ChangeFeedStartFrom.Beginning(partitionKey),
};
const iterator = container.items.getChangeFeedIterator(options);
while (iterator.hasMoreResults) {
const response = await iterator.readNext();
// process this response
}
function getChangeFeedIterator<T>(changeFeedIteratorOptions?: ChangeFeedIteratorOptions): ChangeFeedPullModelIterator<T>
參數
- changeFeedIteratorOptions
- ChangeFeedIteratorOptions
傳回
getEncryptionQueryIterator(EncryptionQueryBuilder, FeedOptions)
查詢加密容器中的所有專案。
Example
讀取所有專案至陣列。
import { CosmosClient, EncryptionQueryBuilder } 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 queryBuilder = new EncryptionQueryBuilder(
`SELECT firstname FROM Families f WHERE f.lastName = @lastName`,
);
queryBuilder.addParameter("@lastName", "Hendricks", "/lastname");
const queryIterator = await container.items.getEncryptionQueryIterator(queryBuilder);
const { resources: items } = await queryIterator.fetchAll();
function getEncryptionQueryIterator(queryBuilder: EncryptionQueryBuilder, options?: FeedOptions): Promise<QueryIterator<ItemDefinition>>
參數
- queryBuilder
- EncryptionQueryBuilder
作業的查詢組態。 有關如何對加密屬性生成查詢的詳細資訊,請參閱 SqlQuerySpec 。
- options
- FeedOptions
用於修改要求(例如,指定分割區索引鍵)。
傳回
Promise<QueryIterator<ItemDefinition>>
query(string | SqlQuerySpec, FeedOptions)
查詢所有專案。
Example
讀取所有專案至陣列。
import { CosmosClient, SqlQuerySpec } 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 querySpec: SqlQuerySpec = {
query: `SELECT * FROM Families f WHERE f.lastName = @lastName`,
parameters: [{ name: "@lastName", value: "Hendricks" }],
};
const { resources: items } = await container.items.query(querySpec).fetchAll();
function query(query: string | SqlQuerySpec, options?: FeedOptions): QueryIterator<any>
參數
- query
-
string | SqlQuerySpec
作業的查詢組態。 有關如何配置查詢的詳細資訊,請參閱 SqlQuerySpec 。
- options
- FeedOptions
用於修改要求(例如,指定分割區索引鍵)。
傳回
QueryIterator<any>
query<T>(string | SqlQuerySpec, FeedOptions)
查詢所有專案。
Example
讀取所有專案至陣列。
import { CosmosClient, SqlQuerySpec } 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 querySpec: SqlQuerySpec = {
query: `SELECT * FROM Families f WHERE f.lastName = @lastName`,
parameters: [{ name: "@lastName", value: "Hendricks" }],
};
const { resources: items } = await container.items.query(querySpec).fetchAll();
function query<T>(query: string | SqlQuerySpec, options?: FeedOptions): QueryIterator<T>
參數
- query
-
string | SqlQuerySpec
作業的查詢組態。 有關如何配置查詢的詳細資訊,請參閱 SqlQuerySpec 。
- options
- FeedOptions
用於修改要求(例如,指定分割區索引鍵)。
傳回
readAll(FeedOptions)
讀取所有專案。
JSON 項目沒有設定的架構。 它們可能包含任意數目的自定義屬性。
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" });
const { resources: containerList } = await container.items.readAll().fetchAll();
function readAll(options?: FeedOptions): QueryIterator<ItemDefinition>
參數
- options
- FeedOptions
用於修改要求(例如,指定分割區索引鍵)。
傳回
readAll<T>(FeedOptions)
讀取所有專案。
SDK 不一定強制執行任何提供的型別 T。 您可以取得或多或少的屬性,而且由您的邏輯強制執行。
JSON 項目沒有設定的架構。 它們可能包含任意數目的自定義屬性。
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" });
const { resources: containerList } = await container.items.readAll().fetchAll();
function readAll<T>(options?: FeedOptions): QueryIterator<T>
參數
- options
- FeedOptions
用於修改要求(例如,指定分割區索引鍵)。
傳回
readChangeFeed(ChangeFeedOptions)
警告
此 API 現已淘汰。
Use getChangeFeedIterator instead.
建立 ChangeFeedIterator 逐一查看變更的頁面
function readChangeFeed(changeFeedOptions?: ChangeFeedOptions): ChangeFeedIterator<any>
參數
- changeFeedOptions
- ChangeFeedOptions
傳回
ChangeFeedIterator<any>
readChangeFeed(PartitionKey, ChangeFeedOptions)
警告
此 API 現已淘汰。
Use getChangeFeedIterator instead.
建立 ChangeFeedIterator 逐一查看變更的頁面
Example
從變更摘要的開頭讀取。
const iterator = items.readChangeFeed({ startFromBeginning: true });
const firstPage = await iterator.fetchNext();
const firstPageResults = firstPage.result
const secondPage = await iterator.fetchNext();
function readChangeFeed(partitionKey: PartitionKey, changeFeedOptions?: ChangeFeedOptions): ChangeFeedIterator<any>
參數
- partitionKey
- PartitionKey
- changeFeedOptions
- ChangeFeedOptions
傳回
ChangeFeedIterator<any>
readChangeFeed<T>(ChangeFeedOptions)
警告
此 API 現已淘汰。
Use getChangeFeedIterator instead.
建立 ChangeFeedIterator 逐一查看變更的頁面
function readChangeFeed<T>(changeFeedOptions?: ChangeFeedOptions): ChangeFeedIterator<T>
參數
- changeFeedOptions
- ChangeFeedOptions
傳回
readChangeFeed<T>(PartitionKey, ChangeFeedOptions)
警告
此 API 現已淘汰。
Use getChangeFeedIterator instead.
建立 ChangeFeedIterator 逐一查看變更的頁面
function readChangeFeed<T>(partitionKey: PartitionKey, changeFeedOptions?: ChangeFeedOptions): ChangeFeedIterator<T>
參數
- partitionKey
- PartitionKey
- changeFeedOptions
- ChangeFeedOptions
傳回
upsert(unknown, RequestOptions)
向上插入專案。
JSON 項目沒有設定的架構。 它們可能包含任意數目的自定義屬性。
function upsert(body: unknown, options?: RequestOptions): Promise<ItemResponse<ItemDefinition>>
參數
- body
-
unknown
表示項目的主體。 可以包含任意數目的使用者定義屬性。
- options
- RequestOptions
用於修改要求(例如,指定分割區索引鍵)。
傳回
Promise<ItemResponse<ItemDefinition>>
upsert<T>(T, RequestOptions)
向上插入專案。
SDK 不一定強制執行任何提供的型別 T。 您可以取得或多或少的屬性,而且由您的邏輯強制執行。
JSON 項目沒有設定的架構。 它們可能包含任意數目的自定義屬性。
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" });
const { resource: createdItem1 } = await container.items.create({
id: "<item id 1>",
properties: {},
});
const { resource: upsertItem1 } = await container.items.upsert({
id: "<item id 1>",
updated_properties: {},
});
const { resource: upsertItem2 } = await container.items.upsert({
id: "<item id 2>",
properties: {},
});
function upsert<T>(body: T, options?: RequestOptions): Promise<ItemResponse<T>>
參數
- body
-
T
表示項目的主體。 可以包含任意數目的使用者定義屬性。
- options
- RequestOptions
用於修改要求(例如,指定分割區索引鍵)。
傳回
Promise<ItemResponse<T>>