Skip to content

defconcepts/fortune

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

741 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Fortune.js

Build Status npm Version License

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 --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).

Example

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.

License

This software is licensed under the MIT license.

About

Application system for Node.js & web browsers.

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • JavaScript 95.2%
  • CSS 3.2%
  • HTML 1.4%
  • Shell 0.2%