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

推荐订阅源

Hacker News - Newest:
Hacker News - Newest: "LLM"
C
Cisco Blogs
L
LINUX DO - 热门话题
S
Schneier on Security
NISL@THU
NISL@THU
T
Threatpost
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
Latest news
Latest news
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
量子位
Stack Overflow Blog
Stack Overflow Blog
The GitHub Blog
The GitHub Blog
月光博客
月光博客
Cyberwarzone
Cyberwarzone
B
Blog
G
GRAHAM CLULEY
L
Lohrmann on Cybersecurity
Microsoft Security Blog
Microsoft Security Blog
Vercel News
Vercel News
小众软件
小众软件
M
MIT News - Artificial intelligence
I
InfoQ
aimingoo的专栏
aimingoo的专栏
C
CXSECURITY Database RSS Feed - CXSecurity.com
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
美团技术团队
Google DeepMind News
Google DeepMind News
T
The Blog of Author Tim Ferriss
Help Net Security
Help Net Security
WordPress大学
WordPress大学
V
Vulnerabilities – Threatpost
T
Tenable Blog
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
N
Netflix TechBlog - Medium
MyScale Blog
MyScale Blog
Blog — PlanetScale
Blog — PlanetScale
Y
Y Combinator Blog
Google DeepMind News
Google DeepMind News
D
Docker
MongoDB | Blog
MongoDB | Blog
Forbes - Security
Forbes - Security
H
Hacker News: Front Page
A
About on SuperTechFans
L
LINUX DO - 最新话题
B
Blog RSS Feed
D
DataBreaches.Net
博客园 - 司徒正美
Recorded Future
Recorded Future
G
Google Developers Blog
Hugging Face - Blog
Hugging Face - Blog

博客园 - 一根神棍研古今

实测: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.