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
|
9 | import localdb from 'localdb'
|
10 | const Notes = new localdb('notes', 'Array', true)
|
11 |
|
12 | // insert some collections and return the collections
|
13 | let 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
|
20 | Notes.remove('category', 'football')
|
21 |
|
22 | // find posts and update
|
23 | const query = {title: 'diary'}
|
24 | const opts = {limit: 2, sort: 1, sortBy: 'title', skip: 0}
|
25 | Notes.find(query, opts).forEach(note => {
|
26 | note.author = 'egoist'
|
27 | Notes.save(note)
|
28 | })
|
29 |
|
30 | // override the whole database and generate meta
|
31 | Notes.override([{title: 'New post'}], true)
|
32 |
|
33 | // populate another class, eg: your Post have a Author field
|
34 | const Post = new localdb('Post', 'Array')
|
35 | const User = new localdb('User', 'Array')
|
36 |
|
37 | // you should have the Author's objectId to create an instance of that class
|
38 | const author = User.extend('some_object_id')
|
39 |
|
40 | Post.add({
|
41 | title: 'mt post title',
|
42 | author: author
|
43 | })
|
44 |
|
45 | // then you can populate that field before .find or .findOne
|
46 | Post.populate('author').findOne()
|
47 |
|
48 | // create an Object database and set some property
|
49 | const Site = new localdb('site', 'Object')
|
50 | Site.set('sitename', 'Google')
|
51 |
|
52 | // get sitename
|
53 | const sitename = Site.get('sitename')
|
54 |
|
55 | // destroy some database
|
56 | Site.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
|
85 | var 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 |
|
125 | 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.
|
126 |
|
127 | permitted use:
|
128 | fork on github
|
129 | change
|
130 | do evil with your copy
|
131 |
|
132 | prohibted use:
|
133 | do evil with copies not of your own
|
134 | open source your copy without declaring your parent copy
|