Friday, April 6, 2012

TaffyDB - The JavaScript Database

TaffyDB is an opensouce library that brings database features into your JavaScript applications.

Introduction

How you ever noticed how JavaScript object literals look a lot like records? And that if you wrap a group of them up in an array you have something that looks a lot like a database table? TaffyDB is a libary to bring powerful database funtionality to that concept and rapidly improve the way you work with data inside of JavaScript.

What makes it sticky

  • Extremely fast
  • Powerful JavaScript centric data selection engine
  • Database inspired features such as count, update, and insert
  • Robust cross browser support
  • Easily extended with your own functions
  • Compatible with any DOM library (jQuery, YUI, Dojo, etc)

Creating a JavaScript Database

Let's create a database containing a collection of posts one might find on service like Twitter or Facebook.
?
1
2
3
4
5
6
7
8
9
var newsfeed = TAFFY([
    {"id":1,"user":"kmart77","stars":3,"text":"JavaScript is a meal best served cold"},
    {"id":2,"user":"lukeisyourfather","stars":5,"text":"Anyone want to meet tonight at Fado?"},
    {"id":3,"user":"bemine","stars":3,"text":"I reallylike the new prirotity inbox #gmail"},
    {"id":4,"user":"keepit","stars":3,"text":"Laughing at this weeks fails video."},
    {"id":5,"user":"piegirl22","stars":3,"text":"It is impossible to change your clothes in a public bathroom. Ick."},
    {"id":6,"user":"kmart77","stars":3,"text":"@kmart77: Have fun in New York!"},
    {"id":7,"user":"justinb","stars":5,"text":"Please set the noise level to earthquake"}
]);

Filtering for data

Now we have a TaffyDB collection containing the JSON objects (records) we passed in. Using the extensive API we can start accessing and working with our data.
?
1
2
newsfeed().count() // returns 7
newsfeed.sort("user") // sort records by username
But the real power comes into play once we start filtering for records. Think of this filtering as using data selectors (if you know jQuery) or building the a database query "where" clause (if you know databases).
?
1
2
newsfeed({user:"kmart77"}).count() // returns 1
// filter for posts where user is equal to kmart77
Keeping this JavaScript centric, we use an object literal to do the record lookup. No ugly string concatenations here.
Now that we understand basic filters lets look at some of the options we have for filtering
?
1
2
3
4
5
6
newsfeed({text:{ends:"#gmail"}}).count() // returns 1
// filter for posts where text ends with #gmail
// you can also use starts
 
newsfeed({stars:{gt:3}}).count() // returns 2
// filter for posts where the star rating is greater than 3
There are a lot more types of filtering including options to ignore case, use functions or regular expressions, and traverse objects within your database.

Working with data

When you filter for data you create a query. If you assign this query to a variable you can pass it around. It is not static and the data will be updated if other logic changes the underling records.
?
1
2
var kmart77 = newsfeed({user:"kmart77"});
// kmart77 is now a first class variable and can be passed to functions
So now that you have your query lets look at some of the methods you can use to work with the data it has access to.
?
1
2
3
4
5
6
7
8
9
kmart77.each(function (r) {
    alert(r.text);
}) // for each record, call the function you've provided and alert the text of the post
kmart77.select(text) // returns an array with the text of each post
 
var kmart77_5Star = kmart77.filter({stars:5}); // apply another filter
 
kmart77_5Star.count() // returns 0
kmart77.count() // still returns 1

Modifying data

Let's make a basic change to a record:
?
1
2
newsfeed({id:"1"}).update({stars:5});
// updates the star rating, returns a query object
Now let's insert another post:
?
1
2
newsfeed.insert({"id":8,"user":"justinb","stars":3,"text":"Have a great weekend everyone!"});
//inserts a new record
Finally let's do a remove:
?

1
2
newsfeed({user:"kmart77"}).remove();
//removes all records for user kmart77

 
 

For web reference-http://taffydb.com/

No comments:

Post a Comment