UNPKG

1.45 kBMarkdownView Raw
1![Tests](https://github.com/SebastianSpeitel/proxystore/workflows/Tests/badge.svg?branch=master)
2
3# proxystore
4
5Persistent object storage using proxies
6
7## Usage
8
9### Short way
10
11```javascript
12import proxy, { ProxyStoreJSON as ProxyStore } from "../src";
13
14const store = proxy(ProxyStore, { path: "store.json" });
15store.foo = "bar";
16```
17
18Now you can use `store` like any other object and it will be saved in `store.json`.
19
20### Long way
21
22```javascript
23import { ProxyStoreJSON as ProxyStore } from "../src";
24
25const store = new ProxyStore({ foo: "baz" }, { path: "store.json" }).store;
26store.foo = "bar";
27```
28
29## TypeScript
30
31All methods take a type to use for the store, so you can provide it for autocompletion.
32
33### Example
34
35```typescript
36interface FooBar {
37 foo: number;
38 bar: string;
39}
40
41const store = proxy<FooBar>(ProxyStore, { path: "store.json" });
42
43store.foo; // works
44store.baz; // Property 'baz' does not exist on type 'FooBar'
45```
46
47## Extensions
48
49You 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.
50
51### Example
52
53```javascript
54class MyProxyStore extends ProxyStore {
55 get(path: PropertyKey[], prop: PropertyKey): any {
56 console.log(`Property ${[...path, prop].join(".")} requested`);
57 return super.get(path, prop);
58 }
59}
60
61const store = proxy({ foo: "bar" }, MyProxyStore);
62store.foo; // Property foo requested
63```