UNPKG

3.83 kBMarkdownView Raw
1# LocalDB
2
3[![js-standard-style](https://cdn.rawgit.com/feross/standard/master/badge.svg)](https://github.com/feross/standard)
4[![NPM](https://nodei.co/npm/localdb.png)](https://nodei.co/npm/localdb/)
5
6## Example
7
8```javascript
9import localdb from 'localdb'
10const Notes = new localdb('notes', 'Array', true)
11
12// insert some collections and return the collections
13let notes = Notes
14 .add({title: 'Today is a big day', category: 'diary'})
15 .add({title: 'I met my ex today', category: 'diary'})
16 .add({title: 'Levandowski is amazing!', category: 'football'})
17 .get()
18
19// remove all post categoried in football
20Notes.remove('category', 'football')
21
22// find posts and update
23const query = {title: 'diary'}
24const opts = {limit: 2, sort: 1, sortBy: 'title', skip: 0}
25Notes.find(query, opts).forEach(note => {
26 note.author = 'egoist'
27 Notes.save(note)
28})
29
30// override the whole database and generate meta
31Notes.override([{title: 'New post'}], true)
32
33// populate another class, eg: your Post have a Author field
34const Post = new localdb('Post', 'Array')
35const User = new localdb('User', 'Array')
36
37// you should have the Author's objectId to create an instance of that class
38const author = User.extend('some_object_id')
39
40Post.add({
41 title: 'mt post title',
42 author: author
43})
44
45// then you can populate that field before .find or .findOne
46Post.populate('author').findOne()
47
48// create an Object database and set some property
49const Site = new localdb('site', 'Object')
50Site.set('sitename', 'Google')
51
52// get sitename
53const sitename = Site.get('sitename')
54
55// destroy some database
56Site.destroy()
57```
58
59## API
60
61### new localdb(name:String, type = 'Array', timestamp = false)
62### new localdb(opts:Object)
63
64创建一个新的数据库,可选类型为 `Array`,`Object`,并赋值给变量 `db`
65
66### db.add(obj:Object)
67
68当类型为 `Array` 时可用,在数据库中增加一条集合
69
70### db.get(where)
71
72`where``null` 时,返回数据库中的内容,返回类型为 `null``Object``Array`
73
74`where` 的类型为 `string` 或者 `number` 时返回对应的 `Object[key]` 或者 `Array[index]`
75
76### db.findOne(query:Object)
77
78查询并返回符合条件的一个集合
79
80### db.find(query:Object, opts:Object)
81
82查询并返回数个集合
83
84```javascript
85var opts = { limit: 0, sortBy: 'index', sort: 1, skip: 0 }
86```
87
88### db.save(obj:Oject)
89
90当类型为 `Array` 时可用,obj 为 `.findOne``.find` 返回的结果,类型为 `Object`,你可以作出更改之后用 `.save` 更新到数据库
91
92### db.set(key:String, value)
93
94当类型为 `Object` 时可用,更新此数据库的一个键值对,没有则新建
95
96### db.remove(key:String, value)
97
98当类型为 `Array` 按键值对删除对应的集合
99当类型为 `Object` 时直接删除该 key
100
101### db.extend(objectId)
102
103创建一个该数据库的 `Pointer` 用于 `populate`
104
105### db.populate(field)
106
107`.find``.findOne` 时获取该 `field` 指向的 `collection`
108
109### db.override(colleciton, reinit = false)
110
111`collection` 整个覆盖旧的数据库
112
113`reinit``true` 将自动按照 `opts` 配置为每个 `Object` 生成 `_id` `createdAt` `updatedAt`
114
115### db.destroy()
116
117销毁数据库
118
119---
120
121相关文章: [一个简单的 localStorage 扩展实现](https://egoist.github.io/2015/09/30/a-light-weight-localstorage-orm/)
122
123## License
124
125This 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.
126
127permitted use:
128fork on github
129change
130do evil with your copy
131
132prohibted use:
133do evil with copies not of your own
134open source your copy without declaring your parent copy