ori

origindb

Blazing fast and flexible JSON database.

Showing:

Popularity

Downloads/wk

23

GitHub Stars

24

Maintenance

Last Commit

5yrs ago

Contributors

2

Package

Dependencies

6

Size (min+gzip)

148.4KB

License

MIT

Type Definitions

Tree-Shakeable

No?

Categories

Readme

OriginDB 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 origindb --save

Usage

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

In the db folder:

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

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

// posts.json
{
  "posts": [
    { "title": "OriginDB 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 OriginDB.

Examples:

var db = require('origindb')('userdb');
var db = require('origindb')('apidb');
var db = require('origindb')('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().phil = 25;
db.save();

// money.json
{
  'phil': 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
{
  'phil': 10,
  'stevo': 5
}

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

// main.js
db('profile').get(['phil', 'name']); // 'Philip La'
db('profile').get(['phil', 'img', 'width']); // 55
db('profile').get(['phil', 'img', 'height'], 0); // 20
db('profile').get(['phil', 'img', 'resolution'], '4k'); // '4k'
db('profile').get(['phil', 'join_date']); // undefined
db('profile').get(['phil', '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 OriginDB. 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 OriginDB. This is useful for chaining methods together.

Examples:

// main.js
db('money')
  .set('phil', 10)
  .set('josh', 89)
  .set('mike', db.get('mike', 0))
  .set('alex', 17);

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

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

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

object()

Get the JSON object to directly manipulate it.

Parameters: None

Returns: Object

Examples:

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

// main.js
var name = 'phil';
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
{
  'phil': 1450815256685,  
  'micheal': 1394814738429
}
// money.js
{
  "phil": 10,
  "some_user": 20,
  "john": 10,
  "mike": 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

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

Current supported adapters are:

  • Files
  • MongoDB

MongoDB

OriginDB stores data in MongoDB like this:

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

// MongoDB
{
    "_id" : ObjectId("567e4741b09bffce48aa98b1"),
    "name" : "money",
    "data" : "{\"phil\":10,\"some_user\":20}"
}
{
    "_id" : ObjectId("567e4741b09bffce48aa98b2"),
    "name" : "seen",
    "data" : "{\"some_user\":1451116353687}"
}
{
    "_id" : ObjectId("567e4741b09bffce48aa98b3"),
    "name" : "posts",
    "data" : "{\"posts\":[{\"title\":\"OriginDB 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('origindb')({
  /**
   * @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 © Phil

Rate & Review

Great Documentation0
Easy to Use0
Performant0
Highly Customizable0
Bleeding Edge0
Responsive Maintainers0
Poor Documentation0
Hard to Use0
Slow0
Buggy0
Abandoned0
Unwelcoming Community0
100