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

js-storage-abstraction

v1.1.2

Published

An abstraction to use the many different storage libraries in the JavaScript ecosystem

Downloads

6

Readme

js-storage-abstraction

Description

An abstraction to use the many different storage libraries in the JavaScript ecosystem

Build your apps storage object with Keys

Provides an abstract Key class that you can extend to make keys of all types.

For example, make a Key that uses AsyncStorage or make a Key that uses SecureStorage or make a Key that uses SessionStorage, etc.

Features

  • Stores everything as a string but provides helper methods for storing/getting data of different types like number, date, boolean, json.
  • Storage keys are based off a string union so you can centralize apps known storage keys. Very TypeScript friendly!

Code!

import { Key, TypedStorage, JSStorage } from 'js-storage-abstraction';

let inMemoryStorage: Record<string, any> = {};

/** the storage ids that your app uses */
type StorageId =
  | 'stringType'
  | 'boolType'
  | 'numType'
  | 'dateType'
  | 'jsonType';

/**
 * This key would pull from async storage
 */
class AsyncKey extends Key<StorageId> {
  get(): Promise<string | null> {
    return inMemoryStorage[this.name];
  }

  async set(value: string): Promise<void> {
    inMemoryStorage[this.name] = value;
  }

  async delete(): Promise<void> {
    delete inMemoryStorage[this.name];
  }
}

/*
  The below key subclasses are just examples of how there can be many different types of `Keys`
  You would still need to override the `get`, `set, & `delete` methods with the appropriate logic.
*/

/**
 * This key would pull from secure storage
 * (but we just extend AsyncKey here for illustration purposes)
 */
class SecureKey extends AsyncKey {}

/**
 * This key would use session storage
 */
class SessionKey extends AsyncKey {}

/**
 * This key takes care of splitting a long value into parts for storages that have item limits
 */
class MultipartSecureKey extends AsyncKey {}

/**
 * This key saves its values in a mysterious format...
 */
class MysteriousKey extends AsyncKey {}

/**
 * your storage is just an object where value is of subclass `Key`
 */
const typedStorage: TypedStorage<StorageId> = {
  stringType: new AsyncKey('stringType'),
  boolType: new MysteriousKey('boolType'),
  numType: new MultipartSecureKey('numType'),
  dateType: new SessionKey('dateType'),
  jsonType: new SecureKey('jsonType'),
};

typedStorage.stringType.set('hello world');

/**
 * Wrap your storage with `JSStorage` class to gain access to functionality like
 * `clear` whole storage or `getJSON` to get current storage as json object
 */
const storage = new JSStorage(typedStorage);

const stringTypeKey: Key = storage.use('stringType'); // the `use` function can retrieve a `Key` from `typedStorage`
stringTypeKey.set('hello world');

const numTypeKey: Key = storage.use('numType');
numTypeKey.setNumber(7);

storage.clear(); // delete whole storage

const storageJSON = storage.getJSON(); // get current storage as json object. useful for debugging, displaying in UI

Examples

Improvements

  • make Keys out of the box for common storage providers. basically did this in example app with localStorage and sessionStorage
  • improve naming of package and JSStorage??