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

lib-generic-store

v0.2.0

Published

A generic implentation of a key-value store

Downloads

4

Readme

node-lib-generic-store

This is a generic implentation of a key-value store. It can be implemented using any 'session stores' that are compatible with the expressjs framework's session middleware *.

* We recommend the use of connect-redis and is marked as a peer dependency for this library. Don't worry if you don't understand what this means.

Why use node-lib-generic-store

The following benefits have been observed:

  • Easily switch between different underlying stores. So you could start with a file system based store and later move to redis without changing your code
  • Additional implentation of 'lock' and 'unlock' on any underlying store
  • Promise based APIs

Introduction

Calling the constructor like this:

let store = new GenericStore(storeType, storeOptions)

gets you a store object. This object supports:

  • all the methods supported by the underlying storeType. eg. store.get()
  • all these methods suffixed with Prom as a promisified version that are easier to use. e.g store.getProm()
  • locking a key for exclusive access while others wait for it to be unclocked

Usage

Usage with file system based store

;(async () => {
    // usage with file store:
    let storeType = 'session-file-store'    
    let storeOptions = {path: './_data'}

    // get the store object
    let store = new GenericStore(storeType, storeOptions)

    // now use promises to perform operations
    await store.setProm('0', {'a': 'value'})
    let length = await store.lengthProm() // = 1
    let data = await store.getProm('0') // = {'a': 'value'}
})();

Usage with redis based store

;(async () => {
    // usage with redis:
    let storeType = 'connect-redis'
    const redis = require("redis-mock"); // can be any of redis, ioredis or redis-mock. refer to their docs for options
    let redisClient = redis.createClient()
    let storeOptions = {client: redisClient};

    // get the store object
    let store = new GenericStore(storeType, storeOptions)

    // now use promises to perform operations
    await store.setProm('0', {'a': 'value'})
    let length = await store.lengthProm() // = 1
    let data = await store.getProm('0') // = {'a': 'value'}
})();

Usage by locking

;(async () => {
    // usage with file store:
    let storeType = 'session-file-store'    
    let storeOptions = {path: './_data'}

    // get the store object
    let store = new GenericStore(storeType, storeOptions)

    // now use promises to perform operations
    await store.setProm('0', {'a': 'value'})
    let kv = await store.lockProm('0')
    let length = await store.lengthProm() // = 1
    let data = await kv.getProm() // = {'a': 'value'}
})();

APIs

  • All APIs return promises
  • options:
{ 
    wait: true  // is the key is locked, wait for it to be released
}

General APIs

store.allProm()

Available only if suported by underlying stores

This method is used to get all key-value (kv) pairs in the store as an array.

store.destoryProm(key, options)

This method is used to destroy/delete a kv from the store given a key.

store.clearProm()

Available only if suported by underlying stores

This method is used to delete all kvs from the store.

store.lengthProm()

Available only if suported by underlying stores

This method is used to get the count of all kvs in the store.

store.getProm(key, options)

This method is used to get a session from the store given a key.

The value returned or in callback(err, value) is null or undefined if the key was not found (and there was no error). A special case is made when error.code === 'ENOENT' to act like callback(null, null).

store.setProm(key, value, options)

This method is used to upsert a kv into the store

store.touchProm(key, value, options)

Available only if suported by underlying stores

This method is used to "touch" a given kv.

Locking APIs

store.lockProm(key, options)

Returns a KV object that acts like a mini-store with only one key-value in there. All General APIs that take in 'key' as the first parameter are supported. Namely: kv.setProm, kv.getProm, kv.destroyProm, kv.touchProm

kv.setProm(value, options)

Same as store.setProm but operates on the key specified during store.lockProm

kv.getProm(options)

Same as store.getProm but operates on the key specified during store.lockProm

kv.destoryProm(options)

Same as store.destoryProm but operates on the key specified during store.lockProm

kv.touchProm(value, options)

Same as store.touchProm but operates on the key specified during store.lockProm

NOTE: If kv is used on any methods other than the ones mentioned here and if that method is supported by the underlying store, the method runs on the store instead of kv. e.g. kv.lengthProm() would be same as store.lengthProm() and return the same result

License

MIT (c) 2020 harleen mann