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

@stolksdorf/pico-db

v1.0.0

Published

A tiny file system based database using JSON files

Downloads

2

Readme

🗃 pico-db

npm version

pico-db is an incredibly simple filesystem-based database where each table is a folder, and each record is it's own JSON file. Useful for small/local projects, unit testing, and quickly bootstrapping new projects.

npm install @stolksdorf/pico-db

Features

  • 75 lines of code
  • no dependacies
  • builtin query engine

Why use json files for records?

  • easy to read and manually edit
  • git diff-able
  • completely flexible on schema

Let's see it in action

const DB = require('pico-db')('./path/to/db_folder');

const Users = DB.table('users');
const Auth = DB.table('users.auth');

await Users.put({ _id : 'foofoo', name : 'Bob', points : 50 });

const alice = await Users.put({ name : 'Alice', points : 25 });
await Auth.put({ method : 'password', value : 'dont_look_plz', user_id : alice._id});

Resulting filestructure

/path/to/db_folder
	/users
		foofoo.json
		thx425nyt5z7nyuz.json
		/auth
			xz53vad9n4yi77tw.json

api

picodb(root_folder_path, opts) -> db instance

Creates a pico-db instance for the given root_folder_path. All tables and records will be stored within that folder, and that folder will be created if it doesn't already exist.

opts
default_opts = {
	id_key : '_id', //Key used to store the id field on records
	id_func : (record)=>base32(16), //Function used to generate the record ids. Can use a hash of the record data if you want
	json_space : '\t', //character used as spaces when stringifying the record JSON. Use '' for compressed JSON
}

Example

const DB = require('pico-db')('./db', {
	id_key : '__ID__',
	id_func : (user)=>user.name[0], // Uses the user's name first letter as it's id (probably not a good idea)
	json_space : '',             // Produces a more compressed json file
});

const Users = DB.table('users');
Users.put({ name : 'bobert'});

/*
{__ID__:'b',name:'bobert'}
*/

db.table(name) -> table instance

Creates a table instance at root_folder_path + name. name can be '.' separated to created nested tables, eg. .table('users.secrets.auth'). Will create the folders if they don't already exist.

db.reset()

Completely removes everything at the root_folder_path from the filesystem.

async table.get(id) / table.get([ids]) -> record(s)

Gets a single record from the filesystem or an array of ids. Returns undefined if the record does not exist.

async table.put(record) -> record

Inserts a new record into the filesystem. If the record did not have an id one will be generated. The record data will be returned.

async table.query(queryObj)

Look up records within a table using a query object. A query object is a object made of key-function pairs. For each of the key's within a record, it calls this function with that key's value. If the function returns a false value, the record is removed from this query. Nesting also works!

const Users = require('pico-db')('./db').table('users');

await Users.put({ name : 'bobert', score: { points : 51 }});
await Users.put({ name : 'alice',  score: { points : 12 }});
await Users.put({ name : 'aaron',  score: { points : 26 }});

const result = await Users.query({
	name : (val)=>val[0]=='a', //Only names that start with 'a'
	score : {
		points : (val)=>val > 25, //Only points over 25
	}
});

/* result
[ { id:'...', name : 'aaron',  score: { points : 26 }} ]
*/

async table.del(id)

Removes a record from the filesystem based on it's id.

table.drop()

Removes all records from the filesystem in this table folder.