Fortune.js

5.5.19 · active · verified Wed Apr 22

Fortune.js is a non-native graph database abstraction layer designed for both Node.js and web browsers, currently at stable version 5.5.19. It provides an application-level implementation of graph-like features, including bi-directional relationships, inverse updates, and referential integrity, all built upon a user-defined data model. The library maintains a steady release cadence, with recent updates focusing on dependency management. Key differentiators include its ability to enable portable storage options and facilitate the sharing of identical data models across server and client environments, streamlining full-stack development. While the core module handles data modeling and relationship management, persistent storage is achieved via optional, separate database adapters for systems like MongoDB, Postgres, or IndexedDB. It avoids traditional ORM complexities by treating records as plain data structures and managing relationships at the application level.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart initializes a Fortune.js store with a basic micro-blogging schema, demonstrating bi-directional and one-to-many relationships, then performs asynchronous create and find operations with includes.

const fortune = require('fortune') // Or use `import fortune from 'fortune';` in an ESM project.

// Define record types for a micro-blogging service
const store = fortune({
  user: {
    name: String,
    // Many-to-many inversely related fields
    following: [ Array('user'), 'followers' ],
    followers: [ Array('user'), 'following' ],
    // One-to-many relationship
    posts: [ Array('post'), 'author' ]
  },
  post: {
    message: String,
    // Many-to-one relationship
    author: [ 'user', 'posts' ]
  }
})

// Example: Create a user and a post, then find them
(async () => {
  // Create a user record
  const [ user ] = await store.create('user', [
    { id: 'user123', name: 'Alice' }
  ]);
  console.log('Created User:', user);

  // Create a post record associated with the user
  const [ post ] = await store.create('post', [
    { id: 'post456', message: 'Hello Fortune.js!', author: user.id }
  ]);
  console.log('Created Post:', post);

  // Find the user and include their posts
  const foundUsers = await store.find('user', ['user123'], null, [['posts']]);
  console.log('Found Users with posts:', JSON.stringify(foundUsers, null, 2));

  // Find the post and include its author
  const foundPosts = await store.find('post', ['post456'], null, [['author']]);
  console.log('Found Posts with author:', JSON.stringify(foundPosts, null, 2));

  // Disconnect the store (important if using a persistent adapter)
  await store.disconnect();
})().catch(console.error);

view raw JSON →