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.
So Mongoose, this weekend’s project.
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.