Foxx Dependencies for More Composable Foxx Apps

Previously on the ArangoDB blog we saw how we can use the configuration field in manifest.json to make Foxx apps configurable and more re-usable. This is all well and good if we just want to pass in simple values to a Foxx app but sometimes you want to pass in entire Foxx apps. This is where dependencies come in to save the day.

Let’s say you want to use the session storage provided by the ArangoDB sessions app available on the Foxx app store using Foxx exports and imports. Because you hadn’t yet heard of configurations, you simply hard-coded the mount path of your copy of the sessions app in your code:

var sessions = Foxx.requireApp('/my/sessions/app').sessionStorage;

After finding out about configurations, you decide to make the sessions app’s mount path configurable by modifying your manifest:

{
  // ...
  "configuration": {
    "sessionsAppPath": {
      "description": "Mount path of the sessions app to use for user sessions.",
      "type": "string"
    }
  }
}

And you adjust your code to make use of the configuration value:

var sessionsAppPath = applicationContext.configuration.sessionsAppPath;
var sessions = Foxx.requireApp(sessionsAppPath).sessionStorage;

This is a huge improvement, but not very convenient. Luckily ArangoDB 2.6 also introduces the dependencies field. Let’s replace the configuration with a dependency:

{
  // ...
  "dependencies": {
    "userSessions": "sessions:^1.0.0"
  }
}

You can define dependencies as the name of a Foxx app followed by a colon and a semver-compatible version number or range.

No need to explicitly use Foxx.requireApp in your code anymore: the app will automagically be imported when you access the corresponding dependency on your applicationContext:

var sessions = applicationContext.dependencies.userSessions;

Just like configurations, an app’s dependencies need to be set up before it becomes active. And just like configurations, dependencies can be managed using the web admin frontend, the foxx-manager CLI or the REST API.

Alan Plum, Geschäftsführer Foss & Haas – Software-Architektur & Entwicklung
Alan Plum, Geschäftsführer Foss & Haas – Software-Architektur & Entwicklung

 

Can’t wait for the ArangoDB 2.6 release and want to try the feature today? You can always compile the latest development version of ArangoDB from our GitHub repository.

This article originally appeared on the ArangoDB blog on May 15, 2015.