Saturday, December 31, 2011

Weekend Project: Mongoose

I've been playing around with Node, Mongo, and Express--all parts of a full-stack Javascript (JS) framework. I'm truly enjoying it, though I do have to say sometimes the guys building this stuff are just way over the top in terms of abstractions and complexity. Keep it simple, guys :)


JS is known as the language for web pages. But that wasn't always the case; ChiliSoft and Microsoft used Javascript in ASP, and Netscape had a crappy implementation in Livewire.

But now it's back and much better. The reason I'm interested is that I know Javascript pretty well--I'd say I'm proficient. It gives me the ability to code the web application from the server to the database to the browser or mobile device in a single language.

And that "full-stack", single-language programming means I don't have to learn new syntax, and switch from one to another, which is tough for old guys like me.


Mongoose is an ORM for Mongodb. If you're not a programmer, here's the basics: Node.js is a 3-year-old web server written in the C language, and it's super fast. But the sweet thing is that it allows anyone to build applications using the very commonly known Javascript.

Mongo is a database that can be coded to in any language, but it works with data structured like Javascript Object Notation (JSON), making it very easy to work with as a JS developer, though it's not the only or primary reason.

Express is a Javascript framework for building web applications that run on Node; it's a lightweight Model-View-Control (MVC) framework. You don't have to use this to build apps in Node, but it's very useful once you get the hang of it.  

So Mongoose, this weekend's project.

Mongoose makes working with Mongo in Javascript much easier. Mostly. Mongo is already pretty easy to work with, but if you're coming from traditional SQL databases, you think more in terms of schema than in structured documents.

Mongoose adds the M in MVC; it gives you the ability to create and use data Models based on Schema you define, and then use those models in your Express/Node apps.

For the uninitiated, a schema defines, effectively, your column names in a database table. Mongo doesn't have tables, though, it has collections of documents, and those documents within the collections can have varying numbers of columns/fields, which can make it very flexible but also maddening if you are expecting certain fields in a document that aren't there and others are, which can happen in multi-developer environments. And single-developer environments, too, by choice or omission.

So it gives your data some structure--a model is basically an object based on your "table" design that you can use with consistency in your app.

There is one gap I've come across that's driving me nuts, though: you can't check to see if a value already exists in a record in the database.

Let's say you are signing up for a new service on the web. You give it your name and email, and it checks to see if that email address is already in use. If it's not, it adds you to the database and moves you along. If the email's already there, it tells you and prevents a second registration with the same email address, and sends you to a login page.

The part where it checks to see if the email address is already in use doesn't seem obvious to me. And after some research, I can say I believe it's the case that you can't check for a value and get a true/false answer back from Mongo.

Instead, you can "upsert"; meaning when it checks for the email address, it updates the record if it exists, and if it doesn't exist, it inserts the record. But I want the option of not inserting it. I might not be ready just yet to add that, so now I have a maintenance problem.

I'm new to Mongoose and Mongo, so maybe someone out there can give me a pointer. I'm going to add the maintenance overhead so I can keep moving forward with my app, but I'd love to find that I'm wrong and just need to look with my good eyes, as Mom says.

And for anyone who's made it this far in the post, there really must be something better to do today :) but if there's not, go check out Node, Express, Mongo, and Mongoose.

No comments:

Post a Comment