Data serialization middleware for Node.js and web browsers.

Fortune provides data serialization middleware, from storage up to the presentation. It covers the whole application system including the data access layer (adapter), business logic layer (transform), and presentation layer (serializer). These layers working together allow for multiple data sources to be exposed via multiple formats through a uniform interface.

View the website for documentation. Get it from npm:

$ npm install fortune --save

There is roughly 2.7k lines of code each for Node.js and the web browser (3.2k shared), and its size is about 22kb (min+gz).

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

// store.js 
const fortune = require('fortune')
module.exports = fortune()
.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 }
.defineType('post', {
  message: { type: String },
  // One-to-many relationship of post author to user posts. 
  author: { link: 'user', inverse: 'posts' }

By default, the data is persisted in memory. There are adapters for databases such as MongoDB, Postgres, and NeDB. Then let's add a HTTP server:

// server.js 
const http = require('http')
const fortune = require('fortune')
const store = require('./store')
// The `` helper function returns a listener function which 
// does content negotiation, and maps the internal response to a HTTP response. 
const server = http.createServer(
store.connect().then(() => server.listen(1337))

This yields an ad hoc JSON-over-HTTP API. There are serializers for Micro API (JSON-LD) and JSON API.

See the plugins page for more details.

This software is licensed under the MIT license.