prax-mongodb 0.9.4

MongoDB database driver for Prax ORM
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
//! MongoDB query engine implementation.

use std::marker::PhantomData;

use bson::{Document, doc};
use futures::TryStreamExt;
use mongodb::Collection;
use prax_query::QueryResult;
use prax_query::filter::FilterValue;
use prax_query::traits::{BoxFuture, Model, QueryEngine};
use tracing::debug;

use crate::client::MongoClient;
use crate::error::MongoError;
use crate::types::filter_value_to_bson;

/// MongoDB query engine that implements the Prax QueryEngine trait.
///
/// Note: MongoDB is a document database, so the SQL-oriented QueryEngine
/// trait methods are adapted to work with MongoDB operations.
#[derive(Clone)]
pub struct MongoEngine {
    client: MongoClient,
}

impl MongoEngine {
    /// Create a new MongoDB engine with the given client.
    pub fn new(client: MongoClient) -> Self {
        Self { client }
    }

    /// Get a reference to the client.
    pub fn client(&self) -> &MongoClient {
        &self.client
    }

    /// Get a typed collection for a model.
    pub fn collection<T>(&self) -> Collection<T>
    where
        T: Model + Send + Sync,
    {
        // Use the model name as the collection name (lowercase, pluralized)
        let collection_name = format!("{}s", T::MODEL_NAME.to_lowercase());
        self.client.collection(&collection_name)
    }

    /// Get a collection by explicit name.
    pub fn collection_by_name<T>(&self, name: &str) -> Collection<T>
    where
        T: Send + Sync,
    {
        self.client.collection(name)
    }

    /// Convert filter values to a MongoDB filter document.
    fn build_filter(sql: &str, params: &[FilterValue]) -> MongoResult<Document> {
        // For MongoDB, we expect the "sql" to actually be a JSON representation
        // of the filter document, or we parse it from a simple query format
        if sql.starts_with('{') {
            // JSON filter
            let filter: Document = serde_json::from_str(sql)
                .map_err(|e| MongoError::query(format!("invalid filter JSON: {}", e)))?;
            Ok(filter)
        } else if sql.is_empty() {
            // Empty filter - match all
            Ok(doc! {})
        } else {
            // Try to parse as a simple field=value format
            // For more complex queries, use the FilterBuilder
            let mut filter = Document::new();

            // Simple parsing: "field1=$1 AND field2=$2"
            for part in sql.split(" AND ") {
                let part = part.trim();
                if let Some(eq_pos) = part.find('=') {
                    let field = part[..eq_pos].trim();
                    let value_placeholder = part[eq_pos + 1..].trim();

                    // Check if it's a parameter placeholder ($1, $2, etc.)
                    if let Some(stripped) = value_placeholder.strip_prefix('$') {
                        if let Ok(param_idx) = stripped.parse::<usize>() {
                            if param_idx > 0 && param_idx <= params.len() {
                                let bson_value = filter_value_to_bson(&params[param_idx - 1])?;
                                filter.insert(field, bson_value);
                            }
                        }
                    } else {
                        // Direct value
                        filter.insert(field, value_placeholder);
                    }
                }
            }

            Ok(filter)
        }
    }
}

use crate::error::MongoResult;

