Skip to content

Commit 513f62c

Browse files
committed
Merge pull request #34 from typicode/0.7.0
Update to lodash 3.0.1
2 parents c42ef38 + 291d3f3 commit 513f62c

File tree

6 files changed

+250
-174
lines changed

6 files changed

+250
-174
lines changed

README.md

Lines changed: 66 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -1,54 +1,50 @@
1-
# LowDB [![Build Status](https://travis-ci.org/typicode/lowdb.svg?branch=master)](https://travis-ci.org/typicode/lowdb) [![](https://img.shields.io/npm/v/lowdb.svg?style=flat)](https://www.npmjs.com/package/lowdb)
1+
# lowdb [![NPM version](https://badge.fury.io/js/lowdb.svg)](http://badge.fury.io/js/lowdb) [![Build Status](https://travis-ci.org/typicode/lowdb.svg?branch=master)](https://travis-ci.org/typicode/lowdb)
22

3-
> Flat JSON file database for Node
3+
> Need a quick way to get a local database?
44
5-
* Serverless
6-
* Multiple databases
7-
* In-memory or disk-based
8-
* 80+ methods from Lo-Dash API
9-
* Atomic writing
10-
* Extendable
11-
12-
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
176

187
```javascript
198
var low = require('lowdb')
209
var db = low('db.json')
2110

22-
db('songs').push({ title: 'low!'})
11+
db('posts').push({ title: 'lowdb is awesome'})
2312
```
2413

25-
Database is automatically created and saved to `db.json` in a readable format.
14+
Database is __automatically__ saved to `db.json`
2615

2716
```javascript
2817
{
29-
"songs": [
30-
{
31-
"title": "low!"
32-
}
18+
"posts": [
19+
{ "title": "lowdb is awesome" }
3320
]
3421
}
3522
```
3623

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__.
3825

3926
```javascript
40-
var song = db('songs').find({ title: 'low!' }).value()
41-
db('songs').remove({ title: 'low!' })
27+
db('posts').find({ title: 'lowdb is awesome' })
4228
```
4329

