Day 28 (week 6) — CRUD
CRUD stands for the four common operations when managing a collection of data in a database.
C — CREATE: this is when you want to add a new document to the collection. An example would be if you had a table of famous mathematicians and you wanted to add a new one, like Newton or Cauchy.
R — READ: this is when you want to read one or more of the documents in your collection and present those results to the user.
U — UPDATE: this is when you want to take an existing document in the collection and update some or all of its attributes. For example, you might learn that Isaac Newton had a strong interest in optics and actually published quite a bit in that field of study.
D — DELETE: this is when you want to delete an existing document from the collection. But really, why would you ever want to delete a mathematician? ;-)
Today was dedicated to implementing these four operations in Express with Mongo on the backend. We had two examples from previous projects to work with: our robots directory and our todo list.
It turns out that Promises are coming back — as they are critical in making queries against the database using mongoose. Working with data in a database with an object relational mapper such as mongoose is much simpler than using the native MongoDB driver.
After looking at examples of using mongoose in the morning, we had time to develop our own model (I chose mathematicians) and develop a mongoose schema. After that, we implemented CRUD on the collection and put on an Express front end.
I was pretty happy with my code, and I also added the ability to store image files for each mathematician in the collection. My page ended up looking something like this. I’m only showing one entry in the directory.

You can see that I have update and delete links for each mathematician, as well as a few pieces of information and a photo. I also have a link to their page on Wikipedia. You can also view all mathematicians as well as create your own.
The code for my mathematician directory is here.
The last part of what I was working on today was to get the Create New Mathematician form to accept an uploaded image. I started by trying to use express-fileupload but evidently it doesn’t play nicely with body-parser. Rather than stay up late tonight trying to fix it, I’m going to get some sleep and come back to it later.
The end of this week will be our halfway point. I’m not sure how I feel about it. I love what I’m doing now, and don’t want the class to end. On the other hand, it will be nice to go to work for a company and be focused on delivering a specific product.