impl QueryEngine for MongoEngine {
    fn query_many<T: Model + prax_query::row::FromRow + Send + 'static>(
        &self,
        sql: &str,
        params: Vec<FilterValue>,
    ) -> BoxFuture<'_, QueryResult<Vec<T>>> {
        let sql = sql.to_string();
        Box::pin(async move {
            debug!(filter = %sql, "Executing query_many");

            let filter = Self::build_filter(&sql, &params)
                .map_err(|e| prax_query::QueryError::database(e.to_string()))?;

            let collection = self
                .client
                .collection_doc(&format!("{}s", T::MODEL_NAME.to_lowercase()));

            let cursor = collection
                .find(filter, None)
                .await
                .map_err(|e| prax_query::QueryError::database(e.to_string()))?;

            let docs: Vec<Document> = cursor
                .try_collect()
                .await
                .map_err(|e| prax_query::QueryError::database(e.to_string()))?;

            docs.iter()
                .map(|d| {
                    let row = crate::row_ref::BsonRowRef::new(d);
                    T::from_row(&row).map_err(|e| {
                        let msg = e.to_string();
                        prax_query::QueryError::deserialization(msg).with_source(e)
                    })
                })
                .collect()
        })
    }

    fn query_one<T: Model + prax_query::row::FromRow + Send + 'static>(
        &self,
        sql: &str,
        params: Vec<FilterValue>,
    ) -> BoxFuture<'_, QueryResult<T>> {
        let sql = sql.to_string();
        Box::pin(async move {
            debug!(filter = %sql, "Executing query_one");

            let filter = Self::build_filter(&sql, &params)
                .map_err(|e| prax_query::QueryError::database(e.to_string()))?;

            let collection = self
                .client
                .collection_doc(&format!("{}s", T::MODEL_NAME.to_lowercase()));

            let doc = collection
                .find_one(filter, None)
                .await
                .map_err(|e| prax_query::QueryError::database(e.to_string()))?
                .ok_or_else(|| prax_query::QueryError::not_found(T::MODEL_NAME))?;

            let row = crate::row_ref::BsonRowRef::new(&doc);
            T::from_row(&row).map_err(|e| {
                let msg = e.to_string();
                prax_query::QueryError::deserialization(msg).with_source(e)
            })
        })
    }

    fn query_optional<T: Model + prax_query::row::FromRow + Send + 'static>(
        &self,
        sql: &str,
        params: Vec<FilterValue>,
    ) -> BoxFuture<'_, QueryResult<Option<T>>> {
        let sql = sql.to_string();
        Box::pin(async move {
            debug!(filter = %sql, "Executing query_optional");

            let filter = Self::build_filter(&sql, &params)
                .map_err(|e| prax_query::QueryError::database(e.to_string()))?;

            let collection = self
                .client
                .collection_doc(&format!("{}s", T::MODEL_NAME.to_lowercase()));

            let doc = collection
                .find_one(filter, None)
                .await
                .map_err(|e| prax_query::QueryError::database(e.to_string()))?;

            match doc {
                Some(doc) => {
                    let row = crate::row_ref::BsonRowRef::new(&doc);
                    T::from_row(&row).map(Some).map_err(|e| {
                        let msg = e.to_string();
                        prax_query::QueryError::deserialization(msg).with_source(e)
                    })
                }
                None => Ok(None),
            }
        })
    }

    fn execute_insert<T: Model + prax_query::row::FromRow + Send + 'static>(
        &self,
        sql: &str,
        params: Vec<FilterValue>,
    ) -> BoxFuture<'_, QueryResult<T>> {
        let sql = sql.to_string();
        Box::pin(async move {
            debug!(data = %sql, "Executing insert");

            let doc: Document = if sql.starts_with('{') {
                serde_json::from_str(&sql)
                    .map_err(|e| prax_query::QueryError::database(e.to_string()))?
            } else {
                let mut doc = Document::new();
                for (i, param) in params.iter().enumerate() {
                    let bson_value = filter_value_to_bson(param)
                        .map_err(|e| prax_query::QueryError::database(e.to_string()))?;
                    doc.insert(format!("field{}", i), bson_value);
                }
                doc
            };

            let collection = self
                .client
                .collection_doc(&format!("{}s", T::MODEL_NAME.to_lowercase()));

            let result = collection
                .insert_one(doc.clone(), None)
                .await
                .map_err(|e| prax_query::QueryError::database(e.to_string()))?;

            // Re-fetch the inserted document keyed on the server-assigned
            // `_id` (or on the client-supplied `_id` if the caller set
            // one) so the return value is the actual persisted row,
            // including server-generated fields.
            let id_filter = bson::doc! { "_id": result.inserted_id };
            let inserted = collection
                .find_one(id_filter, None)
                .await
                .map_err(|e| prax_query::QueryError::database(e.to_string()))?
                .ok_or_else(|| prax_query::QueryError::not_found(T::MODEL_NAME))?;

            let row = crate::row_ref::BsonRowRef::new(&inserted);
            T::from_row(&row).map_err(|e| {
                let msg = e.to_string();
                prax_query::QueryError::deserialization(msg).with_source(e)
            })
        })
    }

    fn execute_update<T: Model + prax_query::row::FromRow + Send + 'static>(
        &self,
        sql: &str,
        params: Vec<FilterValue>,
    ) -> BoxFuture<'_, QueryResult<Vec<T>>> {
        let sql = sql.to_string();
        Box::pin(async move {
            debug!(data = %sql, "Executing update");

            let filter = Self::build_filter(&sql, &params)
                .map_err(|e| prax_query::QueryError::database(e.to_string()))?;
            let set_doc = build_set_doc(&sql, &params)
                .map_err(|e| prax_query::QueryError::database(e.to_string()))?;

            let collection = self
                .client
                .collection_doc(&format!("{}s", T::MODEL_NAME.to_lowercase()));

            // Mongo's `update_many` doesn't hand back the affected
            // documents, so we have to re-fetch. The original filter
            // still selects the same rows post-update (the SET can't
            // un-match them for the filters the Client API emits —
            // we set columns, not the filter columns). One update +
            // one find instead of three round-trips.
            if !set_doc.is_empty() {
                let update = doc! { "$set": set_doc };
                collection
                    .update_many(filter.clone(), update, None)
                    .await
                    .map_err(|e| prax_query::QueryError::database(e.to_string()))?;
            }

            let cursor = collection
                .find(filter, None)
                .await
                .map_err(|e| prax_query::QueryError::database(e.to_string()))?;
            let updated: Vec<Document> = cursor
                .try_collect()
                .await
                .map_err(|e| prax_query::QueryError::database(e.to_string()))?;

            updated
                .iter()
                .map(|d| {
                    let row = crate::row_ref::BsonRowRef::new(d);
                    T::from_row(&row).map_err(|e| {
                        let msg = e.to_string();
                        prax_query::QueryError::deserialization(msg).with_source(e)
                    })
                })
                .collect()
        })
    }

    fn execute_delete(
        &self,
        sql: &str,
        params: Vec<FilterValue>,
    ) -> BoxFuture<'_, QueryResult<u64>> {
        let sql = sql.to_string();
        Box::pin(async move {
            debug!(filter = %sql, "Executing delete");

            let filter = Self::build_filter(&sql, &params)
                .map_err(|e| prax_query::QueryError::database(e.to_string()))?;

            // Get collection name from somewhere - would need model info
            let collection = self.client.collection_doc("documents");

            let result = collection
                .delete_many(filter, None)
                .await
                .map_err(|e| prax_query::QueryError::database(e.to_string()))?;

            Ok(result.deleted_count)
        })
    }

    fn execute_raw(&self, sql: &str, _params: Vec<FilterValue>) -> BoxFuture<'_, QueryResult<u64>> {
        let sql = sql.to_string();
        Box::pin(async move {
            debug!(command = %sql, "Executing raw command");

            // For MongoDB, raw execution means running a database command
            let command: Document = serde_json::from_str(&sql)
                .map_err(|e| prax_query::QueryError::database(e.to_string()))?;

            let _result = self
                .client
                .run_command(command)
                .await
                .map_err(|e| prax_query::QueryError::database(e.to_string()))?;

            Ok(1)
        })
    }

    fn count(&self, sql: &str, params: Vec<FilterValue>) -> BoxFuture<'_, QueryResult<u64>> {
        let sql = sql.to_string();
        Box::pin(async move {
            debug!(filter = %sql, "Executing count");

            let filter = Self::build_filter(&sql, &params)
                .map_err(|e| prax_query::QueryError::database(e.to_string()))?;

            // Get collection name - would need to parse from sql or context
            let collection = self.client.collection_doc("documents");

            let count = collection
                .count_documents(filter, None)
                .await
                .map_err(|e| prax_query::QueryError::database(e.to_string()))?;

            Ok(count)
        })
    }
}

