Reign
A persistent, typed objects implementation for node.js and the browser.
Install / Use
/learn @codemix/ReignREADME
reign
Persistent, typed objects for JavaScript.
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:
- 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
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.
Related Skills
node-connect
353.3kDiagnose OpenClaw node connection and pairing failures for Android, iOS, and macOS companion apps
frontend-design
111.7kCreate distinctive, production-grade frontend interfaces with high design quality. Use this skill when the user asks to build web components, pages, or applications. Generates creative, polished code that avoids generic AI aesthetics.
openai-whisper-api
353.3kTranscribe audio via OpenAI Audio Transcriptions API (Whisper).
qqbot-media
353.3kQQBot 富媒体收发能力。使用 <qqmedia> 标签,系统根据文件扩展名自动识别类型(图片/语音/视频/文件)。
