Created
October 14, 2013 19:53
-
-
Save mlconnor/6981173 to your computer and use it in GitHub Desktop.
A mongodb helper for handlebars
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* MongoDB queries - http://mongodb.github.io/node-mongodb-native/markdown-docs/queries.html */ | |
hbs.registerAsyncHelper('mongo', function(options, opt2) { | |
console.log('args', arguments); // hash, inverse, fn, data | |
// console.log('name', name, "context", context); | |
// cb("async!!!!!"); | |
var queryOptions = {}; | |
if ( U.has(options.hash, "limit") && options.hash.limit.match(/^\d+$/) ) { | |
queryOptions.limit = parseInt(options.hash.limit); | |
} | |
if ( U.has(options.hash, "skip") && options.hash.limit.match(/^\d+$/) ) { | |
queryOptions.skip = parseInt(options.hash.skip); | |
} | |
var fields = U.has(options.hash, "fields") && options.hash.fields.trim.length > 0 ? options.hash.fields.split(/\s*,\s*/) : []; | |
var cursor = globalDb.collection(options.hash.collection).find({}, fields, queryOptions); | |
if ( U.has(options.hash, "sort") ) { | |
var cursorSort = []; | |
var sortFields = options.hash.sort.split(/\s*,\s*/); | |
U.each(sortFields, function(field) { | |
cursorSort.push(field.split(/\./)); | |
}); | |
console.log('adding sort', cursorSort); | |
cursor.sort(cursorSort); | |
} | |
// sort, limit, skip | |
cursor.toArray(function(err, items) { | |
console.log('query complete', err, items); | |
opt2(JSON.stringify(items)); | |
}); | |
//Q.fcall(dbCall) | |
//return "fail"; | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment