Items class
Operations for creating new items, and reading/querying all items
See Item for reading, replacing, or deleting an existing container; use .item(id)
.
Properties
container |
Methods
batch(Operation |
Execute transactional batch operations on items. Batch takes an array of Operations which are typed based on what the operation does. Batch is transactional and will rollback all operations if one fails. The choices are: Create, Upsert, Read, Replace, and Delete Usage example:
|
bulk(Operation |
Execute bulk operations on items. 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:
|
change |
Create a |
change |
Create a Example Read from the beginning of the change feed.
|
change |
Create a |
change |
Create a |
create<T>(T, Request |
Create an 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. There is no set schema for JSON items. They may contain any number of custom properties. Example Create an item.
|
execute |
Execute bulk operations on items. Example
|
get |
Returns an iterator to iterate over pages of changes. The iterator returned can be used to fetch changes for a single partition key, feed range or an entire container. Example
|
get |
Queries all items in an encrypted container. Example Read all items to array.
|
query(string | Sql |
Queries all items. Example Read all items to array.
|
query<T>(string | Sql |
Queries all items. Example Read all items to array.
|
read |
Read all items. There is no set schema for JSON items. They may contain any number of custom properties. Example Read all items to array.
|
read |
Read all items. 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 Read all items to array.
|
read |
Create a |
read |
Create a Example Read from the beginning of the change feed.
|
read |
Create a |
read |
Create a |
upsert(unknown, Request |
Upsert an item. There is no set schema for JSON items. They may contain any number of custom properties. |
upsert<T>(T, Request |
Upsert an 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. There is no set schema for JSON items. They may contain any number of custom properties. Example Upsert an item.
|
Property Details
container
Method Details
batch(OperationInput[], PartitionKey, RequestOptions)
Execute transactional batch operations on items.
Batch takes an array of Operations which are typed based on what the operation does. Batch is transactional and will rollback all operations if one fails. 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" });
// 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[]>>
Parameters
- operations
List of operations. Limit 100
- partitionKey
- PartitionKey
- options
- RequestOptions
Used for modifying the request
Returns
Promise<Response<OperationResponse[]>>
bulk(OperationInput[], BulkOptions, RequestOptions)
Execute bulk operations on items.
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>
Parameters
- operations
List of operations. Limit 100
- bulkOptions
- BulkOptions
Optional options object to modify bulk behavior. Pass { continueOnError: false } to stop executing operations when one fails. (Defaults to true)
- options
- RequestOptions
Used for modifying the request.
Returns
Promise<BulkOperationResponse>
changeFeed(ChangeFeedOptions)
Warning
This API is now deprecated.
Use getChangeFeedIterator
instead.
Create a ChangeFeedIterator
to iterate over pages of changes
function changeFeed(changeFeedOptions?: ChangeFeedOptions): ChangeFeedIterator<any>
Parameters
- changeFeedOptions
- ChangeFeedOptions
Returns
ChangeFeedIterator<any>
changeFeed(PartitionKey, ChangeFeedOptions)
Warning
This API is now deprecated.
Use getChangeFeedIterator
instead.
Create a ChangeFeedIterator
to iterate over pages of changes
Example
Read from the beginning of the change feed.
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>
Parameters
- partitionKey
- PartitionKey
- changeFeedOptions
- ChangeFeedOptions
Returns
ChangeFeedIterator<any>
changeFeed<T>(ChangeFeedOptions)
Warning
This API is now deprecated.
Use getChangeFeedIterator
instead.
Create a ChangeFeedIterator
to iterate over pages of changes
function changeFeed<T>(changeFeedOptions?: ChangeFeedOptions): ChangeFeedIterator<T>
Parameters
- changeFeedOptions
- ChangeFeedOptions
Returns
changeFeed<T>(PartitionKey, ChangeFeedOptions)
Warning
This API is now deprecated.
Use getChangeFeedIterator
instead.
Create a ChangeFeedIterator
to iterate over pages of changes
function changeFeed<T>(partitionKey: PartitionKey, changeFeedOptions?: ChangeFeedOptions): ChangeFeedIterator<T>
Parameters
- partitionKey
- PartitionKey
- changeFeedOptions
- ChangeFeedOptions
Returns
create<T>(T, RequestOptions)
Create an 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.
There is no set schema for JSON items. They may contain any number of custom properties.
Example
Create an item.
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>>
Parameters
- body
-
T
Represents the body of the item. Can contain any number of user defined properties.
- options
- RequestOptions
Used for modifying the request (for instance, specifying the partition key).
Returns
Promise<ItemResponse<T>>
executeBulkOperations(OperationInput[], RequestOptions)
Execute bulk operations on items.
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[]>
Parameters
- operations
List of operations
- options
- RequestOptions
used for modifying the request
Returns
Promise<BulkOperationResult[]>
list of operation results corresponding to the operations
getChangeFeedIterator<T>(ChangeFeedIteratorOptions)
Returns an iterator to iterate over pages of changes. The iterator returned can be used to fetch changes for a single partition key, feed range or an entire container.
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>
Parameters
- changeFeedIteratorOptions
- ChangeFeedIteratorOptions
Returns
getEncryptionQueryIterator(EncryptionQueryBuilder, FeedOptions)
Queries all items in an encrypted container.
Example
Read all items to array.
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>>
Parameters
- queryBuilder
- EncryptionQueryBuilder
Query configuration for the operation. See SqlQuerySpec for more info on how to build a query on encrypted properties.
- options
- FeedOptions
Used for modifying the request (for instance, specifying the partition key).
Returns
Promise<QueryIterator<ItemDefinition>>
query(string | SqlQuerySpec, FeedOptions)
Queries all items.
Example
Read all items to array.
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>
Parameters
- query
-
string | SqlQuerySpec
Query configuration for the operation. See SqlQuerySpec for more info on how to configure a query.
- options
- FeedOptions
Used for modifying the request (for instance, specifying the partition key).
Returns
QueryIterator<any>
query<T>(string | SqlQuerySpec, FeedOptions)
Queries all items.
Example
Read all items to array.
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>
Parameters
- query
-
string | SqlQuerySpec
Query configuration for the operation. See SqlQuerySpec for more info on how to configure a query.
- options
- FeedOptions
Used for modifying the request (for instance, specifying the partition key).
Returns
readAll(FeedOptions)
Read all items.
There is no set schema for JSON items. They may contain any number of custom properties.
Example
Read all items to array.
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>
Parameters
- options
- FeedOptions
Used for modifying the request (for instance, specifying the partition key).
Returns
readAll<T>(FeedOptions)
Read all items.
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
Read all items to array.
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>
Parameters
- options
- FeedOptions
Used for modifying the request (for instance, specifying the partition key).
Returns
readChangeFeed(ChangeFeedOptions)
Warning
This API is now deprecated.
Use getChangeFeedIterator
instead.
Create a ChangeFeedIterator
to iterate over pages of changes
function readChangeFeed(changeFeedOptions?: ChangeFeedOptions): ChangeFeedIterator<any>
Parameters
- changeFeedOptions
- ChangeFeedOptions
Returns
ChangeFeedIterator<any>
readChangeFeed(PartitionKey, ChangeFeedOptions)
Warning
This API is now deprecated.
Use getChangeFeedIterator
instead.
Create a ChangeFeedIterator
to iterate over pages of changes
Example
Read from the beginning of the change feed.
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>
Parameters
- partitionKey
- PartitionKey
- changeFeedOptions
- ChangeFeedOptions
Returns
ChangeFeedIterator<any>
readChangeFeed<T>(ChangeFeedOptions)
Warning
This API is now deprecated.
Use getChangeFeedIterator
instead.
Create a ChangeFeedIterator
to iterate over pages of changes
function readChangeFeed<T>(changeFeedOptions?: ChangeFeedOptions): ChangeFeedIterator<T>
Parameters
- changeFeedOptions
- ChangeFeedOptions
Returns
readChangeFeed<T>(PartitionKey, ChangeFeedOptions)
Warning
This API is now deprecated.
Use getChangeFeedIterator
instead.
Create a ChangeFeedIterator
to iterate over pages of changes
function readChangeFeed<T>(partitionKey: PartitionKey, changeFeedOptions?: ChangeFeedOptions): ChangeFeedIterator<T>
Parameters
- partitionKey
- PartitionKey
- changeFeedOptions
- ChangeFeedOptions
Returns
upsert(unknown, RequestOptions)
Upsert an item.
There is no set schema for JSON items. They may contain any number of custom properties.
function upsert(body: unknown, options?: RequestOptions): Promise<ItemResponse<ItemDefinition>>
Parameters
- body
-
unknown
Represents the body of the item. Can contain any number of user defined properties.
- options
- RequestOptions
Used for modifying the request (for instance, specifying the partition key).
Returns
Promise<ItemResponse<ItemDefinition>>
upsert<T>(T, RequestOptions)
Upsert an 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.
There is no set schema for JSON items. They may contain any number of custom properties.
Example
Upsert an item.
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>>
Parameters
- body
-
T
Represents the body of the item. Can contain any number of user defined properties.
- options
- RequestOptions
Used for modifying the request (for instance, specifying the partition key).
Returns
Promise<ItemResponse<T>>