Exports Usage

Storagefy provides a modular export system for maximum flexibility. Whether you're managing state across React, Vue (Pinia), or Svelte — or choosing betweenlocalStorage, sessionStorage, or IndexedDB — the exported modules are designed to be highly composable and framework-agnostic.

1. Importing Adapters

You can directly import individual storage and framework adapters for advanced use cases or custom integrations.

        
import { IndexedDBAdapter } from "storagefy";
import { LocalStorageAdapter } from "storagefy";
import { SessionStorageAdapter } from "storagefy";

import { PiniaAdapter } from "storagefy";
import { ReactAdapter } from "storagefy";
import { SvelteAdapter } from "storagefy";
        
      

2. Using startStoragefy

This function initializes and returns a storage adapter instance based on the provided configuration. You can reuse this adapter across frameworks.

        
import { startStoragefy }  from "storagefy";
const adapter = startStoragefy({
  dbName: "my-app",
  adapter: "indexedDB",          // the default; or "localStorage" or "sessionStorage"
  encrypt: true,                 // makes the values hard to read — not a way to keep a secret
  password: "my-app-secret",     // the encryption keys are derived from it
  keyDerivation: "hkdf",         // the default; "pbkdf2" only for a password the person types
  expireCheckInterval: 2000,     // ms; 0 turns the expiry timer off
  channelName: "storagefy-sync", // the channel the tabs talk on…
  enableSyncTabs: true,          // …and this storage tells them about its writes
});
        
      

If an adapter is already started, the same instance is returned — unless forceRecreate: true(a new adapter replaces it: the old one is destroyed, and the store adapters kept over it stop keeping their stores — keep them again) or fresh: true (a new adapter is returned and the one started stays).

Sync between tabs needs three settings: channelName and enableSyncTabs on the storage adapter, and syncTabs: true on each store that should follow the other tabs (see below). With only channelName, nothing is synced.

3. Retrieving Current Adapter

Once initialized with startStoragefy, you can retrieve the current adapter via:

        
import { getStorageAdapter } from "storagefy";          
const current = getStorageAdapter();
        
      

4. Ignoring Keys from Persistence

The ignoreKeys option lets you exclude sensitive or transient fields from being persisted. It supports both flat and nested keys using dot notation.

Flat keys

Exclude top-level properties by name:

        
await setInStorage(myStore, "user", {
  ignoreKeys: ["password", "token"]
});

// Given state:
// { name: "Alice", password: "secret", token: "abc123" }

// Stored as (the ignored keys are left out):
// { name: "Alice" }
        
      

Nested keys (dot notation)

Exclude deeply nested properties using dot-separated paths. This works at any depth without affecting sibling or parent keys.

        
await setInStorage(myStore, "user", {
  ignoreKeys: ["credentials.password", "credentials.token"]
});

// Given state:
// {
//   name: "Alice",
//   credentials: {
//     password: "secret",
//     token: "abc123",
//     role: "admin"
//   }
// }

// Stored as:
// {
//   name: "Alice",
//   credentials: {
//     role: "admin"  // unaffected
//   }
// }
        
      

Deep nesting

Paths can go as deep as needed:

        
await setInStorage(myStore, "app", {
  ignoreKeys: ["config.api.secretKey", "config.api.privateToken"]
});

// Given state:
// {
//   config: {
//     api: {
//       secretKey: "key-999",
//       privateToken: "tok-xyz",
//       baseUrl: "https://api.example.com"
//     }
//   }
// }

// Stored as:
// {
//   config: {
//     api: {
//       baseUrl: "https://api.example.com"  // unaffected
//     }
//   }
// }
        
      

Mixing flat and nested keys

You can freely combine both styles in the same array:

        
await setInStorage(myStore, "user", {
  ignoreKeys: [
    "sessionToken",             // flat key
    "profile.password",         // nested key
    "profile.security.pin"      // deeply nested key
  ]
});
        
      

5. Framework Adapters

