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

packdb

v0.9.6

Published

This project is abandoned. It turns out that [`Proxy`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Proxy) messes with the [context of method calls](http://2ality.com/2016/11/proxying-builtins.html#wrapping-instances-of

Downloads

3

Readme

Abandoned

This project is abandoned. It turns out that Proxy messes with the context of method calls which is just no good.


PackDB

A simple single-file object-based database powered by your imagination

Similar to lowdb, but even simpler.

const PackDB = require('../PackDB');

const {data} = new PackDB(`db.json`);

// Set some defaults if your JSON file is empty
if(!data.posts) data.posts = [];
if(!data.user) data.user = {};

// Add a post
data.posts.push({id: 1, title: 'packdb is awesome'});

// Set a user
data.user = {name: 'mpen'};

Data is automatically saved to db.json whenever1 you mutate data:

{
    "posts": [
        {
            "id": 1,
            "title": "packdb is awesome"
        }
    ],
    "user": {
        "name": "mpen"
    }
}

You can use any library you like (or none at all) to manipulate data -- it's just a plain old JavaScript object2.

Why PackDB?

  • It doesn't get any simpler than this
  • It's like, really simple
  • Seriously, that's it's only selling point

Recommended usage

You can force the database to write back to disk and exit immediately when you press Ctrl+C with the following:

/// db.js ///
const PackDB = require('packdb');
const db = new PackDB(`${__dirname}/db.json`);

process.on('SIGINT', function() {
    db.writeNow();
    process.exit();
});

module.exports = db;

Usage:

/// otherfile.js ///
const {data} = require('./db.js');

// use `data` just like you would any other JS object

API

new PackDB(filename, [options])

  • filename string -- where to load/save data
  • options object
    • serialize function used to serialize data. Is passed root data object whenever a write occurs. Should return a string or Buffer. Defaults to JSON.stringify

    • deserialize function used to deserialize data after it's read from disk. Is passed a Buffer. Should return an object. Defaults to JSON.parse

    • minWait number. "Debounce" time, measured in milliseconds (ms). i.e., the amount of time it will wait for more mutations to occur before writing the data back to disk. Defaults to 10.

    • maxWait number. The maximum amount of time to wait between writes if a mutation has occurred. Defaults to 5000.

PackDB.write()

  • Queue up a write if you don't think the Proxy is doing its job (please file an issue if this is the case).

PackDB.writeNow()

  • Write right now. Skip the debounce. You can call this at the end of your script to save a few ms (it will cancel the debounce timer), or if you're really paranoid about data loss3.

PackDB.data

  • Your access point to all the data. It's a getter -- you can't reassign it, but you can read and write to it as you please.

Future

With some slight modifications, PackDB could be made to work with localStorage or other storage mechanisms. For now, it's Node only.

Integrate with msgpack and/or js-serialize to support more data types and smaller file sizes. Automatically choose based on file extension.


1 It's debounced by 10ms up to a maximum of 5s to avoid excessive disk writes.
2 Okay, it's actually a Proxy, but that shouldn't get in your way.
3 If you're that paranoid, you might want to use a professional-grade database.