/// A typed query builder that uses the MongoDB engine.
pub struct MongoQueryBuilder<T: Model> {
    engine: MongoEngine,
    _marker: PhantomData<T>,
}

impl<T: Model> MongoQueryBuilder<T> {
    /// Create a new query builder.
    pub fn new(engine: MongoEngine) -> Self {
        Self {
            engine,
            _marker: PhantomData,
        }
    }

    /// Get the underlying engine.
    pub fn engine(&self) -> &MongoEngine {
        &self.engine
    }

    /// Get a typed collection for this model.
    pub fn collection(&self) -> Collection<T>
    where
        T: Send + Sync,
    {
        self.engine.collection::<T>()
    }
}

/// Parse `SET col1 = $1, col2 = $2` from a SQL-ish UPDATE statement
/// and bind each placeholder to the matching entry in `params`. Returns
/// a BSON `$set` document suitable for [`update_many`]. Tolerant of
/// an absent SET clause (returns empty doc, caller treats that as a
/// filter-only "update nothing" no-op).
fn build_set_doc(sql: &str, params: &[FilterValue]) -> MongoResult<Document> {
    // Locate the SET … WHERE window in the SQL string.
    let lower = sql.to_ascii_lowercase();
    let Some(set_start) = lower.find(" set ") else {
        return Ok(Document::new());
    };
    let set_body_start = set_start + " set ".len();
    let set_body_end = lower[set_body_start..]
        .find(" where ")
        .map(|i| set_body_start + i)
        .unwrap_or(sql.len());
    let body = &sql[set_body_start..set_body_end];

    let mut out = Document::new();
    for assignment in body.split(',') {
        let assignment = assignment.trim();
        let Some(eq) = assignment.find('=') else {
            continue;
        };
        let col = assignment[..eq].trim();
        let rhs = assignment[eq + 1..].trim();
        let Some(idx_str) = rhs.strip_prefix('$') else {
            continue;
        };
        let Ok(idx) = idx_str.parse::<usize>() else {
            continue;
        };
        if idx == 0 || idx > params.len() {
            continue;
        }
        let value = filter_value_to_bson(&params[idx - 1])?;
        out.insert(col, value);
    }
    Ok(out)
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_build_filter_json() {
        let filter = MongoEngine::build_filter(r#"{"name": "Alice"}"#, &[]).unwrap();
        assert_eq!(filter.get_str("name").unwrap(), "Alice");
    }

    #[test]
    fn test_build_filter_empty() {
        let filter = MongoEngine::build_filter("", &[]).unwrap();
        assert!(filter.is_empty());
    }
}