The JavaScript blog.


databases node modules mongo sql mongoose

Node Roundup: Caterpillar, squel, mongoose-currency

Posted on .

You can send in your Node projects for review through our contact form.


Benjamin Lupton sent in Caterpillar (GitHub: bevry / caterpillar, License: MIT, npm: caterpillar), which is a logging system. It supports RFC-standard log levels, but the main reason I thought it was interesting is it's based around the streams2 API.

By piping a Caterpillar stream through a suitable instance of stream.Transform, you can do all kinds of cool things. For example, caterpillar-filter can filter out unwanted log levels, and caterpillar-human adds fancy colours.


I was impressed by Brian Carlson's sql module, and Ramesh Nair sent in squel (GitHub: hiddentao / squel, License: BSD, npm: squel) which is a similar project. This SQL builder module supports non-standard queries, and has good test coverage with Mocha.

Ramesh has included some client-side examples as well, which sounds dangerous but may find uses, perhaps by generating SQL fragments to be used by an API that safely escapes them, or for generating documentation examples.


mongoose-currency (GitHub: catalystmediastudios / mongoose-currency, License: MIT, npm: mongoose-currency) by Paul Smith adds currency support to Mongoose. Money values are stored as an integer that represents the lowest unit of currency (pence, cents). Input can be a string that contains a currency symbol, commas, and a decimal.

The Currency type works by stripping non-numerical characters. I'm not sure if this will work for regions where numbers use periods or spaces to separate groups of digits -- it seems like this module would require localisation support to safely support anything other than dollars.

Paul has included unit tests written with Mocha, so it could be extended to support localised representations of currencies.


node mongo number mongoose

Node Roundup: Knockout, bignumber.js, r...e, Mongoose-Filter-Denormalize

Posted on .

You can send in your Node projects for review through our contact form or @dailyjs.

Node.js Knockout

Node.js Knockout site

Node.js Knockout is currently being judged, the winners will be announced on the 20th of November. The site is actually a small game in itself -- click around to move your character and type to say something.


bignumber.js (License: MIT Expat, npm: bignumber.js) by Michael Mclaughlin is an arbitrary-precision arithmetic module. It works in Node, browsers, and is available as an AMD module. It comes with both tests and benchmarks, which is useful because one of Mile's goals was to write something faster and easier to use than JavaScript versions of Java's BigDecimal.

Objects created with BigNumber behave like the built-in Number type in that they have toExponential, toFixed, toPrecision, and toString methods.

Mike found the only other serious arbitrary-precision library for decimal arithmetic on npm is bigdecimal, which originates from the Google GWT project. Mike has written some examples of bigdecimal's problems to illustrate bugs he found while working with it, and offers bignumber.js as an alternative.


r...e (License: MIT, npm: r...e) by Veselin Todorov is a module for manipulating range expressions. Ranges are specified as separate arguments or strings, and a suitable array will be returned:

range(1, 3).toArray();  
// [1, 2, 3]

range('a', 'c').toArray();  
// ['a', 'b', 'c']

Stepped ranges are supported (0, 10, 5) as well as convenience methods like range(1, 3).include(2), map, join, sum, and so on. It works in browsers, and includes Mocha tests.


Mongoose-Filter-Denormalize (License: MIT) by Samuel Reed is a filtering and denormalization for Mongoose -- it essentially provides a way of preventing Mongoose from accidentally exposing sensitive data:

UserSchema.plugin(filter, {  
  readFilter: {
    'owner' : ['name', 'address', 'fb.id', 'fb.name', 'readOnlyField'],
    'public': ['name', 'fb.name']
  writeFilter: {
    'owner' : ['name', 'address', 'fb.id', 'writeOnlyField']
  // 'nofilter' is a built-in filter that does no processing, be careful with this
  defaultFilterRole: 'nofilter',
  sanitize: true // Escape HTML in strings

Now when passing the result of a findOne or other query to, say, res.send in your Express app, fields can be restricted based on user:

User.findOne({ name: 'Foo Bar' }, User.getReadFilterKeys('public')), function(err, user) {  
  if (err) next(err);
  res.send({ success: true, users: [user] });