npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

underscore-db

v0.12.2

Published

Use JavaScript objects as databases

Downloads

4,299

Readme

underscore-db Build Status NPM version

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.

Live example

Tip You can extend lowdb with underscore-db.

Install

Node

$ npm install lodash underscore-db
var _   = require('lodash');
var _db = require('underscore-db');

_.mixin(_db);

Tip underscore-db is also compatible with underscore

Usage example

Create an empty database object

var db = {
  posts: []
}

Create a post

var newPost = _.insert(db.posts, {title: 'foo'});

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 = _.getById(db.posts, newPost.id);

var post = _.find(db.posts, function(post) {
  return post.title === 'foo'
});

Persist

_.save(db);

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 = _.getById(db.posts, 1);

insert(collection, document)

Adds document to collection, sets an id and returns created document.

var post = _.insert(db.posts, {body: 'New post'});

If the document already has an id, and it is the same as an existing document in the collection, an error is thrown.

_.insert(db.posts, {id: 1, body: 'New post'});
_.insert(db.posts, {id: 1, title: 'New title'}); // Throws an error

upsert(collection, document)

Adds document to collection, sets an id and returns created document.

var post = _.upsert(db.posts, {body: 'New post'});

If the document already has an id, it will be used to insert or replace.

_.upsert(db.posts, {id: 1, body: 'New post'});
_.upsert(db.posts, {id: 1, title: 'New title'});
_.getById(db.posts, 1); // {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 = _.updateById(db.posts, 1, {body: 'Updated body'});

updateWhere(collection, whereAttrs, attrs)

Finds documents using _.where, updates documents and returns updated documents or an empty array.

// Publish all unpublished posts
var posts = _.updateWhere(db.posts, {published: false}, {published: true});

replaceById(collection, id, attrs)

Finds document by id, replaces properties and returns document or undefined.

var post = _.replaceById(db.posts, 1, {foo: 'bar'});

removeById(collection, id)

Removes document from collection and returns it or undefined.

var comment = _.removeById(db.comments, 1);

removeWhere(collection, whereAttrs)

Removes documents from collection using _.where and returns removed documents or an empty array.

var comments = _.removeWhere(db.comments, {postId: 1});

save(db, [destination])

Persists database using localStorage or filesystem. If no destination is specified it will save to db or ./db.json.

_.save(db);
_.save(db, '/some/path/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 = _.load();
var db = _.load('/some/path/db.json');

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.

_.createId = function(collectionName, doc) {
  return collectionName + '-' + doc.name + '-' + _.random(1, 9999);
}

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 Underscore
var topFivePosts = _(db.posts)
  .chain()
  .where({published: true})
  .sortBy(function(post) {
     return post.views;
   })
  .first(5)
  .value();

// Using Lodash
var topFivePosts = _(db.posts)
  .where({published: true})
  .sortBy('views')
  .first(5)
  .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.