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.
var low = require('lowdb');
low('songs').insert({title: 'low!'});
Database is saved to db.json
:
{
"songs": [
{
"title": "low",
"id": "e31aa48c-a9d8-4f79-9fce-ded4c16c3c4c"
}
]
}
low(collection)
Returns or create a Lo-Dash wrapped array. Low also adds Underscore.db methods to the wrapped array.
Therefore you can use any Lo-Dash collection or Underscore.DB methods. Don't forget to add .value()
to get the returned value.
Examples:
var newSong = low('songs').insert({title: 'low!'}).value(); // Underscore.db
var song = low('songs').get(newSong.id).value(); // Underscore.db
var songs = low('songs').where({title: 'low!'}).value(); // Lo-Dash
low.save([path])
Saves database to path
or low.path
which is db.json
by default.
low.load([path])
Loads database from path
or low.path
which is db.json
by default.
low.path
Use this property to change where the database is saved. Default to db.json
low.path = '/some/path/file.json'
low.autoSave
Use this property to save to file when database is changed. Default to true
low.autoSave = true
// get
var song = low('songs', id);
// where
var songs = low('songs', {title: 'low!'});
// create
var song = low('songs', {title: 'low!'}, 1);
// update
var song = low('songs', id, {title: 'new title'});
// updateWhere
var songs = low('songs', {published: false}, {published: true});
// remove
var song = low('songs', id, -1);
// removeWhere
var songs = low('songs', {title: 'low!'}, -1);
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.