#localDB
LocalDB 为开发者提供简单、易用又强大的浏览器端数据存取接口,其被设计用来为 WEB 应用、手机 H5 应用、网页游戏引擎提供浏览器端持久化存储方案。
Feature
- 基于 JSON 文档风格的存储方式
- 支持多种数据格式的存储,例如:函数、正则表达式
- 支持基于文档的富查询及排序功能
- 支持 AMD/CMD/Standalone 等多种模块加载方式
- 支持数据存取加密功能
- 智能存储引擎切换
- 支持域白名单功能,实现跨域共享数据,独特的跨域数据共享解决方案
- 独特的域数据模块化解决方案
- 高安全性(可以通过更改proxy来隐藏数据所存储的真实域)
- 支持 Promise 或 Callback 异步编程
- 支持 BSON objectId
Installation
By Bower
$ bower install localdb
By SPM
$ spm install localdb
通过 bower 安装或者直接下载独立库文件的用户,可以直接在html页面中引用该js文件
<!DOCTYPE html>
<html>
<head>
<script type="text/javascript" src="dist/0.0.1/localdb.js"></script>
</head>
<body>
</body>
<script type="text/javascript">
var db = new LocalDB("foo")
</script>
</html>
LocalDB 支持 requirejs 作为其模块加载器,具体用法如下:
<!DOCTYPE html>
<html>
<head>
<script type="text/javascript" src="emaples/require.js"></script>
</head>
<body>
</body>
<script type="text/javascript">
require(['dist/0.0.1/localdb'],function(LocalDB){
var db = new LocalDB("foo")
}
</script>
</html>
LocalDB 支持 seajs 作为其模块加载器,通过 SPM 安装或者直接下载 seajs 版本的库文件的用户可以利用 seajs 来加载 localDB
<!DOCTYPE html>
<html>
<head>
<script type="text/javascript" src="exmaples/sea.js"></script>
</head>
<body>
</body>
<script type="text/javascript">
seajs.use('localdb-seajs.js', function(LocalDB){
var db = new LocalDB("foo")
})
</script>
</html>
Getting Started
// 创建/获取名为`foo`的db
var db = new LocalDB("foo")
// 创建/获取该db中名为`bar`的collection
var collection = db.collection("bar")
// 插入数据
collection.insert({
a: 5,
b: "abc",
c: /hell.*ld/,
d: {e: 4, f: "5"},
g: function(h){return h*3},
i: [1,2,3]
}).then(function(err){
// 查询数据
// 目前支持添加的数据格式为数字、字符串、正则表达式、对象、函数和数组。
collection.find({
// `where`表示查询条件,相当于`select a,b from table where b == "abc"`中的`where`语句。
where: {
a: {$gt: 3, $lt: 10},
b: "abc"
},
// `projection`表示根据查询的条件构造选择数据内容。
projection: {
a:1,
b:1
}
})
}).then(function(data, err){
// 其表示更新`a`的值为5的数据,设置其`b`的值为`new_string`,设置其`i`的值为`[3,2,1]`
collection.update({
$set: {
b: "new_string",
i: [3,2,1]
}
},{
where: {
a: 5
}
})
}).then(function(err){
// 其表示删除`a`的值大于3、小于10并且不等于5的数据。
collection.remove({
where: {
a: {$gt: 3, $lt: 10, $ne: 5}
}
})
})
API Reference
具体的API请参考这里
Get Involved
- Fork LocalDB
- 将Fork后的LocalDB项目clone到本地
- 命令行执行
git branch develop-own
来创建一个新分支 - 执行
git checkout develop
切换到新创建的分支 - 执行
git remote add upstream https://github.com/wh1100717/localDB.git
将主干库添加为远端库 - 执行
git remote update
来更新主干库上的最新代码 - 执行
git fetch upstream/master
拉取最新代码到本地 - 执行
git rebase upstream/master
进行本地代码合并
- 项目根目录执行
npm install
安装项目所需要的库和工具 - 修改源码或者测试用例
- 执行
grunt test
来测试修改的内容是否能跑通所有测试用例 - 如果测试通过,则将代码提交到remote
- 在Github你Fork的项目中有一个pull request按钮,点击提交代码合并
Support Feature
#####Comparison
- $gt
Matches values that are greater than the value specified in the query. - $gte
Matches values that are greater than or equal to the value specified in the query. - $lt
Matches values that are less than the value specified in the query. - $lte
Matches values that are less than or equal to the value specified in the query. - $ne
Matches all values that are not equal to the value specified in the query. - $in
Matches any of the values that exist in an array specified in the query. - $nin
Matches values that do not exist in an array specified to the query.
#####Logical
- $or
Joins query clauses with a logical OR returns all documents that match the conditions of either clause. - $and
Joins query clauses with a logical AND returns all documents that match the conditions of both clauses. - $not
Inverts the effect of a query expression and returns documents that do not match the query expression. - $nor
Joins query clauses with a logical NOR returns all documents that fail to match both clauses.
#####Element
-
$exists
Matches documents that have the specified field. -
$type
Selects documents if a field is of the specified type.Note:
It is different from the $type API in MongoDB.
It is really easy to determine the type of element in javascript.
Support type input string:string
|object
|function
|array
|number
#####Evaluation
- $mod
Performs a modulo operation on the value of a field and selects documents with a specified result. - $regex
Selects documents where values match a specified regular expression. - $text
Performs text search. - $where
Matches documents that satisfy a JavaScript expression.
#####Array
- $all
Matches arrays that contain all elements specified in the query. - $elemMatch
Selects documents if element in the array field matches all the specified $elemMatch condition. - $size
Selects documents if the array field is a specified size.
#####Projection Operators
- $
Projects the first element in an array that matches the query condition. - $elemMatch
Projects only the first element from an array that matches the specified $elemMatch condition. - $meta
Projects the document’s score assigned during $text operation. - $slice
Limits the number of elements projected from an array. Supports skip and limit slices.
#####Fields
- $inc
Increments the value of the field by the specified amount. - $mul
Multiplies the value of the field by the specified amount. - $rename
Renames a field. - $setOnInsert
Sets the value of a field upon document creation during an upsert. Has no effect on update operations that modify existing documents. - $set
Sets the value of a field in a document. - $unset
Removes the specified field from a document. - $min
Only updates the field if the specified value is less than the existing field value. - $max
Only updates the field if the specified value is greater than the existing field value. - $currentDate
Sets the value of a field to current date, either as a Date or a Timestamp.
#####Array
- $
Acts as a placeholder to update the first element that matches the query condition in an update. - $addToSet
Adds elements to an array only if they do not already exist in the set. - $pop
Removes the first or last item of an array. - $pullAll
Removes all matching values from an array. - $pull
Removes all array elements that match a specified query. - $pushAll
Deprecated. Adds several items to an array. - $push
Adds an item to an array.
#####Modifiers
- $each
Modifies the $push and $addToSet operators to append multiple items for array updates. - $slice
Modifies the $push operator to limit the size of updated arrays. - $sort
Modifies the $push operator to reorder documents stored in an array. - $position
Modifies the $push operator to specify the position in the array to add elements.
#####Bitwise
- $bit
Performs bitwise AND, OR, and XOR updates of integer values.
#####Isolation
- $isolated
Modifies behavior of multi-updates to increase the isolation of the operation.