dbb
v0.2.1
Published
An easy to use database.
Downloads
5
Maintainers
Readme
dbb
An easy to use database.
Example
var DBB = require('dbb');
var db = DBB('db.json');
db('keys').set('key', 'value');
db('posts').insert({ title: 'DBB is amazing' });
In db.json
:
{
"keys": {
"key": "value"
},
"posts": [
{
"_id": "851e4d6-df24-42fb-b478-4a0c93d117e1",
"title": "DBB is amazing"
}
]
}
To query the data:
db('keys').get('key', function(err, key) {
// do something with key
});
db('posts').find({title: 'DBB is amazing'}, function(err, post) {
// do something with post
});
Table of Contents
$ npm install dbb --save
DBB stores data in a field which can be either be an object or an array.
{
"field": {},
"field": []
}
get
and set
method are for a document, also called an object.
// document
"field": {
"key": "value",
"key1": 1,
"key2": 2
}
find
, insert
, and save are for a collection, which is basically an array.
// collection
"field": [
{ _id: "89cbfd78-8ec8-4338-8140-7a3efa5e89c6", name: "phil", age: 7 },
{ _id: "12f3af49-b2e8-4e60-ae1c-9fb259ae6670", name: "jack", age: 20 },
{ _id: "6f33c2bd-7bbf-4b4d-a8e6-1973a3fa4f63", name: "steve", age: 99 }
]
For each DBB's method, there is a synchronous version of it. For example, get
and getSync
or find
and findSync
.
Documents
Collections
DBB(file, options);
Specify what JSON file to use. If it doesn't exist, the JSON file will be created.
Arguments
file
(String): JSON file name.options
(Object): Provide options for DBB to used. Currently, there is only one option which isbackup
. The backup creates a POST request to Hastebin and adds the link to the DBB_BACKUPS field. You can set how long every hour the backup should save.
Examples
var DBB = require('dbb');
var db = DBB('db.json');
// create a back up every 4 hours
DBB('db.json', {backup: 4});
db(field)
Arguments
field
(String): Specify what field to query.
Returns
(Object): Methods to use to query the database.
Examples
db(); // default
db('users');
db('posts').insert({title: 'hello world!'}); // converts to array
In json:
{
"default": {},
"users": {},
"posts": [
{"title": "hello world!"}
]
}
Documents
Get a key from the database.
Arguments
key
(String): Name of the key.callback(err, value)
(Function): A callback which is called when reading the JSON file has finished, or an error occurs. Value is the key's value.
Examples
db().get('key', function(err, value) {
if (err) throw err;
// do something with value
});
Synchronous key
.
Arguments
key
(String): Name of the key.
Returns
(*): Key's value
Examples
var key = db().get('key');
// do something with key
Get the whole object in the database.
Arguments
callback(err, object)
(Function): A callback which is called when reading the JSON file has finished, or an error occurs.
Examples
db().getAll(function(object) {
// do something with the object.
});
Get the whole object in the database.
Arguments
callback(err, object)
(Function): A callback which is called when reading the JSON file has finished, or an error occurs.
Returns
(Object): Object in the database that holds all the key-value pairs.
Examples
var object = db().getAllSync();
// do something with object
Set a key with a value in the database.
Arguments
key
(String): Name of the key.value
(*): Value of the key.callback(err)
(Function): Optional A callback which is called when writing to the JSON file has finished, or an error occurs.
Examples
db().set('key', 'value', function(err, value) {
if (err) throw err;
// key is now save in the database
// do something with value
});
Synchronous set
. Returns undefined.
Arguments
key
(String): Name of the key.value
(*): Value of the key. the JSON file has finished, or an error occurs. Value is the key's value.
Examples
db().setSync('key', 'value');
Collections
Find a document (object) in the database.
Arguments
document
(Object): Document also known as a object.callback(err, doc)
(Function): A callback which is called when reading to the JSON file has finished, or an error occurs. Doc is the document (object).
Examples
db().find({name: 'Phil'}, function(err, doc) {
if (err) throw err;
// do something with doc
});
Synchronous find
.
Arguments
document
(Object): Document also known as a object.
Returns
(Object): Document.
Examples
var phil = db().findSync({name: 'Phil'});
Get the whole collection in the database.
Arguments
callback(err, docs)
(Function): A callback which is called when reading to the JSON file has finished, or an error occurs. Docs is an array.
Examples
db().findAll(function(err, docs) {
if (err) throw err;
// do something with docs
});
Synchronous findAll
.
Returns
(Array): Collection of docs.
Examples
var docs = db().getAll();
Insert a document (object) into the database.
Arguments
document
(Object): Document also known as a object.callback(err, doc)
(Function): Optional A callback which is called when writing to the JSON file has finished, or an error occurs. Doc is the document inserted.
Examples
db().insert({name: 'Phil', email: '[email protected]'}, function(err, doc) {
if (err) throw err;
// do something with doc
});
Synchronous insert
. Returns undefined.
Arguments
document
(Object): Document also known as a object.
Examples
db().insert({name: 'Phil', email: '[email protected]'});
Remove a key in the database.
Arguments
key
(String): Name of the key.callback(err)
(Function): Optional A callback which is called when writing to the JSON file has finished, or an error occurs.
Examples
db().remove('key', function(err) {
if (err) throw err;
// key is now remove from the database
});
db('users').remove({name: 'Phil'}, function(err) {
if (err) throw err;
// document is now remove from the database
});
Synchronous sync
. Returns undefined.
Arguments
key
(String): Name of the key.
Examples
db().removeSync('key');
db('users').removeSync({name: 'Phil'});
Save a document in the database.
Arguments
document
(*): Usually an object.callback(err)
(Function): Optional A callback which is called when writing to the JSON file has finished, or an error occurs.
Examples
db('users').find({name: 'phil'}, function(err, user) {
if (err) throw err;
user.money++;
user.emails.push('[email protected]');
user.posts = [];
db('users').save(user, function(err) {
if (err) throw err;
});
});
Synchronous save
. Returns undefined.
Arguments
document
(*): Usually an object.
Examples
var user = db('users').find({name: 'phil'});
user.name = 'jack';
db('users').saveSync(user);
DBB is a convenient method for storing data without setting up a database server. However, if you need high performance and scalability more than simplicity, you should stick to databases like MongoDB.