Lowdb
Work in progress
A small local database for small projects :cat: (powered by lodash API)
const db = low('db.json')
db.defaults({ posts: [], user: {} })
.value()
db.get('posts')
.push({ id: 1, title: 'lowdb is awesome'})
.value()
db.get('posts')
.find({ id: 1 })
.value()
db.set('user.name', 'typicode')
.value()
Data is automatically saved to db.json
.
{
"posts": [
{ "id": 1, "title": "lowdb is awesome"}
],
"user": {
"name": "typicode"
}
}
Lowdb is perfect for CLIs, small servers, Electron apps and npm packages in general.
It supports Node, the browser and uses lodash API, so it's very simple to learn. Actually... you may already know how to use lowdb :wink:
Migrating from from 0.12 to 0.13? See this guide.
Why lowdb?
- Lodash API
- Minimal and simple to use
- Highly flexible
- Custom storage (file, browser, in-memory, ...)
- Custom format (JSON, BSON, YAML, XML, ...)
- Mixins (id support, ...)
- Read-only or write-only modes
- Encryption
Important lowdb doesn't support Cluster.
Install
npm install lowdb@next lodash@4 --save
A standalone UMD build is also available on npmcdn for testing and quick prototyping:
<script src="http://npmcdn.com/lodash/dist/lodash.min.js"></script>
<script src="http://npmcdn.com/lowdb/dist/lowdb.min.js"></script>
<script>
var db = low('db')
</script>
API
low([source, [options])
source
string or null, will be passed to storageoptions
object
storage
object, by default lowdb/lib/file-sync
or lowdb/lib/browser
.
read
function or nullwrite
function or null
format
object
serialize
function, by default JSON.stringify
deserialize
function, by default JSON.parse
writeOnChange
boolean
Creates a database instance. Use options to configure lowdb, here are some examples:
low()
low('db.json', { storage: require('lowdb/lib/file-async') })
low('some-source', { storage: require('./my-custom-storage') })
low('db.json', { writeOnChange: false })
const fileSync = require('lowdb/lib/file-sync')
low('db.json', {
storage: {
read: fileSync.read
}
})
low('db.json', {
storage: {
write: fileSync.write
}
})
db._
Database lodash instance. Use it to add your own utility functions or third-party mixins like underscore-contrib or underscore-db.
db._.mixin({
second: function(array) {
return array[1]
}
})
const post1 = db.get('posts').first().value()
const post2 = db.get('posts').second().value()
db.state()
Use whenever you want to access or modify the underlying database object.
db.state()
You can use it to drop database or replace it with a new object
const newState = {}
db.state(newState)
db.write()
db.write([source])
Persists database using storage.write
option. Depending on the storage, it may return a promise (for example, with `file-async').
By default, lowdb automatically calls it when database changes.
const db = low('db.json')
db.write()
db.write('copy.json')
db.read([source])
Reads source using storage.read
option. Depending on the storage, it may return a promise.
const db = low('db.json')
db.read()
db.read('copy.json')
Guide
How to query
With lowdb, you get access to the entire lodash API, so there are 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 methods is lazy, that is, execution is deferred until .value()
is called.
Examples
Check if posts exists.
db.has('posts')
Set posts.
db.set('posts', []).value()
Sort the top five posts.
db.get('posts')
.filter({published: true})
.sortBy('views')
.take(5)
.value()
Retrieve post titles.
db.get('posts')
.map('title')
.value()
Get the number of posts.
db.get('posts').size()
Make a deep clone of posts.
db.get('posts').cloneDeep()
Update a post.
db('posts')
.chain()
.find({ title: 'low!' })
.assign({ title: 'hi!'})
.value()
Remove posts.
db.get('posts').remove({ title: 'low!' })
How to use id based resources
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.
const db = low('db.json')
db._.mixin(require('underscore-db'))
const postId = db.get('posts').insert({ title: 'low!' }).value().id
const post = db.get('posts').getById(postId).value()
uuid is more minimalist and returns a unique id that you can use when creating resources.
const uuid = require('uuid')
const postId = db.get('posts').push({ id: uuid(), title: 'low!' }).value().id
const post = db.get('posts').find({ id: postId }).value()
How to use custom storage or format
low()
accepts custom storage or format. Simply create objects with read/write
or serialize/deserialize
methods. See src/file-sync.js
code source for a full example.
const myStorage = {
read: (source, deserialize) =>
write: (dest, obj, serialize) =>
}
const myFormat = {
format: {
serialize: (obj) =>
deserialize: (data) =>
}
}
low(source, {
storage: myStorage,
format: myFormat
})
How to encrypt data
Simply encrypt
and decrypt
data in format.serialize
and format.deserialize
methods.
For example, using cryptr:
const Cryptr = require("./cryptr"),
const cryptr = new Cryptr('my secret key')
const db = low('db.json', {
format: {
deserialize: (str) => {
const decrypted = cryptr.decrypt(str)
const obj = JSON.parse(decrypted)
return obj
},
serialize: (obj) => {
const str = JSON.stringify(obj)
const encrypted = cryptr.encrypt(str)
return encrypted
}
}
})
Changelog
See changes for each version in the release notes.
Limits
lowdb is a convenient method for storing data without setting up a database server. It is fast enough and safe to be used as an embedded database.
However, if you seek high performance and scalability more than simplicity, you should probably stick to traditional databases like MongoDB.
License
MIT - Typicode