Change storage, file format (JSON, YAML, ...) or add encryption via adapters
Extend it with lodash, ramda, ... for super powers!
Automatically switches to fast in-memory mode during tests
Install
npm install lowdb
Usage
Lowdb is a pure ESM package. If you're having trouble using it in your project, please read this.
import { JSONFilePreset } from 'lowdb/node'// Read or create db.jsonconst defaultData = { posts: [] }const db = await JSONFilePreset('db.json', defaultData)// Update db.jsonawait db.update(({ posts }) => posts.push('hello world'))// Alternatively you can call db.write() explicitely later// to write to db.jsondb.data.posts.push('hello world')await db.write()
// db.json{ "posts": [ "hello world" ]}
TypeScript
You can use TypeScript to check your data types.
type Data = { messages: string[]}const defaultData: Data = { messages: [] }const db = await JSONPreset<Data>('db.json', defaultData)db.data.messages.push('foo') // ✅ Successdb.data.messages.push(1) // ❌ TypeScript error
Lodash
You can extend lowdb with Lodash (or other libraries). To be able to extend it, we're not using JSONPreset here. Instead, we're using lower components.
import { Low } from 'lowdb'import { JSONFile } from 'lowdb/node'import lodash from 'lodash'type Post = { id: number title: string}type Data = { posts: Post[]}// Extend Low class with a new `chain` fieldclass LowWithLodash<T> extends Low<T> { chain: lodash.ExpChain<this['data']> = lodash.chain(this).get('data')}const defaultData: Data = { posts: [],}const adapter = new JSONFile<Data>('db.json', defaultData)const db = new LowWithLodash(adapter)await db.read()// Instead of db.data use db.chain to access lodash APIconst post = db.chain.get('posts').find({ id: 1 }).value() // Important: value() must be called to execute chain
If you've published an adapter for lowdb, feel free to create a PR to add it here.
Writing your own adapter
You may want to create an adapter to write db.data to YAML, XML, encrypt data, a remote storage, ...
An adapter is a simple class that just needs to expose two methods:
class AsyncAdapter { read() { /* ... */ } // should return Promise<data> write(data) { /* ... */ } // should return Promise<void>}class SyncAdapter { read() { /* ... */ } // should return data write(data) { /* ... */ } // should return nothing}
For example, let's say you have some async storage and want to create an adapter for it:
import { api } from './AsyncStorage'class CustomAsyncAdapter { // Optional: your adapter can take arguments constructor(args) { // ... } async read() { const data = await api.read() return data } async write(data) { await api.write(data) }}const adapter = new CustomAsyncAdapter()const db = new Low(adapter)
To create an adapter for another format than JSON, you can use TextFile or TextFileSync.
For example:
import { Adapter, Low } from 'lowdb'import { TextFile } from 'lowdb/node'import YAML from 'yaml'class YAMLFile { constructor(filename) { this.adapter = new TextFile(filename) } async read() { const data = await this.adapter.read() if (data === null) { return null } else { return YAML.parse(data) } } write(obj) { return this.adapter.write(YAML.stringify(obj)) }}const adapter = new YAMLFile('file.yaml')const db = new Low(adapter)
Limits
Lowdb doesn't support Node's cluster module.
If you have large JavaScript objects (~10-100MB) you may hit some performance issues. This is because whenever you call db.write, the whole db.data is serialized using JSON.stringify and written to storage.
Depending on your use case, this can be fine or not. It can be mitigated by doing batch operations and calling db.write only when you need it.
If you plan to scale, it's highly recommended to use databases like PostgreSQL or MongoDB instead.