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 |
|
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/). If you need something similar for the browser, check [Underscore-db](https://github.com/typicode/underscore-db)._
|
15 |
|
16 | ## Usage
|
17 |
|
18 | ```javascript
|
19 | var low = require('lowdb')
|
20 | var db = low('db.json')
|
21 |
|
22 | db('songs').push({ title: 'low!'})
|
23 | ```
|
24 |
|
25 | Database 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 |
|
37 | Data can be queried and manipulated using any Lo-Dash method.
|
38 |
|
39 | ```javascript
|
40 | var song = db('songs').find({ title: 'low!' }).value()
|
41 | db('songs').remove({ title: 'low!' })
|
42 | ```
|
43 |
|
44 | You 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 |
|
50 | Creates a disk-based or in-memory database instance. If a filename is provided, it loads or creates it.
|
51 |
|
52 | ```javascript
|
53 | var db = low() // in-memory
|
54 | var db = low('db.json') // disk-based
|
55 | ```
|
56 |
|
57 | __low.mixin(source)__
|
58 |
|
59 | Use 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.
|
63 | low.mixin({
|
64 | second: function(array) {
|
65 | if (array.length >= 2) return array[1]
|
66 | }
|
67 | })
|
68 |
|
69 | var song = db('songs').second().value()
|
70 | ```
|
71 |
|
72 | __db.object__
|
73 |
|
74 | Database 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
|
77 | console.log(db.object) // { songs: [ { title: 'low!' } ] }
|
78 | db('songs').value() === db.object.songs
|
79 | ```
|
80 |
|
81 | __db.save()__
|
82 |
|
83 | LowDB 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
|
86 | delete db.object.songs
|
87 | db.save()
|
88 | ```
|
89 |
|
90 | ## Documentation
|
91 |
|
92 | ### Operations
|
93 |
|
94 | 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.
|
95 |
|
96 | Sort the top five songs.
|
97 |
|
98 | ```javascript
|
99 | db('songs')
|
100 | .where({published: true})
|
101 | .sortBy('views')
|
102 | .first(5)
|
103 | .value()
|
104 | ```
|
105 |
|
106 | Retrieve song titles.
|
107 |
|
108 | ```javascript
|
109 | db('songs')
|
110 | .pluck('titles')
|
111 | .value()
|
112 | ```
|
113 |
|
114 | Get the number of songs.
|
115 |
|
116 | ```javascript
|
117 | db('songs').size()
|
118 | ```
|
119 |
|
120 | Make a deep clone of songs.
|
121 |
|
122 | ```javascript
|
123 | db('songs').cloneDeep().value
|
124 | ```
|
125 |
|
126 | Update a song.
|
127 |
|
128 | ```javascript
|
129 | db('songs').find({ title: 'low!' }).assign({ title: 'hi!'})
|
130 | ```
|
131 |
|
132 | Remove songs.
|
133 |
|
134 | ```javascript
|
135 | db('songs').remove({ title: 'low!' })
|
136 | ```
|
137 |
|
138 | ### Id-based resources support
|
139 |
|
140 | 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.
|
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
|
145 | low.mixin(require('underscore-db'))
|
146 |
|
147 | var db = low('db.json')
|
148 |
|
149 | var songId = db('songs').insert({ title: 'low!' }).value().id
|
150 | var song = db('songs').get(songId).value()
|
151 | ```
|
152 |
|
153 | Or simply use [uuid](https://github.com/broofa/node-uuid).
|
154 |
|
155 | ```javascript
|
156 | var uuid = require('uuid')
|
157 |
|
158 | var songId = db('songs').push({ id: uuid(), title: 'low!' }).value().id
|
159 | var song = db('songs').find({ id: songId }).value()
|
160 | ```
|
161 |
|
162 | In 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 |
|
177 | See details changes for each version in the [release notes](https://github.com/typicode/lowdb/releases).
|
178 |
|
179 | ## Limits
|
180 |
|
181 | 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.
|
182 |
|
183 | However, if you need high performance and scalability more than simplicity, you should stick to databases like MongoDB.
|
184 |
|
185 | ## License
|
186 |
|
187 | LowDB is released under the MIT License.
|