bx
in-memory storage for node
__
___~~~` `~~__
___~~~ `~_
|~_ `~_
| ~_ ___ ~~ |
| ~_ __~~~ |
| ~_ __~~~ |
| | |
| | |
| | bx |
~_ | |
~_ | __|
~_ | __~~~
~_ | __~~~
~|__~~~
bx lets you store things in memory. It has a few special features:
- Setting a timeout before object is destroyed
- JSON Schema validation using tv4
- Clean & simple API
- Badass ASCII art
Installation
Just make sure you have node and npm on your system:
$ npm install bx
API
The code is pretty simple and well-commented, but here's an overview and example usage for each function. All examples assume that a bx has
been required as bx
. For more, feel free to look through the test suite as well.
Bx(opts)
Create a new data store.
Arguments
- opts {object} - options
- debug {boolean} - enable / disable logging
- schema {object} - JSON schema to check against
Example
var box = new bx();
var people = new bx({
debug: true,
schema: {
title: "people",
type: "object",
required: ["name", "age", "job"],
properties: {
name: {
type: "string"
},
age: {
type: "number"
},
job: {
type: "string"
}
}
}
});
var stuff = new bx({ debug: true });
put(key, val, time)
Add an object to the data store and specify how long it will live.
Arguments
- key {string} - key
- val {?} - value
- time {number=} - optional expiration time (in ms)
Example
var box = new bx();
box.put('bob', {
name: "Bob Johnson",
hometown: "Boston, MA",
age: 24
}, 2000);
box.put('color', 'red');
setTimeout(function() {
console.log(box.get('bob'));
console.log(box.get('color'));
}, 1000);
setTimeout(function() {
console.log(box.get('bob'));
console.log(box.get('color'));
}, 3000);
check(data)
Check to see if data fits the schema, if there is a schema.
Arguments
- data {?} - data to verify
Example
var studentSchema = {
'title': 'students',
'type': 'object',
'required': ['name', 'major', 'gpa'],
'properties': {
'name': {
'type': 'string'
},
'major': {
'type': 'string'
},
'gpa': {
'type': 'number'
}
}
};
var me = {
name: "Ty",
major: "Computer Science",
gpa: -500
};
var students = new box({
schema: studentSchema
});
students.check('bad data');
students.check(me);
get(key)
Retrieve data from the data store if the key exists.
Arguments
Example
var box = new bx();
box.put('name', 'Ty');
box.get('name');
box.get();
box.get('oops');
mget(keys)
Get multiple values from the data store.
Arguments
- keys {Array} - Array of keys
Example
var box = new bx();
box.put('apple1', 'gala');
box.put('apple2', 'granny smith');
var food = box.mget([
'apple1',
'apple2'
]);
console.log(food);
del(key)
Delete a key from the data store.
Arguments
- key {string} - Key to delete
Example
var box = new bx();
box.put('salary', 0);
var a = box.get('salary');
box.del('salary');
var b = box.get('salary');
mdel(keys)
Delete keys from the data store.
Arguments
- keys {Array} - Keys to delete
Example
var box = new bx();
var keys = ['a', 'b', 'c'];
for (var i in keys) {
box.put(keys[i], i);
}
box.mdel(['a', 'b', 'c']);
var all = box.mget(['a', 'b', 'c']);
clear
Clear the data store.
Example
var box = new bx({
schema: {
"title": "fruits",
"type": "array"
}
});
box.put('apples', ['gala', 'granny smith', 'red delicious']);
box.put('berries', ['blueberries', 'strawberries']);
box.clear();
var blah = box.get('apples');
all
Get entire data store.
Example
var box = new bx();
box.put('a', 'a'.charCodeAt(0));
box.put('b', 'banana');
box.put('me', {
'name': 'Ty',
'age': 19
});
var all = box.all();
keys
Get an array of the keys in the data store.
Example
var box = new bx();
box.put('snoop', 'dogg');
box.put('kanye', 'west');
box.put('dr', 'dre');
var k = box.keys();
vals
Get an array of the values in the data store.
Example
var box = new bx();
box.put('snoop', 'dogg');
box.put('kanye', 'west');
box.put('dr', 'dre');
var v = box.vals();
len
Get the number of items stored.
Example
var box = new bx();
console.log(box.len());
box.put('a', 0);
console.log(box.len());
size
Get the length in bytes of the data store.
Example
var box = new bx();
console.log(box.size());
box.put('a', 'b');
console.log(box.size());
box.clear();
console.log(box.size());
exp(key)
Get the time of deletion for a key.
Arguments
- key {string} - Key to check
Example
var box = new bx();
box.put('a', 0);
box.put('b', 1, 2000);
console.log(box.exp('a'));
console.log(box.exp('b'));