flyvictor / fortune

A framework for prototyping hypermedia APIs.
http://fortunejs.com
MIT License
4 stars 18 forks source link

Fortune.js Build Status

Hello nerds. Fortune is a web framework for prototyping hypermedia APIs that implement the JSON API specification. It comes with a modular persistence layer, with adapters for NeDB (built-in), MongoDB, MySQL, Postgres, & SQLite.

Get it by installing from npm:

$ npm install fortune

Contributions welcome.

Changes since fork of daliwali/fortune

This is not a complete list but it should cover most of the changes. There may also be more commits which are not linked here. I recommend looking at the tests and commit log if you are unsure on how to use these features.

Features

Fortune implements everything you need to get started with JSON API, with a few extra features:

It does not come with any authentication or authorization, you should implement your own application-specific logic (see keystore.js for an example).

Custom Types

Custom type defines its own schema and hooks and might be injected to any resource's schema side by side to standard data types and incorporates its data hooks into the resources' ones. Usage example:

   app.customType("date-timezone", {
     date: String,
     timeZone: String
   }).beforeWrite([{
     name: 'datetz2db',
     priority: -1,
     init: function() {
       return function(req, res) {
         return DateTz.todb(this)
       }
     }
   }]).afterRead([{
     name: 'datetz4db',
     priority: 1000,
     init: function() {
       return function(req, res) {
         return DateTz.fromdb(this);
       }
     }
   }]) 

   app.resource("schedule", {
   ...
     arrival: 'date-timezone'
   ...
   })

The datetz2db and datetz4db will be automatically attached to the parent resource hook chain.

Guide & Documentation

The full guide and API documentation are located at fortunejs.com.

Basic Usage

Here is a minimal application:

var fortune = require('fortune');
var app = fortune({ /* debug: true */ });

app.resource('person', {
  name: String,
  age: Number,
  pets: ['pet'] // "has many" relationship to pets
});

app.resource('pet', {
  name: String,
  age: Number,
  owner: 'person' // "belongs to" relationship to a person
});

app.listen(1337);

This exposes a few routes for the person and pet resources, as defined by the JSON API specification:

HTTP Person Pet Notes
GET /people /pets Get a collection of resources, accepts query ?ids=1,2,3...
POST /people /pets Create a resource
GET /people/:id /pets/:id Get a specific resource, or multiple: 1,2,3
PUT /people/:id /pets/:id Create or update a resource
PATCH /people/:id /pets/:id Patch a resource (see RFC 6902)
DELETE /people/:id /pets/:id Delete a resource
GET /people/:id/pets /pets/:id/owner Get a related resource (one level deep)

Unit Testing

Tests are written with Mocha, and are run against the built-in NeDB adapter, plus MongoDB & MySQL on Travis. You will also need to have the developer dependencies installed. To run tests:

$ npm test

Client-side Implementations

Meta

Example services

There are a number of example services in the examples folder. These are easily run, as an example :

For release history and roadmap, see CHANGELOG.md.

Fortune is licensed under the MIT license, see LICENSE.md. #