44-
You can also use id-based methods by extending LowDB with [Underscore-db](https://github.com/typicode/underscore-db).
45-
4630
## Install
4731

4832
```bash
4933
npm install lowdb --save
5034
````
5135

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+
5248
## API
5349
5450
__low([filename, options])__
@@ -64,24 +60,25 @@ When a filename is provided you can set options.
6460
6561
```javascript
6662
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)
6965
})
7066
```
7167
7268
__low.mixin(source)__
7369
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.
7571
7672
```javascript
7773
// Must be called before calling db('songs') for functions to be available.
7874
low.mixin({
7975
second: function(array) {
80-
if (array.length >= 2) return array[1]
76+
return array[1]
8177
}
8278
})
8379
84-
var song = db('songs').second().value()
80+
var song1 = db('songs').first()
81+
var song2 = db('songs').second()
8582
```
8683
8784
__low.stringify(obj)__ and __low.parse(str)__
@@ -118,18 +115,21 @@ __db.saveSync([filename])__
118115

119116
Synchronous version of `db.save()`
120117
121-
## Documentation
118+
## Guide
122119
123120
### Operations
124121
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.
126123
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.
128127
129128
Sort the top five songs.
130129
131130
```javascript
132131
db('songs')
132+
.chain()
133133
.where({published: true})
134134
.sortBy('views')
135135
.first(5)
@@ -139,9 +139,7 @@ db('songs')
139139
Retrieve song titles.
140140
141141
```javascript
142-
db('songs')
143-
.pluck('titles')
144-
.value()
142+
db('songs').pluck('titles')
145143
```
146144
147145
Get the number of songs.
@@ -153,13 +151,17 @@ db('songs').size()
153151
Make a deep clone of songs.
154152
155153
```javascript
156-
db('songs').cloneDeep().value()
154+
db('songs').cloneDeep()
157155
```
158156
159157
Update a song.
160158
161159
```javascript
162-
db('songs').find({ title: 'low!' }).assign({ title: 'hi!'})
160+
db('songs')
161+
.chain()
162+
.find({ title: 'low!' })
163+
.assign({ title: 'hi!'})
164+
.value()
163165
```
164166
165167
Remove songs.
@@ -168,40 +170,48 @@ Remove songs.
168170
db('songs').remove({ title: 'low!' })
169171
```
170172
171-
### Id-based resources support
173+
### Id support
172174
173-
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.
174176
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.
176178
177179
```javascript
178180
low.mixin(require('underscore-db'))
179181
180182
var db = low('db.json')
181183
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)
184186
```
185187
186-
Or simply use [uuid](https://github.com/broofa/node-uuid).
188+
[uuid](https://github.com/broofa/node-uuid) returns a unique id.
187189
188190
```javascript
189191
var uuid = require('uuid')
190192
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 })
193195
```
194196
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.
196200
197201
```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)
206+
207+
low.stringify = function(obj) {
208+
var str = JSON.stringify(obj)
209+
return cipher.update(str, 'utf8', 'hex') + cipher.final('hex')
210+
}
211+
212+
low.parse = function(encrypted) {
213+
var str = decipher.update(encrypted, 'hex', 'utf8') + decipher.final('utf8')
214+
return JSON.parse(str)
205215
}
206216
```
207217
@@ -211,10 +221,10 @@ See details changes for each version in the [release notes](https://github.com/t
211221
212222
## Limits
213223
214-
LowDB is a convenient method for storing data without setting up a database server. It's fast enough and safe to be used as an embedded database.
224+
lowdb is a convenient method for storing data without setting up a database server. It's fast enough and safe to be used as an embedded database.
215225
216226
However, if you need high performance and scalability more than simplicity, you should stick to databases like MongoDB.
217227
218228
## License
219229
220-
LowDB is released under the MIT License.
230+
MIT - [Typicode](https://github.com/typicode)

package.json

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
{
22
"name": "lowdb",
3-
"version": "0.6.1",
3+
"version": "0.7.0",
44
"description": "Flat JSON file database",
55
"keywords": [
66
"flat",
77
"file",
8+
"local",
89
"database",
910
"JSON",
1011
"lo-dash",
@@ -30,13 +31,14 @@
3031
},
3132
"homepage": "https://github.com/typicode/lowdb",
3233
"dependencies": {
33-
"lodash": "~2.4.1",
34-
"steno": "^0.3.1"
34+
"lodash": "^3.0.1",
35+
"steno": "^0.3.2"
3536
},
3637
"devDependencies": {
3738
"husky": "^0.6.2",
3839
"mocha": "^1.21.4",
3940
"rimraf": "^2.2.8",
41+
"sinon": "^1.12.2",
4042
"underscore-db": "^0.8.0"
4143
}
4244
}

src/disk.js

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
var fs = require('fs')
2+
var path = require('path')
3+
var _ = require('lodash')
4+
var steno = require('steno')
5+
6+
function getTempFile(file) {
7+
return path.join(
8+
path.dirname(file),
9+
'.~' + path.basename(file)
10+
)
11+
}
12+
13+
module.exports = {
14+
read: function (file) {
15+
if (fs.existsSync(file)) return fs.readFileSync(file)
16+
},
17+
18+
write: function(file, data) {
19+
steno(getTempFile(file))
20+
.setCallback(function(err, data, next) {
21+
if (err) throw err
22+
fs.rename(getTempFile(file), file, function(err) {
23+
if (err) throw err
24+
next()
25+
})
26+
})
27+
.write(data)
28+
},
29+
30+
writeSync: function(file, data) {
31+
fs.writeFileSync(getTempFile(file), data)
32+
fs.renameSync(getTempFile(file), file)
33+
}
34+
}

0 commit comments

Comments
 (0)