zoukankan      html  css  js  c++  java
  • TaffyDB 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.

  • 相关阅读:
    前端面试
    react 【npx createreactapp myapp】执行错误
    npm yarn安装完成后,查不到版本号
    I love cnblogs
    万万没想到VFP也可以这样硬,调用微信的硬能力,扫码、上报位置、支付都可以
    VFP为公众号添加一个报名功能,代码不多,但谁能得扬名立万
    公众号回复消息不能超过5秒,VFP大数据处理来不及怎么办?
    爆肝怒赞,不会也会了,VFPBS用Form调用webapi和文件上传
    狐友们,万万不可掉队,VFP开发企业微信第一关回调该怎么配
    十行代码完成公众号对话,VFP的能力就是这么强悍,你学会了吗?
  • 原文地址:https://www.cnblogs.com/William_Fire/p/2733252.html
Copyright © 2011-2022 走看看