Skip to content

djyde/StoreDB

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

36 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

StoreDB

[DEPRECATED] Use localForage instead.

English version

StoreDB是一个基于localStorage的本地储存库,通过模拟MongoDB的一些API和概念(如“集(collection)”和“文档(document)”),使你能使用 localStorage 储存复杂数据。

Why StoreDB?

  • StoreDB使你在无须配置数据库的情况下,在静态页面中也能实现大量数据储存和交互。这意味着你能用StoreDB非常简便地建立一个功能强大的SPA(单页面应用,Single Page Application)。

  • StoreDB也适用于demo产品的开发。比如,假定你正在参加编程马拉松,你的团队只不过是想做出一个用以展示的demo,却不得不花费时间在远程或本地架设server,再配置数据库,白白浪费了宝贵的时间。使用StoreDB,你只需嵌入一段javascript代码就能实现丰富的数据交互。

  • 使用AngularJS配合StoreDB更是如虎添翼。

Demo

Everfeed - RSS Reader http://everfeed.ml/

MoniCoin - 比特币虚拟交易 http://djyde.github.io/MoniCoin

Tutorial

入门指南

Install

bower:

$ bower install storedb

HTML:

<script type="text/javascript" src="/path/to/storedb.js"></script>

Quick Start

插入(Insert)

向名为players的集合中插入一条文档:

storedb('players').insert({"name":"Randy","sex":"male","score":20},function(err,result){
  if(!err){
    //do sth...
  } else //do sth...
})

查询(Find)

查询players集合中nameRandy的文档:

storedb('players').find({"name":"Randy"},function(err,result){
  if(!err){
    //use result to do sth...
  } else //do sth...
})

如果需要查询集合中所有文档,将参数设置为空即可:

storedb('players').find()

函数将返回一个数组类型。

更新(Update)

players集合中nameRandyscore增加10

storedb('players').update({"name":"Randy"},{"$inc":{"score":"10"}},function(err){
  if(!err){
    //do sth...
  } else //do sth...
})

你可能已经注意到,StoreDB拥有和MongoDB一样的修改器!关于修改器类型请查看API

如果修改器为空,则默认为$set修改器:

storedb('players').update({"name":"Randy"}, {"sex":"male","name":"kriss"})

删除(Remove)

删除在players集合中nameRandy的一条文档:

storedb('players').remove({"name":"Randy"},function(err){
  if(!err){
    //do sth...
  } else //do sth...
})

如果要把整个集合删除,把参数设置为空:

storedb('players').remove()

APIs

storedb(collectionName)

  • collectionNamestring,需要操作的集合名。如果集合不存在,则自动创建。
.insert(newObj,callback)
  • newObjJSON object,插入的文档。
  • callbackfunction,包含参数errresult:无错误时err返回undefinedresult返回此次创建的文档对象。
  • 系统会自动为每一条文档创建unix时间戳id——_id,可通过callback中的result._id查看插入文档时所创建的id。
.find()
  • 返回Array,该集合所有文档。
.find(matchObj,callback)
  • matchObjJSON object,匹配的文档
  • callbackfunction,包含参数errresult:无错误时err返回undefinedresult返回查询结果数组。
.update(matchObj,upsert,callback)
  • matchObjJSON object,匹配的文档
  • upsertJSON object,对象中key应为修改器类型,value为修改对象。例如:
storedb('collectionA').update({"foo":"hi"},{"$set":{"bar":"hello"}},function(err){})
  • callbackfunction,包含参数err:无错误时err返回undefined

修改器类型:

  • $inc:为目标增加(或减小)对应数值
  • $set: 修改目标内容
  • $push:为目标数组插入对应元素
.remove()
  • 移除该集合所有文档
.remove(matchObj,callback)
  • matchObjJSON object,匹配的对应要删除的文档。
  • callbackfunction,包含参数err:无错误时err返回undefined

Donate

由于支付宝取消支付页面,如果你支持 StoreDB,可以通过转账到我的支付宝账户 djyde520@gmail.com