Taffy DB version 1.4

Taffy DB There as been a lot of improvements with Taffy DB since I first open sourced it on March 10th. Aside from being faster and smaller there are also a ton of new ways to search and use your data when you have it within a Taffy DB collection.

In case you missed it, Taffy DB is an open source JavaScript Database library that builds thin and very useful data layer into your Ajax apps. Taffy DB 1.4 introduces object and array comparison along with a host of data type comparisons. You can also filter based on length.

Consider the following JSON collection:

var people_collection = [

{name:”Bob”,friends:[”Dan”,”Sarah”]},

{name:”Sarah”,friends:[”Dan”]},

{name:”Dan”,friends:[”Bob”,”Sarah”]},

{name:”Kyle”,friends:[”Sarah”]}

];

Using regular JavaScript you would have to do something like this to find all the people who are friends with Dan.

var friendsOfDan = (function () {
var f = [];
for(var x = 0;x<people_collection.length;x++)
{
for(var y = 0;y<people_collection[x].friends.length;y++)
{
if (people_collection[x].friends[y] == “Dan”)
{
f[f.length] = people_collection[x].name;
}
}
}
return f;
} ());

But that is a pain. It also isn’t easily portable to other types of problems with your friends collection. Using this method how would you find friends of both Dan and Sarah? What if you want to modify those people records who are friends of Dan or access another variable in their object (gender or birthrate, for example). It would be a mess.

With Taffy DB all this becomes really easy. First you create a Taffy collection:

var people = new TAFFY(people_collection);

Then you find Dan’s friends:

people.find({friends:{contains:”Dan”}});

Then you find friends of both Dan and Sarah by nesting another call to find:

people.find({friends:{contains:”Dan”}},people.find({friends:{contains:”Sarah”}}));

Cool huh? How about if you want to introduce some grade school drama into the example and delete anyone who is friends were Sarah? Easy:

people.remove({friends:{contains:”Sarah”}});

You should probably also delete Sarah herself:

people.remove({name:”Sarah”});

Simple. Easy. Amazingly flexible. You can filter in over a dozen different ways as well as sort, run updates, apply functions, and more. If it isn’t part of your library you may want to take a serious peek.

Comments are closed.