Skip to content

Commit

Permalink
feat: function for connecting to the database
Browse files Browse the repository at this point in the history
And creating a collection, if it doesn't exist. This doesn't work! Lots of result errors from the compiler.
  • Loading branch information
extua committed Jun 19, 2024
1 parent 36abca6 commit b635df3
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 9 deletions.
2 changes: 1 addition & 1 deletion docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,4 @@ services:
MONGO_INITDB_ROOT_PASSWORD: PASSWORD
volumes:
- ./db:/data/db
# - ./docker-entrypoint-initdb.d/mongo-init.js:/docker-entrypoint-initdb.d/mongo-init.js:ro
- ./docker-entrypoint-initdb.d/mongo-init.js:/docker-entrypoint-initdb.d/mongo-init.js:ro
16 changes: 8 additions & 8 deletions docker-entrypoint-initdb.d/mongo-init.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
print('Creating database');

db = db.getSiblingDB('communities');
db.createCollection(
"hourlySnapshot",
{
timeseries: {
timeField: "timestamp",
metaField: "metadata",
granularity: "hours"
}});
// db.createCollection(
// "hourlySnapshot",
// {
// timeseries: {
// timeField: "timestamp",
// metaField: "metadata",
// granularity: "hours"
// }});
print('Created database');
54 changes: 54 additions & 0 deletions json_adder/examples/setup_db.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
use bson::{doc, Document};
use futures::TryStreamExt;
#[allow(unused_imports)]
#[allow(dead_code)]
use mongodb::{
bson,
options::{
ClientOptions, CreateCollectionOptions, ServerApi, ServerApiVersion, TimeseriesGranularity,
TimeseriesOptions,
},
Client, Collection,
};
use std::error::Error;
use tokio;

#[tokio::main]

async fn main() -> Result<Collection<Document>, Error> {
// boilerplate connection code
let uri: &str = "mongodb://ADMIN:PASSWORD@localhost:27017";
let mut client_options = ClientOptions::parse_async(uri).await?;

// Set the server_api field of the client_options object to Stable API version 1
let server_api = ServerApi::builder().version(ServerApiVersion::V1).build();
client_options.server_api = Some(server_api);

// Create a new client and connect to the server
let client: Client = Client::with_options(client_options)?;

let db_list: Vec<String> = client.list_database_names(doc! {}, None).await?;
println!("List databases: {:?}", db_list);
let db = client.database("communities");

// List collections in the database
let coll_list: Vec<String> = db.list_collection_names(doc! {}).await?;
println!("List collections in database: {:?}", coll_list);

// If the collection doesn't exist, create it
if coll_list.iter().any(|e| e != "hourly_snapshot") {
let ts_opts = TimeseriesOptions::builder()
.time_field("timestamp".to_string())
.meta_field(Some("label".to_string()))
.granularity(Some(TimeseriesGranularity::Hours))
.build();
let coll_opts = CreateCollectionOptions::builder()
.timeseries(ts_opts)
.build();
db.create_collection("hourly_snapshot", coll_opts).await?;
};

// Return the collection
let snapshot_collection: Collection<Document> = db.collection("hourly_snapshot");
return snapshot_collection
}

0 comments on commit b635df3

Please sign in to comment.