npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

resource-juggling

v0.0.3

Published

Easy REST for JugglingDB ORM and Express

Downloads

4

Readme

resource-juggling: Easy REST for JugglingDB and Express

Note: resource-juggling is still in a quite early stage of development.

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'

Set up your JugglingDB models:

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

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

Comment = schema.define 'Comment',
  author:
    type: String
  content:
    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.

Routing

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.