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 🙏

© 2025 – Pkg Stats / Ryan Hefner

localforage-node-localstorage-driver

v0.0.1

Published

a localForage driver for Node.js and Deno using node-localstorage

Readme

localforage-node-localstorage-driver

a localForage driver for Node.js and Deno using node-localstorage

A LocalForage driver for Node.js and Deno environments that uses node-localstorage to provide localStorage API-compatible persistent storage.

Why Use This?

LocalForage is a fast and simple storage library that provides a simple key/value API with support for multiple backends including IndexedDB, WebSQL, and localStorage. However, it's primarily designed for browser environments.

This driver allows you to use LocalForage in Node.js and Deno environments by leveraging the node-localstorage package, which provides an API-compatible implementation of the browser's localStorage. This enables you to:

  • Use the same data access API across both browser and Node.js applications
  • Persist data between application restarts in Node.js
  • Maintain a consistent storage interface in isomorphic JavaScript applications
  • Take advantage of LocalForage's simple promises-based API

Installation

npm install localforage-node-localstorage-driver

or

yarn add localforage-node-localstorage-driver

Basic Usage

Registering the Driver and Creating an Instance

import localforage from 'localforage';
import { defineDriver, createInstance, NODE_LOCALSTORAGE } from 'localforage-node-localstorage-driver';

// First, define the driver (do this once in your application)
async function setup() {
  // Register the driver
  await defineDriver();
  
  // Create a LocalForage instance that uses the node-localstorage driver
  const store = createInstance({
    name: 'myApp',
    storeName: 'myStore'
  });
  
  // Now you can use all LocalForage methods
  await store.setItem('user', { id: 1, name: 'John' });
  const user = await store.getItem('user');
  console.log(user); // { id: 1, name: 'John' }
}

setup();

Using With Existing LocalForage Instance

If you already have a LocalForage instance and want to add this driver to it:

import localforage from 'localforage';
import { defineDriver, NODE_LOCALSTORAGE } from 'localforage-node-localstorage-driver';

async function setupWithExistingInstance() {
  // Register the driver
  await defineDriver();
  
  // Configure LocalForage to use node-localstorage driver
  localforage.config({
    driver: NODE_LOCALSTORAGE,
    name: 'myApp',
    storeName: 'myStore'
  });
  
  // Now you can use LocalForage as usual
  await localforage.setItem('key', 'value');
}

Advanced Configuration

The driver supports the standard LocalForage configuration options plus additional options specific to the node-localstorage driver:

const store = createInstance({
  // Standard LocalForage options
  name: 'myApp',
  storeName: 'myStore',
  description: 'My Application Store',
  
  // node-localstorage specific options
  storagePath: './custom-storage-path', // Custom path for storing data (default: './.localforage')
  size: 10 * 1024 * 1024 // Custom size limit in bytes (default: 5MB)
});

Storage Location

By default, the driver creates a ./.localforage directory in your application root to store the data. You can customize this by specifying a storagePath in the configuration options.

API Reference

This driver supports all the standard LocalForage methods:

Core Methods

  • setItem(key, value): Stores data in the storage.
  • getItem(key): Retrieves data from the storage.
  • removeItem(key): Removes an item from the storage.
  • clear(): Removes all items from the storage.
  • length(): Gets the number of items in the storage.
  • key(index): Gets the name of the key at the provided index.
  • keys(): Gets the list of all keys in the storage.
  • iterate(iteratorCallback): Iterates through each value in the storage.

Driver-specific Exports

  • defineDriver(): Registers the Node.js localStorage driver with LocalForage.
  • createInstance(options): Creates a new LocalForage instance using the Node.js localStorage driver.
  • NODE_LOCALSTORAGE: The driver name constant to use when manually configuring LocalForage.

Examples

Storing and Retrieving Complex Data

import { createInstance } from 'localforage-node-localstorage-driver';

async function manageUserPreferences() {
  const store = createInstance({ name: 'preferences' });
  
  // Store complex object
  await store.setItem('userPreferences', {
    theme: 'dark',
    notifications: {
      email: true,
      push: false
    },
    recentSearches: ['node.js', 'localstorage', 'persistent storage']
  });
  
  // Later, retrieve it
  const preferences = await store.getItem('userPreferences');
  console.log(preferences.theme); // 'dark'
}

Iterating Through All Items

import { createInstance } from 'localforage-node-localstorage-driver';

async function processAllItems() {
  const store = createInstance();
  
  // First add some items
  await store.setItem('item1', { value: 'first' });
  await store.setItem('item2', { value: 'second' });
  await store.setItem('item3', { value: 'third' });
  
  // Now iterate through all items
  await store.iterate((value, key, iterationNumber) => {
    console.log(`[${iterationNumber}] ${key}: ${JSON.stringify(value)}`);
    
    // If you want to stop iteration early, return any non-undefined value
    if (key === 'item2') {
      return true; // This will stop the iteration and return true
    }
  });
}

License

MIT License