OriginDB
Blazing fast and flexible JSON database.
Each key in the db
object corresponds to a JSON file.
This can also be view as:
Install
$ npm install origindb --save
Usage
var db = 'db';;;;;
In the db
folder:
// money.json // seen.json // posts.json
API
db(databaseDirectory, options)
Create a new database.
Parameters:
databaseDirectory
: String - The folder where all the json files will be stored at when using thefiles
adapter. Otherwise, it is the name of where the data will be stored at.options
- Objectadapter
- String or Function. Defaults tofiles
.
Returns: Function(key)
key
: String - JSON file in the database directory- Returns: Object - Methods of OriginDB.
Examples:
var db = 'userdb';
var db = 'apidb';
var db = '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.jsobjectphil = 25;db; // money.json 'phil': 25
get(property, defaultValue)
Get a property and if it does not exist, return the default value instead.
Parameters:
property
: String | ArraydefaultValue
: any
Returns: any - Depends on what is in property.
Example:
// money.json 'phil': 10 'stevo': 5 // main.js; // 10; // 0; // 5
// profile.json 'phil': 'name': 'Philip La' 'img': 'width': 55 'height': 20 // main.js; // 'Philip La'; // 55; // 20; // '4k'; // undefined; // { 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;//=> trueconsole;//=> 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 ; // food.json after 'noodle': true
set(property, value)
Set a property with a value. This method automatically saves.
Parameters:
property
: String | Arrayvalue
: any
Returns: Object - Methods of OriginDB. This is useful for chaining methods together.
Examples:
// main.js ; // money.json 'phil': 10 'josh': 89 'mike': 0 'alex': 17
;
// profile.json before 'phil': 'name': 'Steven Hausen' 'img': 'width': 55 'height': 20 // main.js ; // 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.jsvar name = 'phil';console; // 1450814470721console; // 1394814738429objectname = ;db; // join_date.json 'phil': 1450815256685 'micheal': 1394814738429
// money.js "phil": 10 "some_user": 20 "john": 10 "mike": 23 // main.jsvar users = object;var total = Object; console; // 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.jsvar db = 'mongodb://localhost:27017/myproject' adapter: 'mongo'; ;;; // MongoDB "_id" : "name" : "money" "data" : "{\"phil\":10,\"some_user\":20}" "_id" : "name" : "seen" "data" : "{\"some_user\":1451116353687}" "_id" : "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 = /** * @param * @param * @param * @param * @return */ { return {}; };
License
MIT © Phil