npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

origindb

v2.6.1

Published

Blazing fast and flexible JSON database.

Downloads

118

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