Cheerio with Node.js and CoffeeScript | ArangoDB Tutorial

Foxx’ main purpose is to create a beautiful API for your AngularJS, EmberJS or Backbone.js front-end. However, sometimes you want to do more. We, for example, needed to parse some HTML files. ArangoDB is capable of using some Node.js modules, but unfortunately Cheerio was not one of those. One problem was that we did not support loading of JSON data modules. So, this was a good excuse to rewrite the module loader in ArangoDB to make it even more Node.js-friendly.

With those improvements, that are currently available in ArangoDB’s devel branch. You can now also “require” a JSON data file. These files must have a filename ending with “.json”. If the filename ends with “.coffee” it is treated as coffee-script file and automatically compiled into JavaScript.

A Foxx app can now contain its own “node_modules” directory to include Node modules which it requires. This makes it much easier to deploy a Foxx app.

More info...

CoffeeScript in ArangoDB: Feature Preview | ArangoDB 2013

In my blog post about npm packages, I tried to use underscore for ArangoDB. I found that the easiest way to archive this, is using the nodes package manager NPM. Node packages and modules follow the Common.JS specification, so they can be used by ArangoDB.

Why not try to use the package coffee-script as well? Install it using

npm install coffee-script

and that’s it. Unfortunately, CoffeeScript use a module “path”, which is not a CommonJS module. I assume that most of the functionality is part or will be part of the module “fs”. The “path.js” from node.js is simple JavaScript code with some references like

'path.exists is now called `fs.exists`'

Being brave, I simply copied the file into my module path and tried again. The next obstacle is node’s global variable “process”. Luckily, this is only used to check for Windows in the module “path”. Also ignoring the module “vm” used to execute JavaScript code, this allows one to actually load CoffeScript into ArangoDB.

arangosh> process = {};
{ }

arangosh> var cs = require("coffee-script");

arangosh> cs.compile("a = 1\nopposite = true\na = -a if opposite", {});
(function() {
  var a, opposite;

  a = 1;

  opposite = true;

  if (opposite) {
    a = -a;
  }

}).call(this);

So, I can now use CoffeeScript definition within ArangoDB. Some of the loaders must be adjusted to check for both “.js” and “.coffee” files. Afterwards it should be possible, to define an action in CoffeeScript as well as JavaScript.

More info...

ArangoDB: Using JavaScript in the Database | ArangoDB 2012

Jan was invited as a speaker to “Js.Everywhere” in Paris. He talked about using Javascript in a database, well, ArangoDB, to be precise, giving lots of examples on “actions” and related concepts in ArangoDB.

More info...

Using mruby as Alternative: Server-Side | ArangoDB Blog 2012

Note: We changed the name of the database in May 2012. AvocadoDB is now called ArangoDB.

Introduction

One of the design goals of AvocadoDB is:

Use AvocadoDB as an application server and fuse your application and database together for maximal throughput (more…)

More info...

Get the latest tutorials,
blog posts and news: