TaffyDB with Titanium

TaffyDB is a pure-javascript library that allows you to use a JSON string as a database

I'm currently working on a project where I need to have multiple user-defined playlists.  I gave some thoughts on an easy-to-use storage system, and after playing with storing JSON strings as Titanium Properties, decided to hack a bit on TaffyDB.  TaffyDB is a pure-javascript library written by Ian Smith (@biastoact), which allows you to use a JSON string as a database.  Two fellow Titanium Titans, Dan Tamas (@dantamas) and Ben Bahrenburg (@bencoding) worked out a version of TaffyDB that works with Titanium and that provides persistency via automatically saving your JSON to the file system.

Their library works great but is missing one important feature I used to use with TaffyDB on Node: Loading the library with initial data.  I made some changes to the library, which I will publish later after I make sure everything is working fine which you can find here.  However, even without my contribution, the library is pretty powerfull and its worth looking at.  To make it easier to understand (and for me to remember in the future), I've put together a simple test script that demonstrates loading initial data, querying records, inserting, updating, deleting and listing.

 

var w=Ti.UI.createWindow({
	backgroundColor: '#fff'
});

var taffy = require('/lib/taffydb4ti').taffy;

var inJSON=[
		{playlistname:'Playlist 1',songname:'p1song1.mp3'},
		{playlistname:'Playlist 1',songname:'p1song2.mp3'},
		{playlistname:'Playlist 1',songname:'p1song3.mp3'},
		{playlistname:'Playlist 1',songname:'p1song4.mp3'},
		{playlistname:'Playlist 2',songname:'p2song1.mp3'},
		{playlistname:'Playlist 2',songname:'p2song2.mp3'},
		{playlistname:'Playlist 2',songname:'p2song3.mp3'}
	];

// create a new data store with initial data, such as a output from a web service
// taffydb4ti will create a file for this data store and will auto-save it to disk
// file is saved in applicationDataDirectory + 'titaffydb' + datastorename
var my_db = new taffy('myplaylists',{autocommit:true},inJSON);

// search for a rec
console.log('============= Trying query');
var recordSet=my_db({playlistname:'Playlist 1'}).get(); // recordSet is a now native array
console.log('Total songs on ' + recordSet[0].playlistname + ' : ' + recordSet.length);
//

// dump all recs
console.log('============= Dumping all recs');
my_db().each(function (rec) {
	console.log(rec.playlistname + ':' + rec.songname);
});
//

// add a record
my_db.insert({playlistname:'Playlist 3',songname:'newsong.mp3'});
//

// dump all recs now with a new record
console.log('============= Dumping all recs');
my_db().each(function (rec) {
	console.log(rec.playlistname + ':' + rec.songname);
});
//

// updates a record
my_db({playlistname:'Playlist 1',songname:'p1song1.mp3'}).update({'songname':'ichanged.mpg3'});
//

// dump all recs now with an updated record
console.log('============= Dumping all recs');
my_db().each(function (rec) {
	console.log(rec.playlistname + ':' + rec.songname);
});
//

// deletes a record
my_db({songname:'p2song2.mp3'}).remove();
//

// dump all recs now with a record deleted
console.log('============= Dumping all recs');
my_db().each(function (rec) {
	console.log(rec.playlistname + ':' + rec.songname);
});
//

my_db.save(); // have to explicitly call save after a remove?

// load previously saved data store
var my_db2 = new taffy('myplaylists');
//

// dump records to prove that we now have loaded data from disk
console.log('============= Dumping all recs loaded from disk');
my_db2().each(function(rec){
	console.log(rec.songname);
})
//

This sample code doesn't display anything on the screen, but rather sends output to your Titanium Studio Console:

[INFO] :   ============= Trying query
[INFO] :   Total songs on Playlist 1 : 4
[INFO] :   ============= Dumping all recs
[INFO] :   Playlist 1:p1song1.mp3
[INFO] :   Playlist 1:p1song2.mp3
[INFO] :   Playlist 1:p1song3.mp3
[INFO] :   Playlist 1:p1song4.mp3
[INFO] :   Playlist 2:p2song1.mp3
[INFO] :   Playlist 2:p2song2.mp3
[INFO] :   Playlist 2:p2song3.mp3
[INFO] :   ============= Dumping all recs
[INFO] :   Playlist 1:p1song1.mp3
[INFO] :   Playlist 1:p1song2.mp3
[INFO] :   Playlist 1:p1song3.mp3
[INFO] :   Playlist 1:p1song4.mp3
[INFO] :   Playlist 2:p2song1.mp3
[INFO] :   Playlist 2:p2song2.mp3
[INFO] :   Playlist 2:p2song3.mp3
[INFO] :   Playlist 3:newsong.mp3
[INFO] :   ============= Dumping all recs
[INFO] :   Playlist 1:ichanged.mpg3
[INFO] :   Playlist 1:p1song2.mp3
[INFO] :   Playlist 1:p1song3.mp3
[INFO] :   Playlist 1:p1song4.mp3
[INFO] :   Playlist 2:p2song1.mp3
[INFO] :   Playlist 2:p2song2.mp3
[INFO] :   Playlist 2:p2song3.mp3
[INFO] :   Playlist 3:newsong.mp3
[INFO] :   ============= Dumping all recs
[INFO] :   Playlist 1:ichanged.mpg3
[INFO] :   Playlist 1:p1song2.mp3
[INFO] :   Playlist 1:p1song3.mp3
[INFO] :   Playlist 1:p1song4.mp3
[INFO] :   Playlist 2:p2song1.mp3
[INFO] :   Playlist 2:p2song3.mp3
[INFO] :   Playlist 3:newsong.mp3
[INFO] :   ============= Dumping all recs loaded from disk
[INFO] :   ichanged.mpg3
[INFO] :   p1song2.mp3
[INFO] :   p1song3.mp3
[INFO] :   p1song4.mp3
[INFO] :   p2song1.mp3
[INFO] :   p2song3.mp3
[INFO] :   newsong.mp3

Hope you find this useful and consider using this cool library for your next Titanium project.

blog comments powered by Disqus

<< Go back to the previous page