GearsDB: A simple abstraction for the Google Gears Database

Posted by: Dion Almaer on 06/04/2007
I have been having fun playing with Google Gears. As I build applications with it (such as RSS Bling) I find myself repeating a few things on the database side.

Just wanting objects back

Instead of working with result sets, I just want to think in objects / hashes coming back.

To grab one fella I can:


bob = db.selectRow('person', 'id = 1');

or

bob = db.selectOne('select * from person where id = 1');
bob = db.selectOne('select * from person where id = ?', [1]);

console.log(bob.name); // write out 'Bob'


It is more interesting when you return back a bunch:


db.selectAll('select * from person where name like ?', ['bob%'], function(person) {
document.getElementById('selectAll').innerHTML += ' ' + person.name;
});


The callback gets passed in an object representation of the row. You can get back all of the results, but you should favour dealing with a row at a time instead of waiting around.


Inserting and updating

You often have an object that you want to fling in. This is easy to do via:


var bob = {name: 'Bob', url: 'http://bob.com', description: 'whee'};
db.insertRow('person', bob);
db.insertRow('person', bob, 'name = ?', ['Bob']);


The last form will only do the insert if another Bob isn't already in there.

You can then update via:


db.updateRow('person', person); // assumes that 'id' is the id, else you pass it in


or force the row in via insert or update (if it exists update, else insert):


db.forceRow('person', person);


To round things off you can db.deleteRow('person', bob);, db.dropTable("person");, and db.run('create table....');.

run() is a simple wrapper around execute() that handles logging and such for you.

To get started with the database you just need to:


var db = new GearsDB('gears-test'); // db name


All of this is in an open source project gears-dblib, and you can see simple tests/examples running.

The project also runs with Firebug Lite so you can play with simple console.log type things even if you don't have Firebug installed (e.g. want to test on IE). Very nice indeed.

Of course, someone will rewrite ActiveRecord and Hibernate in JavaScript shortly to work with Gears ;)

About Dion Almaer

Dion Almaer

Dion Almaer is the founder and CTO of Adigio, Inc. He is an architect, mentor, pragmatic, and evangelist of technologies such as J2EE, JDO, AOP, and Groovy. He is the Editor-in-Chief of TheServerSide.com J2EE Community and enjoys working in the community. He is a member of the Java Community Process, where he participates on various expert groups.

More About Dion »

Why Attend the NFJS Tour?

  • » Cutting-Edge Technologies
  • » Agile Practices
  • » Peer Exchange

Current Topics:

  • Languages on the JVM: Scala, Groovy, Clojure
  • Enterprise Java
  • Core Java, Java 7
  • Agility
  • Testing: Geb, Spock, Easyb
  • REST
  • NoSQL: MongoDB, Cassandra
  • Hadoop
  • Spring 3
  • Automation Tools: Git, Hudson, Sonar
  • HTML5, Ajax, jQuery, Usability
  • Mobile Applications - iPhone and Android
  • More...
Learn More »

NFJS, the Magazine

May Issue Now Available
  • Client-Side MVC with Spine.js, Part 1

    by Craig Walls
  • On Prototypal Inheritance, Part 2

    by Raju Gandhi
  • Making use of Scala Lazy Collections

    by Venkat Subramaniam
  • Integration Testing Web Applications Using Gradle

    by Kenneth Kousen
Learn More »