Tuesday, March 11th, 2008

Taffy DB Javascript Database

Category: Ajax, Database

<p>Working client-side with data can be challenging and projects have tried to address this in different fashions. Ian Smith, creator of Joe’s Goals, has his own approach. He’s created Taffy DB, a lightweight Javascript database
that allows you to insert, update, delete, order, loop, and query against a client-side collection of data. The great thing about it is that it’s library agnostic and can work with all of the popular frameworks.

The idea came to me as I was working on Joe’s Goals 2.0 and I realized that one of the hardest parts about building any Web 2.0 application is working with data. There is no good way to use JavaScript by itself to gather, search, and maintain a collection of data. There are lots of great ways to great interfaces now days, but what about the data behind them? Taffy DB is the result of the research and testing I did for Joe’s Goals and is now avaliable for everyone to use.

Some of the key features include:

  • Under 10K!
  • Simple, JavaScript Centric Syntax
  • Fast
  • Easy to include in any web application
  • Compatible with major Ajax libraries: YUI, JQuery, Dojo, Prototype, EXT, etc
  • CRUD Interface (Create, Read, Update, Delete)
  • Sorting
  • Looping
  • Advanced Queries

The syntax to use the libary is very straightforward:

Updating:

javascript
< view plain text >
  1. friends.update(
  2.     {
  3.     state:"CA",
  4.     married:"Yes"
  5.     },
  6.     {
  7.     name:"Joyce"
  8.     }
  9.     );

Inserting:

javascript
< view plain text >
  1. friends.insert(
  2.     {name:"Brian",
  3.     gender:"M",
  4.     married:"No",
  5.     age:52,
  6.     state:"FL",
  7.     favorite_foods:["fruit","steak"]
  8.     });

In addition, Taffy DB provides 12 different conditional statements (e.g.: “greaterthan”, “equal”, “like”) to help build advanced queries that can further filter your results.

Ian has created a Getting Started Guide which will help in getting up to speed on the full capabilities of Taffy DB.

Related Content:

11 Comments »

Comments feed TrackBack URI

I’ve seen a ton of these kind of systems, but I’ve never seen one on the client-side. To be honest, I’ve never even thought of the idea. This is great stuff. It’s probably not the first time such a simple idea has been implemented in JavaScript, but it’s the first time I’ve seen it, so to me it’s entirely new and amazing. I might actually use this in my web application (whenever that gets done).

Comment by musicfreak — March 11, 2008

Hmm, once i worked on something like this, but i stoped because its not safe

Anyone can go to firebug and execute some js that delete all your database etc

Maybe i am wrong but i don’t think so

Comment by porf — March 12, 2008

I wrote a similar library for JavaScript that follows the ActiveRecord pattern but never released it publicly because there isn’t a ton of use for a client-side database other than long life single page apps. I really like the interface that Ian put on this DB though, it fits in better with the normal JavaScript programming style than ActiveRecord does. Suggestion: Make your documentation page wider than 400px because you’re wasting my screen space ;-).

Comment by Andy Kant — March 12, 2008

poft: Not if you implement some sort of security mechanism. I mean, if you just allow any SQL query to be run on your server at any time, then yes, it’s unsafe. But you have to implement some sort of authorization system just like you would in a normal application.

Comment by musicfreak — March 12, 2008

would be fun to get this into a dojo.data store. You could even persist w/ dojox.storage…that’d be pretty sweet, particularly for browsers that don’t yet have sqlite built in.

Comment by slightlyoff — March 13, 2008

Will it work on IE7?
infact I would like to ask that can any library like Dojo can cater a buggy IE7/8 ‘s Cookie bug issue and store things normally?

Comment by Adnan Siddiqi — March 14, 2008

@porf & musicfreak.

You both missed the point. This isn’t a library that allows you to write Javascript to connect to your server side database.

This is a database written in Javascript that runs in the browser. It’s a bit like cookies with an SQLeque interface.

Comment by Ben — March 18, 2008

hım i traying that’d be pretty sweet, particularly for browsers that don’t yet have sqlite built in.

Comment by Toas — June 21, 2008

I have a similar solution for this issue. My library is called Kombai. It allows you to select, insert, update, delete, order a client collection of data( syntax look like MySQL ):
//create a new database
var mydata = K.create.database({
fieldName: ["name", "phone"],
primaryKey: “phone”
});
// insert in to database
mydata.Insert(["ohay", "0987654321"]);
mydata.Insert(["oh", "0987654322"]);
mydata.Insert(["ha", "0987654323"]);
mydata.Insert(["hy", "0987654324"]);

mydata.Insert({”phone” : “0987654324?, “name” : “hy”});
mydata.Insert({”phone” : “0987654325?, “name” : “yo”});

//update
mydata.Update({”phone” : “232093430?, where: “name like ‘o%’”});
mydata.Update({”name”: “none”, where: “phone ==’0987654325?”});

//select
mydata.Select({fieldName: “phone”, where: “name like ‘%o%’”, orderBy: “phone”, Limit: 3});

//delete
mydata.Delete({Where: “name == ‘ohay’”});

//drop database:
mydata.drop();

other you can use select in DOM :

//getElementsByClassName
var list = K.select({from: document, where: “className == ‘toolbox’”});
var list2 = K.selcet({from: K(’id’), where: “nodeName == ‘SPAN’”});

Link download: http://vnjs.net/www/src/kombai.rar

Comment by mnx2012 — February 12, 2009

Hi All,
I am Akbar,
using the same script in my application
it is working fine in all cases.
orderby is not working
i am using following sample code

Untitled Document

function getSampleData()
{
var data = K.create.database({fieldName: ['id', 'name', 'age'],autoIncrement: ['id', {start: 1, step: 1}]});

data.Insert(['ghik', 46]);
data.Insert(['abcd', 15]);
data.Insert(['bced', 28]);
data.Insert(['cdef', 12]);
data.Insert(['defg', 9]);
data.Insert(['efgh', 38]);
data.Insert(['fghi', 35]);
data.Insert({name: ‘hikl’, age: 19});
data.Insert({name: ‘iklm’, age: 32});
data.Insert({name: ‘klmn’, age: 27});
data.Insert({name: ‘lmno’, age: 22});

var result = data.Select({id:”id”,name:”name”,orderby:”name”,limit:[0,11]});

var resultData = new TAFFY(result);

var tabbody = document.createElement(“tbody”);

resultData.forEach(function(data){

//alert(data.name);

var tabrow = document.createElement(“tr”);

var tabcell1 = document.createElement(“td”);
var tabcell2 = document.createElement(“td”);
var tabcell3 = document.createElement(“td”);

tabcell1.appendChild(document.createTextNode(data.id));
tabcell2.appendChild(document.createTextNode(data.name));
tabcell2.appendChild(document.createTextNode(data.age));

tabrow.appendChild(tabcell1);
tabrow.appendChild(tabcell2);
tabrow.appendChild(tabcell3);

tabbody.appendChild(tabrow);

});

var tab = document.createElement(“table”);

tab.appendChild(tabbody);
document.getElementById(‘showTable’).appendChild(tab);
}

Comment by skakbar — July 25, 2009

Hi
I am using this script ,it is working fine.I have one issue..how to get data from two different tables in one single query..

Comment by saradhimvs — July 27, 2009

Leave a comment

You must be logged in to post a comment.