Thursday, May 19, 2011

rel: Arel-inspired SQL query builder for Node.js

Wynn sat down with Nick Quaranto at Red Dirt Ruby Conference to talk about Gemcutter, RubyGems.org, and how to get started creating your own Ruby gem.

Arguably, Arel was one of the biggest new features introduced in Rails 3. Arel simplifies building complex SQL statements using idiomatic Ruby.

With Rel, Carl Woodward brings the power of Arel to Node.js. Written in CoffeeScript, Rel makes quick work of building SQL statements for a variety of relational databases.

Rel can be installed via npm:

npm install rel

We can then begin building a query:

users = new Rel.Table 'users'

If we want all users in our CMS we could use the star method:

users.project(Rel.star()).toSql()

Rel really shines, however, when using several chained operators, including joins:

users.join(photos).on(users.column('id').eq(photos.column('user_id')))# => SELECT * FROM users INNER JOIN photos ON users.id = photos.user_id

For a complete list of features, check out the very readable Vows-based specs in the repo on GitHub.

[Source on GitHub]

No comments:

Post a Comment