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

gitkv

v0.0.2

Published

This is a key-value based "database" library that uses Git for the underlying storage. It provides a get/set interface for storing and retrieving data using hosted on GitHub. Other providers can be added in the future. This is useful in scenarios where yo

Downloads

4

Readme

gitkv

This is a key-value based "database" library that uses Git for the underlying storage. It provides a get/set interface for storing and retrieving data using hosted on GitHub. Other providers can be added in the future. This is useful in scenarios where your data will be deployed / accessible from high-speed data sources, but you don't care about your write latency.

Inspired by this @cramforce tweet. I don't necessarily recommend using this in production. Mostly written by claude-3-opus.

Features

  • Set and retrieve key-value pairs using a simple API
  • Automatically commit and push changes to a GitHub repository
  • Create and manage branches for organizing data
  • Open pull requests programmatically
  • Retrieve file contents from the repository

Installation

To use this library in your project, you can install it via npm:

pnpm i gitkv
# or yarn add gitkv
# or npm i gitkv

Usage

First, import the GithubProvider class from the library:

import { GithubProvider } from "gitkv";

Then create an instance of the GithubProvider:

const config = {
  personalAccessToken: "YOUR_GITHUB_PERSONAL_ACCESS_TOKEN",
  repo: "YOUR_REPOSITORY_NAME",
  branch: "BRANCH_NAME", // Optional, defaults to 'main'
  owner: "YOUR_GITHUB_USERNAME",
};

const provider = new GithubProvider(config);

Setting a Key-Value Pair

To set a key-value pair, use the set method:

const path = "path/to/your/key"; // The path to the key relative from the root of the repository
const data = "Your data goes here";
const message = "Commit message";

// Commits and pushes the changes to the repository. To queue multiple changes, use the `add` method.
const commit = await provider.set(path, data, message);

Retrieving a Value

To retrieve the value associated with a key, use the get method:

const path = "path/to/your/key";

const value = await provider.get(path);

You likely want to implement some caching around your get calls, as this will be slow.

Staging Changes

You can also stage changes using the add method (similar to git add):

provider.add("path/to/your/key", "Your data goes here");

Committing Changes

To commit the staged changes, use the commit method:

const commit = provider.commit("Commit message");

Pushing Changes

To push the committed changes to the GitHub repository, use the push method:

const commitCount = await provider.push();

Opening a Pull Request

You can open a pull request programmatically using the openPullRequest method:

const pullRequestUrl = await provider.openPullRequest(
  "Pull Request Title",
  "Pull Request Body",
  "head-branch",
);

Error Handling

The library throws a GitKVError when an error occurs. You can catch and handle these errors in your code:

import { GitKVError } from "gitkv";

try {
  // Your code here
} catch (error) {
  if (error instanceof GitKVError) {
    console.error("Git DB Error:", error.message);
  } else {
    console.error("Unknown Error:", error);
  }
}

License

This library is open-source and available under the MIT License.