Noding From 0-60 with Stacey Mulcahy, Rami Sayar and MVA
Today, July 29th, Microsoft Virtual Academy had in store a great course, delivered masterfully by Stacy and Rami. It was a 7 hour event! During seven hours these fellows were talking about Node.js, Express, databases with MongoDB, debugging and deploying Node.js and extending Node.js with Azure and Web Jobs; wow!. This course has all it takes to become popular at MVA!
During the first hour Stacey and Rami went through the obligatory introduction to Node.js layering good arguments for event driven programming, asynchronous vs synchronous (blocking), the classic "Hello Node", etc. They did all these, and much more, using Visual Studio Code, a very sweet editing tool which I think is going to give The Others Big Ones (Sublime, WebStorm, Notepad++, ect) a good run for their money! Yeah, Visual Studio Code is as light as pancakes and ready to give you a sugar high! Go get it boys and girls!
| Visual Studio Code; Note the GIT icon it is telling me that the status of the Git repository is 5 new changes. Sweet! |
After the first hour of the course, still an introduction to many different Node related subjects, the event picked up speed and intensity. I do not intend to echo here anything that Stacey and Rami talked about nor I would change one word, but would like to add my two cents.
MongoDB Without Mongoose is Like Hot Chocolate without Marshmallows!
I just think that they needed five to ten more minutes to really complete the material by adding Object Modeling with Mongoose to their MongoDB + Node example (chapter 13). I feel that using Mongoose changes the nomenclature of the subject and completes it; although I understand that after 7 hours of talking and explaining and running demos 5 more minutes would feel like an eternity!Mongoose provides a simple schema based solution to help us model our application data and includes built-in types, query building, validation and much more just out of the box. Built-in queries help us filter, and find data very easily. Let me show you what I mean; but first you will need to install Mongoose.
To install Mongoose you need to run on the command line:
npm install mongoose --save
The save flag modifies the package.json for Chapter 13 of the course and adds to it a new module; Mongoose.
Mongoose Schema: Building the Model
With the help of Mongoose the following code defines two entities for our system: Account and BankData. I am using the same definitions that the course lays out and only providing object data modeling as an add-on.
Note that BankData has a collection of Accounts which is encapsulated by the "acconuts" array property on bankDataModel. Note that the model contains an Account and BankData defined by Mongoose. To add this Has-a collection of accounts to BankData Mongoose tells us that we need to define an Account Schema. Take a look at how it is done from line 6 to 10; then on line 15 BankData Schema describes the collection of accounts.
Lines 19 and 20 tells us how this mapping is done by Mongoose and we build a model containing our Mongoose object models. Finally the script exports this model so it can be required from the application.
Mongoose it or Loose it
We are now ready to consume the model. Line 3 requires it. Note that now this app does not deal with MongoDB at all; Mongoose is doing all the heavy lifting for saving, finding, querying. The only thing we need to do is provide the URL for the connection to the database. This is assuming that MongoDB is installed and running on your system.The above script is just demonstrates how to use the model to Create, Read (find), Update, Delete (CRUD) using Mongoose. You can compare this code to the code provided at the course to realize how sweet Mongoose is. Some facilities provided:
- model.save(callback): saves the entity model to the doc database. save takes a callback. After the item has been saved the _id of the item is set.
- model.find(callback): Finds all items. find also takes a query argument which is used by Mongoose to filter and a callback taking this form model.find(query, callback).
- model.findById(id, callback). Do I need to say more?
- model.remove(callback). You know what this does.
My two cents are that adding object modeling with Mongoose to MongoDB for Node facilitates our work, adds semantics and helps our code concentrate on our business cases. And this concludes my little tribute to this MVA magnificent course. This is not, by a long shot, all that Mongoose has to offer and I would not be surprised if I find myself writing more about this in the future.
No comments:
Post a Comment