mongodb_gridfs/bucket/
mod.rs

1mod delete;
2mod download;
3mod drop;
4mod find;
5mod rename;
6mod upload;
7use crate::options::GridFSBucketOptions;
8use mongodb::Database;
9
10/// GridFS bucket. A prefix under which a GridFS system’s collections are stored.
11/// [Spec](https://github.com/mongodb/specifications/blob/master/source/gridfs/gridfs-spec.rst#configurable-gridfsbucket-class)
12#[derive(Clone, Debug)]
13pub struct GridFSBucket {
14    pub(crate) db: Database,
15    pub(crate) options: Option<GridFSBucketOptions>,
16    // internal: when true should check the indexes
17    pub(crate) never_write: bool,
18}
19
20impl GridFSBucket {
21    /**
22     * Create a new GridFSBucket object on @db with the given @options.
23     */
24    pub fn new(db: Database, options: Option<GridFSBucketOptions>) -> GridFSBucket {
25        GridFSBucket {
26            db,
27            options,
28            never_write: true,
29        }
30    }
31}
32
33#[cfg(test)]
34mod tests {
35    use super::{GridFSBucket, GridFSBucketOptions};
36    use mongodb::Client;
37    use mongodb::{error::Error, Database};
38    use uuid::Uuid;
39    fn db_name_new() -> String {
40        "test_".to_owned()
41            + Uuid::new_v4()
42                .hyphenated()
43                .encode_lower(&mut Uuid::encode_buffer())
44    }
45
46    #[tokio::test]
47    async fn grid_f_s_bucket_new() -> Result<(), Error> {
48        let client = Client::with_uri_str(
49            &std::env::var("MONGO_URI").unwrap_or("mongodb://localhost:27017/".to_string()),
50        )
51        .await?;
52        let dbname = db_name_new();
53        let db: Database = client.database(&dbname);
54        let bucket = GridFSBucket::new(db.clone(), Some(GridFSBucketOptions::default()));
55
56        if let Some(options) = bucket.options {
57            assert_eq!(
58                options.bucket_name,
59                GridFSBucketOptions::default().bucket_name
60            );
61        }
62        assert_eq!(bucket.db.name(), db.name());
63
64        Ok(())
65    }
66}