Underscore.js

LocallyDB is an embedded JavaScript NoSql database for Node.js and node-webkit. It's designed to store and retrieve data locally, easily and in stored folders and files. The concept of LocallyDB as an API is easy to understand and makes for better data storing in your desktop and web applications.

LocallyDB provides a very powerful data selection method with the same javascript comparison and logical operators your use to: >, <, ==, &&, ||...

LocallyDB is very lightweight and it only has one dependency, underscore

Source Code

Github Repo

The project is hosted on GitHub with bugs and feature discussion on the issues page.

Installation

You can install LocallyDB via npm from your terminal (in your project folder) by typing:

npm install locallydb

An example that explains everything

// load locallydb
var locallydb = require('locallydb');

// load the database (folder) in './mydb', will be created if doesn't exist 
var db = new locallydb('./mydb');

// load the collection (file) in './mydb/monsters', will be created if doesn't exist 
var collection = db.collection('monsters');

// Insert/add/push a list of elements
collection.insert([
  {name: "sphinx", mythology: "greek", eyes: 2, sex: "f", hobbies: ["riddles","sitting","being a wonder"]},
  {name: "hydra", mythology: "greek", eyes: 18, sex: "m", hobbies: ["coiling","terrorizing","growing"]},
  {name: "huldra", mythology: "norse", eyes: 2, sex: "f", hobbies: ["luring","terrorizing"]},
  {name: "cyclops", mythology: "greek", eyes: 1, sex: "m", hobbies: ["staring","terrorizing"]},
  {name: "fenrir", mythology: "norse", eyes: 2, sex: "m", hobbies: ["growing","god-killing"]},
  {name: "medusa",  mythology: "greek", eyes: 2, sex: "f", hobbies: ["coiling","staring"]}
]);

// Insert/add/push only one element
collection.insert({name: "HamoIzm", mythology: "amazigh", eyes: 2, sex: "m", hobbies: ["riddles","hunting"]});

// retrieve elements
collection.where({name: "HamoIzm"});
=> [{name: "HamoIzm", mythology: "amazigh", eyes: 2, sex: "m", hobbies: ["riddles","hunting"], cid:6}]

// retrieve by cid (cid is not index in array, cid is related with addition)
collection.get(3);
=> {name: "cyclops", mythology: "greek", eyes: 1, sex: "m", hobbies: ["staring","terrorizing"], cid:3}

// retrieve elements (monsters) with >= 2 eyes (an array)
collection.where("@eyes >= 2");
=> [
  {name: "sphinx", mythology: "greek", eyes: 2, sex: "f", hobbies: ["riddles","sitting","being a wonder"], cid:0},
  {name: "hydra", mythology: "greek", eyes: 18, sex: "m", hobbies: ["coiling","terrorizing","growing"], cid:1},
  {name: "huldra", mythology: "norse", eyes: 2, sex: "f", hobbies: ["luring","terrorizing"], cid:2},
  {name: "fenrir", mythology: "norse", eyes: 2, sex: "m", hobbies: ["growing","god-killing"], cid:4},
  {name: "medusa",  mythology: "greek", eyes: 2, sex: "f", hobbies: ["coiling","staring"], cid:5},
  {name: "HamoIzm", mythology: "amazigh", eyes: 2, sex: "m", hobbies: ["riddles","hunting"], cid:6}
]

// retrieve elements with (2 eyes and from the greek mythology) or from the amazing mythology
collection.where("(@eyes == 2 && @mythology == 'greek') || (@mythology == 'amazing')");
=> [
  {name: "sphinx", mythology: "greek", eyes: 2, sex: "f", hobbies: ["riddles","sitting","being a wonder"], cid:0},
  {name: "medusa",  mythology: "greek", eyes: 2, sex: "f", hobbies: ["coiling","staring"], cid:5},
  {name: "HamoIzm", mythology: "amazing", eyes: 2, sex: "m", hobbies: ["riddles","hunting"], cid:6}
]

// retrieve elements creation date
collection.get(6).$created;

// retrieve elements last edit date
collection.get(6).$updated;

// List all elements in the collection
collection.items;

// Update an element, it will add un-exsited key and replace existed ($created and cid can't be changed)
collection.update(5, {eyes: 3, food:"waloo"});
collection.get(5);
=> {name: "medusa",  mythology: "greek", eyes: 3, food:"waloo", sex: "f", hobbies: ["coiling","staring"], cid:5}

// Replace the element with the same cid and $created
collection.replace(6, {car: "Ferrari"});
collection.get(6);
=> {car: "Ferrari", cid:6}

// Delete an item by cid
collection.remove(1);

// Save all to files
collection.save();

// Now the data in the collection is
[
  {name: "sphinx", mythology: "greek", eyes: 2, sex: "f", hobbies: ["riddles","sitting","being a wonder"]},
  {name: "huldra", mythology: "norse", eyes: 2, sex: "f", hobbies: ["luring","terrorizing"]},
  {name: "cyclops", mythology: "greek", eyes: 1, sex: "m", hobbies: ["staring","terrorizing"]},
  {name: "fenrir", mythology: "norse", eyes: 2, sex: "m", hobbies: ["growing","god-killing"]},
  {name: "medusa",  mythology: "greek", eyes: 3, food:"waloo", sex: "f", hobbies: ["coiling","staring"], cid:5},
  {car: "Ferrari", cid:6}
]

Change Log

0.0.3September 05, 2014
Fix where function.

0.0.2September 04, 2014

0.0.1Auguest 25, 2014
Add updating methods (update and replace).

0.0.0Auguest 18, 2014
Initial release of LocallyDB.