Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/sebastianspeitel/proxystore

Persistent object storage using proxies
https://github.com/sebastianspeitel/proxystore

es6-proxies npm-package storage

Last synced: 1 day ago
JSON representation

Persistent object storage using proxies

Awesome Lists containing this project

README

        

![Tests](https://github.com/SebastianSpeitel/proxystore/workflows/Tests/badge.svg?branch=master)
[![install size](https://packagephobia.now.sh/badge?p=@sebastianspeitel/proxystore)](https://packagephobia.now.sh/result?p=@sebastianspeitel/proxystore)

# proxystore

Persistent object storage using proxies

## Usage

### Short way

```javascript
import proxy, { ProxyStoreJSON as ProxyStore } from "../src";

const store = proxy(ProxyStore, { path: "store.json" });
store.foo = "bar";
```

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

### Long way

```javascript
import { ProxyStoreJSON as ProxyStore } from "../src";

const store = new ProxyStore({ foo: "baz" }, { path: "store.json" }).store;
store.foo = "bar";
```

## TypeScript

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

### Example

```typescript
interface FooBar {
foo: number;
bar: string;
}

const store = proxy(ProxyStore, { path: "store.json" });

store.foo; // works
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

```javascript
class MyProxyStore extends ProxyStore {
get(path: PropertyKey[], prop: PropertyKey): any {
console.log(`Property ${[...path, prop].join(".")} requested`);
return super.get(path, prop);
}
}

const store = proxy({ foo: "bar" }, MyProxyStore);
store.foo; // Property foo requested
```