LocalDB
Example
const Notes = 'notes' 'Array' true // insert some collections and return the collectionslet notes = Notes // remove all post categoried in footballNotes // find posts and updateconst query = title: 'diary'const opts = limit: 2 sort: 1 sortBy: 'title' skip: 0Notes // override the whole database and generate metaNotes // populate another class, eg: your Post have a Author fieldconst Post = 'Post' 'Array'const User = 'User' 'Array' // you should have the Author's objectId to create an instance of that classconst author = User Post // then you can populate that field before .find or .findOnePost // create an Object database and set some propertyconst Site = 'site' 'Object'Site // get sitenameconst sitename = Site // destroy some databaseSite
API
new localdb(name:String, type = 'Array', timestamp = false)
new localdb(opts:Object)
创建一个新的数据库,可选类型为 Array
,Object
,并赋值给变量 db
db.add(obj:Object)
当类型为 Array
时可用,在数据库中增加一条集合
db.get(where)
where
为 null
时,返回数据库中的内容,返回类型为 null
或 Object
或 Array
where
的类型为 string
或者 number
时返回对应的 Object[key]
或者 Array[index]
db.findOne(query:Object)
查询并返回符合条件的一个集合
db.find(query:Object, opts:Object)
查询并返回数个集合
var opts = limit: 0 sortBy: 'index' sort: 1 skip: 0
db.save(obj:Oject)
当类型为 Array
时可用,obj 为 .findOne
或 .find
返回的结果,类型为 Object
,你可以作出更改之后用 .save
更新到数据库
db.set(key:String, value)
当类型为 Object
时可用,更新此数据库的一个键值对,没有则新建
db.inc/dec(key, number)
当类型为 Object
时可用,让一个 key 的值自增/自减 number 个单位, key 值不存在时默认为 0
db.remove(key:String, value)
当类型为 Array
按键值对删除对应的集合
当类型为 Object
时直接删除该 key
db.extend(objectId)
创建一个该数据库的 Pointer
用于 populate
db.populate(field)
在 .find
或 .findOne
时获取该 field
指向的 collection
db.override(colleciton, reinit = false)
用 collection
整个覆盖旧的数据库
reinit
为 true
将自动按照 opts
配置为每个 Object
生成 _id
createdAt
updatedAt
db.destroy()
销毁数据库
相关文章: 一个简单的 localStorage 扩展实现
License
This project is released under SOX license that means you can do whatever you want to do, but you have to open source your copy on github if you let the public uses it. All copies should be released under the same license. The owner of each copy is only reponsible for his own copy, not for the parents, not for the children.
permitted use:
fork on github
change
do evil with your copy
prohibted use:
do evil with copies not of your own
open source your copy without declaring your parent copy