JSPM

  • ESM via JSPM
  • ES Module Entrypoint
  • Export Map
  • Keywords
  • License
  • Repository URL
  • TypeScript Types
  • README
  • Created
  • Published
  • Downloads 995
  • Score
    100M100P100Q107016F
  • License MIT

node.js resource

Package Exports

  • resource

This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (resource) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

Readme

JavaScript Resource engine

Purpose

The resource library provides an easy way to define schema for JavaScript objects and JavaScript function definitions. Through creating resources, the developer is freed from having to write the same validation and interface boiler-plate code over and over again. Resources are friendly to use and highly introspect-able allowing for very easy reflection.

Features

  • Simple API
  • Resources can persist to multiple datasource types ( Memory / Couch / FS / Mongo / etc..)
  • Provides validation and schema for JavaScript Objects and Functions
  • Resources are highly introspect-able
  • Built-in hook system for resource methods
  • Resources can be CommonJS Modules
  • Resources support NPM dependencies with lazy installs and resource method deferment
  • JSON-Schema compatible
  • Large library of pre-built resources available

Installation

npm install resource

Define a new resource

var resource = require('resource'),
    creature = resource.define('creature');

Add resource properties

creature.property('title');

Add resource properties with JSON-Schema

creature.property('type', { type: "string", enum: ['dragon', 'unicorn', 'pony'], default: "dragon"});
creature.property('life', { type: "number", default: 10 });

Persisting resources to a datasource

//
// Can also persist to 'fs', 'mongo', 'couch', etc...
//
creature.persist('memory');

creature.property('type');

creature.create({ id: 'bobby', type: 'dragon' }, function (err, result) {
  console.log(err);
  console.log(result.id);
  console.log(result.type);
});

Enabling persistence will also add: creature.get, creature.destroy, creature.update, creature.find, creature.all.

Persisting resources with options

//
// The fs datasource uses the "path" property instead of
// "host" and "port"
//
creature.persist({
  type: 'couch',
  host: 'this-is-a-sandbox.iriscouch.com',
  port: 5984,
  username: 'guest',
  password: 'parakeet'
});

Adding resource methods

creature.method('poke', function () {
  return 'poked';
});

Adding resource methods with JSON-Schema for arguments

var talk = function (text) {
  var result = {
    text: text
  }
  return result;
}
creature.method('talk', talk, {
  "description": "echos back a string",
  "properties": {
    "text": {
      "type": "string",
      "default": "hello!",
      "required": true
    }
  }
});

Adding resource methods with complex JSON-Schema arguments

var fire = function (options, callback) {
  var result = {
    status: "fired",
    direction: options.direction,
    power: options.power
  };
  return callback(null, result);
}
creature.method('fire', fire, { 
  "description": "fires a lazer at a certain power and direction",
  "properties": {
    "options": {
      "type": "object",
      "properties": {
        "power": {
          "type": "number",
          "default": 1,
          "required": true
        },
        "direction": {
          "type": "string",
          "enum": ["up", "down", "left", "right"],
          "required": true,
          "default": "up"
        }
      },
      "callback": {
        "type": "function",
        "required": true
      }
    }
}});

Using resource.before() and resource.after() hooks

creature.persist('memory');

creature.before('create', function (data, next) {
  console.log('before creature.create')
  data.id += '-a';
  next(null, data)
});

creature.after('create', function (data, next) {
  console.log('after creature.create')
  data.foo = "bar";
  next(null, data);
});

creature.create({ id: 'bobby' }, function (err, result) {
  console.log(err, result);
  // OUTPUTS: null { id: 'bobby-a', foo: 'bar' }
});

Exporting a resource in a module

exports.creature = creature;

Setting NPM Dependencies in a resource

Uses same syntax as npm package.json

exports.dependencies = {
  "colors": "*"
};

Use resources in an application

resource.use() intelligently loads resources and can lazily install required npm dependencies while deferring resource method invocation.

//
// resource.use() is the preferred way to load resources
//
var resource = require('resource'),
creature = resource.use('creature');
//
// node's built-in require() will also work,
// but is not preferred over resource.use()
//
var creature = require('./creature');

Additional Resources

Additional resources are available at https://github.com/bigcompany/resources

Tests

npm test