55
66[ ![ Build Status] ( https://travis-ci.org/MindFlavor/AzureSDKForRust.svg?branch=master )] ( https://travis-ci.org/MindFlavor/AzureSDKForRust ) [ ![ Coverage Status] ( https://coveralls.io/repos/MindFlavor/AzureSDKForRust/badge.svg?branch=master&service=github )] ( https://coveralls.io/github/MindFlavor/AzureSDKForRust?branch=master ) ![ stability-unstable] ( https://img.shields.io/badge/stability-unstable-yellow.svg )
77
8- [ ![ tag] ( https://img.shields.io/github/tag/mindflavor/AzureSDKForRust.svg )] ( https://github.com/MindFlavor/AzureSDKForRust/tree/cosmos_0.43.1 ) [ ![ release] ( https://img.shields.io/github/release/mindflavor/AzureSDKForRust.svg )] ( https://github.com/MindFlavor/AzureSDKForRust/releases/tag/cosmos_0.43.1 ) [ ![ commitssince] ( https://img.shields.io/github/commits-since/mindflavor/AzureSDKForRust/cosmos_0.43.1 )] ( https://github.com/MindFlavor/AzureSDKForRust/commits/master )
8+ [ ![ tag] ( https://img.shields.io/github/tag/mindflavor/AzureSDKForRust.svg )] ( https://github.com/MindFlavor/AzureSDKForRust/tree/cosmos_0.100.0 ) [ ![ release] ( https://img.shields.io/github/release/mindflavor/AzureSDKForRust.svg )] ( https://github.com/MindFlavor/AzureSDKForRust/releases/tag/cosmos_0.100.0 ) [ ![ commitssince] ( https://img.shields.io/github/commits-since/mindflavor/AzureSDKForRust/cosmos_0.100.0 )] ( https://github.com/MindFlavor/AzureSDKForRust/commits/master )
99
1010[ ![ GitHub contributors] ( https://img.shields.io/github/contributors/MindFlavor/AzureSDKForRust.svg )] ( https://github.com/MindFlavor/AzureSDKForRust/graphs/contributors )
1111
@@ -50,7 +50,7 @@ You can find examples in the [```examples```](https://github.com/MindFlavor/Azur
5050``` rust
5151#[macro_use]
5252extern crate serde_derive;
53- // Using the prelude module of the CosmosDB crate makes easier to use the Rust Azure SDK for Cosmos
53+ // Using the prelude module of the Cosmos crate makes easier to use the Rust Azure SDK for Cosmos
5454// DB.
5555use azure_sdk_core :: prelude :: * ;
5656use azure_sdk_cosmos :: prelude :: * ;
@@ -97,14 +97,15 @@ async fn main() -> Result<(), Box<dyn Error>> {
9797 let authorization_token = AuthorizationToken :: new_master (& master_key )? ;
9898
9999 // Next we will create a Cosmos client.
100- let client = ClientBuilder :: new (account , authorization_token . clone () )? ;
100+ let client = ClientBuilder :: new (account , authorization_token )? ;
101101 // We know the database so we can obtain a database client.
102- let database_client = client . with_database ( & database_name );
102+ let database_client = client . with_database_client ( database_name );
103103 // We know the collection so we can obtain a collection client.
104- let collection_client = database_client . with_collection ( & collection_name );
104+ let collection_client = database_client . with_collection_client ( collection_name );
105105
106106 // TASK 1 - Insert 10 documents
107107 println! (" Inserting 10 documents..." );
108+ let mut session_token = None ;
108109 for i in 0 .. 10 {
109110 // define the document.
110111 let document_to_insert = Document :: new (MySampleStruct {
@@ -114,31 +115,42 @@ async fn main() -> Result<(), Box<dyn Error>> {
114115 a_timestamp : chrono :: Utc :: now (). timestamp (),
115116 });
116117
117- // insert it!
118- collection_client
119- . create_document ()
120- . with_document (& document_to_insert )
121- . with_partition_keys (PartitionKeys :: new (). push (& document_to_insert . document. a_number)? )
122- . with_is_upsert (true ) // this option will overwrite a preexisting document (if any)
123- . execute ()
124- . await ? ;
118+ // insert it and store the returned session token for later use!
119+ session_token = Some (
120+ collection_client
121+ . create_document ()
122+ . with_partition_keys (
123+ PartitionKeys :: new (). push (& document_to_insert . document. a_number)? ,
124+ )
125+ . with_is_upsert (true ) // this option will overwrite a preexisting document (if any)
126+ . execute_with_document (& document_to_insert )
127+ . await ?
128+ . session_token, // get only the session token, if everything else was ok!
129+ );
125130 }
126131 // wow that was easy and fast, wasnt'it? :)
127132 println! (" Done!" );
128133
134+ let session_token = ConsistencyLevel :: from (session_token . unwrap ());
135+
129136 // TASK 2
130- println! (" \ n Streaming documents" );
131- // we limit the number of documents to 3 for each batch as a demonstration. In practice
132- // you will use a more sensible number (or accept the Azure default).
133- let stream = collection_client . list_documents (). with_max_item_count (3 );
134- let mut stream = Box :: pin (stream . stream :: <MySampleStruct >());
135- // TODO: As soon as the streaming functionality is stabilized
136- // in Rust we can substitute this while let Some... into
137- // for each (or whatever the Rust team picks).
138- while let Some (res ) = stream . next (). await {
139- let res = res ? ;
140- println! (" Received {} documents in one batch!" , res . documents. len ());
141- res . documents. iter (). for_each (| doc | println! (" {:#?}" , doc ));
137+ {
138+ println! (" \ n Streaming documents" );
139+ // we limit the number of documents to 3 for each batch as a demonstration. In practice
140+ // you will use a more sensible number (or accept the Azure default).
141+ let stream = collection_client
142+ . list_documents ()
143+ . with_consistency_level (session_token . clone ())
144+ . with_max_item_count (3 );
145+ let mut stream = Box :: pin (stream . stream :: <MySampleStruct >());
146+ // TODO: As soon as the streaming functionality is stabilized
147+ // in Rust we can substitute this while let Some... into
148+ // for each (or whatever the Rust team picks).
149+ while let Some (res ) = stream . next (). await {
150+ let res = res ? ;
151+ println! (" Received {} documents in one batch!" , res . documents. len ());
152+ res . documents. iter (). for_each (| doc | println! (" {:#?}" , doc ));
153+ }
142154 }
143155
144156 // TASK 3
@@ -147,6 +159,7 @@ async fn main() -> Result<(), Box<dyn Error>> {
147159 . query_documents ()
148160 . with_query (& (" SELECT * FROM A WHERE A.a_number < 600" . into ())) // there are other ways to construct a query, this is the simplest.
149161 . with_query_cross_partition (true ) // this will perform a cross partition query! notice how simple it is!
162+ . with_consistency_level (session_token )
150163 . execute :: <MySampleStruct >() // This will make sure the result is our custom struct!
151164 . await ?
152165 . into_documents () // queries can return Documents or Raw json (ie without etag, _rid, etc...). Since our query return docs we convert with this function.
@@ -165,6 +178,7 @@ async fn main() -> Result<(), Box<dyn Error>> {
165178 });
166179
167180 // TASK 4
181+ let session_token = ConsistencyLevel :: from (query_documents_response . session_token. clone ());
168182 for ref document in query_documents_response . results {
169183 // From our query above we are sure to receive a Document.
170184 println! (
@@ -174,11 +188,9 @@ async fn main() -> Result<(), Box<dyn Error>> {
174188
175189 // to spice the delete a little we use optimistic concurreny
176190 collection_client
177- . with_document (
178- & document . result. id,
179- PartitionKeys :: new (). push (& document . result. a_number)? ,
180- )
191+ . with_document_client (& document . result. id as & str , document . result. a_number. into ())
181192 . delete_document ()
193+ . with_consistency_level (session_token . clone ())
182194 . with_if_match_condition ((& document . document_attributes). into ())
183195 . execute ()
184196 . await ? ;
@@ -188,6 +200,7 @@ async fn main() -> Result<(), Box<dyn Error>> {
188200 // Now the list documents should return 4 documents!
189201 let list_documents_response = collection_client
190202 . list_documents ()
203+ . with_consistency_level (session_token )
191204 . execute :: <serde_json :: Value >() // you can use this if you don't know/care about the return type!
192205 . await ? ;
193206 assert_eq! (list_documents_response . documents. len (), 4 );
0 commit comments