azure_data_cosmos
The Cosmos DB crate.
azure-data-cosmos
offers functionality needed to interact with Cosmos DB from Rust. As an abstraction over the Cosmos DB
Rest API, anything that is possible through that Rest API
should also be possible with this crate.
Examples
use azure_data_cosmos::prelude::*;
use azure_core::Context;
use serde::{Deserialize, Serialize};
#[derive(Serialize, Deserialize, Debug)]
struct MySampleStruct {
id: String,
string: String,
number: u64,
}
impl azure_data_cosmos::CosmosEntity for MySampleStruct {
type Entity = u64;
fn partition_key(&self) -> Self::Entity {
self.number
}
}
#[tokio::main]
async fn main() -> azure_core::Result<()> {
let primary_key =
std::env::var("COSMOS_PRIMARY_KEY").expect("Set env variable COSMOS_PRIMARY_KEY first!");
let account = std::env::var("COSMOS_ACCOUNT").expect("Set env variable COSMOS_ACCOUNT first!");
let database_name = std::env::args()
.nth(1)
.expect("please specify the database name as the first command line parameter");
let collection_name = std::env::args()
.nth(2)
.expect("please specify the collection name as the second command line parameter");
let authorization_token = AuthorizationToken::primary_from_base64(&primary_key)?;
let client = CosmosClient::new(account, authorization_token);
let database = client.database_client(database_name);
let collection = database.collection_client(collection_name);
println!("Inserting 10 documents...");
for i in 0..10 {
let document_to_insert = MySampleStruct {
id: format!("unique_id{}", i),
string: "Something here".to_owned(),
number: i * 100, };
collection
.create_document(document_to_insert)
.is_upsert(true)
.await?;
}
Ok(())
}
License: MIT