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

@waveplay/stashy

v2.1.3

Published

Flexible storage for Node, React Native, and the browser

Downloads

121

Readme

GitHub license Tests npm minizipped size

Storage API for Node, React Native, and the browser

Stashy uses the best storage backend for each environment.

Live demo project for web

Getting started

Install the package:

npm install @waveplay/stashy

Use functions such as get(key) or set(key, value). These will be delegated to the correct backend based on the environment your code is running in.

import stashy from '@waveplay/stashy'

// Get a value from storage
const displayName = stashy.getString('name')
console.log(`Your username is ${displayName}`)

// Update a value in storage
// It'll be saved and available when you come back later
stashy.set('name', 'Pkmmte Xeleon')

See the sample project for more usage examples.

API reference

| Function | Description | | ----------------- | -------------------------------------------------------- | | clearAll | Clears all data from the backend. | | delete | Deletes the value for the given key. | | get | Gets the value for the given key. | | getAsync | Gets the value for the given key asynchronously. | | getBoolean | Gets the boolean value for the given key. | | getBooleanAsync | Gets the boolean value for the given key asynchronously. | | getNumber | Gets the number value for the given key. | | getNumberAsync | Gets the number value for the given key asynchronously. | | getString | Gets the string value for the given key. | | getStringAsync | Gets the string value for the given key asynchronously. | | set | Sets the value for the given key. |

Default instance

When you use import stashy from '@waveplay/stashy', you're importing the default instance of Stashy. This instance is created with the default options and will use the best backend for your environment.

This may be considered unnecessary overhead if you're creating new instances of Stashy in your code instead of using the default instance. Import from @waveplay/stashy/core instead to avoid this overhead.

import { Stashy } from '@waveplay/stashy/core'

const stashy = new Stashy({
	// ... your custom options
})

Backends

Stashy comes with a few backends available out of the box. You can also create your own backend by implementing the StashyBackend interface.

import { Stashy } from '@waveplay/stashy'
import { StashyBackend } from '@waveplay/stashy/backend'

class CustomBackend implements StashyBackend {
	// ... your custom backend implementation
}

const stashy = new Stashy({
	backend: new CustomBackend()
})

// or with different backends per environment
const stashy = new Stashy({
	backend: {
		native: new MmkvBackend(),
		ssr: new CookieBackend(),
		web: new CustomBackend()
	}
})

AsyncStorage

This backend uses @react-native-async-storage/async-storage to store data. It is the default backend for native and only works with async functions such as getStringAsync().

import { AsyncStorageBackend } from '@waveplay/stashy/backend/async-storage'

Cookie

Cookie-backed storage perfect for server-side environments. It is the default backend for SSR and requires a context object to be passed with every function call.

import { CookieBackend } from '@waveplay/stashy/backend/cookie'

Env

Meant to use process.env variables. Supports dot notation for nested objects.

import { EnvBackend } from '@waveplay/stashy/backend/env'

Note: This will not load the .env file for you. Use with dotenv if you need to load the .env file. Frameworks like Next.js and Pilot.js do this automatically.

LocalStorage

Relies on localStorage to store data. It is the default backend for web.

import { LocalStorageBackend } from '@waveplay/stashy/backend/local-storage'

MMKV

Extremely fast and efficient storage for React Native, but requires that you install react-native-mmkv separately. If you're using Expo, you will also need to generate a new development client build.

import { MmkvBackend } from '@waveplay/stashy/backend/mmkv'

Debugging

You can pass your own logger (such as pino) to Stashy when creating a new instance and it will be used to log events. This is useful for debugging backend and environment issues.

The id property is included in each log message to help you differentiate between multiple instances of Stashy.

import { Stashy } from '@waveplay/stashy'
import pino from 'pino'

const stashy = new Stashy({
	id: 'my-stash',
	logger: pino({ level: 'debug' })
})

Credits

This project was originally developed for WavePlay.

License

The MIT License.