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

kstor

v1.0.10

Published

Simple key value storage.

Downloads

24

Readme

Kstor

Key value store for configs or simple local databases. Includes file encryption as a deterent to users modifying your config.

Install

$ npm install kstor

Usage

Using ES6

import { KStor } from 'kstor';
const defaults = {
  name: 'app',
  description: 'My application description.',
  version: '1.0.0',
  keywords: [
    'todos',
    'notes',
    'messages'
  ],
  author: {
    name: 'Milton Waddams',
    email: '[email protected]'
  }
};
const store = new KStor('myconfig', defaults);
// or new KStor({ /* options here */}, defaults);

// Getting a value.
// returns > 'app'
const name = store.get('name');

// Setting a value.
store.set('version', '1.0.1');

// Getting a nested value from indexed array.
// returns > 'notes'
const keyword = store.get('keywords[1]');

// Getting nested value from object.
const email = store.get('author.email');

// Check if has value.
// returns > true
const desc = store.has('description');

Storage Paths

The storage path for your config is very flexible. Here are the majority of examples you might encounter.

APP_NAME denotes the directory name of your module or the package.json name of your module. HOME denotes the home directory for your system. For example on mac /Users/YOUR_NAME.

Options

For most use cases you will not need to set any options. By default the "name" and "dir" options are set automatically. When "dir" is specified the default directory is overriden.

By default configs are saved to "$HOME/kstor/configs". The file name will be your specified "name" option or the name of the directory from which you instantiated.

name

Filename when not specified is named as your_directory_name.json

dir

Dir when not specified defaults to $HOME/kstor/configs

entrypoint

When an entrypoint is specified the store is mapped to this key. There are use cases where this can be handy.

const defaults = {
  blogs: {
    blog1: { /* props here */ },
    blog2: { /* props here */ }
  }
}
const options = {
  entrypoint: 'blogs'
}

Which would result in your store essentially being the following data structure, allowing your gets and sets to ignore the top level "blogs" property key.

// Your store basically thinks or sees this even
// though the entire structure is still present.
const result = {
    blog1: { /* props here */ },
    blog2: { /* props here */ }
}

encryptionKey

When an encryption key is specified it will encrypt your config. Since you are passing your encrypting key in plain text this doesn't secure the file however if the typical user were to open the config it deters them from making changes.

transform

When data is loaded you can run the data through a transform which is useful for ensuring types. You must return a value.

// IMPORTANT: your data structure may be different!!

// Ensure date is of type Date.
const options2 = {
  transform: (k, v) => {
    for (const key in v) {
      if (key === 'date' && !(v[key] instanceof Date))
        v[key] = new Date(v[key]);
    }
    return v;
  }
}

API

has

Checks if config has a given property.

get

Gets a value from the given by property path with optionally passing a default value to be set.

set

Sets a value by property path or iterates an object setting each property.

del

Deletes a key from the config.

clear

Clears the config to an empty object.

snapshot

Creates and returns a clone of your config.

db (get)

Gets the entire config using getter.

const data = this.db;

db (set)

Sets the entire config using setter.

this.db = { /* your new object */ }

size

Returns the size of the configuration.

const defaults = {
  name: 'app',
  description: 'My application description.',
  version: '1.0.0',
  keywords: [
    'todos',
    'notes',
    'messages'
  ],
  author: {
    name: 'Milton Waddams',
    email: '[email protected]'
  }
}

// In the above size would be 5
// as it counts only the top
// level keys.
const size = store.size;

iterable

Gets an iterable instance for performing forEach etc... See: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Iterators_and_Generators

The KStor instance is also an iterable class, hence you can perform for of operations as well.

// Consider this data structure.
const teams = {
  jets: { city: 'New York' },
  eagles: { city: 'Philadelphia' },
  patriots: { city: 'New England' }
};

for(const item of store) {
  const key = item.key // this would be: jets, eagles, patriots.
  const value = item.value // would be each object ex: { city: 'New York' }
}

// OR
const iterator = [...store];

iterator.forEach(function(item) {
  // do something.
});

Docs

See https://blujedis.github.io/kstor/

Change

See CHANGE.md

License

See LICENSE.md