Onigiri – WebStorage library for JavaScript

Some time ago I had to create a system which was able to save data on a client’s browser. Of course, the most simplest solution which came to my mind was cookies. But I needed a solution in which my data will be accessible for application opened in multiple tabs. I needed a better solution. Soon, I found specification of LocalStorage and IndexedDB, which is a part of HTML5 WebStorage specification. This amazing technology enables you to save data directly on the client’s browser and makes them accessible at any time from any place.

OnigiriJS – simple and delicious WebStorage library

Although API of LocalStorage is very simple, the API of IndexedDB is rather complex and difficult to use. Those two technologies were created to accomplish same result: saving data. The main difference between them is that you cannot search for data saved in LocalStorage.

Following the example of Dmitry Baranovskiy who created amazing JavaScript graphic library Raphael, I decided to build small library which extend API of LocalStorage.
The goal was to make this library fast and very simple to use. Let’s see what we’ve got here:

Basic usage

// first you have to set name for you database
var db = onigiri.db.set('my-db');

// and user list
db.save('users', [
    { id: 1, name: 'John', age : 25 },
    { id: 2, name: 'Jane', age : 45 },
    { id: 3, name: 'Marry', age : 25 },
]);

// find all users who are 25 years old
users = db.find('users', {
    age: 25
});

// find user by id
var john = db.findById('users', 1);

// John is getting older
john.age = 30;
db.save('users', [ john ]);

// remove John
db.remove('users', 1);

Future releases

  • IndexedDB support
  • More complex find conditions

You can download Onigiri here: https://github.com/LukaszKrawczyk/onigiri