项目作者: SebastianSpeitel

项目描述 :
Persistent object storage using proxies
高级语言: TypeScript
项目地址: git://github.com/SebastianSpeitel/proxystore.git
创建时间: 2020-02-22T11:12:46Z
项目社区:https://github.com/SebastianSpeitel/proxystore

开源协议:GNU General Public License v3.0

下载


Tests
@sebastianspeitel/proxystore"">install size

proxystore

Persistent object storage using proxies

Usage

Short way

  1. import proxy, { ProxyStoreJSON as ProxyStore } from "../src";
  2. const store = proxy(ProxyStore, { path: "store.json" });
  3. store.foo = "bar";

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

Long way

  1. import { ProxyStoreJSON as ProxyStore } from "../src";
  2. const store = new ProxyStore({ foo: "baz" }, { path: "store.json" }).store;
  3. store.foo = "bar";

TypeScript

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

Example

  1. interface FooBar {
  2. foo: number;
  3. bar: string;
  4. }
  5. const store = proxy<FooBar>(ProxyStore, { path: "store.json" });
  6. store.foo; // works
  7. 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

  1. class MyProxyStore extends ProxyStore {
  2. get(path: PropertyKey[], prop: PropertyKey): any {
  3. console.log(`Property ${[...path, prop].join(".")} requested`);
  4. return super.get(path, prop);
  5. }
  6. }
  7. const store = proxy({ foo: "bar" }, MyProxyStore);
  8. store.foo; // Property foo requested