DailyJS

DailyJS

The JavaScript blog.


Tagmongo
Featured

mobile node modules npm mongo generators libraires

Orientation and Vibration APIs, Mongomery

Posted on .

Orientation and Vibration APIs for Games

Andrzej Mazur sent in an article about using the orientation and vibration APIs for game development:

The Device Orientation API specification is still in the Working Draft status, so there might be some code-breaking changes introduced in the future. It's an experimental technology and should be treated as such. All implementations are still missing the compassneedscalibration event. Also, there are differences in browser implementations which should be taken into consideration. For instance, Chrome and Opera don't support the devicemotion event.

The Vibration API is another feature that can boost the mobile experience. Most devices support it (except iOS Safari and Opera Mini), and it works great on Firefox OS devices.

I've never used the vibration API, but apparently you can access it with window.navigator.vibrate. It even supports an array of values for vibrating multiple times. Andrzej's argument is that you can use these APIs to differentiate your game from other mobile web games, and while this is true it'll take a fair bit of experimentation to get orientation-based controls working well!

Mongomery

Coderaiser has sent in a few generator-based modules recently, but I missed Mongomery (GitHub: coderaiser/node-mongomery, License: MIT, npm: mongomery), a module that uses generators for MongoDB. It uses ruff, which was Coderaiser's module for giving generators a more EventEmitter-inspired API:

var mongomery = require('mongomery');

mongomery(function*(mongo) {  
  var url = 'mongodb://localhost:27017/myproject',
  var db = yield mongo.connect(url),
  var collection = db.collection('mongolog'),
  var docs = yield collection.find({}).toArray();

  docs.forEach(function(item) {
    console.log(item);
  });

  db.close();
}).on('error', function(error) {
  console.log(error.message);
});

It'll take us a few years to really understand how new features like generators should be used in terms of databases, but I'm very interested in using generator syntax for databases.

An alternative that I've enjoyed experimenting with is Mongorito, which is more of an ODM-style API. The syntax is more succinct than Mongoose, while still remaining familiar.

Featured

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.

Caterpillar

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.

squel

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

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.

Featured

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

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

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();  
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

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] });
});

Featured

databases node modules mongo documentation async markdown

Node Roundup: MongloDB, parseq.js, node-netpbm

Posted on .

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

MongloDB

MongloDB Logo

MongloDB (GitHub: onglo / MongloDB, License: MIT) by Christian Sullivan is a database written with JavaScript that's compatible with MongoDB's queries. It has a plugin system for persistence, and a datastore for Titanium Mobile -- this effectively allows a form of MongoDB to be used within iOS and Android applications.

Monglo has a DataStore API that can be used to persist data locally or remotely. It's based around an object that implements each CRUD operation:

var monglo = require('./index').Monglo  
  , db = monglo('DemoDB')
  ;

function DemoStore(){  
  return {
     insert: function() {}
   , update: function() {}
   , open: function() {}
   , remove: function() {}
   , all: function() {}
  };
}

db.use('store', new DemoStore());  

parseq.js

parseq.js (GitHub: sutoiku / parseq, License: MIT, npm: parseq) from Sutoiku, Inc. is a flow control library for organising parallel and sequential operations. To manage asynchronous operations, this can be passed. If several calls are made, then this() can be passed, and the next function will receive an array that contains the results in the order they were called.

The same author also recently released jsdox, which is another JSDoc to Markdown generator.

netpbm

netpbm (GitHub: punkave / node-netpbm, License: MIT, npm: netpbm) by Tom Boutell scales and converts images using the netpbm toolkit, which is a venerable set of graphics programs found on many Unix systems.

This library is a wrapper around the netpbm binaries, and takes advantage of the fact that most netpbm programs only read one row of pixels at a time into memory to keep memory usage low.

Featured

libraries node modules mongo filesystem

Node Roundup: mongo-lite, smog, sshfs-node

Posted on .

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

mongo-lite

mongo-lite (GitHub: alexeypetrushin / mongo-lite, License: MIT, npm: mongo-lite) by Alexey Petrushin aims to simplify MongoDB by removing the need for most callbacks, adding reasonable defaults like safe updates, and offering optional compact IDs.

The chainable API looks more like MongoDB's command-line interface:

var db = require('mongo-lite').connect('mongodb://localhost/test', ['posts', 'comments']);  
db.posts.insert({ title: 'first' }, function(err, post) {  
  // Use post
});

There's also a Fiber-based API, so it can be used in a synchronous fashion.

smog

smog (License: MIT, npm: smog) from Fractal is a web-based MongoDB interface. It displays collections, and allows them to be sorted and edited. It also supports administration features, like shutting down servers, CPU/bandwidth usage graphs, and replica set management.

It's built with Connect, and there's an experimental GTK+ desktop interface made with the pane module by the same authors.

sshfs-node

sshfs-node (License: MIT, npm: sshfs-node) by Charles Bourasseau allows remote filesystems to be mounted using SSH. It uses sshfs and requires keys for authentication, rather than passwords.

It comes with Vows tests, and the same author has also released fs2http.