reign

18

A persistent, typed objects implementation for node.js and the browser.

@see github.com/codemix/reign

phpnode

reign

Persistent, typed objects for JavaScript.

Build Status

What?

Provides a realm (like a namespace) which can contain various kinds of typed object, and mechanisms for storing and loading those objects to disk.

Currently supports various kinds of data type:

As well as the following builtins:

Examples

See the examples directory.

API Documentation

Currently very work in progress, see src/README.md.

Installation

Install via npm.

npm install reign

Usage

var Backing = require('backing');
var Realm = require('reign').Realm;

var backing = new Backing({
  name: 'example',
  arenaSize: 1024 * 1024,
  arenaSource: {
    type: 'mmap', // can also be 'array-buffer' to use storage which will not survive program termination.
    dirname: __dirname + '/../data'
  }
});

var realm = new Realm(backing);

// `T` is an object containing all the registered types in the realm, e.g. `T.String` or `T.Object`.
var T = realm.T;

var StructType = realm.StructType;

// Initialize the realm, loading the data files. (Returns a promise)
realm.init().then(function () {
  const Thing = new StructType({
    id: T.Uint32,
    name: T.String,
    description: T.String,
    extra: T.Object // Holds additional properties.
  });

  let thing = realm.get('London');
  if (!thing) {
    // This must be the first time we've run this program.
    thing = new Thing({
      id: 123,
      name: 'London',
      description: 'The city of london.',
      extra: {
        type: 'Place'
      }
    });
    console.log('Saving a new Thing called London');
    realm.set('London', thing);
  }
  else {
    console.log('Loaded an existing thing called London')
  }

  console.log(JSON.stringify(thing, null, 2));

  if (thing.extra.type !== 'City') {
    console.log('London is a city, not just a place.')
    thing.extra.type = 'City';
  }

});

Run this example more than once to see different results.

License

Published by codemix under a permissive MIT License, see LICENSE.md.