React

To use Storagefy with Zustand, Redux, Jotai, or custom React stores:

          
import { getReactAdapter } from "storagefy";
// Over a new localStorage adapter (or over the one startStoragefy() started, without adapterParams)
const react = getReactAdapter({
  adapterParams: { adapter: "localStorage", dbName: "my-react-app" }
});

await react.getFromStorage(myStore, "user"); // load what was kept — before setInStorage, which writes the state

await react.setInStorage(myStore, "user", {
  timeout: 3600000,
  ignoreKeys: [
    "tempId",               // flat key
    "auth.password",        // nested key
    "auth.session.token"    // deeply nested key
  ],
  syncTabs: true
});
          
        

Or use the helper:

          
// startStoragefy() must be called first
await setReactStorage(myStore, "settings", {
  timeout: 300000,
  ignoreKeys: ["_temp", "loading", "auth.password"],
  syncTabs: true
});
          
        

Vue (Pinia)

Get a preconfigured Pinia adapter using:

          
// startStoragefy() must be called first
const { setInStorage, getFromStorage, flush } = getPiniaAdapter({
  adapterParams: { adapter: "sessionStorage", dbName: "my-vue-app" }
});

await getFromStorage(myStore, "user"); // load what was kept — before setInStorage, which writes the state

await setInStorage(myStore, "user", {
  timeout: 3600000,
  ignoreKeys: [
    "tempId",               // flat key
    "auth.password",        // nested key
    "auth.session.token"    // deeply nested key
  ],
  syncTabs: true
});

// Signing out: clear the store, then make sure the cleared state is stored
myStore.$reset();
await flush("user"); // true once stored — the page may close
          
        

Or use the helper:

          
await setPiniaStorage(myStore, "user", {
  timeout: 3600000,
  ignoreKeys: ["tempId", "auth.password"],
  syncTabs: true
});
          
        

Svelte

Integrate Svelte writable stores:

          
const svelte = getSvelteAdapter({
  adapterParams: { adapter: "indexedDB", dbName: "my-svelte-app" }
});

await svelte.getFromStorage(myStore, "theme"); // load what was kept — before setInStorage, which writes the state

await svelte.setInStorage(myStore, "theme", {
  timeout: 86400000,
  ignoreKeys: [
    "preview",              // flat key
    "user.password",        // nested key
    "user.api.secretKey"    // deeply nested key
  ],
  syncTabs: true
});
          
        

Or simply use the shorthand:

          
// startStoragefy() must be called first            
await setSvelteStorage(myStore, "theme", {
  timeout: 86400000,
  ignoreKeys: ["preview", "user.password"],
  syncTabs: true
});
          
        

All set* functions accept the same parameters:

  • store: The store instance you want to persist
  • key: A string key under which to store the data
  • options (optional):
    • timeout: expiration of the stored state, in milliseconds after each write — not a time limit of the operation: after it, the stored state is gone. A change of anignoreKeys key alone writes nothing, so it does not renew it
    • ignoreKeys: Array of keys to exclude from persistence. Supports flat keys ("password") and dot-notation paths for nested keys ("auth.password", "auth.api.token").
    • syncTabs: If true, this store applies the changes of the other tabs — the storage adapter needs channelName and enableSyncTabs. Only a state newer than the store's is applied (every write has a revision); with IndexedDB and localStorage, a tab shown again reads storage again
    • onError: called with an Error when the state could not be saved (storage full…) or a change from another tab could not be read. getFromStorage(store, key, { onError }) also tells a stored state that cannot be read

6. Exported API Summary

  • IndexedDBAdapter, LocalStorageAdapter, SessionStorageAdapter
  • PiniaAdapter, ReactAdapter, SvelteAdapter
  • startStoragefy(config)
  • getStorageAdapter()
  • getPiniaAdapter(), getReactAdapter(), getSvelteAdapter()
  • setPiniaStorage(), setReactStorage(), setSvelteStorage()