Fork on GitHub Piggu Fortune.js

Fortune.js is a high-level I/O library for web applications, in the server and the browser. It provides an integrated feature set for working with data. The latest version is 1.0.0-rc.1, get it from npm:

npm install fortune@latest --save

Example

Let's build an API that models Twitter's basic functionality:

import fortune from 'fortune'
import http from 'http'

const store = fortune.create()

// The `net.http` function returns a listener function which does content
// negotiation, parses headers, and maps the response to an HTTP response.
const server = http.createServer(fortune.net.http(store))

store.defineType('user', {
  name: { type: String },

  // Following and followers are inversely related (many-to-many).
  following: { link: 'user', inverse: 'followers', isArray: true },
  followers: { link: 'user', inverse: 'following', isArray: true },

  // Many-to-one relationship of user posts to post author.
  posts: { link: 'post', inverse: 'author', isArray: true }
})

store.defineType('post', {
  message: { type: String },

  // One-to-many relationship of post author to user posts.
  author: { link: 'user', inverse: 'posts' }
})

store.connect().then(() => server.listen(1337))

This yields a hypermedia API that conforms to the Micro API and JSON API specifications. The default serializers emit an index route with hyperlinks and respond to OPTIONS requests appropriately, so that clients which understand the media types can consume the API without technical documentation. The JSON API serializer emits routes specified here.

By default, it is backed by an embedded document store, NeDB, which runs in memory by default, but has options to persist to disk. There are adapters for other databases such as MongoDB and Postgres, enumerated in the plugins page.