The Joy of Using TaffyDB

Among a few JavaScript libraries, I enjoyed using – one of them will be TaffyDB. TaffyDB, is a javascript libraries designed for data retrieval, filtering, sorting and searching based on JSON. Instead of writing a loop to traverse the JSON structure looking for matching key and value, we just assigned the json object to Taffy.

It’s something like writing a JSON-like query to retrieve, update, sort, search and filter data from JSON object assigned to it (much like writing a SQL in SQLLite).

Check out a TAFFY syntax to retrieve an array of value of people that contains the character “s”. Yup, line 60, ContactsDataSource({name:{likenocase:”C”}}).get(), just that line!

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
<html>
<head>
	<title>TaffyDB</title>
	<script src="./lib/taffy-min.js"></script>
	<script>
		// Let's make a sample of our JSON.
		var Contacts = [
			{
				name:"Apple",
				age:"23",
				uniqueNo:"A001"			
			},
			{
				name:"Benny",
				color:"18",
				uniqueNo:"A002"			
			},
			{
				name:"Bismarck",
				color:"15",
				uniqueNo:"A003"			
			},
			{
				name:"Christopher",
				color:"26",
				uniqueNo:"A004"			
			},
			{
				name:"Connie",
				color:"16",
				uniqueNo:"A005"						
			},
			{
				name:"Chung Wei",
				color:"19",
				uniqueNo:"A006"			
			},
			{
				name:"Desmond",
				color:"20",
				uniqueNo:"A007"			
			},
			{
				name:"Eddie",
				color:"21",
				uniqueNo:"A008"			
			}
		];
		console.log(Contacts);
 
		//Now we are going to pass in Contacts into TAFFY. And assigned to ContactsDataSource.
		var ContactsDataSource = TAFFY(Contacts);
 
		//Let's get everything from JSON and return in array.
		console.log(ContactsDataSource().get());
 
		//Now let's get those people with the name that contains the character with "c", 
		//likenocase means, a sql "like" search without case sensitivity.
 
		console.log(ContactsDataSource({name:{likenocase:"C"}}).get());
 
		//For cases where you know, json data contains some sort of unique id that only return one result. 
		//Just use first();
		console.log(ContactsDataSource({uniqueNo:"A007"}).first());
	</script>
</head>
 
<body>
	<h1>Just look at console.log</h1>
</body>
</html>

All you need to use TaffyDB is just head towards http://www.taffydb.com/ and download the taffy-min file. Insert it into your javascript project.

Infact, if you are into ENYOJS development, you can also use Taffy to ease your development, especially in dealing with searching and filtering lengthy JSON records. Take note, anything assigned to Taffy will become a function meaning if you have var FruitDB assigned to TAFFY(JSON), to access FruitDB, you must use FruitDB() instead.

Using a taffy object, it is always in this format,

FruitDB({insert query}).method()

Is there more methods and goodies from Taffy you say? Of course!! I won’t recommended anything in this site, if it wasn’t a “good stuff“. Check out their documentation here on how to write simple queries.

That’s all for today folks, hope this helps in development projects. Seriously, this is a great library to have in all kinds of Javascript project!! Here’s today’s short tutorial files.
TaffyTutorial (2985 downloads)

 

Comments

comments

Leave a Reply

Your email address will not be published. Required fields are marked *