Fortune is an application system for Node.js & web browsers. It provides a 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 --saveThere 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 `fortune.net.http` helper function returns a listener function which
// does content negotiation, and maps the internal response to a HTTP response.
const server = http.createServer(fortune.net.http(store))
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.