UNPKG

4.46 kBMarkdownView Raw
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)
2
3> Flat JSON file database for Node
4
5* Serverless
6* Multiple databases
7* In-memory or disk-based
8* 80+ methods from Lo-Dash API
9* Asynchronous and fault-tolerant writing
10* Extendable
11
12LowDB 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/). If you need something similar for the browser, check [Underscore-db](https://github.com/typicode/underscore-db)._
15
16## Usage
17
18```javascript
19var low = require('lowdb')
20var db = low('db.json')
21
22db('songs').push({ title: 'low!'})
23```
24
25Database is automatically created and saved to `db.json` in a readable format.
26
27```javascript
28{
29 "songs": [
30 {
31 "title": "low!"
32 }
33 ]
34}
35```
36
37Data can be queried and manipulated using any Lo-Dash method.
38
39```javascript
40var song = db('songs').find({ title: 'low!' }).value()
41db('songs').remove({ title: 'low!' })
42```
43
44You can also use id-based methods by extending LowDB with [Underscore-db](https://github.com/typicode/underscore-db).
45
46## API
47
48__low([filename])__
49
50Creates a disk-based or in-memory database instance. If a filename is provided, it loads or creates it.
51
52```javascript
53var db = low() // in-memory
54var db = low('db.json') // disk-based
55```
56
57__low.mixin(source)__
58
59Use it to extend Lo-Dash globally with your own utility functions or third-party libraries.
60
61```javascript
62// Must be called before calling db('songs') for functions to be available.
63low.mixin({
64 second: function(array) {
65 if (array.length >= 2) return array[1]
66 }
67})
68
69var song = db('songs').second().value()
70```
71
72__db.object__
73
74Database object. Useful if you want to access the content of your JSON file and don't want to go through Lo-Dash methods.
75
76```javascript
77console.log(db.object) // { songs: [ { title: 'low!' } ] }
78db('songs').value() === db.object.songs
79```
80
81__db.save()__
82
83LowDB automatically saves to disk. However, in case you directly modify the content of the database object, you'll need to manually call `save`.
84
85```javascript
86delete db.object.songs
87db.save()
88```
89
90## Documentation
91
92### Operations
93
94With 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.
95
96Sort the top five songs.
97
98```javascript
99db('songs')
100 .where({published: true})
101 .sortBy('views')
102 .first(5)
103 .value()
104```
105
106Retrieve song titles.
107
108```javascript
109db('songs')
110 .pluck('titles')
111 .value()
112```
113
114Get the number of songs.
115
116```javascript
117db('songs').size()
118```
119
120Make a deep clone of songs.
121
122```javascript
123db('songs').cloneDeep().value
124```
125
126Update a song.
127
128```javascript
129db('songs').find({ title: 'low!' }).assign({ title: 'hi!'})
130```
131
132Remove songs.
133
134```javascript
135db('songs').remove({ title: 'low!' })
136```
137
138### Id-based resources support
139
140Being 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.
141
142[Underscore-db](https://github.com/typicode/underscore-db) provides a set of helpers for creating and manipulating id-based resources.
143
144```javascript
145low.mixin(require('underscore-db'))
146
147var db = low('db.json')
148
149var songId = db('songs').insert({ title: 'low!' }).value().id
150var song = db('songs').get(songId).value()
151```
152
153Or simply use [uuid](https://github.com/broofa/node-uuid).
154
155```javascript
156var uuid = require('uuid')
157
158var songId = db('songs').push({ id: uuid(), title: 'low!' }).value().id
159var song = db('songs').find({ id: songId }).value()
160```
161
162In both cases, your `db.json` will then look like this.
163
164```javascript
165{
166 "songs": [
167 {
168 "id": "e31aa48c-a9d8-4f79-9fce-ded4c16c3c4c",
169 "title": "low!"
170 }
171 ]
172}
173```
174
175## Changelog
176
177See details changes for each version in the [release notes](https://github.com/typicode/lowdb/releases).
178
179## Limits
180
181LowDB 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.
182
183However, if you need high performance and scalability more than simplicity, you should stick to databases like MongoDB.
184
185## License
186
187LowDB is released under the MIT License.