Skip to content

Instantly share code, notes, and snippets.

@recidive
Created January 20, 2014 17:03
Show Gist options
  • Select an option

  • Save recidive/8524078 to your computer and use it in GitHub Desktop.

Select an option

Save recidive/8524078 to your computer and use it in GitHub Desktop.

Bring structure to Node.js applications with Prana

Prana is an innovative microframework created by Henrique Recidive that combines an ODM system with an extensions/plugins system to provide a robust and flexible base for creating applications and frameworks of any kind.

Prana has a highly dynamic unified API for interacting with resources, so you can manipulate everything in your application being it settings, application metadata or user submitted content.

By creating a new type in Prana it automatically creates and return a model object that you can use and interact with it right away.

// Create the application.
var application = new Prana();

// Create the type 'Post'.
var Post = application.type('post', {
  title: 'Post',
  description: 'A blog post.'
});

// Create a post.
var post = new Post({
  name: 'some-post',
  title: 'A test post',
  body: 'A simple test post.'
});

// Save the post.
post.save();

Prana also provides a way for extensions to alter each other resources through hooks. You can create an extension and implement hooks which will be called at the right time, allowing you to alter existing resources and also to add new ones.

// Extension prototype object.
var myExtensionPrototype = {
  // The post() hook.
  post: function(posts, callback) {
    // 'posts' will have all previously added posts.
    for (var postName in posts) {
      // Add a new property 'someProperty' to every post.
      posts[postName].someProperty = 'some value';
    }
    callback();
  }
};

// Add an extension programmatically.
application.extension('my-extension', {
  title: 'My Extension',
  description: 'This is just an example extension.',
  prototype: myExtensionPrototype
});

// Initialize all extensions.
application.init(function(extensions, types) {
  console.log('%d extensions loaded', Object.keys(extensions).length);
});

With a simple method call, it can get data from JSON files, hook implementations and the storage, and aggregate them into a single data container object, so it makes it easy for the applications or extensions to ship with default resources and also ensures everything on your application can be extended on an uniform manner.

Post.list({}, function(error, posts) {
  // Do something with posts.
  console.log(posts);
});

Prana has a built in event system, so every action on resources of any type will trigger asynchronous events.

// Global event listener.
application.on('save', function(type, item) {
  console.log('Global save event fired. Item type: %s.', type.name);
});

// Type specific event listener.
Post.on('save', function(item) {
  console.log('Type specific save event fired.');
});

Prana comes with a built in Memory Storage and also there's a MongoDB Storage that you can download and plug in.

The code is elegantly crafted and thoroughly documented. Its concepts are very easy to grasp, you can use it to add consistency and extensibility capabilities to your Node.js application.

The author is using Prana in his new web application framework called Choko, for all its data persisting, variables/settings handling and extensions system.

For more examples on how to use Prana you can check the examples folder on the project's source code on GitHub.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment