3.0.1 • Published 8 years ago

mistdb v3.0.1

Weekly downloads
5
License
MIT
Repository
github
Last release
8 years ago

mistDB Build Status Dependency Status devDependency Status

Blazing fast and flexible JSON database.

db(key).set(property, value)

Each key in the db object corresponds to a JSON file.

This can also be view as:

db(file).set(property, value)

Install

$ npm install mistdb --save

Usage

var db = require('mistdb')('db');
db('money').set('rambo', 10);
db('money').set('some_user', db('money').get('rambo') + 10);
db('seen').set('some_user', Date.now());
db('posts').set('posts', [
  { title: 'mistDB is awesome!', body: '...', likes: 10 },
  { title: 'flexbility ', body: '...', likes: 3 },
  { title: 'something someting something', body: '...', likes: 8 }
]);

In the db folder:

// money.json
{
  "rambo": 10
}

// seen.json
{
  "some_user": 1439674925906
}

// posts.json
{
  "posts": [
    { "title": "mistDB is awesome!", "body": "...", "likes": 10 },
    { "title": "flexbility ", "body": "...", "likes": 3 },
    { "title": "something someting something", "body": "...", "likes": 8 }
  ]
}

API

db(databaseDirectory, options)

Create a new database.

Parameters:

  • databaseDirectory: String - The folder where all the json files will be stored at when using the files adapter. Otherwise, it is the name of where the data will be stored at.
  • options - Object
    • adapter - String or Function. Defaults to files.

Returns: Function(key)

  • key: String - JSON file in the database directory
  • Returns: Object - Methods of mistDB.

Examples:

var db = require('mistdb')('userdb');
var db = require('mistdb')('apidb');
var db = require('mistdb')('mongodb://localhost:27017/myproject', {adapter: 'mongo'});

save()

Saves the current data in the database to all files. Does not write to a file if no changes were made.

Parameters: None

Returns: undefined

Example:

// main.js
db('money').object().rambo = 25;
db.save();

// money.json
{
  'rambo': 25
}

get(property, defaultValue)

Get a property and if it does not exist, return the default value instead.

Parameters:

  • property: String | Array
  • defaultValue: any

Returns: any - Depends on what is in property.

Example:

// money.json
{
  'rambo': 10,
  'stevo': 5
}

// main.js
db('money').get('rambo'); // 10
db('money').get('jared', 0); // 0
db('money').get('stevo', 0); // 5
// profile.json
{
  'rambo': {
    'name': 'ramboip La',
    'img': {
      'width': 55,
      'height': 20
    }
  }
}

// main.js
db('profile').get(['rambo', 'name']); // 'ramboip La'
db('profile').get(['rambo', 'img', 'width']); // 55
db('profile').get(['rambo', 'img', 'height'], 0); // 20
db('profile').get(['rambo', 'img', 'resolution'], '4k'); // '4k'
db('profile').get(['rambo', 'join_date']); // undefined
db('profile').get(['rambo', 'img']); // { width: 55, height: 20 }

has(property)

Checks to see if it has a property.

Parameters:

  • property: String | Array

Returns: Boolean

Example:

{
  'bar': 'some kind of data',
  'baz': 'some kind of data'
}

console.log(db('foo').has('bar'));
//=> true
console.log(db('foo').has('boo'));
//=> false

delete(property)

Delete a property. Shorthand for delete db('key').object()['prop']; db.save();

Parameters:

  • property: String | Array

Returns: Object - Methods of mistDB. This is useful for chaining methods together.

Example:

// food.json before
{
  'soup': true,
  'noodle': true
}

db('food').delete('soup');

// food.json after
{
  'noodle': true
}

set(property, value)

Set a property with a value. This method automatically saves.

Parameters:

  • property: String | Array
  • value: any

Returns: Object - Methods of mistDB. This is useful for chaining methods together.

Examples:

