(
&self,
collection: &str,
id: &str,
)
| 15 | |
| 16 | impl NodeDbRemote { |
| 17 | pub(super) async fn document_get_impl( |
| 18 | &self, |
| 19 | collection: &str, |
| 20 | id: &str, |
| 21 | ) -> NodeDbResult<Option<Document>> { |
| 22 | let collection = quote_identifier(collection); |
| 23 | let sql = format!("SELECT id, data FROM {collection} WHERE id = $1"); |
| 24 | let (_, rows) = self.query_raw(&sql, &[&id]).await?; |
| 25 | |
| 26 | if let Some(row) = rows.first() { |
| 27 | let doc_id = row |
| 28 | .first() |
| 29 | .and_then(|v| v.as_str()) |
| 30 | .unwrap_or(id) |
| 31 | .to_string(); |
| 32 | |
| 33 | let mut doc = Document::new(doc_id); |
| 34 | |
| 35 | // If the second column is JSON, parse it into fields. |
| 36 | if let Some(Value::Object(fields)) = row.get(1) { |
| 37 | for (k, v) in fields { |
| 38 | doc.set(k.clone(), v.clone()); |
| 39 | } |
| 40 | } else if let Some(Value::String(json_str)) = row.get(1) |
| 41 | && let Ok(parsed) = |
| 42 | sonic_rs::from_str::<HashMap<String, serde_json::Value>>(json_str) |
| 43 | { |
| 44 | for (k, v) in &parsed { |
| 45 | doc.set(k.clone(), json_to_value(v)); |
| 46 | } |
| 47 | } |
| 48 | |
| 49 | Ok(Some(doc)) |
| 50 | } else { |
| 51 | Ok(None) |
| 52 | } |
| 53 | } |
| 54 | |
| 55 | pub(super) async fn document_put_impl( |
| 56 | &self, |
no test coverage detected