Socket
Socket
Sign inDemoInstall

bx

Package Overview
Dependencies
1
Maintainers
1
Versions
13
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    bx

in-memory storage for node


Version published
Weekly downloads
3
decreased by-82.35%
Maintainers
1
Created
Weekly downloads
 

Readme

Source

bx

in-memory storage for node

build-status

NPM


                  __
           ___~~~`  `~~__
     ___~~~              `~_
    |~_                     `~_
    |  ~_               ___ ~~ |
    |    ~_        __~~~       |
    |      ~_ __~~~            |
    |        |                 |
    |        |                 |
    |        |       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(); //no debug & no schema

var people = new bx({ //debug & schema
    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 }); //debug & no schema
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); //will die in 2 seconds

box.put('color', 'red'); //this will live until manually deleted

setTimeout(function() {
    console.log(box.get('bob')); //object
    console.log(box.get('color')); //"red"
}, 1000);

setTimeout(function() {
    console.log(box.get('bob')); //undefined
    console.log(box.get('color')); //"red"
}, 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'); //false
students.check(me); //true
get(key)

Retrieve data from the data store if the key exists.

Arguments

  • key {string} - key

Example

var box = new bx();
box.put('name', 'Ty');

box.get('name'); //"Ty"
box.get(); //undefined
box.get('oops'); //undefined
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); //{ 'apple1': 'gala', 'apple2': 'granny smith' }
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'); //0

box.del('salary');
var b = box.get('salary'); //undefined
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'); //undefined
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(); //{object (a, b, me)}
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(); //['snoop', 'kanye', 'dr']
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(); //['dogg', 'west', 'dre']
len

Get the number of items stored.

Example

var box = new bx();
console.log(box.len()); //0

box.put('a', 0);
console.log(box.len()); //1
size

Get the length in bytes of the data store.

Example

var box = new bx();
console.log(box.size()); //2

box.put('a', 'b');
console.log(box.size()); //19

box.clear();
console.log(box.size()); //2
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')); //undefined
console.log(box.exp('b')); //{2000 + time of creation}

Keywords

FAQs

Last updated on 11 Jun 2015

Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc