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
use psl::parser_database::SourceFile;
use schema_core::schema_connector::{ConnectorError, ConnectorResult};
use std::sync::Arc;
use url::Url;

pub(crate) async fn mongo_setup(schema: &str, url: &str) -> ConnectorResult<()> {
    let url = Url::parse(url).map_err(ConnectorError::url_parse_error).unwrap();
    let db_name = url.path().trim_start_matches('/').to_string();
    let client = mongodb_client::create(url).await.unwrap();

    client
        .database(&db_name)
        .drop(Some(
            mongodb::options::DropDatabaseOptions::builder()
                .write_concern(mongodb::options::WriteConcern::builder().journal(true).build())
                .build(),
        ))
        .await
        .unwrap();

    let parsed_schema =
        psl::parse_schema(SourceFile::new_allocated(Arc::from(schema.to_owned().into_boxed_str()))).unwrap();

    for model in parsed_schema.db.walk_models() {
        client
            .database(&db_name)
            .create_collection(model.database_name(), None)
            .await
            .unwrap();
    }

    Ok(())
}