#azure-sdk #azure #azure-rest #sdk #cloud #iot #wrapper

azure_data_cosmos

Rust wrappers around Microsoft Azure REST APIs - Azure Cosmos DB

19 breaking releases

new 0.20.0 Apr 24, 2024
0.19.0 Jan 5, 2024
0.18.0 Dec 8, 2023
0.17.0 Nov 3, 2023
0.1.0 Jan 25, 2022

#93 in Web programming

Download history 385/week @ 2024-01-03 576/week @ 2024-01-10 438/week @ 2024-01-17 516/week @ 2024-01-24 354/week @ 2024-01-31 328/week @ 2024-02-07 545/week @ 2024-02-14 624/week @ 2024-02-21 840/week @ 2024-02-28 1325/week @ 2024-03-06 1131/week @ 2024-03-13 912/week @ 2024-03-20 1019/week @ 2024-03-27 1261/week @ 2024-04-03 584/week @ 2024-04-10 334/week @ 2024-04-17

3,337 downloads per month

MIT license

500KB
12K SLoC

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

// Using the prelude module of the Cosmos crate makes easier to use the Rust Azure SDK for Cosmos DB.
use azure_data_cosmos::prelude::*;
use azure_core::Context;
use serde::{Deserialize, Serialize};

// This is the stuct we want to use in our sample.
// Make sure to have a collection with partition key "number" for this example to
// work (you can create with this SDK too, check the examples folder for that task).
#[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's get Cosmos primary key and account name from env variables.
    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");

    // First, create an authorization token. There are two types of tokens: primary and resource constrained.
    // Please check the Azure documentation or the examples folder on how to create and use token-based permissions.
    let authorization_token = AuthorizationToken::primary_key(&primary_key)?;

    // Next we will create a Cosmos client.
    let client = CosmosClient::new(account, authorization_token);

    // We know the database so we can obtain a database client.
    let database = client.database_client(database_name);
    // We know the collection so we can obtain a collection client.
    let collection = database.collection_client(collection_name);

    // Insert 10 documents
    println!("Inserting 10 documents...");
    for i in 0..10 {
        // define the document.
        let document_to_insert = MySampleStruct {
            id: format!("unique_id{}", i),
            string: "Something here".to_owned(),
            number: i * 100, // this is the partition key
        };

        // insert it
        collection
            .create_document(document_to_insert)
            .is_upsert(true)
            .await?;
    }

    Ok(())
}

License: MIT

Dependencies

~7–21MB
~316K SLoC