You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
LowDB uses Lo-Dash functional programming API instead of a MongoDB-like API. This makes it quite unique and different.
13
-
14
-
_LowDB powers [JSON Server](https://github.com/typicode/json-server) and [JSONPlaceholder](http://jsonplaceholder.typicode.com/). See also [underscore-db](https://github.com/typicode/underscore-db)._
15
-
16
-
## Usage
5
+
## Example
17
6
18
7
```javascript
19
8
var low =require('lowdb')
20
9
var db =low('db.json')
21
10
22
-
db('songs').push({ title:'low!'})
11
+
db('posts').push({ title:'lowdb is awesome'})
23
12
```
24
13
25
-
Database is automatically created and saved to `db.json` in a readable format.
14
+
Database is __automatically__saved to `db.json`
26
15
27
16
```javascript
28
17
{
29
-
"songs": [
30
-
{
31
-
"title":"low!"
32
-
}
18
+
"posts": [
19
+
{ "title":"lowdb is awesome" }
33
20
]
34
21
}
35
22
```
36
23
37
-
Data can be queried and manipulated using any Lo-Dash method.
24
+
You can query and manipulate it using __any lodash 3.0 method__.
38
25
39
26
```javascript
40
-
var song =db('songs').find({ title:'low!' }).value()
41
-
db('songs').remove({ title:'low!' })
27
+
db('posts').find({ title:'lowdb is awesome' })
42
28
```
43
29
44
-
You can also use id-based methods by extending LowDB with [Underscore-db](https://github.com/typicode/underscore-db).
45
-
46
30
## Install
47
31
48
32
```bash
49
33
npm install lowdb --save
50
34
````
51
35
36
+
## Features
37
+
38
+
* Small
39
+
* Serverless
40
+
* lodash rich API
41
+
* In-memory or disk-based
42
+
* __Hackable__ (mixins, id, encryption, ...)
43
+
44
+
It's also __very easy to learn and use__ since it has __only 8 methods and properties__.
45
+
46
+
_lowdb powers [json-server](https://github.com/typicode/json-server) package and [jsonplaceholder](http://jsonplaceholder.typicode.com/) website._
47
+
52
48
## API
53
49
54
50
__low([filename, options])__
@@ -64,24 +60,25 @@ When a filename is provided you can set options.
64
60
65
61
```javascript
66
62
var db = low('db.json', {
67
-
autosave: true, // changes are automatically written to file (true by default)
68
-
async: true // changes are written synchronously or asynchronously (true by default)
63
+
autosave: true, // automatically save database on change (default: true)
64
+
async: true // asyncrhonous write (default: true)
69
65
})
70
66
```
71
67
72
68
__low.mixin(source)__
73
69
74
-
Use it to extend Lo-Dash globally with your own utility functions or third-party libraries.
70
+
Use it to extend lodash globally with your own utility functions or third-party libraries.
75
71
76
72
```javascript
77
73
// Must be called before calling db('songs') for functions to be available.
78
74
low.mixin({
79
75
second: function(array) {
80
-
if (array.length >= 2) return array[1]
76
+
return array[1]
81
77
}
82
78
})
83
79
84
-
var song = db('songs').second().value()
80
+
var song1 = db('songs').first()
81
+
var song2 = db('songs').second()
85
82
```
86
83
87
84
__low.stringify(obj)__ and __low.parse(str)__
@@ -118,18 +115,21 @@ __db.saveSync([filename])__
118
115
119
116
Synchronous version of `db.save()`
120
117
121
-
## Documentation
118
+
## Guide
122
119
123
120
### Operations
124
121
125
-
With LowDB you get access to the entire [Lo-Dash API](http://lodash.com/), so there's many, many ways to query and manipulate data. Here are a few examples to get you started.
122
+
With LowDB you get access to the entire [lodash API](http://lodash.com/), so there's many ways to query and manipulate data. Here are a few examples to get you started.
126
123
127
-
Please note also that data is returned by reference, this means that modifications to returned objects may change the database. To avoid such behaviour, you need to use `.cloneDeep().value()`.
124
+
Please note that data is returned by reference, this means that modifications to returned objects may change the database. To avoid such behaviour, you need to use `.cloneDeep()`.
125
+
126
+
Also, the execution of chained methods is lazy, that is, execution is deferred until `.value()` is called.
Being able to retrieve data using an id can be quite useful, particularly in servers. To add id-based resources support to LowDB, you have 2 options.
175
+
Being able to retrieve data using an id can be quite useful, particularly in servers. To add id-based resources support to lowdb, you have 2 options.
174
176
175
-
[Underscore-db](https://github.com/typicode/underscore-db) provides a set of helpers for creating and manipulating id-based resources.
177
+
[underscore-db](https://github.com/typicode/underscore-db) provides a set of helpers for creating and manipulating id-based resources.
176
178
177
179
```javascript
178
180
low.mixin(require('underscore-db'))
179
181
180
182
var db = low('db.json')
181
183
182
-
var songId = db('songs').insert({ title: 'low!'}).value().id
183
-
var song = db('songs').get(songId).value()
184
+
var songId = db('songs').insert({ title: 'low!' }).id
185
+
var song = db('songs').get(songId)
184
186
```
185
187
186
-
Or simply use [uuid](https://github.com/broofa/node-uuid).
188
+
[uuid](https://github.com/broofa/node-uuid) returns a unique id.
187
189
188
190
```javascript
189
191
var uuid = require('uuid')
190
192
191
-
var songId = db('songs').push({ id: uuid(), title: 'low!'}).value().id
192
-
var song = db('songs').find({ id: songId }).value()
193
+
var songId = db('songs').push({ id: uuid(), title: 'low!' }).id
194
+
var song = db('songs').find({ id: songId })
193
195
```
194
196
195
-
In both cases, your `db.json` will then look like this.
197
+
### Encryption support
198
+
199
+
In some cases, you may want to make it harder to read database content. By overwriting, `low.stringify` and `low.parse`, you can add custom encryption.
196
200
197
201
```javascript
198
-
{
199
-
"songs": [
200
-
{
201
-
"id": "e31aa48c-a9d8-4f79-9fce-ded4c16c3c4c",
202
-
"title": "low!"
203
-
}
204
-
]
202
+
var crypto = require('crypto')
203
+
204
+
var cipher = crypto.createCipher('aes256', secretKey)
205
+
var decipher = crypto.createDecipher('aes256', secretKey)
0 commit comments