This repository has been archived by the owner. It is now read-only.
CRUD in Node.js made easy
CoffeeScript JavaScript
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Failed to load latest commit information.

resource-juggling: Easy REST for JugglingDB and Express

resource-juggling is a utility that connects two excellent Node.js libraries:

The motivation for doing this is to improve the experience of building CRUD applications, an area where Node.js has traditionally been perceived as weak. With resource-juggling, doing Create-Read-Update-Delete becomes as easy as:

Import the dependencies:

{Schema} = require 'jugglingdb'
express = require 'express'
resource = require 'express-resource'
resourceJuggling = require 'resource-juggling'

Note: examples in this README are in CoffeeScript, but the library works just fine also with plain-JavaScript Node.js applications.

Set up your JugglingDB models:

# We use Redis in this example
schema = new Schema 'redis', {}

# Define our model(s)
Post = schema.define 'Post'
    type: String
    length: 255
    type: Schema.text

Comment = schema.define 'Comment',
    type: String
    type: Schema.text

# Define some relationships
Post.hasMany Comment,
  as: 'comments'
  foreignKey: 'postId'

Comment.belongsTo Post,
  as: 'post'
  foreignKey: 'postId'

Note: In this example we're only showing two models and a relation between them. You can of course add as many models and as deep relationships as you need for your application.

Set up your web server:

app = express.createServer()
app.listen 8080

Create routes for your models:

# Posts are on top level
posts = app.resource resourceJuggling.getResource
  schema: schema
  name: 'Post'

# Comments are under posts, so we use request.Post.comments
# as the collection
comments = app.resource 'posts', resourceJuggling.getResource
  schema: schema
  name: 'Comment'
  collection: (request) -> request.Post.comments
posts.add comments

...and that is it! You have just defined a content model with Posts and Comments, and built all the necessary RESTful routes for them.

Get resource-juggling with:

$ npm install resource-juggling


Now you can find all the posts with a GET to /, or add a new one with a POST to the same URL. Individual posts are accessible for GET, PUT, and DELETE at /{postId}.

Similarly, comments to posts will be available under /{postId}/comments, and individual comments at /{postId}/comments/{commentId}.

By default all routes can serve both JSON and HTML. For HTML, however, you must create some templates. Templates can be in any format supported by Express, and should be placed in:

  • views/{model}/index.{ext} for the listing template
  • views/{model}/show.{ext} for the template showing individual items

So, for a comment and Jade templating these would be views/Comment/index.jade and views/Comment/show.jade.

Setting URL prefixes to resources

There are situations where you want to have custom URL prefixes for various resources. This can be done easily by passing the prefix as the base argument when instantiating a resource.

For example:

server.resource 'user', resourceJuggling.getResource
  schema: schema
  name: 'User'
  base: '/system/'

In this situation users index would be located in /system/user, an individual user at /system/user/1 etc.