storage

Simple JSON file storage module for node.js. It features a mongoDB inspired syntax and is purely asynchronous. From version 0.1.4 on based on CoffeeScript.

npm install storage
2 downloads in the last week
27 downloads in the last month

storage_node

Simple JSON file storage module for node.js. It features a mongoDB inspired syntax and is purely asynchronous. Current version: 0.1.4. Note: Since storage - from version 0.1.4 onwards - is based on CoffeeScript the examples are provided in both Java- and CoffeeScript.

usage

initialization

var storage = require('storage').init('path');
storage = require('storage').init 'path'

creating a branch

storage.data.new('users');
storage.data.new 'users'

adding data

for (var i = 0; i < 10; i += 1) {
  var name = 'user_' + i;
  var age = i * 4;
  storage.data.users.new({
    name: name,
    age: age
  });
}
for i in [0..9]
  name = 'user_' + i
  age = i * 4
  storage.data.users.new
    name: name
    age: age

search syntax

p: {'eq', to} equal to
p: {'nq', to} not equal to
p: {'gt', to} greater than
p: {'lt', to} lower than

It is possible to specify a set of options to match:
p: [{gt: to_1}, {lt: to_2}]

retrieving data

storage.data.users.find({
  age: {gt: 8}
}, function (found) {
  console.log(found);
});
storage.data.users.find
  age: {gt: 8}
  (found) ->
    console.log found

Remark: It is possible (from v0.1.1 on) to get all the data of a certain type by calling: allUsers = storage.data.users.all(); Although keeping this function sync might appear to contradict with node's main principles it proves to be the right choice for the amount of data that is usually handled with storage.

more details on retrieving data (from v0.1.3 onwards)

If no option matched (= no data was retrieved) found is set to null. This enables the following:

storage.data.users.find({
  age: {gt: 8}
}, function (found) {
  if (found) {
    // we got something!
  }
});
storage.data.users.find
  age: {gt: 8}
  (found) ->
    if found then doSomething()

If only one element was found accessing it directly is possible:

storage.data.users.find({
  age: {eq: 8}
}, function (found) {
  console.log(found.name);
});
storage.data.users.find
  age: {eq: 8}
  (found) ->
    console.log found.name

The id property is now directly attached to all of the retrieved elements:

storage.data.users.find({
  age: {lt: 12}
}, function (found) {
  for (var k in found) {
    // access id either as "k" or as the object's "id" property
    console.log(k + ' is the same as ' + found[k].id);
  }
});
storage.data.users.find
  age: {lt: 12}
  (found) ->
    for k of found
      console.log k + ' is the same as ' + found[k].id

modifying data

storage.data.users.modify({
  name: {eq: 'user_4'}
}, {
  name: 'user_40'
}, function (found) {
  console.log(found);
});
storage.data.users.modify(
  {name: {eq: 'user_4'}}
  {name: 'user_40'}
  (found) ->
    console.log found
)

removing data

storage.data.users.remove({
  age: [{gt: 4}, {lt: 16}]
}, function (found) {
  console.log(found);
});
storage.data.users.remove
  age: [{gt: 4}, {lt: 16}]
  (found) ->
    console.log found

Remark: It is possible in (v0.1.3) to remove all the data of a certain type by calling removedUsers = storage.data.users.clear();

saving changes

storage.save();
storage.save()
npm loves you