Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 19 additions & 2 deletions sdk/cosmos/.cspell.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
"autoscale",
"azurecosmos",
"backoff",
"backpressure",
"bytearray",
"canadacentral",
"canadaeast",
Expand All @@ -26,9 +27,11 @@
"cloneable",
"colls",
"cosmosclient",
"cutover",
"Daad",
"dedicatedgateway",
"derefs",
"dhat",
"Dmaster",
"documentdb",
"dotproduct",
Expand All @@ -40,10 +43,12 @@
"enablepreview",
"EUAP",
"euclidian",
"evals",
"fabianm",
"failback",
"failovers",
"FILETIME",
"flamegraph",
"fract",
"francecentral",
"francesouth",
Expand All @@ -54,6 +59,7 @@
"germanywestcentral",
"hostnames",
"hotfixes",
"idents",
"IMDS",
"inclusivity",
"isquery",
Expand All @@ -62,7 +68,10 @@
"keepalive",
"koreacentral",
"koreasouth",
"Kusto",
"linearizability",
"LLZA",
"memcheck",
"MEMORYSTATUSEX",
"moka",
"multihash",
Expand All @@ -86,6 +95,11 @@
"othercoll",
"partitionkey",
"partitionkeyrangeid",
"perfcontainer",
"perfdb",
"perfresults",
"PIDS",
"Pigw",
"Pkrange",
"pkranges",
"pksysdocs",
Expand All @@ -96,17 +110,20 @@
"PPAF",
"PPCB",
"pushback",
"pyroscope",
"qname",
"qself",
"RAII",
"readfeed",
"replicaset",
"reqs",
"Replicaset",
"reqs",
"Retriable",
"retryable",
"rfind",
"RNTBD",
"roundtrips",
"RUPM",
"rwcache",
"sess",
"southafricanorth",
Expand Down Expand Up @@ -179,4 +196,4 @@
]
}
]
}
}
1 change: 1 addition & 0 deletions sdk/cosmos/azure_data_cosmos/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@

### Breaking Changes

