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

推荐订阅源

Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
Engineering at Meta
Engineering at Meta
罗磊的独立博客
Apple Machine Learning Research
Apple Machine Learning Research
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
有赞技术团队
有赞技术团队
小众软件
小众软件
GbyAI
GbyAI
J
Java Code Geeks
B
Blog
Blog — PlanetScale
Blog — PlanetScale
宝玉的分享
宝玉的分享
M
MIT News - Artificial intelligence
T
The Exploit Database - CXSecurity.com
Security Latest
Security Latest
阮一峰的网络日志
阮一峰的网络日志
NISL@THU
NISL@THU
T
Tenable Blog
S
Schneier on Security
T
Tor Project blog
V
V2EX
S
Secure Thoughts
P
Privacy International News Feed
Spread Privacy
Spread Privacy
博客园 - 三生石上(FineUI控件)
博客园_首页
T
Threatpost
月光博客
月光博客
Know Your Adversary
Know Your Adversary
Cyberwarzone
Cyberwarzone
T
The Blog of Author Tim Ferriss
H
Help Net Security
The Hacker News
The Hacker News
A
Arctic Wolf
B
Blog RSS Feed
雷峰网
雷峰网
The Last Watchdog
The Last Watchdog
S
SegmentFault 最新的问题
博客园 - 【当耐特】
Cisco Talos Blog
Cisco Talos Blog
Vercel News
Vercel News
Microsoft Security Blog
Microsoft Security Blog
A
About on SuperTechFans
P
Palo Alto Networks Blog
Attack and Defense Labs
Attack and Defense Labs
N
News | PayPal Newsroom
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
S
Securelist
L
LangChain Blog
I
InfoQ

博客园 - 一根神棍研古今

实测: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 Introduction TaffyDB Beginner's Guide 作为资深程序员,必定会掌握的十句谎话 诺基亚推两款WP8新机,为何股价反而大跌15%
TaffyDB Writing queries
一根神棍研古今 · 2012-10-22 · via 博客园 - 一根神棍研古今

Writing queries

The heart of TaffyDB and any database is running queries against your data. This is done after creation of your database by calling the root function and building Filter Objects.

// Create a new empty database
var db = TAFFY();

// Run a query against the DB to return all rows
db();

// Real world example - remove all records
db().remove();

Looking up individual records

Every object within a TaffyDB collection has a ___id value set by TaffyDB. This value is not intended for you to know, but is useful when building dynamic applications. This can be used to lookup a record by passing it into the root function as a string.

// Looks up a record based on its id
db("T000008R000002");

// Real world example - update records "status" column
db("T000008R000002").update({status:"Active"});

This also works if you have a copy of the whole record.

// get the first record
var firstRecord = db().first();

// look up this record again
db(firstRecord);

// Real world example - update records "status" column
db(firstRecord).update({status:"Active"});

Using functions

To give you full control over the results of your query you can always pass in a function. Just have it return true if you want the record in your final results.

// functional example, returns all records
db(function () {
	return true;
});


// Real world example - function returns records with a status of active
db(function () {
	return (this.status == "Active") ? true : false;
});

Basic queries

TaffyDB uses a very JavaScript centric Filter Object for looking up queries. There is no string concatenation and you can quickly compose these by hand or dynamically from within your app. The filter object is compared against each record using some business rules and if it passes the record remains in the results set.

The most common Filter Object is used simply to check if a column is equal to value.

// does a match for column and value
db({column:"value"});


// Real world example - records with a status of active
db({status:"Active"});

This is the short form of this

// does a match for column and value
db({column:{is:"value"}});


// Real world example - records with a status of active
db({status:{is:"Active"}});

The "is" part of this expressions can be swapped out for a variety of other comparisons as listed below.

Using !(bang)

For any comparison operator you can quote it and add a ! sign to reverse the meaning.

// does a match for column that is not a value
db({column:{"!is":"value"}});

// Real world example - records with a status other than of active
db({status:{"!is":"Active"}});

Adding additional filters

