Basic HTTP reactive application that uses Ratpack, MongoStreams and Server-sent events to send notifications to the front-end when a Document is updated in the MongoDB Collection.
We use a DevExtreme DataGrid component to provide a simple HTMl page to show and edit the data, this DataGrid uses a custom DataSource that fetches data from a Back-end endpoint that implements the required logic to query data from a MongoDB collection, parametrized by a DevExtreme loadOptions object.
- MongoDB
- Java 8+
- [Optional] Moonshine-IDE
- Download and install VirtualBox.
- Download and install Vagrant.
- Open a command line and change directory to the base path of this project.
- Execute the
vagrant up
command, once the process completes an Ubuntu-MongoDB virtual machine will be running. - Continue to step 2.
Click to expand!
https://docs.mongodb.com/manual/tutorial/install-mongodb-on-ubuntu/.
https://docs.mongodb.com/manual/tutorial/install-mongodb-on-windows/.
install mongosh as a separate package.
Install MongoDB tools (mongoimport).
Add the path to mongoDB files to windows PATH
In my case it was
C:\Program Files\MongoDB\Server\5.0\bin\
C:\Program Files\MongoDB\Tools\100\bin\
It wasn't required to add mongosh app to the windows PATH variable, but it was installed on this directory:
C:\Users\HP\AppData\Local\Programs\mongosh
Stop any mongodb server instance.
Run the following commands to set up a simple, single-node replica set (for testing purposes).
mkdir C:\mongodb\data
mongod --replSet rs0 --dbpath "C:\mongodb\data"
mkdir -p /mongodb/data
mongod --replSet rs0 --dbpath /mongodb/data
mongosh mongodb://<MONGODB_SERVER_IP>:27017/test
rs.initiate()
Import the grades collection from the file vagrant/grades.json
into the test database like this:
mongoimport --db test --collection grades --drop --file grades.json
Once the mongodb server is running you can continue and run this project
./gradlew run
gradlew.bat run
Open the project in Moonshine with File > Open/Import Project
or by double-clicking on ratpack-push.javaproj.
Project > Run Gradle Command
. This will run the default command gradle clean runApp.
-
GET /api/grades: Receives a DevExtreme loadOptions object as a request parameter. Can be used to test DevExtreme components.
-
(GET, POST, PUT, DELETE) /api/grades: this endpoint exposes a basic CRUD functionality.
-
GET /grades/stream: This endpoint uses mongo Change Streams to respond with a Server-sent event when any operation (insert, update, delete) is performed on the Grades collection (similar to what we have in the Ratpack back-end demo app).
-
GET /frontend: This is an endpoint for testing the back-end endpoints. It responds with an HTML page that displays a list of Grades and dynamically updates the table when a notification (Server-sent event) is received.
Any modification/insertion into the restaurants collection will trigger a server-sent event in the Ratpack application.
Connect to mongosh
mongosh mongodb://<MONGODB_SERVER_IP>:27017/test
Then within mongosh To update a single document:
db.grades.updateOne(
{ quizScore: { $gte: 90 } },
[{ $set: { examScore: { $round: [ { $multiply: [ { $rand: {} }, 100 ] }, 2 ] } } }]
);
To update multiple documents:
try {
db.grades.updateMany(
{ examScore: { $lte: 25 } },
[{ $set: { examScore: { $round: [ { $multiply: [ { $rand: {} }, 100 ] }, 2 ] } } }],
);
} catch (e) {
print(e);
}