Friday, August 19, 2011

Creating a Node ExpressJS project on github: MadisonHQ

In this introduction to node, I will be using ExpressJS.  If you have not already done so, install NPM and then as root, "npm install express".


On my dev platform, I create a new Express Project:

express -s madisonHQ

This command creates a new folder "madisonHQ" and adds a skel app.js.  Head to git and create a new repository.


Now, lets get this setup on Git.
cd madisonHQ
git remote add madisonHQ git@github.com:mikekunze/madisonHQ.git
git init
git add .
git commit -m 'first commit'
git push -u madisonHQ master


As you can see here, https://github.com/mikekunze/madisonHQ, my new project is synced and repo'ed.

Depending on your environment, you might need to get the express library added to the project.  This is easy.  Inside the madisonHQ directory:
npm install express

Wednesday, August 17, 2011

MongoDB relational query and data customization

Lets talk about roles.  Roles, for my implementation, represents an application container.  An account may be granted a permission to this role, and run its applications.


For my relationalness, I always create a role permission collection, p_roles, that links references to the account identifier as well as the role identifier.

This is where my last example came in.  This time, however, I want to populate a grid of accounts that do not exist in a role, so that the interface user may add them to it.  This required two things:
  • an array of account ids we want to exclude
  • a 'not in' query
This example might be more complex than it needs to be, but the complexity lies in keeping each forEach loop asynchronous. I needed to customize the returned data because the accounts collection contains password hashes and other information not necessary for the front end user.

var p_roles = mods.mongoose.model('p_roles');
var accounts = mods.mongoose.model('accounts');
var ninAccounts = [];
var inAccounts = [];
p_roles.find({ role_id: query.role_id }, function(err, docs) {
var async = mods.async;
var pushNinAccounts = function(item, callback) {
if(item) {
accounts.findOne({ _id: item.account_id }, function(err, account) {
if(account != null) {
ninAccounts.push(account._id);
callback();
} else callback();
});
}
};
async.forEach(docs, pushNinAccounts , function(err) {
if(err)
console.log(err);
accounts.find({ _id: { $nin: ninAccounts } }, function(err, docs) {
var pushinAccounts = function(item, callback) {
if(item) {
var pushItem = {
description: item.name,
_id: item._id,
role_id: query.role_id
};
inAccounts.push(pushItem);
callback();
} else callback();
};
async.forEach(docs, pushinAccounts, function(err) {
if(err)
console.log(err);
var response = {
success: true,
results: inAccounts.length,
items: inAccounts
}
res.send(response);
});
});
});
});
view raw ninAccounts.js hosted with ❤ by GitHub

Sunday, August 14, 2011

MongoDB and Relational queries? Yes it can!

Hello internet, today I would like to give a little information on a misconception with MongoDB.  NoSQL explicitly denounces relational queries as a thing of the past, however it is necessary to normalize  your data and keep it efficient and avoid having duplicate information.



For my discussion today, I will refer to vendors permissions, which was for a sample purchasing app I demoed inside portalstack.

To do a relational look up, you need to do a bit of async programming.  For me, I used the async library provided by the nodejs community.

First, I query my permissions collection, p_vendors, for an account_id and the vendor_id, both are ObjectID references.

Next, I use async to foreach the returned permission set.  When the async call is complete, it returns the prepared data to ExpressJS and its sent off to the client.


app.get('/vendors.json', function(req, res) {
var session = req.session;
var query = mods.url.parse(req.url,true).query;
var p_vendors = mods.mongoose.model('p_vendors');
var vendors = mods.mongoose.model('vendors');
var results = [];
p_vendors.find({ account_id: session.account_id }, function(err, docs) {
var async = mods.async;
var pushDoc = function(item, callback) {
if(item) {
vendors.findOne({ _id: item.vendor_id, description: new RegExp(query.query, 'i')}, function(err, vendor) {
if(vendor != null) {
results.push(vendor);
callback();
} else callback();
});
}
};
async.forEach(docs, pushDoc , function(err) {
if(err)
console.log(err);
var response = {
success: true,
results: results.length,
items: results
}
res.send(response);
});
});
});

Thursday, August 11, 2011

Installing NodeJS and NPM

I will be installing NodeJS 4.1.0 on ubuntu 11.04.

Requirements

  • sudo apt-get install open-ssl libssl-dev


Get the archive
wget http://nodejs.org/dist/node-v0.4.10.tar.gz

Unpack the archive
tar zxvf node-v0.4.10.tar.gz ; cd node-v0.4.10/


Configure the compiler and install
sudo ./configure && sudo make && sudo make install


Finally, install NPM.  This you must do as root, otherwise it gets messy
sudo su -
curl http://npmjs.org/install.sh | sh