Using the almighty comma you can add additional lookups to you Filter Object.

// does a match for column that is a value and column2 is a value
db({column:"value",column2:"value"});

// Real world example - records with a status of active and a role of admin
db({status:"Active",role:"Admin"});

You can also pass in additional Filter Objects into the function

// does a match for column that is a value and column2 is a value
db({column:"value"},{column2:"value"});

// Real world example - records with a status of active and a role of admin
db({status:"Active"},{role:"Admin"});

Using arrays for IN and OR

In a database you can use "in" to pass in a collection of values to compare against. This is possible in TaffyDB via the array.

// does a match for column that is one of two values
db({column:["value","value2"]);

// Real world example - records with a status of active or pending
db({status:["Active","Pending"]});

You can also pass in an array of Filter Objects with each one being treated as a logical OR.

// does a match for column that is one of two values
db([{column:"value"},{column:"value2"}]);

// Real world example - records with a status of active or pending
db([{status:"Active"},{status:"Pending"}]);

Bringing it all together

A real world example of a complex query.

// return records where the role is Admin and the status is Active. 
// Also return records where the role is Admin, the status is Pending, and the manager_review is true

db({role:"Admin"},[{status:"Active"},{status:"Pending",manager_review:true}]);

Comparison Operators

In addition to the default "is" operator there are a lot of other operators you can use to lookup records.

is Example:
{column:{is:value}}
Used to see if a column is of same type and value of supplied value.
== Example:
{column:{'==':value}}
Used to see if a column matches a supplied value using JavaScript's liberal coercion rules.
=== Example:
{column:{'===':value}}
Used to see if a column is of same type and value of supplied value.
isnocase Example:
{column:{isnocase:value}}
Used to see if a column value is equal to a supplied value. Ignores case of column and value.
left Example:
{column:{left:value}}
Used to see if the start of a column is the same as a supplied value.
leftnocase Example:
{column:{leftnocase:value}}
Used to see if the start of a column is the same as a supplied value. Ignores case of column and value.
right Example:
{column:{right:value}}
Used to see if the end of a column is the same as a supplied value.
rightnocase Example:
{column:{rightnocase:value}}
Used to see if the end of a column is the same as a supplied value. Ignores case of column and value
like Example:
{column:{like:value}}
Used to see if column contains a supplied value.
likenocase Example:
{column:{likenocase:value}}
Used to see if column contains a supplied value. Ignores case of column and value
regex Example:
{column:{regex:value}}
Used to see if column matches a supplied regular expression.
lt Example:
{column:{lt:value}} or {column:{'<':value}}
Used to see if column is less than a supplied value.
lte Example:
{column:{lte:value}} or {column:{'<=':value}}
Used to see if column is less than or equal to a supplied value.
gt Example:
{column:{gt:value}} or {column:{'>':value}}
Used to see if column is greater than a supplied value.
gte Example:
{column:{gte:value}}  or {column:{'>=':value}}
Used to see if column is greater than or equal to a supplied value.
has Example:
{column:{has:value}}
Used to see if column that is an object has a value or object appearing in its tree.
hasAll Example:
{column:{hasAll:value}}
Used to see if column that is an object has a value or object appearing in its tree.
isSameArray Example:
{column:{isSameArray:value}}
Used to see if column is an array and is the same as a supplied array.
isSameObject Example:
{column:{isSameObject:value}}
Used to see if column is an object and is the same as a supplied object.
isString Example:
{column:{isString:true}}
Used to see if column a string.
isNumber Example:
{column:{isNumber:true}}
Used to see if column a number.
isArray Example:
{column:{isArray:true}}
Used to see if column an array.
isObject Example:
{column:{isObject:true}}
Used to see if column an object.
isFunction Example:
{column:{isFunction:true}}
Used to see if column a function.
isBoolean Example:
{column:{isBoolean:true}}
Used to see if column a boolean (true/false).
isNull Example:
{column:{isNull:true}}
Used to see if column null.
isUndefined Example:
{column:{isUndefined:true}}
Used to see if column undefined.