DailyJS

DailyJS

The JavaScript blog.


TagORM
Featured

database ORM iojs frameworks ES6 node express

Node Roundup: io.js 2.2.1, firenze.js, Express Happiness

Posted on .

io.js 2.2.1

io.js 2.2.1 has been released. The notable change in this release is the switch back to this.client in the IncomingMessage constructor in the http core module. The original change broke compatibility with request, which is used by npm. The io.js contributors found out about it by running make test-npm. There's an interesting discussion about it in the io.js pull requests.

firenze.js

Fahad Ibnay Heylaal sent in firenze.js (GitHub: fahad19/firenze, License: MIT, npm: firenze), an ORM for SQL written in ES6. It's built with Babel so it should work with Node 0.10.x and 0.12.x.

The API is promise based:

var posts = new Posts();  
posts.find('first', {  
  conditions: { id: 1 }
}).then(function(post) {
  var title = post.get('title');
  var postObject = post.toObject();
  var title = postObject.title;
});

To define a schema, you first describe a collection (table) and then a model:

var Posts = db.createCollectionClass({  
  table: 'posts',
  modelClass: function () {
    return Post;
  }
});

var Post = db.createModelClass({ // or db.Model()  
  alias: 'Post',
  collectionClass: Posts,
  schema: {
    id: { type: 'integer' },
    title: { type: 'string' },
    body: { type: 'text' }
  }
});

But you can also use cool ES6 syntax like this:

class Post extends f.Model {  
  constructor(attributes = {}, extend = {}) {
    super(attributes, extend);
  }
}

The project has a lot more methods for querying and saving data -- take a look at the project's homepage to see the full documentation.

Express Happiness

Express Happiness (GitHub: andreas-trad/express-happiness, License: WTFPL, npm: express-happiness) by Andreas Trantidis is a framework built on Express that offers the following features:

  • A JSON route tree
  • Strictly defined, centralised error handling
  • Route permissions
  • Parameter validation
  • Automatic REST API documentation generation
  • Data mocking

Validation is automatic, which means each parameter a route receives is defined in terms of the type. The route tree looks like what you might expect: a list of paths with HTTP verbs that map to RESTful methods. Routes can also be dynamic so you can include parameters.

Because the error handling is all in one place it's a lot easier to manage than a basic Express application. You can easily define error handlers for 404, 500, and any other error codes, and the framework will send the right thing back to the client.

The validation performed by Express Happiness is done through validator, which is a popular and well-maintained validation module.

Featured

testing databases node modules strings ORM

Node Roundup: Buster.JS, Word, Persist

Posted on .

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

Buster.JS

A reader sent in Buster.JS (GitHub: busterjs, npm: buster, License: BSD) by Christian Johansen and August Lilleaas, which is a new test framework for Node and browsers. It's made up of a large set of modules that support everything from CommonJS assertions to JsTestDriver. The entire suite of modules can be viewed in the Buster.JS module documentation.

Asynchronous testing is supported through a callback function, similar to other frameworks, and promises. Any object with a then method is considered a promise:

function someAsyncTestFunction() {  
  var promise = {
    then: function (callback) {
      this.callbacks = this.callbacks || [];
      this.callbacks.push(callback);
    }
  };

  setTimeout(function () {
    buster.assert(true);
    var callbacks = promise.callbacks || [];

    for (var i = 0, l = callbacks.length; i < l; ++i) {
      callbacks[i]();
    }
  }, 100);

  return promise;
}

Buster.JS supports setup and teardown methods, which can be nested inside groups of tests. Setup and teardown methods can also be asynchronous.

Tests can be written with TDD or BDD syntax, and several reporters are included, including a XML (which should work with test automation tools).

Buster.JS overview includes highlights and examples for the major features of the framework.

Word

Word (License: MIT, npm: word) by Veselin Todorov is a string library. It has methods for stripping slashes, stripping quotes, random strings, auto HTML links, and a lot more. It plays nicely with Express, so if you want to use these as helper methods then just call app.helpers({ word: word });

Persist

Persist (npm: persist, License: MIT) by Joe Ferner and Jeff Kunkle is a new ORM framework that supports SQLite, MySQL, PostgreSQL, and Oracle. Models and their relationships are defined using a chainable API. This example is from the project's documentation:

var persist = require('persist')  
  , type = persist.type;

// define some model objects
var Phone = persist.define('Phone', {  
  'number': type.STRING
});

var Person = persist.define('Person', {  
  'name': type.STRING
}).hasMany(this.Phone);

persist.connect({  
  driver: 'sqlite3',
  filename: 'test.db',
  trace: true
}, function(err, connection) {
  Person.using(connection).all(function(err, people) {
    // people contains all the people
  });
});

As you can see, the Phone and Person models are defined in a fashion reminiscent of popular Node ODMs like Mongoose. The hasMany method sets up a relationship between the two models, and this also supports a through option for more complex join models.

The connection.chain(chainables, callback) method is one of Persist's interesting features: it can take an array of chainable queries in sequence. This solves a common Node control flow issue without having to use a control flow library (or lots of nested callbacks).