What is unstorage?
The 'unstorage' npm package is a versatile storage utility that provides a unified API for interacting with various storage backends. It allows developers to easily switch between different storage mechanisms such as local storage, session storage, and even remote storage solutions without changing the core logic of their applications.
What are unstorage's main functionalities?
Local Storage
This feature allows you to interact with local storage using a simple API. You can set, get, and remove items from local storage.
const { createStorage } = require('unstorage');
const localStorage = createStorage();
// Set a value
await localStorage.setItem('key', 'value');
// Get a value
const value = await localStorage.getItem('key');
console.log(value); // Outputs: 'value'
// Remove a value
await localStorage.removeItem('key');
Session Storage
This feature allows you to interact with session storage using the same API as local storage. You can set, get, and remove items from session storage.
const { createStorage } = require('unstorage');
const sessionStorage = createStorage({ driver: 'session' });
// Set a value
await sessionStorage.setItem('key', 'value');
// Get a value
const value = await sessionStorage.getItem('key');
console.log(value); // Outputs: 'value'
// Remove a value
await sessionStorage.removeItem('key');
Remote Storage
This feature allows you to interact with remote storage solutions via HTTP. You can set, get, and remove items from a remote storage backend.
const { createStorage } = require('unstorage');
const remoteStorage = createStorage({ driver: 'http', baseURL: 'https://api.example.com/storage' });
// Set a value
await remoteStorage.setItem('key', 'value');
// Get a value
const value = await remoteStorage.getItem('key');
console.log(value); // Outputs: 'value'
// Remove a value
await remoteStorage.removeItem('key');
Other packages similar to unstorage
localforage
LocalForage is a fast and simple storage library for JavaScript. It improves the offline experience of your web app by using asynchronous storage (IndexedDB or WebSQL) with a simple, localStorage-like API. Compared to unstorage, LocalForage is more focused on client-side storage and does not provide a unified API for different storage backends.
idb-keyval
idb-keyval is a small library that provides a simple key-value store backed by IndexedDB. It is very lightweight and easy to use, making it a good choice for simple storage needs. However, it lacks the flexibility and unified API of unstorage, which supports multiple storage backends.
store2
store2 is a versatile storage library for all JavaScript environments, including Node.js and browsers. It provides a unified API for localStorage, sessionStorage, and memory storage. While it offers similar functionality to unstorage, it does not support remote storage solutions.
unstorage
Universal key-value Storage
- Works in all environments (Browser, NodeJS and Workers)
- Asynchronous API
- Unix-style mountable paths (multi provider)
- Default in-memory storage
- Tree-shakable and lightweight core
WIP:
- JSON serialization and native types (destr)
- State compression
- State hydration
- Links (soft/junction)
- Binary data
getKeys
and clear
with sub-path- IPC/HTTP interface
- Virtual fs (fs-like interface)
- Watcher
- Reactivity
- Basic array operations
Providers
Usage
Install unistorage
npm package:
yarn add unistorage
npm i unistorage
import { createStorage } from 'unistorage'
const storage = createStorage()
await storage.getItem('foo:bar')
await storage.getItem('/foo/bar')
storage.hasItem(key)
Checks if storage contains a key. Resolves to either true
or false
.
await storage.hasItem('foo:bar')
storage.getItem(key)
Gets value of a key in storage. Resolves to either string
or null
.
await storage.getItem('foo:bar')
storage.setItem(key, value)
Add Update a value to storage.
await storage.setItem('foo:bar', 'baz')
storage.removeItem(key)
Remove a value from storage.
await storage.removeItem('foo:bar')
storage.getKeys()
Get all keys. Returns an array of string
.
await storage.getKeys()
storage.clear()
Removes all stored key/values.
await storage.clear()
storage.mount(mountpoint, provider)
By default, everything is stored in memory. We can mount additional storage space in a Unix-like fashion.
When operating with a key
that starts with mountpoint, instead of default storage, mounted provider will be called.
import { createStorage, fsStorage } from 'unistorage'
const storage = createStorage()
storage.mount('/output', fsStorage({ dir: './output' }))
await storage.setItem('/output/test', 'works')
await storage.setItem('/foo', 'bar')
storage.dispose()
Disposes all mounted storages to ensure there are no open-handles left. Call it before exiting process.
await storage.dispose()
Contribution
- Clone repository
- Install dependencies with
yarn install
- Use
yarn dev
to start jest watcher verifying changes - Use
yarn test
before push to ensure all tests and lint checks passing
License
MIT