JSPM

  • ESM via JSPM
  • ES Module Entrypoint
  • Export Map
  • Keywords
  • License
  • Repository URL
  • TypeScript Types
  • README
  • Created
  • Published
  • Downloads 953
  • Score
    100M100P100Q106791F
  • 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

Resource engine

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.

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