Appcelerator Blog

The Leading Resource for All Things Mobile

TaffyDB – A lightweight JSON-based database for your app

0 Flares 0 Flares ×

TaffyDB is a Pure-JavaScript database abstraction that stores data in JSON format. This means that you feed a JSON file to TaffyDB and you automatically get a set of methods to execute against this data, such as INSERT, DELETE, GET (SELECT) and even JOIN!

The TaffyDB library was written by Ian Smith to be used on the Web and Node.js. Then Titanim Titan Ben Bahrenburg modified it to work with Titanium and Dan Tamas did some additional hackery on top of Ben’s work. The result is a very useful module that you drop into your Titanium app, and you get an instant database.

Consider the following example:

var inJSON=[

// Load the Taffy object
var taffy = require('taffydb4ti').taffy;

// Create an instance, give it a name and the JSON to start with
var my_db = new taffy('phonesdb',{autocommit:true},inJSON);; // because it is new

// returns an array with all rows
var recordSet=my_db().get();

    // inside this loop you have access to
    // rec.model
    // rec.version
    // rec.os

This example should be self-explanatory but I’d like to point something out. In the line

var my_db = new taffy(‘phonesdb’,{autocommit:true},inJSON);

We are giving it the ‘phonesdb’ name. This tells the library to store your data as a file, your table if you will. This means that you don’t have to worry about actually saving this data. You can grab the JSON file, send it to a web service for saving remotely, then re-download, instantiate an manipulate again…you get the picture.

Working with data

To get all records you do an operation similar to a “select *” statement:
var recordSet=my_db().get();

To insert a new record to your TaffyDB you use the following syntax:

my_db.insert({model:'New Model',os:'New OS',version:'New Version'});

Then if you wish to perform a query, say all records where OS===iOS, you do something like this:

var recordSet=my_db({os:'iOS'}).get();

You can do some more complex queries. Check out the official documentation for more examples.

Sample App

I have built a sample cross-platform app using the library.

Feel free to grab the code from The code has some additional goodies. As you can see on the screenshot, both apps look a bit different, as they are using the native controls and UX paradigms for each platform. The source code includes samples on using Android’s ActionBar, capturing the Android’s Back Button and using a single Alloy source code to achieve a truly cross-platform look and feel.

TaffyDB is not meant to be a complete replacement to SQLite, ACS or any other scalable, secure and robust database system. However it can be a total life saver for datasets that are smaller and don’t have aggressive security and scalability requirements.

0 Flares Twitter 0 Facebook 0 Google+ 0 LinkedIn 0 Email -- 0 Flares ×


  1. Joe

    Does TiTaffy support updating a record?

  2. Thomas Fallbeck

    There are more modern alternatives with significantly easier APIs than Taffy now available. Taffy hasn’t been updated in 2 years.

    You should check out ForerunnerDB, especially if you are already familiar with MongoDB, it’s like MongoDB but for your browser.

Comments are closed.

Sign up for updates!

Become a mobile leader. Take the first step to scale mobile innovation throughout your enterprise.
Get in touch
computer and tablet showing Appcelerator software
Start free, grow from there.
0 Flares Twitter 0 Facebook 0 Google+ 0 LinkedIn 0 Email -- 0 Flares ×