- `ContainerClient::create_item()` and `ContainerClient::upsert_item()` now require an `item_id: &str` parameter (same pattern as `replace_item` and `read_item`). The item id is passed to the driver via `ItemReference` so the body never needs to be parsed to extract the document id.
- Renamed `replace_throughput` to `begin_replace_throughput` on `ContainerClient` and `DatabaseClient`. The return type changed from `ResourceResponse<ThroughputProperties>` to `ThroughputPoller`. ([#4096](https://github.com/Azure/azure-sdk-for-rust/pull/4096))
- Removed `CreateDatabaseOptions::with_throughput()`. Database-level shared throughput provisioning is no longer supported through the SDK. Use container-level throughput instead. ([#4147](https://github.com/Azure/azure-sdk-for-rust/pull/4147))

Expand Down
2 changes: 1 addition & 1 deletion sdk/cosmos/azure_data_cosmos/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ async fn example(cosmos_client: CosmosClient) -> Result<(), Box<dyn std::error::
let container = cosmos_client.database_client("myDatabase").container_client("myContainer").await?;

// Create an item
container.create_item("partition1", item, None).await?;
container.create_item("partition1", "1", item, None).await?;

// Read an item
let item_response = container.read_item("partition1", "1", None).await?;
Expand Down
9 changes: 8 additions & 1 deletion sdk/cosmos/azure_data_cosmos/examples/cosmos/create.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,10 @@ pub enum Subcommands {
#[arg(long, short)]
partition_key: String,

/// The id of the new item.
#[arg(long, short)]
item_id: String,

/// The JSON of the new item.
#[arg(long, short)]
json: String,
Expand Down Expand Up @@ -77,6 +81,7 @@ impl CreateCommand {
database,
container,
partition_key,
item_id,
json,
show_updated,
} => {
Expand All @@ -94,7 +99,9 @@ impl CreateCommand {
None
};

let response = container_client.create_item(pk, item, options).await?;
let response = container_client
.create_item(pk, &item_id, item, options)
.await?;

println!("Created item successfully");

Expand Down
8 changes: 7 additions & 1 deletion sdk/cosmos/azure_data_cosmos/examples/cosmos/upsert.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@ pub struct UpsertCommand {
#[arg(long, short)]
partition_key: String,

/// The id of the new item.
#[arg(long, short)]
item_id: String,

/// The JSON of the new item.
#[arg(long, short)]
json: String,
Expand All @@ -46,7 +50,9 @@ impl UpsertCommand {
None
};

let response = container_client.upsert_item(pk, item, options).await?;
let response = container_client
.upsert_item(pk, &self.item_id, item, options)
.await?;
println!("Item updated successfully");

if self.show_updated {
Expand Down
38 changes: 26 additions & 12 deletions sdk/cosmos/azure_data_cosmos/src/clients/container_client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -254,6 +254,7 @@ impl ContainerClient {
///
/// # Arguments
/// * `partition_key` - The partition key of the new item.
/// * `item_id` - The id of the new item.
/// * `item` - The item to create. The type must implement [`Serialize`] and [`Deserialize`](serde::Deserialize)
/// * `options` - Optional parameters for the request
///
Expand All @@ -276,7 +277,7 @@ impl ContainerClient {
/// };
/// # let container_client: azure_data_cosmos::clients::ContainerClient = panic!("this is a non-running example");
/// container_client
/// .create_item("category1", p, None)
/// .create_item("category1", "product1", p, None)
/// .await?;
/// # }
/// ```
Expand Down Expand Up @@ -308,7 +309,7 @@ impl ContainerClient {
/// operation.content_response_on_write = Some(ContentResponseOnWrite::Enabled);
/// let options = ItemWriteOptions::default().with_operation_options(operation);
/// let created_item = container_client
/// .create_item("category1", p, Some(options))
/// .create_item("category1", "product1", p, Some(options))
/// .await?
/// .into_body().json::<Product>();
/// # Ok(())
Expand All @@ -317,16 +318,22 @@ impl ContainerClient {
pub async fn create_item<T: Serialize>(
&self,
partition_key: impl Into<PartitionKey>,
item_id: &str,
item: T,
options: Option<ItemWriteOptions>,
) -> azure_core::Result<ItemResponse<()>> {
let options = options.unwrap_or_default();
let body = serde_json::to_vec(&item)?;
let driver_pk = partition_key.into().into_driver_partition_key();

// Build the driver's item reference from our stored container metadata.
let item_ref = ItemReference::from_name(
&self.container_ref,
partition_key.into().into_driver_partition_key(),
item_id.to_owned(),
);

// Create the driver operation and apply ItemWriteOptions fields.
let operation =
CosmosOperation::create_item(self.container_ref.clone(), driver_pk).with_body(body);
let operation = CosmosOperation::create_item(item_ref).with_body(body);
let operation = apply_item_options(operation, options.session_token, options.precondition);

// Execute through the driver.
Expand Down Expand Up @@ -447,6 +454,7 @@ impl ContainerClient {
///
/// # Arguments
/// * `partition_key` - The partition key of the item to create or replace.
/// * `item_id` - The id of the item to create or replace.
/// * `item` - The item to create. The type must implement [`Serialize`] and [`Deserialize`](serde::Deserialize)
/// * `options` - Optional parameters for the request
///
Expand All @@ -469,7 +477,7 @@ impl ContainerClient {
/// };
/// # let container_client: azure_data_cosmos::clients::ContainerClient = panic!("this is a non-running example");
/// container_client
/// .upsert_item("category1", p, None)
/// .upsert_item("category1", "product1", p, None)
/// .await?;
/// # Ok(())
/// # }
Expand Down Expand Up @@ -502,24 +510,30 @@ impl ContainerClient {
/// operation.content_response_on_write = Some(ContentResponseOnWrite::Enabled);
/// let options = ItemWriteOptions::default().with_operation_options(operation);
/// let updated_product = container_client
/// .upsert_item("category1", p, Some(options))
/// .upsert_item("category1", "product1", p, Some(options))
/// .await?
/// .into_body().json::<Product>()?;
/// Ok(())
/// # }
pub async fn upsert_item<T: Serialize>(
&self,
partition_key: impl Into<PartitionKey>,
item_id: &str,
item: T,
options: Option<ItemWriteOptions>,
) -> azure_core::Result<ItemResponse<()>> {
let options = options.unwrap_or_default();
let body = serde_json::to_vec(&item)?;
let driver_pk = partition_key.into().into_driver_partition_key();

// Build the driver's item reference from our stored container metadata.
let item_ref = ItemReference::from_name(
&self.container_ref,
partition_key.into().into_driver_partition_key(),
item_id.to_owned(),
);

// Create the driver operation and apply ItemWriteOptions fields.
let operation =
CosmosOperation::upsert_item(self.container_ref.clone(), driver_pk).with_body(body);
let operation = CosmosOperation::upsert_item(item_ref).with_body(body);
let operation = apply_item_options(operation, options.session_token, options.precondition);

// Execute through the driver.
Expand Down Expand Up @@ -954,9 +968,9 @@ mod tests {
assert_send(client.read_throughput(todo!()));
assert_send(client.begin_replace_throughput(todo!(), todo!()));
assert_send(client.delete(todo!()));
assert_send(client.create_item::<serde_json::Value>("", todo!(), todo!()));
assert_send(client.create_item::<serde_json::Value>("", todo!(), todo!(), todo!()));
assert_send(client.replace_item::<serde_json::Value>("", todo!(), todo!(), todo!()));
assert_send(client.upsert_item::<serde_json::Value>("", todo!(), todo!()));
assert_send(client.upsert_item::<serde_json::Value>("", todo!(), todo!(), todo!()));
assert_send(client.read_item::<serde_json::Value>("", todo!(), todo!()));
assert_send(client.delete_item("", todo!(), todo!()));
assert_send(client.execute_transactional_batch(todo!(), todo!()));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -132,10 +132,10 @@ pub async fn batch_mixed_operations() -> Result<(), Box<dyn Error>> {
};

container_client
.create_item(&partition_key, &item1, None)
.create_item(&partition_key, &item1.id, &item1, None)
.await?;
container_client
.create_item(&partition_key, &item2, None)
.create_item(&partition_key, &item2.id, &item2, None)
.await?;

// Now execute a batch with mixed operations
Expand Down Expand Up @@ -206,7 +206,7 @@ pub async fn batch_atomicity_on_failure() -> Result<(), Box<dyn Error>> {
};

container_client
.create_item(&partition_key, &item1, None)
.create_item(&partition_key, &item1.id, &item1, None)
.await?;

// Try to create a batch that will fail (trying to delete a non-existent item)
Expand Down
Loading
Loading