Skip to content

Commit 30e58ba

Browse files
committed
CreateDocument::into_future
1 parent e454b62 commit 30e58ba

File tree

11 files changed

+121
-152
lines changed

11 files changed

+121
-152
lines changed

sdk/data_cosmos/examples/attachments_00.rs

Lines changed: 11 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,21 @@
1-
use azure_core::Context;
21
use azure_data_cosmos::prelude::*;
32
use serde::{Deserialize, Serialize};
4-
use std::borrow::Cow;
53
use std::error::Error;
64

7-
// Now we create a sample struct. The Cow trick
8-
// allows us to use the same struct for serializing
9-
// (without having to own the items if not needed) and
10-
// for deserializing (where owning is required).
11-
// We do not need to define the "id" field here, it will be
12-
// specified in the Document struct below.
5+
// Now we create a sample struct.
136
#[derive(Serialize, Deserialize, Clone, Debug)]
14-
struct MySampleStruct<'a> {
15-
id: Cow<'a, str>,
16-
a_string: Cow<'a, str>,
7+
struct MySampleStruct {
8+
id: String,
9+
a_string: String,
1710
a_number: u64,
1811
a_timestamp: i64,
1912
}
2013

21-
impl<'a> azure_data_cosmos::CosmosEntity<'a> for MySampleStruct<'a> {
22-
type Entity = &'a str;
14+
impl azure_data_cosmos::CosmosEntity for MySampleStruct {
15+
type Entity = String;
2316

24-
fn partition_key(&'a self) -> Self::Entity {
25-
self.id.as_ref()
17+
fn partition_key(&self) -> Self::Entity {
18+
self.id.clone().into()
2619
}
2720
}
2821

@@ -50,17 +43,14 @@ async fn main() -> Result<(), Box<dyn Error + Send + Sync>> {
5043
let id = format!("unique_id{}", 100);
5144

5245
let doc = MySampleStruct {
53-
id: Cow::Borrowed(&id),
54-
a_string: Cow::Borrowed("Something here"),
46+
id,
47+
a_string: "Something here".into(),
5548
a_number: 100,
5649
a_timestamp: chrono::Utc::now().timestamp(),
5750
};
5851

5952
// let's add an entity.
60-
match client
61-
.create_document(Context::new(), &doc, CreateDocumentOptions::new())
62-
.await
63-
{
53+
match client.create_document(doc.clone()).into_future().await {
6454
Ok(_) => {
6555
println!("document created");
6656
}

sdk/data_cosmos/examples/database_00.rs

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -49,12 +49,11 @@ async fn main() -> Result<(), Box<dyn Error + Send + Sync>> {
4949
}"#;
5050
let document: Value = serde_json::from_str(data)?;
5151

52-
let options = CreateDocumentOptions::new()
53-
.is_upsert(true)
54-
.partition_key(&43u32)
55-
.unwrap();
5652
let resp = collection_client
57-
.create_document(Context::new(), &document, options)
53+
.create_document(document)
54+
.is_upsert(true)
55+
.partition_key(&43u32)?
56+
.into_future()
5857
.await?;
5958

6059
println!("resp == {:?}", resp);

sdk/data_cosmos/examples/document_00.rs

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,22 +4,21 @@ use serde::{Deserialize, Serialize};
44
// DB.
55
use azure_core::prelude::*;
66
use azure_data_cosmos::prelude::*;
7-
use std::borrow::Cow;
87
use std::error::Error;
98

109
#[derive(Clone, Serialize, Deserialize, Debug)]
11-
struct MySampleStruct<'a> {
12-
id: Cow<'a, str>,
13-
a_string: Cow<'a, str>,
10+
struct MySampleStruct {
11+
id: String,
12+
a_string: String,
1413
a_number: u64,
1514
a_timestamp: i64,
1615
}
1716

18-
impl<'a> azure_data_cosmos::CosmosEntity<'a> for MySampleStruct<'a> {
19-
type Entity = &'a str;
17+
impl azure_data_cosmos::CosmosEntity for MySampleStruct {
18+
type Entity = String;
2019

21-
fn partition_key(&'a self) -> Self::Entity {
22-
self.id.as_ref()
20+
fn partition_key(&self) -> Self::Entity {
21+
self.id.clone()
2322
}
2423
}
2524

@@ -115,8 +114,8 @@ async fn main() -> Result<(), Box<dyn Error + Send + Sync>> {
115114
// data in them. Let's create a Document. The only constraint
116115
// is that we need an id and an arbitrary, Serializable type.
117116
let doc = MySampleStruct {
118-
id: Cow::Owned("unique_id100".to_owned()),
119-
a_string: Cow::Borrowed("Something here"),
117+
id: "unique_id100".into(),
118+
a_string: "Something here".into(),
120119
a_number: 100,
121120
a_timestamp: chrono::Utc::now().timestamp(),
122121
};
@@ -132,7 +131,8 @@ async fn main() -> Result<(), Box<dyn Error + Send + Sync>> {
132131
// the document attributes.
133132

134133
let create_document_response = collection_client
135-
.create_document(Context::new(), &doc, CreateDocumentOptions::new())
134+
.create_document(doc.clone())
135+
.into_future()
136136
.await?;
137137
println!(
138138
"create_document_response == {:#?}",

sdk/data_cosmos/examples/document_entries_00.rs

Lines changed: 11 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -2,28 +2,22 @@ use azure_core::prelude::*;
22
use azure_data_cosmos::prelude::*;
33
use futures::stream::StreamExt;
44
use serde::{Deserialize, Serialize};
5-
use std::borrow::Cow;
65
use std::error::Error;
76

8-
// Now we create a sample struct. The Cow trick
9-
// allows us to use the same struct for serializing
10-
// (without having to own the items if not needed) and
11-
// for deserializing (where owning is required).
12-
// We do not need to define the "id" field here, it will be
13-
// specified in the Document struct below.
7+
// Now we create a sample struct.
148
#[derive(Serialize, Deserialize, Clone, Debug)]
15-
struct MySampleStruct<'a> {
16-
id: Cow<'a, str>,
17-
a_string: Cow<'a, str>,
9+
struct MySampleStruct {
10+
id: String,
11+
a_string: String,
1812
a_number: u64,
1913
a_timestamp: i64,
2014
}
2115

22-
impl<'a> azure_data_cosmos::CosmosEntity<'a> for MySampleStruct<'a> {
23-
type Entity = &'a str;
16+
impl azure_data_cosmos::CosmosEntity for MySampleStruct {
17+
type Entity = String;
2418

25-
fn partition_key(&'a self) -> Self::Entity {
26-
self.id.as_ref()
19+
fn partition_key(&self) -> Self::Entity {
20+
self.id.clone()
2721
}
2822
}
2923

@@ -51,18 +45,14 @@ async fn main() -> Result<(), Box<dyn Error + Send + Sync>> {
5145
let mut response = None;
5246
for i in 0u64..5 {
5347
let doc = MySampleStruct {
54-
id: Cow::Owned(format!("unique_id{}", i)),
55-
a_string: Cow::Borrowed("Something here"),
48+
id: format!("unique_id{}", i),
49+
a_string: "Something here".into(),
5650
a_number: i,
5751
a_timestamp: chrono::Utc::now().timestamp(),
5852
};
5953

6054
// let's add an entity.
61-
response = Some(
62-
client
63-
.create_document(Context::new(), &doc, CreateDocumentOptions::new())
64-
.await?,
65-
);
55+
response = Some(client.create_document(doc.clone()).into_future().await?);
6656
}
6757

6858
println!("Created 5 documents.");

sdk/data_cosmos/examples/document_entries_01.rs

Lines changed: 12 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,21 @@
11
use azure_core::Context;
22
use azure_data_cosmos::prelude::*;
33
use serde::{Deserialize, Serialize};
4-
use std::borrow::Cow;
54
use std::error::Error;
65

76
#[derive(Serialize, Deserialize, Clone, Debug)]
8-
struct MySampleStruct<'a> {
9-
id: Cow<'a, str>,
10-
a_string: Cow<'a, str>,
7+
struct MySampleStruct {
8+
id: String,
9+
a_string: String,
1110
a_number: u64,
1211
a_timestamp: i64,
1312
}
1413

15-
impl<'a> azure_data_cosmos::CosmosEntity<'a> for MySampleStruct<'a> {
16-
type Entity = &'a str;
14+
impl azure_data_cosmos::CosmosEntity for MySampleStruct {
15+
type Entity = String;
1716

18-
fn partition_key(&'a self) -> Self::Entity {
19-
self.id.as_ref()
17+
fn partition_key(&self) -> Self::Entity {
18+
self.id.clone()
2019
}
2120
}
2221

@@ -42,19 +41,17 @@ async fn main() -> Result<(), Box<dyn Error + Send + Sync>> {
4241
let client = client.into_collection_client(collection_name);
4342

4443
let mut doc = MySampleStruct {
45-
id: Cow::Owned(format!("unique_id{}", 500)),
46-
a_string: Cow::Borrowed("Something here"),
44+
id: format!("unique_id{}", 500),
45+
a_string: "Something here".into(),
4746
a_number: 600,
4847
a_timestamp: chrono::Utc::now().timestamp(),
4948
};
5049

5150
// let's add an entity.
5251
let create_document_response = client
53-
.create_document(
54-
Context::new(),
55-
&doc,
56-
CreateDocumentOptions::new().is_upsert(true),
57-
)
52+
.create_document(doc.clone())
53+
.is_upsert(true)
54+
.into_future()
5855
.await?;
5956

6057
println!(

sdk/data_cosmos/examples/readme.rs

Lines changed: 11 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -3,25 +3,24 @@ use serde::{Deserialize, Serialize};
33
// Using the prelude module of the Cosmos crate makes easier to use the Rust Azure SDK for Cosmos.
44
use azure_data_cosmos::prelude::*;
55
use futures::stream::StreamExt;
6-
use std::borrow::Cow;
76
use std::error::Error;
87

98
// This is the stuct we want to use in our sample.
109
// Make sure to have a collection with partition key "a_number" for this example to
1110
// work (you can create with this SDK too, check the examples folder for that task).
12-
#[derive(Serialize, Deserialize, Debug)]
13-
struct MySampleStruct<'a> {
14-
id: Cow<'a, str>,
15-
a_string: Cow<'a, str>,
11+
#[derive(Serialize, Deserialize, Debug, Clone)]
12+
struct MySampleStruct {
13+
id: String,
14+
a_string: String,
1615
a_number: u64,
1716
a_timestamp: i64,
1817
}
1918

2019
// Here we mark "a_number" as partition key.
21-
impl<'a> azure_data_cosmos::CosmosEntity<'a> for MySampleStruct<'a> {
20+
impl azure_data_cosmos::CosmosEntity for MySampleStruct {
2221
type Entity = u64;
2322

24-
fn partition_key(&'a self) -> Self::Entity {
23+
fn partition_key(&self) -> Self::Entity {
2524
self.a_number
2625
}
2726
}
@@ -71,20 +70,18 @@ async fn main() -> Result<(), Box<dyn Error + Send + Sync>> {
7170
for i in 0..10 {
7271
// define the document.
7372
let document_to_insert = MySampleStruct {
74-
id: Cow::Owned(format!("unique_id{}", i)),
75-
a_string: Cow::Borrowed("Something here"),
73+
id: format!("unique_id{}", i),
74+
a_string: "Something here".into(),
7675
a_number: i * 100, // this is the partition key
7776
a_timestamp: chrono::Utc::now().timestamp(),
7877
};
7978

8079
// insert it and store the returned session token for later use!
8180
session_token = Some(
8281
collection_client
83-
.create_document(
84-
Context::new(),
85-
&document_to_insert,
86-
CreateDocumentOptions::new().is_upsert(true),
87-
)
82+
.create_document(document_to_insert.clone())
83+
.is_upsert(true)
84+
.into_future()
8885
.await?
8986
.session_token, // get only the session token, if everything else was ok!
9087
);

sdk/data_cosmos/examples/user_permission_token.rs

Lines changed: 8 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -120,14 +120,10 @@ async fn main() -> Result<(), Box<dyn Error + Send + Sync>> {
120120
.clone()
121121
.into_database_client(database_name.clone())
122122
.into_collection_client(collection_name.clone())
123-
.create_document(
124-
Context::new(),
125-
&document,
126-
CreateDocumentOptions::new()
127-
.is_upsert(true)
128-
.partition_key(&"Gianluigi Bombatomica")
129-
.unwrap(),
130-
)
123+
.create_document(document.clone())
124+
.is_upsert(true)
125+
.partition_key(&"Gianluigi Bombatomica")?
126+
.into_future()
131127
.await
132128
{
133129
Ok(_) => panic!("this should not happen!"),
@@ -169,14 +165,10 @@ async fn main() -> Result<(), Box<dyn Error + Send + Sync>> {
169165
let create_document_response = client
170166
.into_database_client(database_name)
171167
.into_collection_client(collection_name)
172-
.create_document(
173-
Context::new(),
174-
&document,
175-
CreateDocumentOptions::new()
176-
.is_upsert(true)
177-
.partition_key(&"Gianluigi Bombatomica")
178-
.unwrap(),
179-
)
168+
.create_document(document)
169+
.is_upsert(true)
170+
.partition_key(&"Gianluigi Bombatomica")?
171+
.into_future()
180172
.await?;
181173
println!(
182174
"create_document_response == {:#?}",

sdk/data_cosmos/src/clients/collection_client.rs

Lines changed: 5 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -101,21 +101,11 @@ impl CollectionClient {
101101
}
102102

103103
/// create a document in a collection
104-
pub async fn create_document<'a, D: Serialize + CosmosEntity<'a>>(
104+
pub fn create_document<D: Serialize + CosmosEntity + Send + 'static>(
105105
&self,
106-
ctx: Context,
107-
document: &'a D,
108-
options: CreateDocumentOptions,
109-
) -> crate::Result<CreateDocumentResponse> {
110-
let mut request = self.prepare_doc_request_pipeline(http::Method::POST);
111-
112-
options.decorate_request(&mut request, document)?;
113-
let response = self
114-
.pipeline()
115-
.send(ctx.clone().insert(ResourceType::Documents), &mut request)
116-
.await?;
117-
118-
Ok(CreateDocumentResponse::try_from(response).await?)
106+
document: D,
107+
) -> CreateDocumentBuilder<D> {
108+
CreateDocumentBuilder::new(self.clone(), document)
119109
}
120110

121111
/// query documents in a collection
@@ -191,7 +181,7 @@ impl CollectionClient {
191181
self.cosmos_client().pipeline()
192182
}
193183

194-
fn prepare_doc_request_pipeline(&self, http_method: http::Method) -> Request {
184+
pub(crate) fn prepare_doc_request_pipeline(&self, http_method: http::Method) -> Request {
195185
let path = &format!(
196186
"dbs/{}/colls/{}/docs",
197187
self.database_client().database_name(),

sdk/data_cosmos/src/cosmos_entity.rs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,18 +4,18 @@ use http::request::Builder;
44
use serde::Serialize;
55

66
/// CosmosDB partition key. Every CosmosDB entity must implement it.
7-
pub trait CosmosEntity<'a> {
7+
pub trait CosmosEntity {
88
/// Returned type.
9-
type Entity: Serialize + 'a;
9+
type Entity: Serialize;
1010

1111
/// Return partition key value as reference.
12-
fn partition_key(&'a self) -> Self::Entity;
12+
fn partition_key(&self) -> Self::Entity;
1313
}
1414

15-
impl<'a> CosmosEntity<'a> for serde_json::Value {
16-
type Entity = &'a Self;
17-
fn partition_key(&'a self) -> Self::Entity {
18-
self
15+
impl CosmosEntity for serde_json::Value {
16+
type Entity = Self;
17+
fn partition_key(&self) -> Self::Entity {
18+
self.clone()
1919
}
2020
}
2121

0 commit comments

Comments
 (0)