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```javascript
11import proxy, { ProxyStoreFS as ProxyStore } from "../dist";
12
13const store = proxy(ProxyStoreFS, { path: "store.json" });
14store.foo = "bar";
15```
16
17Now you can use `store` like any other object and it will be saved in `store.json`.
18
19### Long way
20```javascript
21import { ProxyStoreFS as ProxyStore } from "../dist";
22
23const store = new ProxyStore({ foo: "baz" }, { path: "store.json" }).store;
24store.foo = "bar";
25```
26
27## TypeScript
28
29All methods take a type to use for the store, so you can provide it for autocompletion.
30
31### Example
32
33```typescript
34interface FooBar {
35 foo: number;
36 bar: string;
37}
38
39const store = proxy<FooBar>(ProxyStore, { path: "store.json" });
40
41store.foo; // works
42store.baz; // Property 'baz' does not exist on type 'FooBar'
43```
44
45## Extensions
46
47You 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.
48
49### Example
50
51```javascript
52class MyProxyStore extends ProxyStore {
53 get(path: PropertyKey[], prop: PropertyKey): any {
54 console.log(`Property ${[...path, prop].join(".")} requested`);
55 return super.get(path, prop);
56 }
57}
58
59const store = proxy({ foo: "bar" }, MyProxyStore);
60store.foo; // Property foo requested
61```