惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

酷 壳 – CoolShell
酷 壳 – CoolShell
H
Hacker News: Front Page
P
Palo Alto Networks Blog
T
ThreatConnect
Apple Machine Learning Research
Apple Machine Learning Research
博客园_首页
T
True Tiger Recordings
P
Privacy & Cybersecurity Law Blog
B
Blog
IT之家
IT之家
Last Week in AI
Last Week in AI
F
Full Disclosure
Hacker News: Ask HN
Hacker News: Ask HN
C
Comments on: Blog
Microsoft Azure Blog
Microsoft Azure Blog
C
Cybersecurity and Infrastructure Security Agency CISA
Microsoft Security Blog
Microsoft Security Blog
博客园 - 【当耐特】
N
News and Events Feed by Topic
NISL@THU
NISL@THU
腾讯CDC
雷峰网
雷峰网
Security Latest
Security Latest
李成银的技术随笔
M
Microsoft Research Blog - Microsoft Research
L
LangChain Blog
L
Lohrmann on Cybersecurity
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
C
Check Point Blog
Y
Y Combinator Blog
Recent Announcements
Recent Announcements
博客园 - Franky
N
News | PayPal Newsroom
V
V2EX
A
About on SuperTechFans
The Register - Security
The Register - Security
月光博客
月光博客
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
Google Online Security Blog
Google Online Security Blog
MyScale Blog
MyScale Blog
Cisco Talos Blog
Cisco Talos Blog
Vercel News
Vercel News
WordPress大学
WordPress大学
C
Cyber Attacks, Cyber Crime and Cyber Security
The Hacker News
The Hacker News
IntelliJ IDEA : IntelliJ IDEA – the Leading IDE for Professional Development in Java and Kotlin | The JetBrains Blog
IntelliJ IDEA : IntelliJ IDEA – the Leading IDE for Professional Development in Java and Kotlin | The JetBrains Blog
爱范儿
爱范儿
A
Arctic Wolf
L
LINUX DO - 最新话题
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More

博客园 - 一根神棍研古今

实测:Windows 8.1 (Windows Blue) 第三方桌面应用无法支持Retina屏,效果与Windows8.0似无差别。 MAC OSX中游戏花屏或在VMWARE下开启3D发生花屏及贴图错误的解决 MAC OSX 中,删除右键菜单中的多余重复项。 Windows2003上使用IIS7 Express使用FastCgi运行php 对淘宝一些规则的一些研究分享 傅立叶变换最直白最容易理解最直接最真实最有深度的解释 四种聚类方法之比较 人工大脑项目 —— Nengo MAC PRO RETINA 下使用VMWARE以BOOTCAMP启动WIN8的性能下降一个档次 【转】千万不要在夏季开发苹果应用,否则后果很严重 IOS小技巧积累 2012年全球最愚蠢的设计第一是微软,第二还是微软 - 一根神棍研古今 淘宝的服务器弱爆了,由此严重怀疑阿里云的稳定性 小红帽的故事居然是从法国民间的一个狼人诱奸小女孩的故事改来的 TAFFY Working with data TaffyDB Writing queries TaffyDB Introduction 作为资深程序员,必定会掌握的十句谎话 诺基亚推两款WP8新机,为何股价反而大跌15%
TaffyDB Beginner's Guide
一根神棍研古今 · 2012-10-22 · via 博客园 - 一根神棍研古今

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.