Need a quick way to get a local database?
var low = require('lowdb')
var db = low('db.json')
db('posts').push({ title: 'lowdb is awesome'})
Database is automatically saved to db.json
{
"posts": [
{ "title": "lowdb is awesome" }
]
}
You can query and manipulate it using any lodash 3 method
db('posts').find({ title: 'lowdb is awesome' })
npm install lowdb --save
- Small
- Serverless
- lodash rich API
- In-memory or disk-based
- Hackable (mixins, id, encryption, ...)
It's also very easy to learn and use since it has only 8 methods and properties.
lowdb powers json-server package and jsonplaceholder website.
low([filename, options])
Creates a disk-based or in-memory database instance. If a filename is provided, it loads or creates it.
var db = low() // in-memory
var db = low('db.json') // disk-based
When a filename is provided you can set options.
var db = low('db.json', {
autosave: true, // automatically save database on change (default: true)
async: true // asyncrhonous write (default: true)
})
low.mixin(source)
Use it to extend lodash globally with your own utility functions or third-party libraries.
// Must be called before calling db('songs') for functions to be available.
low.mixin({
second: function(array) {
return array[1]
}
})
var song1 = db('songs').first()
var song2 = db('songs').second()
low.stringify(obj) and low.parse(str)
Overwrite these methods to customize JSON stringifying and parsing.
db.object
Database object. Useful for batch operations or to directly access the content of your JSON file.
console.log(db.object) // { songs: [ { title: 'low!' } ] }
db('songs').value() === db.object.songs
db.save([filename])
Saves database to file.
var db = low('db.json')
db.save() // saves to db.json
db.save('copy.json')
Note: In case you directly modify the content of the database object, you'll need to manually call save
delete db.object.songs
db.save()
db.saveSync([filename])
Synchronous version of db.save()
With LowDB you get access to the entire lodash API, so there's many ways to query and manipulate data. Here are a few examples to get you started.
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()
.
Also, the execution of chained methods is lazy, that is, execution is deferred until .value()
is called.
Sort the top five songs.
db('songs')
.chain()
.where({published: true})
.sortBy('views')
.first(5)
.value()
Retrieve song titles.
db('songs').pluck('titles')
Get the number of songs.
db('songs').size()
Make a deep clone of songs.
db('songs').cloneDeep()
Update a song.
db('songs')
.chain()
.find({ title: 'low!' })
.assign({ title: 'hi!'})
.value()
Remove songs.
db('songs').remove({ title: 'low!' })
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.
underscore-db provides a set of helpers for creating and manipulating id-based resources.
low.mixin(require('underscore-db'))
var db = low('db.json')
var songId = db('songs').insert({ title: 'low!' }).id
var song = db('songs').get(songId)
uuid returns a unique id.
var uuid = require('uuid')
var songId = db('songs').push({ id: uuid(), title: 'low!' }).id
var song = db('songs').find({ id: songId })
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.
var crypto = require('crypto')
var cipher = crypto.createCipher('aes256', secretKey)
var decipher = crypto.createDecipher('aes256', secretKey)
low.stringify = function(obj) {
var str = JSON.stringify(obj)
return cipher.update(str, 'utf8', 'hex') + cipher.final('hex')
}
low.parse = function(encrypted) {
var str = decipher.update(encrypted, 'hex', 'utf8') + decipher.final('utf8')
return JSON.parse(str)
}
See details changes for each version in the release notes.
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.
However, if you need high performance and scalability more than simplicity, you should stick to databases like MongoDB.
MIT - Typicode