Beginner's Guide

TaffyDB is very easy to get started with. This brief turtoial will introduce you to a few of the core concepts and should be enough to get started even if you aren't already a web developer.

Step 1: Creating a quick sandbox

It is recmmended that you download and use your own copy of TaffyDB, but for the purposes of playing around you can use the latest version right off of GitHub. If you have never created a web page before you can simply create a text document, include the code below, and save it to your desktop as test.html.

<html>
	<head>
		<script src="https://github.com/typicaljoe/taffydb/raw/master/taffy.js"></script>
	</head>
	<body>
	</body>
				
</html>

The script tag imports TaffyDB right onto the page.

Step 2: Your First DB

Let's create a DB to store some city information. You'll need another script tag and you'll be writing JavaScript.

<html>
	<head>
		<script src="https://github.com/typicaljoe/taffydb/raw/master/taffy.js"></script>
		<script>

		var cities = TAFFY();

		</script>
	</head>
	<body>
	</body>
				
</html>

Step 3: Adding records

You can add records on creation and via the .insert() method. Below we are doing both.

<html>
	<head>
		<script src="https://github.com/typicaljoe/taffydb/raw/master/taffy.js"></script>
		<script>

		var cities = TAFFY([{name:"New York",state:"WA"},{name:"Las Vegas",state:"NV"},{name:"Boston",state:"MA"}]);
	
		cities.insert({name:"Portland",state:"OR"});

		</script>
	</head>
	<body>
	</body>
				
</html>

Step 4: Your first query

Our city db is itself a function that accepts a filter object (an object that will be matched to records). Let's query for Boston. We will alert the .count() method to see our results.

<html>
	<head>
		<script src="https://github.com/typicaljoe/taffydb/raw/master/taffy.js"></script>
		<script>

		var cities = TAFFY([{name:"New York",state:"WA"},{name:"Las Vegas",state:"NV"},{name:"Boston",state:"MA"}]);
	
		cities.insert({name:"Portland",state:"OR"});

		alert(cities({name:"Boston"}).count());

		</script>
	</head>
	<body>
	</body>
				
</html>

Step 5: Chaining methods

You can chain together. Below we will get the first two records from the DB and use the .each() method to alert their names.

<html>
	<head>
		<script src="https://github.com/typicaljoe/taffydb/raw/master/taffy.js"></script>
		<script>

		var cities = TAFFY([{name:"New York",state:"WA"},{name:"Las Vegas",state:"NV"},{name:"Boston",state:"MA"}]);
	
		cities.insert({name:"Portland",state:"OR"});

		cities().limit(2).each(function (r) {alert(r.name)});

		</script>
	</head>
	<body>
	</body>
				
</html>

Step 6: Updating data

You may have noticed that New York is in the wrong state. Let's fix that and alert the new state.

<html>
	<head>
		<script src="https://github.com/typicaljoe/taffydb/raw/master/taffy.js"></script>
		<script>

		var cities = TAFFY([{name:"New York",state:"WA"},{name:"Las Vegas",state:"NV"},{name:"Boston",state:"MA"}]);
	
		cities.insert({name:"Portland",state:"OR"});

		cities({name:"New York"}).update({state:"NY"});

		alert(cities({name:"New York"}).first().state);


		</script>
	</head>
	<body>
	</body>
				
</html>

Step 7: Sorting

Sorting is a big part of TaffyDB using the .order() method.

<html>
	<head>
		<script src="https://github.com/typicaljoe/taffydb/raw/master/taffy.js"></script>
		<script>

		var cities = TAFFY([{name:"New York",state:"WA"},{name:"Las Vegas",state:"NV"},{name:"Boston",state:"MA"}]);
	
		cities.insert({name:"Portland",state:"OR"});

		alert(cities().order("name").first().name);

		</script>
	</head>
	<body>
	</body>
				
</html>

What's next

There are a lot of other methods you can use against your data. Some highlights include .remove() to delete records, .get() to get an array of records, and .supplant to merge records with a string template. Continue to browse the docs for more ways to use TaffyDB.