Titanium Appcelerator – TaffyDB FlatFile Mod (local JSONDB)

Over the weekends, I’ve decided to fork and modify the TaffyDB module for Titanium Appcelerator by typicalJoe, to support two function – save to a flat file and retrieve from it in json. I love taffyDB syntax, is so simple and jsondb like.

This is useful when you are manipulating small data like feeds and json coming from servers. In some situation like this, it is much faster than storage using SQLLite, after not all data that we store contains lots of relation in that case SQL is not needed. I will write one for phonegap users to use later. You may use and download these for all kinds of project if you feel like it, a simple donation to “Buy Me A Coffee” will do too (at the sidebar below the ads) would help 😉

Anyway, let’s move on I’ve added two functions for flatfile storage see code below :-

   root.saveFlatFile = function(filename) {
        var fileObj =Titanium.Filesystem.getFile(Titanium.Filesystem.applicationDataDirectory,filename);
        fileObj.write(JSON.stringify(TOb)); // the response you get
        Ti.API.info("FlatFileDB saved");

and to Retrieve saved file..

   TAFFY.loadFlatFile = function(filename) {
	var fileObj =Titanium.Filesystem.getFile(Titanium.Filesystem.applicationDataDirectory,filename);
			var fileObjRef = fileObj.read().text;
			return fileObjRef;

Next we move on to how to implement and use it. By default all files will be saved into applicationDataDirectory, you can change the folder to point at your resource data folder if you like by modifying taffy.js.

To load a file, just use loadFlatfile and contain it into a TAFFY object to be used as TaffyDB…

    var MyDB =TAFFY( TAFFY.loadFlatFile('db.json') );

To save the loaded TAFFYdb into a flatfile. Just use saveToFlatFile function…

    // MyDB must be a TAFFY base object that you declared earlier on.

This is just an extra extension to what TaffyDB is, if you want to know on how TaffyDB function head over to documentation in taffydb.com for more info.

Tested in iphone and android. You can try out my own kitchen sink for sample on how to use them.
TaffyDB Mod (1082 downloads)

UPDATE : If you want to design a table like schema for your JSON and populate some records to use with this. Try out this tool