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