database module. Because MongoDB is a NoSQL database, a lot of the query handling is handled by the application and not the database. We use the MongoDB driver library to perform all calls with the database.
The connection to the database is done in server/database/init.go, using the provided environmental variables to connect to the database.
Retrieving from database
To retrieve an item from the database, you can follow the example fromFindJudgeByToken:
Decode to convert the resultant object into a defined model (Judge in this case). This method of FindOne is used across the app.
To retrieve multiple items from the database, we can use a cursor object to get all items:
Inserting into/updating the database
To update an item in the database, we can use the following format:err object in this case, ignoring the result object from the db update.
Multiple instances of Jury
Jury is not designed to run with multiple instances of the app connecting to the same database. This will cause many problems such as the following:- Comparisons matrix will not be correct due to it being maintained on the server
- Judging Clock will not be synced across instances
Transactions
A lot of the database functions run multiple database operations in a row. We want these operations to all happen at the same time otherwise there may be race conditions and inconsistencies among the DB. The solution to this is to use transactions. We have written a wrapper function for this, so you only need to call the wrapper and make sure all database functions pass in the context with actx context.Context parameter: