Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/liammartens/relational-statestore
https://github.com/liammartens/relational-statestore
Last synced: 7 days ago
JSON representation
- Host: GitHub
- URL: https://github.com/liammartens/relational-statestore
- Owner: LiamMartens
- License: mit
- Created: 2023-11-24T20:49:34.000Z (12 months ago)
- Default Branch: main
- Last Pushed: 2024-02-03T15:16:45.000Z (9 months ago)
- Last Synced: 2024-10-18T16:13:47.637Z (18 days ago)
- Language: TypeScript
- Size: 136 KB
- Stars: 0
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Relational Statestore
[![npm](https://img.shields.io/npm/v/relational-statestore)](https://www.npmjs.com/package/relational-statestore) [![npm](https://img.shields.io/badge/license-MIT-blue)](https://www.npmjs.com/package/relational-statestore)
This is an in-memory graph like datastore for managing relationships between nodes.
The purpose of this library is not persistent data storage but rather a relational state which can be useful for certain use cases such as:- Keeping track of child/parent relationships
- Keeping track of related DOM elements## Usage
```typescript
import { RelationalStatestore, Relationship } from "relational-statestore";type UserType = {
id: string;
name: string;
};// create the local store
export const store = new RelationalStatestore();// add nodes
const userA = { id: "a", name: "User A" };
const userB = { id: "b", name: "User B" };
const nodeA = store.addNode(userA);
const nodeB = store.addNode(userB);// add relationship
class AreFriends extends Relationship {}
// it is possible to use the original data objects to refer to the nodes
store.addEdge(userA, userB, new AreFriends());
// it is also possible to use the nodes itself
store.addEdge(nodeA, nodeB, new AreFriends());// we can now find all the edges for our users
const allEdges = store.edgesFor(userA);
// the result is an array with 1 edge: Edge
// we can also look for specific relationships
const friends = store.relationshipsFor(userA, AreFriends);
// this will return the same list - filtered by AreFriends
```## Subscribing to events
It is also possible to subscribe to the store to run side-effects.
```typescript
import { RelationalStatestore, Relationship } from "relational-statestore";type UserType = {
id: string;
name: string;
};// create the local store
export const store = new RelationalStatestore();
store.subscribe("*", (...event) => {});
store.subscribe("node:added", (eventname, node) => {});
store.subscribe("node:data:updated", (eventname, node) => {});
```