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.
LowDB is built on Lo-Dash, this makes it quite different and unique compared to other serverless databases often based on MongoDB API.
LowDB powers JSON Server and JSONPlaceholder.
var low = require('lowdb')
low('songs').insert({title: 'low!'})
Database is automatically created and saved to db.json
in a readable format.
{
"songs": [
{
"title": "low!",
"id": "e31aa48c-a9d8-4f79-9fce-ded4c16c3c4c"
}
]
}
To query data, you can use Lo-Dash methods.
var songs = low('songs').where({ title: 'low!' }).value()
Or LowDB equivalent short syntax.
var songs = low('songs', { title: 'low' })
Changes can also be monitored.
low.on('add', function(name, object) {
console.log(object + 'added to' + name)
})
get x 1000 0.837708 ms
update x 1000 4.433322 ms
insert x 1000 11.78481 ms
remove x 1000 24.60179 ms
low(collection)
Returns or create a Lo-Dash wrapped array.
You can then use methods like: where
, find
, filter
, sortBy
, groupBy
, ... and also methods from Underscore.db.
var topFiveSongs = low('posts')
.where({published: true})
.sortBy('views')
.first(5)
.value();
var songTitles = low('songs')
.pluck('titles')
.value()
var total = low('songs').size()
If you just want to modify the database, without getting the returned array or object, you can omit .value()
low.save([path])
Saves database to path
or low.path
. By default db.json
.
low.load([path])
Loads database from path
or low.path
. By default db.json
.
low.path
Database location. By default db.json
.
low.path = '/some/path/file.json'
autoSave
Set to false
to disable save on change, this turns LowDB into a read-only in-memory database. By default true
.
low.autoSave = true
LowDB short syntax covers only the most common operations but lets you write really concise code.
low('songs', id)
// -> low('songs').get(id).value()
low('songs', {title: 'low!'})
// -> low('songs').where({title: 'low!'}).value()
low('songs', {title: 'low!'}, +1)
// -> low('songs').insert({title: 'low!'}).value()
low('songs', {title: 'low!'}, -1)
// -> low('songs').removeWhere({title: 'low!'}).value()
low('songs', id, -1)
// -> low('songs').remove(id).value()
low('songs', id, {title: 'new title'})
// -> low('songs').update(id, {title: 'new title'}).value()
low('songs', {published: false}, {published: true})
// -> low('songs').updateWhere({published: false}, {published: true}).value()
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.