liwi-subscribe-store 13.1.1 → 13.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +63 -3
- package/package.json +2 -2
package/README.md
CHANGED
|
@@ -13,6 +13,12 @@
|
|
|
13
13
|
<a href="https://npmjs.org/package/liwi-subscribe-store"><img src="https://img.shields.io/npm/types/liwi-subscribe-store.svg?style=flat-square" alt="types"></a>
|
|
14
14
|
</p>
|
|
15
15
|
|
|
16
|
+
## About
|
|
17
|
+
|
|
18
|
+
A proxy around a [`liwi-store`](../liwi-store) `Store` that adds live query subscriptions. It implements the full `Store` interface, so it is a drop-in replacement: every write goes through to the wrapped store and then notifies listeners, which is how open queries learn about inserts, updates and deletes.
|
|
19
|
+
|
|
20
|
+
Only writes made **through this proxy, in this process** are observed. Changes written directly to the database (another process, mongo shell, migrations) are not detected.
|
|
21
|
+
|
|
16
22
|
## Install
|
|
17
23
|
|
|
18
24
|
```bash
|
|
@@ -21,8 +27,62 @@ npm install --save liwi-subscribe-store
|
|
|
21
27
|
|
|
22
28
|
## Usage
|
|
23
29
|
|
|
24
|
-
```
|
|
25
|
-
import
|
|
30
|
+
```ts
|
|
31
|
+
import { SubscribeStore } from "liwi-subscribe-store";
|
|
32
|
+
|
|
33
|
+
const store = new SubscribeStore(mongoStore);
|
|
34
|
+
```
|
|
35
|
+
|
|
36
|
+
With mongo, prefer the helper which builds the same thing with the types already applied:
|
|
37
|
+
|
|
38
|
+
```ts
|
|
39
|
+
import { createMongoSubscribeStore } from "liwi-mongo";
|
|
40
|
+
|
|
41
|
+
export const tasksStore = createMongoSubscribeStore<Task>(
|
|
42
|
+
createMongoStore("tasks"),
|
|
43
|
+
);
|
|
44
|
+
```
|
|
45
|
+
|
|
46
|
+
Queries created from it support `subscribe` / `fetchAndSubscribe`:
|
|
47
|
+
|
|
48
|
+
```ts
|
|
49
|
+
const query = tasksStore.createQueryCollection({
|
|
50
|
+
criteria: { completed: false },
|
|
51
|
+
sort: { created: 1 },
|
|
52
|
+
limit: 100,
|
|
53
|
+
});
|
|
26
54
|
|
|
27
|
-
|
|
55
|
+
const subscription = query.fetchAndSubscribe((error, changes) => {
|
|
56
|
+
if (error) throw error;
|
|
57
|
+
for (const change of changes) {
|
|
58
|
+
// change.type: "initial" | "inserted" | "updated" | "deleted"
|
|
59
|
+
}
|
|
60
|
+
});
|
|
61
|
+
|
|
62
|
+
// later
|
|
63
|
+
subscription.stop();
|
|
28
64
|
```
|
|
65
|
+
|
|
66
|
+
## API
|
|
67
|
+
|
|
68
|
+
### `new SubscribeStore(store)`
|
|
69
|
+
|
|
70
|
+
Wraps a `SubscribableStore`. Beyond the `Store` interface it adds:
|
|
71
|
+
|
|
72
|
+
- `subscribe(listener): () => void` — listen to every write going through the proxy; returns an unsubscribe function.
|
|
73
|
+
- `callSubscribed(action): void` — dispatch an action to listeners manually (used by the store implementation, e.g. after an out-of-band write you want queries to see).
|
|
74
|
+
|
|
75
|
+
### `Actions<Model>`
|
|
76
|
+
|
|
77
|
+
```ts
|
|
78
|
+
type Actions<Model> =
|
|
79
|
+
| { type: "inserted"; next: Model[] }
|
|
80
|
+
| { type: "updated"; changes: [Model, Model][] } // [previous, next]
|
|
81
|
+
| { type: "deleted"; prev: Model[] };
|
|
82
|
+
```
|
|
83
|
+
|
|
84
|
+
### `AbstractSubscribableStoreQuery`
|
|
85
|
+
|
|
86
|
+
Base class for store implementations providing subscribable queries. It holds the subscribe store (`setSubscribeStore` / `getSubscribeStore`) and implements `fetchAndSubscribe` / `subscribe` on top of an abstract `_subscribe`; implementations turn store actions into query `Changes` by testing each affected model against the query criteria. `liwi-mongo` does this in memory with [mingo](https://github.com/kofrasa/mingo).
|
|
87
|
+
|
|
88
|
+
Queries built this way are fixed: `changeParams` / `changePartialParams` throw — create a new query instead.
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "liwi-subscribe-store",
|
|
3
|
-
"version": "13.
|
|
3
|
+
"version": "13.2.0",
|
|
4
4
|
"description": "subscribe store proxy",
|
|
5
5
|
"keywords": [],
|
|
6
6
|
"homepage": "https://github.com/christophehurpeau/liwi",
|
|
@@ -37,7 +37,7 @@
|
|
|
37
37
|
}
|
|
38
38
|
},
|
|
39
39
|
"dependencies": {
|
|
40
|
-
"liwi-store": "13.
|
|
40
|
+
"liwi-store": "13.2.0"
|
|
41
41
|
},
|
|
42
42
|
"devDependencies": {
|
|
43
43
|
"@pob/rollup-esbuild": "10.0.0",
|