@sebastianspeitel/proxystore

Persistent object storage using proxies

Usage no npm install needed!

<script type="module">
  import sebastianspeitelProxystore from 'https://cdn.skypack.dev/@sebastianspeitel/proxystore';
</script>

README

Tests install size

proxystore

Persistent object storage using proxies

Usage

Short way

import proxy, { ProxyStoreJSON as ProxyStore } from "../src";

const store = proxy(ProxyStore, { path: "store.json" });
store.foo = "bar";

Now you can use store like any other object and it will be saved in store.json.

Long way

import { ProxyStoreJSON as ProxyStore } from "../src";

const store = new ProxyStore({ foo: "baz" }, { path: "store.json" }).store;
store.foo = "bar";

TypeScript

All methods take a type to use for the store, so you can provide it for autocompletion.

Example

interface FooBar {
  foo: number;
  bar: string;
}

const store = proxy<FooBar>(ProxyStore, { path: "store.json" });

store.foo; // works
store.baz; // Property 'baz' does not exist on type 'FooBar'

Extensions

You can implement your own ways of serializing the store. Just extends the class ProxyStore overwrite the methods you want and call proxy with your own class as parameter.

Example

class MyProxyStore extends ProxyStore {
  get(path: PropertyKey[], prop: PropertyKey): any {
    console.log(`Property ${[...path, prop].join(".")} requested`);
    return super.get(path, prop);
  }
}

const store = proxy({ foo: "bar" }, MyProxyStore);
store.foo; // Property foo requested