Security News
ESLint is Now Language-Agnostic: Linting JSON, Markdown, and Beyond
ESLint has added JSON and Markdown linting support with new officially-supported plugins, expanding its versatility beyond JavaScript.
Lowdb is a small local JSON database for Node, Electron, and the browser. It's a simple and lightweight way to store data locally using a JSON file. It is ideal for small projects, quick prototypes, and applications that do not require a full-fledged database.
Create and Read Data
This feature allows you to create and read data from a JSON file. The code initializes a lowdb instance, sets default values, adds a new post, and retrieves it.
const low = require('lowdb');
const FileSync = require('lowdb/adapters/FileSync');
const adapter = new FileSync('db.json');
const db = low(adapter);
db.defaults({ posts: [] }).write();
db.get('posts').push({ id: 1, title: 'lowdb is awesome' }).write();
const post = db.get('posts').find({ id: 1 }).value();
console.log(post);
Update Data
This feature allows you to update existing data in the JSON file. The code updates the title of the post with id 1 and retrieves the updated post.
db.get('posts').find({ id: 1 }).assign({ title: 'lowdb is super awesome' }).write();
const updatedPost = db.get('posts').find({ id: 1 }).value();
console.log(updatedPost);
Delete Data
This feature allows you to delete data from the JSON file. The code removes the post with id 1 and retrieves the remaining posts.
db.get('posts').remove({ id: 1 }).write();
const posts = db.get('posts').value();
console.log(posts);
NeDB is a lightweight JavaScript database that is similar to MongoDB but is meant for small projects. It supports indexing and querying and can be used both in Node.js and in the browser. Compared to lowdb, NeDB offers more advanced querying capabilities and indexing.
LokiJS is a fast, in-memory database for Node.js and browsers. It is designed for performance and can handle large datasets efficiently. LokiJS offers more advanced features like indexing, binary serialization, and live querying compared to lowdb.
json-server is a full fake REST API with zero coding in less than 30 seconds. It is ideal for quick prototyping and mocking REST APIs. Unlike lowdb, json-server is more focused on providing a RESTful interface to your JSON data.
Flat JSON file database for Node
LowDB uses Lo-Dash functional progamming API instead of a MongoDB-like API. This makes it quite unique and different.
LowDB powers JSON Server and JSONPlaceholder. If you need something similar for the browser, check Underscore.db.
var low = require('lowdb')
var db = low('db.json')
db('songs').push({ title: 'low!'})
Database is automatically created and saved to db.json
in a readable format.
{
"songs": [
{
"title": "low!"
}
]
}
Data can be queried and manipulated using any Lo-Dash method.
var song = db('songs').find({ title: 'low!' }).value()
db('songs').remove({ title: 'low!' })
You can also use id-based methods by extending LowDB with Underscore.db.
low([filename])
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
low.mixin(source)
Use it to extend Lo-Dash 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) {
if (array.length >= 2) return array[1]
}
})
var song = db('songs').second().value()
db.object
Database object. Useful if you want to access the content of your JSON file and don't want to go through Lo-Dash methods.
console.log(db.object) // { songs: [ { title: 'low!' } ] }
db('songs').value() === db.object.songs
db.save()
LowDB automatically saves to disk. However, in case you directly modify the content of the database object, you'll need to manually call save
.
delete db.object.songs
db.save()
With LowDB you get access to the entire Lo-Dash API, so there's many, many ways to query and manipulate data. Here are a few examples to get you started.
Sort the top five songs.
db('songs')
.where({published: true})
.sortBy('views')
.first(5)
.value()
Retrieve song titles.
db('songs')
.pluck('titles')
.value()
Get the number of songs.
db('songs').size()
Make a deep clone of songs.
db('songs').cloneDeep().value
Update a song.
db('songs').find({ title: 'low!' }).assign({ title: 'hi!'})
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.insert({ title: 'low!' }).value().id
var song = db.get(songId).value()
Or simply use uuid.
var uuid = require('uuid')
var songId = db.push({ id: uuid(), title: 'low!' }).value().id
var song = db.find({ id: songId }).value()
In both cases, your db.json
will then look like this.
{
"songs": [
{
"id": "e31aa48c-a9d8-4f79-9fce-ded4c16c3c4c",
"title": "low!"
}
]
}
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.
LowDB is released under the MIT License.
FAQs
Tiny local JSON database for Node, Electron and the browser
The npm package lowdb receives a total of 532,725 weekly downloads. As such, lowdb popularity was classified as popular.
We found that lowdb demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
ESLint has added JSON and Markdown linting support with new officially-supported plugins, expanding its versatility beyond JavaScript.
Security News
Members Hub is conducting large-scale campaigns to artificially boost Discord server metrics, undermining community trust and platform integrity.
Security News
NIST has failed to meet its self-imposed deadline of clearing the NVD's backlog by the end of the fiscal year. Meanwhile, CVE's awaiting analysis have increased by 33% since June.