Simple to use type-safe local JSON database with fluent Query Builder 🦉
If you know JavaScript, you know how to use lowdb.
Read or create db.json
const db = await JSONFilePreset('db.json', { posts: [] })Use plain JavaScript to change data
const post = { id: 1, title: 'lowdb is awesome', views: 100 }
// In two steps
db.data.posts.push(post)
await db.write()
// Or in one
await db.update(({ posts }) => posts.push(post))New: Use fluent Query Builder for complex queries (similar to Laravel Eloquent)
// Query with chainable API
const activePosts = await db
.query('posts')
.where('status', 'active')
.where('views', '>', 50)
.orderBy('created_at', 'desc')
.limit(10)
.get()
// Insert
db.query('posts').insert({ id: 2, title: 'Another post', views: 200 })
await db.write()
// Update matching records
db.query('posts').where('id', 1).update({ title: 'Updated title' })
await db.write()
// Delete matching records
db.query('posts').where('views', '<', 10).delete()
await db.write()// db.json
{
"posts": [
{ "id": 1, "title": "lowdb is awesome", "views": 100 }
]
}In the same spirit, query using native Array functions:
const { posts } = db.data
posts.at(0) // First post
posts.filter((post) => post.title.includes('lowdb')) // Filter by title
posts.find((post) => post.id === 1) // Find by id
posts.toSorted((a, b) => a.views - b.views) // Sort by viewsIt's that simple. db.data is just a JavaScript object, no magic.
Become a sponsor and have your company logo here 👉 GitHub Sponsors
- Lightweight — Core library ~2KB gzipped
- Minimalist — Simple API, no complexity
- TypeScript — Full type safety
- Plain JavaScript — Works with vanilla JS
- Fluent Query Builder — Laravel-like chainable API for complex queries
- Safe atomic writes
- Hackable:
- 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
npm install lowdbLowdb 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.json
const defaultData = { posts: [] }
const db = await JSONFilePreset('db.json', defaultData)
// Update db.json
await db.update(({ posts }) => posts.push('hello world'))
// Alternatively you can call db.write() explicitly later
// to write to db.json
db.data.posts.push('hello world')
await db.write()// db.json
{
"posts": [ "hello world" ]
}The fluent Query Builder provides a Laravel-like API for complex queries:
import { JSONFileSyncPreset } from 'lowdb/node'
type User = {
id: number
name: string
email: string
age: number
status: 'active' | 'inactive'
}
type Data = {
users: User[]
}
const db = JSONFileSyncPreset<Data>('db.json', { users: [] })
// WHERE conditions
const activeUsers = db.query('users').where('status', 'active').get()
const adults = db.query('users').where('age', '>=', 18).get()
// Multiple conditions (AND)
const activeAdults = db
.query('users')
.where('status', 'active')
.where('age', '>=', 18)
.get()
// Supported operators: =, ==, !=, <>, >, >=, <, <=, in, like
const seniors = db.query('users').where('age', '>=', 65).get()
const byEmail = db.query('users').where('email', 'like', 'gmail').get()
const statuses = db.query('users').where('status', 'in', ['active', 'pending']).get()
// ORDER BY
const byAge = db.query('users').orderBy('age', 'desc').get()
// LIMIT & OFFSET (pagination)
const page1 = db.query('users').limit(10).get()
const page2 = db.query('users').limit(10).offset(10).get()
// First result
const first = db.query('users').where('status', 'active').first()
// Count
const total = db.query('users').count()
const activeCount = db.query('users').where('status', 'active').count()
// Check existence
const hasActive = db.query('users').where('status', 'active').exists()
// Aggregations
const names = db.query('users').pluck('name') // Extract single column
const uniqueStatuses = db.query('users').distinct('status') // Unique values
// INSERT
db.query('users').insert({ id: 1, name: 'John', email: 'john@example.com', age: 30, status: 'active' })
db.write()
// INSERT MANY
db.query('users').insertMany([
{ id: 2, name: 'Jane', email: 'jane@example.com', age: 28, status: 'active' },
{ id: 3, name: 'Bob', email: 'bob@example.com', age: 35, status: 'inactive' }
])
db.write()
// UPDATE matching records
const updated = db.query('users').where('status', 'inactive').update({ status: 'pending' })
db.write()
// DELETE matching records
const deleted = db.query('users').where('age', '<', 18).delete()
db.write()
// SELECT specific columns
const emails = db.query('users').select('id', 'email').get()
// Transform results
const userNames = db
.query('users')
.where('status', 'active')
.map(user => user.name)
// Custom filter
const filtered = db
.query('users')
.filter(user => user.email.endsWith('@example.com'))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') // ✅ Success
db.data.messages.push(1) // ❌ TypeScript errorYou 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` field
class 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')
const db = new LowWithLodash(adapter, defaultData)
await db.read()
// Instead of db.data use db.chain to access lodash API
const post = db.chain.get('posts').find({ id: 1 }).value() // Important: value() must be called to execute chainSee src/examples/ directory.
where(column, operator?, value?)— Add WHERE conditionorWhere(column, operator?, value?)— Add OR WHERE condition
Supported Operators: =, ==, !=, <>, >, >=, <, <=, in, like
select(...columns)— Select specific columnsfirst()— Get first result or nullget()— Get all results as arrayfind(id)— Find by id (assumes 'id' column)pluck(column)— Extract single columndistinct(column)— Get unique values
orderBy(column, direction?)— Order results (ascordesc, defaultasc)
limit(count)— Limit result countoffset(count)— Skip n results
count()— Count matching recordsexists()— Check if results exist
insert(record)— Insert single recordinsertMany(records)— Insert multiple recordsupdate(data)— Update matching recordsdelete()— Delete matching records
map(fn)— Transform results with functionfilter(fn)— Filter with custom predicate
Lowdb provides four presets for common cases.
JSONFilePreset(filename, defaultData)JSONFileSyncPreset(filename, defaultData)LocalStoragePreset(name, defaultData)SessionStoragePreset(name, defaultData)
See src/examples/ directory for usage.
Lowdb is extremely flexible, if you need to extend it or modify its behavior, use the classes and adapters below instead of the presets.
Lowdb has two classes (for asynchronous and synchronous adapters).
import { Low } from 'lowdb'
import { JSONFile } from 'lowdb/node'
const db = new Low(new JSONFile('file.json'), {})
await db.read()
await db.write()import { LowSync } from 'lowdb'
import { JSONFileSync } from 'lowdb/node'
const db = new LowSync(new JSONFileSync('file.json'), {})
db.read()
db.write()Calls adapter.read() and sets db.data.
Note: JSONFile and JSONFileSync adapters will set db.data to null if file doesn't exist.
db.data // === null
db.read()
db.data // !== nullCalls adapter.write(db.data).
db.data = { posts: [] }
db.write() // file.json will be { posts: [] }
db.data = {}
db.write() // file.json will be {}Calls fn() then db.write().
db.update((data) => {
// make changes to data
// ...
})
// files.json will be updatedAccess fluent Query Builder for a data collection.
const results = db.query('posts').where('status', 'active').get()Holds your db content. If you're using the adapters coming with lowdb, it can be any type supported by JSON.stringify.
For example:
db.data = 'string'
db.data = [1, 2, 3]
db.data = { key: 'value' }Adapters for reading and writing JSON files.
import { JSONFile, JSONFileSync } from 'lowdb/node'
new Low(new JSONFile(filename), {})
new LowSync(new JSONFileSync(filename), {})In-memory adapters. Useful for speeding up unit tests. See src/examples/ directory.
import { Memory, MemorySync } from 'lowdb'
new Low(new Memory(), {})
new LowSync(new MemorySync(), {})Synchronous adapter for window.localStorage and window.sessionStorage.
import { LocalStorage, SessionStorage } from 'lowdb/browser'
new LowSync(new LocalStorage(name), {})
new LowSync(new SessionStorage(name), {})Adapters for reading and writing text. Useful for creating custom adapters.
Adapters for easily supporting other data formats or adding behaviors (encrypt, compress...).
import { DataFile } from 'lowdb/node'
new DataFile(filename, {
parse: YAML.parse,
stringify: YAML.stringify
})
new DataFile(filename, {
parse: (data) => { decypt(JSON.parse(data)) },
stringify: (str) => { encrypt(JSON.stringify(str)) }
})If you've published an adapter for lowdb, feel free to create a PR to add it here.
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 { Low } from 'lowdb'
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, {})See src/adapters/ for more examples.
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, {})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.
Contributions are welcome! Please feel free to submit a Pull Request.
MIT © Typicode