Persistent, typed objects for JavaScript.
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:
- T.Any
- T.Array
- T.Boolean
- T.Int8
- T.Int16
- T.Int32
- T.Uint8
- T.Uint16
- T.Uint32
- T.Float32
- T.Float64
- T.HashMap
- T.HashSet
- T.Object
- T.String
- T.InternedString
See the examples directory.
Currently very work in progress, see src/README.md.
Install via npm.
npm install reign
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.
Published by codemix under a permissive MIT License, see LICENSE.md.