restposten

Data persistence module for flatiron based on JSON Schema

npm install restposten
16 downloads in the last month

Restposten

Restposten is a data persistence module for flatiron. It's an adaptation of resourceful. You can replace resourceful in your flatiron app by Restposten. It uses JSON Schema draft v4 for data validation. It also has support for JSON Schema links, which are directly mapped to relations in the internal data model. Being a replacement for resourceful you can use it with restful, too.

Installation

npm install restposten

You also have to provide a database engine, a cache and a JSON Schema validator.

Currently, there are two database engines: restposten-mongodb.

npm install restposten-mongodb

The recommended schema validator is JaySchema.

npm install jayschema

API documentation

You can find the API docs at http://n-fuse.github.io/restposten/.

Usage

Simple example

This example uses restposten-mongodb and JaySchema.

var restposten = require('restposten');
var mongo = require('restposten-mongodb');
var JaySchema = require('jayschema');

restposten.validator = new JaySchema();

mongo.connect({ host: 'localhost', port: 27017 }, function (err, db) {
  if (err)
    throw err;

  restposten.database = db;

  // Example schema
  var userSchema = {
    "$schema": "http://json-schema.org/draft-04/schema#",
    "id": "http://example.org/api/v1/user.schema.json",
    "title": "user",
    "description": "System user",
    "required": [ "id", "email" ],
    "properties": {
      "_id": {
        "type": "string"
      },
      "name": {
        "type": "string"
      },
      "email": {
        "type": "string",
        "format": "email"
      }
    }
  };

  // Register a schema for validation.
  // This is only required, if you have schemas that reference each other.
  restposten.validator.register(userSchema);

  // Define the User resource.
  var User = restposten.define('user', userSchema);

  // Create a user, who's missing an email address. This will throw an error.
  User.create({
    "_id": "pvorb",
    "name": "Paul Vorbach"
  }, function (err, u1) {
    if (err)
      throw err;

    console.log(u1);
  });
});

See the JSON Schema specification for more advanced examples and further documentation on JSON Schema.

More advanced examples

You can see more advanced examples in examples/.

Generating the documentation

You need to have JSDoc 3 installed to be able to generate the API documentation files. Once you have JSDoc 3 installed, check out the Repository once again with the name "restposten-pages" into the same directory that contains "restposten". Then you have to checkout the branch gh-pages (git checkout gh-pages). Afterwards, you should be able to run

$ sh generate-restposten.sh

When the script finishes, the documentation in "restposten-pages" should be up-to-date.

Contributors

License

MIT License

npm loves you