// main.js
db('money')
  .set('rambo', 10)
  .set('enriique', 89)
  .set('mann', db.get('mann', 0))
  .set('zayan', 17);

// money.json
{
  'rambo': 10,
  'enriique': 89,
  'mann': 0,
  'zayan': 17
}
db('tickets').set('rambo', db('tickets').get('rambo').concat[generateTicket()]);
// profile.json before
{
  'rambo': {
    'name': 'Steven Hausen',
    'img': {
      'width': 55,
      'height': 20
    }
  }
}

// main.js
db('profile')
  .set(['rambo', 'name'], 'Steven Hausen')
  .set(['rambo', 'friends'], ['herculus', 'a Gryphon'])
  .set(['rambo', 'wins', 'game_format'], 9)
  .set(['rambo', 'img', 'height'], 200);

// profile.json after
{
  'rambo': {
    'name': 'Steven Hausen',
    'img': {
      'width': 55,
      'height': 200
    },
    'friends': ['herculus', 'a Gryphon'],
    'wins': {
      'game_format': 9
    }
  }
}

object()

Get the JSON object to directly manipulate it.

Parameters: None

Returns: Object

Examples:

// join_date.json
{
  'rambo': 1450814470721,
  'micheal': 1394814738429
}

// main.js
var name = 'rambo';
console.log(db('join_date').object()[name]); // 1450814470721
console.log(db('join_date').object().micheal); // 1394814738429
db('join_date').object()[name] = new Date();
db.save();

// join_date.json
{
  'rambo': 1450815256685,  
  'micheal': 1394814738429
}
// money.js
{
  "rambo": 10,
  "some_user": 20,
  "john": 10,
  "mann": 23,
}

// main.js
var users = db('money').object();
var total = Object.keys(users).reduce(function(acc, cur) {
  return acc + users[cur];
}, 0);

console.log(total); // 63

Other methods

Some other methods that are provided by lodash are: keys, transform, values, and update.

Adapters

mistDB defaults to files adapter which just uses files in a folder to store the data. However, mistDB supports other ways to store data.

Current supported adapters are:

  • Files
  • MongoDB

MongoDB

mistDB stores data in MongoDB like this:

// app.js
var db = require('mistdb')('mongodb://localhost:27017/myproject', {adapter: 'mongo'});
db('money')
  .set('rambo', 10)
  .set('some_user', db('money').get('rambo') + 10);
db('seen').set('some_user', Date.now());
db('posts').set('posts', [
  { title: 'mistDB is awesome!', body: '...', likes: 10 },
  { title: 'flexbility ', body: '...', likes: 3 },
  { title: 'something someting something', body: '...', likes: 8 }
]);

// MongoDB
{
	"_id" : ObjectId("567e4741b09bffce48aa98b1"),
	"name" : "money",
	"data" : "{\"rambo\":10,\"some_user\":20}"
}
{
	"_id" : ObjectId("567e4741b09bffce48aa98b2"),
	"name" : "seen",
	"data" : "{\"some_user\":1451116353687}"
}
{
	"_id" : ObjectId("567e4741b09bffce48aa98b3"),
	"name" : "posts",
	"data" : "{\"posts\":[{\"title\":\"mistDB is awesome!\",\"body\":\"...\",\"likes\":10},{\"title\":\"flexbility \",\"body\":\"...\",\"likes\":3},{\"title\":\"something someting something\",\"body\":\"...\",\"likes\":8}]}"
}

You can also create your own adapters. For example, a adapter that does not save to file but instead does nothing.

var db = require('mistdb')({
  /**
   * @param {String} name - name of location of where the data will be stored.
   * @param {Object} objects - the internal in-memory cache object
   * @param {Object} checksums - every key in objects' checksums
   * @param {Object} options
   * @return {Function} save function
   */
  adapter: function(name, objects, checksums, options) {
    return function noopSave() {};
  }
});

License

MIT © rambo