WEB-Project 2 Storing and Accessing Sensor Data using MongoDB Solved
You are being provided with an index.js which provides the required commandline behavior. What you speci cally need to do is add code to the provided sensors.js source le as per the requirements in that le.
The API is extremely similar to that for your previous project. The di erences include:
• The Sensors class must provide persistent storage by using a mongodb database.
• An instance of the Sensors class is created externally by calling an async newSensors() factory method. This is for reasons discussed in class.
• All application errors are reported using an array of AppError objects. The provided AppError class simply wraps an error code and message; the code will make it easy to classify errors in future projects.
The behavior of the program is illustrated in this annotated log.
1.3 Provided Files The prj2-sol directory contains a start for your project. It contains the following les:
sensors.js This skeleton le constitutes the guts of your project. You will need to esh out the skeleton, adding code as per the documentation. You should feel free to add any auxiliary function or method de nitions as required.
The provided code does most (not all) the validations necessary for this project.
index.js This le provides the complete command-line behavior which is required by your program. It requires sensors.js. You must not modify this le; this ensures that your Sensors class meets its speci cations and facilitates automated testing by testing only the Sensors API.
app-error.js A trival class for application errors.
validate.js Validation code from the previous project. Note that it provides generic validation based on types for input parameters. More validation will be necessary, especially for checking that a parameter speci es some other object via its id.
README A README le which must be submitted along with your project. It contains an initial header which you must complete (replace the dummy entries with your name, B-number and email address at which you would like to receive project-related email). After the header you may include any content which you would like read during the grading of your project.
Additionally, the course data directory contains sensor data les. It’s content is identical to the previous project except that the error data les have been enhanced with objects which shows the errors resulting from references to nonexisting objects.
1.4 MongoDB MongoDB is a popular nosql database. It allows storage of collections of documents to be accessed by a primary key named _id.
In terms of JavaScript, mongodb documents correspond to arbitrarily nested JavaScript Objects having a top-level _id property which is used as a primary key. If an object does not have an _id property, then one will be created with a unique value assigned by mongodb.
• MongoDB provides a basic repertoire of CRUD Operations.
• All asynchronous mongo library functions can be called directly using await.
• It is important to ensure that all database connections are closed. Otherwise your program will not exit gracefully.
You can play with mongo by starting up a mongo shell:
$ $ mongo
MongoDB shell version v3.6.3 connecting to: mongodb://127.0.0.1:27017 MongoDB server version: 3.6.3
Server has startup warnings: ...
help
db.help() help on db methods
...
exit quit the mongo shell
Since mongodb is available for di erent languages, make sure that you are looking at the nodejs documentation.
• You can get a connection to a mongodb server using the mongo client’s asynchronous connect() method.
• Once you have a connection to a server, you can get to a speci c database using the synchronous db() method.
• From a database, you can get to a speci c collection using the synchronous collection() method.
• Given a collection, you can asynchronously insert into it using the insert*() methods. However, the code for using insert*() to do replacements can get complicated. An easier alternative may be to use the asynchronous replaceOne() method with the upsert option.
• Given a collection, you can asynchronously nd() a cursor which meets the criteria speci ed by a query to find(). The query can be used to lter the collection; speci cally, if the lter speci es an _id, then the cursor returned by the find() should contain at most one result.
• Given a cursor, you can modify it using the synchronous sort(), skip() and limit() methods.
• Given a cursor, you can get all its results as an array using the asynchronous toArray() method.
1.5 Hints The following steps are not prescriptive in that you may choose to ignore them as long as you meet all project requirements.
1. Read the project requirements thoroughly. Look at the sample log to make sure you understand the necessary behavior. Review the material covered in class including the user-store example.
2. Look into debugging methods for your project. Possibilities include:
• Logging debugging information onto the terminal using console.log() or console.error().
• Use the chrome debugger as outlined in this article.
The couple of minutes spent looking at this link and setting up chrome as your debugger for this project will be more than repaid in the time saved adding and removing console.log() statements to your code.
3. Consider how you can use mongo to implement this project. to access its sensor type and readings easily. Try to use mongo’s facilities as much as possible; for example, instead of writing code for ltering, design your database objects such that you can use the ltering capabilities of mongo’s nd() method; use the cursor modi cation methods like skip() and limit()
to implement paging within results.
Since opening a connection to a database is an expensive operation, it is common to open up a connection at the start of a program and hang on to that connection for the duration of the program. It is also important to remember to close the connection before termination of the program.
[Note that the command-line program for this project performs only a single command for each program run. Nevertheless, the API provided for Sensors allows for multiple operations for each instance; hence you should associate the database connection with the instance of Sensors.]
4. Start your project by creating a work/prj2-sol directory in the i444 or i544 directory corresponding to your github repository. Change into the newly created prj2-sol directory and initialize your project by running npm init -y. This will create a package.json le; this le should be committed to your repository.
5. Install the mongodb client library using npm install mongodb. It will install the library and its dependencies into a node_modules directory created within your current directory. It will also create a package-¬ lock.json which must be committed into your git repository.
The created node_modules directory should not be committed to git. You can ensure that it will not be committed by simply mentioning it in a .gitignore le. You should have already taken care of this if you followed the directions provided when setting up github. If you have not done so, please add a line containing simply node_modules to a .gitignore le at the top-level of your i444 or i544 github project.
6. Copy the provided les into your project directory:
$ cp -pr $HOME/cs544/projects/prj2/prj2-sol/* .
This should copy in the README template, the index.js, the sensors.js skeleton le and the utility les app-error.js and validate.js into your project directory.
7. You should be able to run the project but all commands will return without any results until you replace the @TODO sections with suitable code.
The provided code does have su cient functionality to get a usage message:
$ ./index.js
usage: index.js MONGO_DB_URL CMD [ARGS...] Command CMD can be add sensor-type|sensor|sensor-data NAME=VALUE... clear clear all sensor data