Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Integration of MongoDB #8

Closed
wants to merge 31 commits into from
Closed

Integration of MongoDB #8

wants to merge 31 commits into from

Conversation

colemanirby
Copy link
Contributor

@colemanirby colemanirby commented Oct 2, 2024

In this branch we have introduced integration with MongoDB to store MMRs. There is a toggle that is introduced that allows storage in local files or in MongoDB. There are two functions implemented: write_to_db and load_from_db. Write to DB can take in any generic object and store it in MongoDB after being give the db name, collection name, and a connection. After an object is written into the DB, the corresponding (unique) object ID will be stored in the cookies for retrieval in a later session. load_from_db can load any generic object so long as you provide the db name, collection name, object id, and connection.

This is related to issue #2

Copy link
Contributor

@driemworks driemworks left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

looks pretty good, mostly just needs error handling

Copy link
Contributor

@juangirini juangirini left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The store module needs some refactoring, please take a look at my comment below for more details.
The store should be completely transparent to the endpoints, and they should know nothing about its implementation details, only its API. The store could be a file, a MySQL, or just an interface with another HTTP API... the endpoints wouldn't tell the difference.

For that, we should keep all the dependencies and business logic related to the DB in store.rs.
I have drafted a solution with that approach, which you can see in this diff https://github.com/ideal-lab5/murmur-api/compare/juan/draft-store-approach

The solution proposed is to create a new Store struct that holds the db connection as a property. It implements three methods: init, load and write, they are what main.rs knows about.
Ideally, we should go a step forward and make this a Trait, then we could have multiple Store implementations of that trait... but it looks like State<> doesn't support traits.

Keep in mind that it is just a draft and it needs to be finished off: the new endpoint isn't ready (and it is commented out), there might be some warnings to clean up and the hardcoded const needs to be moved to environment variables

@colemanirby
Copy link
Contributor Author

colemanirby commented Oct 4, 2024

I intend to add error handling and then will raise another PR. I'm afraid that trying to do generic support will need a lot of custom code. An example of using multiple databases is given here: https://github.com/rwf2/Rocket/blob/v0.5/examples/databases/src/main.rs. It appears (at first glance) that rocket hard couples your database to your API. Notice that they mount each database to a different URL as well.

@colemanirby colemanirby requested a review from driemworks October 4, 2024 21:33
@colemanirby colemanirby requested a review from juangirini October 6, 2024 16:42
@juangirini
Copy link
Contributor

I'm afraid that trying to do generic support will need a lot of custom code. An example of using multiple databases is given here: https://github.com/rwf2/Rocket/blob/v0.5/examples/databases/src/main.rs. It appears (at first glance) that rocket hard couples your database to your API. Notice that they mount each database to a different URL as well.

We don't need multiple concurrent databases like in that example, we only need one at a time. You can decouple the database to your API in Rocket, and that is what I have done in the example that I shared with you earlier, please take a look at it https://github.com/ideal-lab5/murmur-api/compare/juan/draft-store-approach#diff-42cb6807ad74b3e201c5a7ca98b911c5fa08380e942be6e4ac5807f8377f87fcR162-R164

@colemanirby
Copy link
Contributor Author

Requested changes have been implemented.

Copy link
Contributor

@juangirini juangirini left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should be good after addressing the open comments

@juangirini
Copy link
Contributor

This looks ready to be merged, but there's a DB connection error, I guess the MongoDB is down?

Kind: Server selection timeout: No available servers. Topology: { Type: ReplicaSetNoPrimary, Set Name: atlas-xtx5fv-shard-0, Servers: [ { Address: useast-shard-00-01.m8j6h.mongodb.net:27017, Type: Unknown, Error: Kind: I/O error: received fatal alert: InternalError, labels: {} }, { Address: useast-shard-00-02.m8j6h.mongodb.net:27017, Type: Unknown, Error: Kind: I/O error: received fatal alert: InternalError, labels: {} }, { Address: useast-shard-00-00.m8j6h.mongodb.net:27017, Type: Unknown, Error: Kind: I/O error: received fatal alert: InternalError, labels: {} } ] }, labels: {}

@juangirini
Copy link
Contributor

Closing in favour of #17

@juangirini juangirini closed this Oct 9, 2024
@juangirini juangirini deleted the coleman_dev branch October 14, 2024 08:13
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
Status: Done
Development

Successfully merging this pull request may close these issues.

3 participants