couchdb_orm/client/couchdb/actions/db/seed/
mod.rs1use crate::regexes::COUCHDB_DB_RULE;
18use awc::Client;
19use serde::{de::DeserializeOwned, Serialize};
20use std::fmt::Debug;
21
22pub mod errors;
23
24use errors::DBSeedError;
25
26async fn post_request(
27 client: &awc::Client,
28 uri: &str,
29 body: &serde_json::Value,
30) -> Result<bool, Box<dyn std::error::Error>> {
31 match client.post(uri).send_json(body).await {
32 Ok(mut response) => {
33 if response.status().as_u16() >= 400 {
35 let bytes: Vec<u8> = response.body().await?.iter().cloned().collect();
36 let error: serde_json::Value =
37 serde_json::from_str(std::str::from_utf8(bytes.as_slice()).unwrap()).unwrap();
38 return Err(Box::new(DBSeedError::new(&format!(
39 "Error with the request to {}: error: \n{}",
40 &uri, error
41 ))));
42 } else {
43 return Ok(true);
44 }
45 }
46 Err(error) => {
47 return Err(Box::new(DBSeedError::new(&format!(
48 "Error with the request to {}: error: \n{}",
49 uri, error
50 ))))
51 }
52 }
53}
54
55pub async fn seed_db<T: Clone + Serialize + DeserializeOwned + Debug>(
56 client: &Client,
57 db_name: &str,
58 data: Vec<T>,
59 host: &str,
60) -> Result<bool, Box<dyn std::error::Error>> {
61 if !COUCHDB_DB_RULE.is_match(db_name) {
62 return Err(Box::new(DBSeedError::new(&format!(
63 "{} string doesn't respect the regex rule {}",
64 db_name,
65 COUCHDB_DB_RULE.as_str()
66 ))));
67 }
68
69 let chunks: Vec<Vec<T>> = data.as_slice().chunks(100).map(|m| m.to_vec()).collect();
70
71 let uri: String = format!("{}/{}/_bulk_docs", host, db_name,);
73
74 for (index, chunk) in chunks.iter().enumerate() {
75 println!("chunk {} seeded", index);
76 let json_body: serde_json::Value = serde_json::json!({
80 "docs": chunk.iter().map(|o| {
81 let mut value = serde_json::to_value(o).unwrap();
82 let value = value.as_object_mut().unwrap();
83 value.remove("_rev");
84 serde_json::to_value(value).unwrap()
85 }).collect::<serde_json::Value>(),
86 });
87 post_request(&client, &uri, &json_body).await?;
89 }
90 Ok(true)
91}
92
93#[cfg(test)]
94mod tests {}