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