Adds functions to Underscore/Lodash for manipulating database-like objects.
It adds:
getById
insert
upsert
updateById
updateWhere
replaceById
removeById
removeWhere
save
load
createId
Data can be persisted using the filesystem or localStorage.
Tip You can extend lowdb with underscore-db.
Install
Node
$ npm install lodash underscore-db
var _ = ;var _db = ; _;
Tip underscore-db is also compatible with underscore
Usage example
Create an empty database object
var db = posts:
Create a post
var newPost = _;
Display database console.log(db)
posts: title: "foo" id: "5ca959c4-b5ab-4336-aa65-8a197b6dd9cb"
Retrieve post using underscore-db get
or underscore find
method
var post = _; var post = _;
Persist
_;
API
The following database object is used in API examples.
var db = posts: id: 1 body: 'one' published: false id: 2 body: 'two' published: true comments: id: 1 body: 'foo' postId: 1 id: 2 body: 'bar' postId: 2
getById(collection, id)
Finds and returns document by id or undefined.
var post = _;
insert(collection, document)
Adds document to collection, sets an id and returns created document.
var post = _;
If the document already has an id, and it is the same as an existing document in the collection, an error is thrown.
_;_; // Throws an error
upsert(collection, document)
Adds document to collection, sets an id and returns created document.
var post = _;
If the document already has an id, it will be used to insert or replace.
_;_;_; // {id: 1, title: 'New title'}
updateById(collection, id, attrs)
Finds document by id, copies properties to it and returns updated document or undefined.
var post = _;
updateWhere(collection, whereAttrs, attrs)
Finds documents using _.where
, updates documents and returns updated documents or an empty array.
// Publish all unpublished postsvar posts = _;
replaceById(collection, id, attrs)
Finds document by id, replaces properties and returns document or undefined.
var post = _;
removeById(collection, id)
Removes document from collection and returns it or undefined.
var comment = _;
removeWhere(collection, whereAttrs)
Removes documents from collection using _.where
and returns removed documents or an empty array.
var comments = _;
save(db, [destination])
Persists database using localStorage or filesystem. If no destination is specified it will save to db
or ./db.json
.
_;_;
load([source])
Loads database from localStorage or filesystem. If no source is specified it will load from db
or ./db.json
.
var db = _;var db = _;
id
Overwrite it if you want to use another id property.
_id = '_id';
createId(collectionName, doc)
Called by underscore-db when a document is inserted. Overwrite it if you want to change id generation algorithm.
_ { return collectionName + '-' + docname + '-' + _;}
FAQ
How to query?
Everything you need for querying is present in Underscore and Lodash: where
, find
, map
, reduce
, filter
, reject
, sortBy
, groupBy
, countBy
, ...
See http://lodash.com/docs or http://underscorejs.org.
Example:
// Using Underscorevar topFivePosts = value; // Using Lodashvar topFivePosts = value;
How to reduce file size?
With Lodash, you can create custom builds and include just what you need.
$ npm install -g lodash-cli$ lodash include=find,forEach,indexOf,filter,has
For more build options, see http://lodash.com/custom-builds.
Changelog
See details changes for each version in the release notes.
License
underscore-db is released under the MIT License.