Skip to content

Browser Storage Plugin

BrowserStoragePlugin uses an injected DOM Storage object. Pass localStorage for durable browser storage, sessionStorage for per-tab data, or a compatible fake in tests.

Installation

bash
npm install @routier/browser-storage-plugin

Basic Usage

ts
import { DataStore } from "@routier/datastore";
import { s } from "@routier/core/schema";
import { LocalStoragePlugin } from "@routier/browser-storage-plugin";

const userSchema = s
    .define("users", {
        id: s.string().key().identity(),
        email: s.string().distinct(),
        name: s.string(),
        createdAt: s.date().default(() => new Date()),
    })
    .compile();

class Ctx extends DataStore {
    users = this.collection(userSchema).proxy().create();

    constructor() {
        super(new LocalStoragePlugin("app"));
    }
}

const ctx = new Ctx();
await ctx.users.addAsync({ name: "Ada", email: "[email protected]" });
await ctx.saveChangesAsync();

Constructor

ts
new BrowserStoragePlugin("app", localStorage)
new BrowserStoragePlugin("preview", sessionStorage)

Notes

  • Each logical collection is stored as one key and rewritten as a whole.
  • Storage is synchronous and typically limited to about 5 MB per origin.
  • Concurrent writers across tabs are not supported; the last whole-collection write wins.
  • Use Dexie for larger data or multiple browser writers.

Released under the MIT License.