fortune

An I/O library for web applications.

Fortune is an I/O library for web applications.

View the website for documentation. Get it from npm:

$ npm install fortune@latest --save

Currently beta software. Things will break, check the changelog.

  • Define record types and get CRUD + hypermedia for free.
  • Provide an adapter for data storage, allowing data to be backed by anything from text files to distributed databases.
  • Handle I/O via a standard serializer interface, be agnostic about implementation.
  • Map to a stateless protocol (typically HTTP), with events as side effects.

Here is a minimal example application, including a web server:

import fortune from 'fortune'
import http from 'http'
 
const store = fortune.create()
const server = http.createServer(fortune.net.http(store))

This sets up an instance of Fortune with default options, and an HTTP server instance. The fortune.net.http module returns a listener function that does content negotiation to determine which serializers to use for I/O, and forwards Node's built-in request and response objects to the serializers.

store.defineType('user', {
  name: { type: String },
  groups: { link: 'group', inverse: 'members', isArray: true }
})
 
store.defineType('group', {
  name: { type: String },
  members: { link: 'user', inverse: 'group', isArray: true }
})

Defining record types. There is a many-to-many relationship between user and group on the groups and members fields respectively.

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

Finally we need to call connect before we do anything with the instance. Then we can let the server listen, which yields a HTTP API that conforms to the Micro API and JSON API specifications. By default, it is backed by an embedded document store, NeDB, which doesn't persist to disk by default.

For the Micro API serializer, we get a set of internal pre-defined routes. Note that by default, the routes are obfuscated to the client, to encourage the use of hypermedia.

VerbRoute (unobfuscated)Description
GET/Get the entry point including links to collections.
GET/:typeGet a collection of records belonging to that type.
POST/:typeCreate a record belonging to that type.
PATCH/:typeUpdate records belonging to that type.
DELETE/:typeDelete an entire collection of records by type.
GET/:type/:idsGet records of a type by comma separated IDs.
PATCH/:type/:idsUpdate records of a type by comma separated IDs.
DELETE/:type/:idsDelete records of a type by comma separated IDs.
GET/:type/:ids/:linkGet related records corresponding to a type and IDs.
PATCH/:type/:ids/:linkUpdate related records corresponding to a type and IDs.
DELETE/:type/:ids/:linkDelete related records corresponding to a type and IDs.

The JSON API serializer emits routes specified here.

Fortune is licensed under the MIT license.