fortune

High-level I/O library for web applications.

Fortune is a high-level I/O library for web applications.

View the website for documentation. Get it from npm:

$ npm install fortune@latest --save

Currently in release candidate stage. Things may break, check the changelog.

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.

Fortune is licensed under the MIT license.