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

@ashnazg/chok-db

v1.0.3

Published

a FS-based doc db for pico-scale datasets

Downloads

5

Readme

  • What: A filesystem-based pico-NoSQL-DB that allows mutations by filesystem operations while running to co-exist with API state changes.
  • Why: For every project that needs commerical-scale and -robustness on millions of rows, there's several 'our business runs on XLS and manual labor' automatable projects who's data would easily fit in the microcontroller in my garage door opener. Some of these use cases also need the kind of history-tracking that git would give me. So I built an extremely KISS storage layer that allows me to use unixy tools to do bulk record maintenance.
  • How: using chokidar to to monitor the filesystem kept the complexity of this library to a minimum.
const chokdb = require('@ashnazg/chok-db');
const client = await chokdb({path: 'folder_of_jsons/'});

client.create({a:1,b:2}); // autoassigns new ID
client.update(1, {a:2,b:1});
console.log(client.read(1));
client.del(1);

The three mutators return promise({db, id}) as a convenience.

Want to just grab the whole map of rows? Skip the ID param on read().

const map = client.read();

(Both usages of read() are synchronous.)

fast or write-guaranteed?

All mutators complete the work in-memory synchronously, and then asynchronously write to disk.

If you await the create/update/del call before responding to caller, you're in write-guaranteed mode.

if you want the ID that create() used without waiting for the file write, just check client.last_id_created synchronously right after create() returns.

(Regardless of fast vs safe usage, client.last_id_created is always zero when the service initializes and not retained between runs.)

Customize Behavior

The chokdb({path}) function takes several other inputs; you could swap JSON for YAML or turn on verbose logging:

const yaml_backed_client = await chokdb({path: 'myyamls/', parser: string2yaml, serializer: yaml2string});

// here's all the current debug/log hooks:
const client = await chokdb({
	path,
	log: {
		write(id, body) { console.log("writing", id, body); },
		evt(chok_event_name, row_filename) { console.log("chokidar event", chok_event_name, row_filename); },
		parseErr(filename, error) { console.error('got', error, 'when I parsed', filename); }, // if you don't provide a parseErr, it just throws the error to chokidar instead.
	}
});

stream of events

Want to be notified of changes?

I wanted a way to push updates to websocket clients, so once the data's persisted to disk, these optional hooks are called:

const client = await chokdb({
	path,
	onadd(dying_record) {}
	onchange(altered_record) {}
	onunlink(new_record) {} // this is the only hook that happens _before_ the memory DB is updated.
});

Pico-Validation

Want to see if a user-provided value is a safe and present row ID?

client.validate(id); // throws on any 404 or unsanitary input.

ID field

Like SQLite, chokdb rows expose the internal auto increment to you. (This makes it easy for me to pass the row from DB/FE/UI/sub-UI and back again.)

But the ID field in the row is not changable; attempts will be reset next time you update().

Nor is there a way through this API to renumber a row. (But since the memory DB tracks changes on disk, this is trivial from the filesystem: mv 1.json 5.json)

Release 1.0.3

  • fixed: To be fully unixy, row files' last line also has a '\n'
  • new: .write(id) and .update(id) now support the idea that the second (body) param is optional -- in that mode, just save the mutations made directly to the memory copy.

Release 1.0.2

Just fixed this README

Release 1.0.1

  • bugfix: catastrophic typo in unlink handler
  • feature: chokdb will now mkdir the path you give it if it doesn't exist. (Make sure the base already exists; this isn't mkdir -p.)
  • tweak: unused IDs are now routinely reused. (I know how classic RDBS's do it, but I think that if your data integrity doctrine depends on this instead of foreign-keys/cascades, you've already lost.)