shumpei / jdb.js

jdb.js is a framework that helps coding with Indexed Database API (IDB). As you know, 'J' is the next character of 'I' in alphabetical order, so 'JDB' means the 'Better IDB API':-)
28 stars 1 forks source link

jdb.js - Framework for Indexed Database API

"jdb.js" is a framework for programing easier with W3C Indexed Database API. 'J' is the next character of 'I' in alphabetical order, so "JDB" means "better IDB". Compared with using "naked" Indexed Database API, using jdb.js is better as follows:

Following code is an example of using jdb.js.

// Create a new reference of the database.
var db = new JDBDatabase('database name', 1);

// Create a new reference of the object store.
var CatStore = new JDBObjectStore({
  name: 'CatStore', // Name of this store.
  database: db,     // Reference of the database.
  key: { path: 'id', autoIncrement: true }, // Definition of the key property.
  indexes: {
    ageIdx: { path: 'age' } // Definition of the index.
  }
});

// Open database connection.
db.open().success(function() {
  console.log('database opened');
});

// Put an object to the object store.
CatStore.put({
  name: 'Nana',
  age: 7,
  gender: 'female'
});

// Get object from the store.
var catId = 1;
CatStore.get(catId, function(cat, error) {
  ...
});

// Iterate all cats in the store.
CatStore.all().iterate(function(cat) {
  ...
});

// Get the cats which age is less equal 5.
var criteria = JDBCriteria.byIndex('ageIdx').le(5);
CatStore.criteria(criteria).list(function(cats) {
  ...
});

For more information, see the tutorial.

CONTRIBUTING IS ALWAYS WELCOME!