Archive for April 2014

MongoDB “Eager Fetching” Workaround in GeddyJS

As you all know, there’s no such thing as a JOIN in MongoDB. The burden of applying logic is on the application side.

SQL Adapters (like MySQL and PostgreSQL) have an eager fetching solution from the GeddyJS Model ORM. Eager fetching would look like this (taken from the code sample in the GeddyJS tutorial):

this.index = function (req, resp, params) {
  var self = this;

  geddy.model.ToDo.all({}, {includes: 'steps'}, function(err, toDos) {
    if (err) {
      throw err;
    }
    self.respondWith(toDos, {type:'ToDo'});
  });
};

This would definitely work for an SQL Adapter.. but what about MongoDB with linked collections? The GeddyJS dev team might put this feature up in the future but it has yet to be done.

So let’s do a workaround for now! Let’s assume that we have two separate models (and collections) named ToDo and Step. Read more

GeddyJS, MongoDB, and Querying Subdocs? Try Mongoose

For the past few months I’ve been working on studying NodeJS. Currently, I’m trying to work with the GeddyJS Framework (geddyjs.org). Now they have a database agnostic ORM built into it that can support the likes of MySQL and PostgreSQL but also MongoDB and even flat files.

Unfortunately, there are some things that can’t work very well with the built in ORM (like MongoDB though it does have some functionality) and you might end up on your own there while some things aren’t supported. Now it’s still possible to use a different ORM so don’t npm remove yet!

For example, you have a schema that looks like this:

var Deck = function () {

  this.defineProperties({
    title: {type: 'string', required: true},
    cards: {type: 'object', objectProp: [{name: 'string', type: 'string', id: 'string'}]}
  });

...

Now let’s say you wanted to find all Decks that had a certain card name in it. You can’t use the built in Geddy Model ORM for that yet.

Come in MongooseJS (mongoosejs.com). Actually you can use any other ORM for that matter but for simplicity’s sake let’s use Mongoose. Read more