@procedures/mongo 0.42.0 → 0.44.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/CHANGELOG.md +23 -0
- package/README.md +47 -0
- package/dist/.tsbuildinfo +1 -1
- package/dist/index.d.ts +2 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +2 -0
- package/dist/index.js.map +1 -1
- package/dist/storage/database-changes.d.ts +120 -0
- package/dist/storage/database-changes.d.ts.map +1 -0
- package/dist/storage/database-changes.js +273 -0
- package/dist/storage/database-changes.js.map +1 -0
- package/dist/storage/index.d.ts +2 -0
- package/dist/storage/index.d.ts.map +1 -1
- package/dist/storage/index.js +2 -0
- package/dist/storage/index.js.map +1 -1
- package/dist/storage/materializer.d.ts +12 -2
- package/dist/storage/materializer.d.ts.map +1 -1
- package/dist/storage/materializer.js +27 -25
- package/dist/storage/materializer.js.map +1 -1
- package/dist/storage/storage-hints.d.ts +23 -2
- package/dist/storage/storage-hints.d.ts.map +1 -1
- package/dist/storage/storage-hints.js +45 -37
- package/dist/storage/storage-hints.js.map +1 -1
- package/package.json +3 -3
- package/src/index.ts +3 -0
- package/src/storage/database-changes.ts +357 -0
- package/src/storage/index.ts +3 -0
- package/src/storage/materializer.ts +36 -25
- package/src/storage/storage-hints.ts +56 -37
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,28 @@
|
|
|
1
1
|
# @procedures/mongo
|
|
2
2
|
|
|
3
|
+
## 0.44.0
|
|
4
|
+
|
|
5
|
+
### Patch Changes
|
|
6
|
+
|
|
7
|
+
- 25039f7: `@procedures/mongo` gets one change watcher for a whole database, and its watchers take a logger (CLI-88). This change is additive. Thus it is a PATCH of the client group (decision D13).
|
|
8
|
+
|
|
9
|
+
- `watchDatabaseChanges(db, options)` watches the change stream of a database and gives each raw change event to `onChange`, one at a time.
|
|
10
|
+
- After an error, the watcher waits for a bounded backoff. Then it opens the stream again with `startAfter` and the token of the last change. It loses no change and gives no change twice.
|
|
11
|
+
- An `invalidate` event, a lost resume token and an error of the first open that does not go away stop the watcher. The error goes to `onError`.
|
|
12
|
+
- `watchStorageChanges` and `ViewMaterializer` take the option `logger` (the `Logger` of `@procedures/client`). The fields of each line hold its context.
|
|
13
|
+
- The option `log` is deprecated: use `logger`. When you give both, `logger` wins. Without `logger` and without `log`, the console output does not change.
|
|
14
|
+
- Updated dependencies [4dada47]
|
|
15
|
+
- Updated dependencies [19b7a25]
|
|
16
|
+
- @procedures/client@0.44.0
|
|
17
|
+
- @procedures/collections@0.44.0
|
|
18
|
+
|
|
19
|
+
## 0.43.0
|
|
20
|
+
|
|
21
|
+
### Patch Changes
|
|
22
|
+
|
|
23
|
+
- @procedures/client@0.43.0
|
|
24
|
+
- @procedures/collections@0.43.0
|
|
25
|
+
|
|
3
26
|
## 0.42.0
|
|
4
27
|
|
|
5
28
|
### Patch Changes
|
package/README.md
CHANGED
|
@@ -735,6 +735,53 @@ await watch.stop();
|
|
|
735
735
|
- **The storage of a partition** (`forPartition()`) publishes and adopts only the documents of its partition. Its stream reads the whole collection. The documents of the counters are in another collection, and they never make a hint.
|
|
736
736
|
- **Your own stream.** `storageChangeOf(change, form)` reads the change of an item from a change event, for an app that watches the collection itself: `{ id, partition, deleted, raw, version, seq }`, or `undefined`.
|
|
737
737
|
|
|
738
|
+
### One watcher for a whole database: `watchDatabaseChanges`
|
|
739
|
+
|
|
740
|
+
`watchDatabaseChanges(db, options)` watches the change stream of a whole database (roadmap CLI-88). It gives each raw change event to `onChange`. After an error, it opens the stream again from its resume token. Change streams need a replica set.
|
|
741
|
+
|
|
742
|
+
```ts
|
|
743
|
+
import { watchDatabaseChanges } from "@procedures/mongo";
|
|
744
|
+
|
|
745
|
+
const watch = watchDatabaseChanges(db, {
|
|
746
|
+
fullDocument: "updateLookup",
|
|
747
|
+
fullDocumentBeforeChange: "whenAvailable",
|
|
748
|
+
resumeAfter: savedToken, // or startAfter, or startAtOperationTime
|
|
749
|
+
onChange: async (change) => {
|
|
750
|
+
await apply(change); // the event of the driver, with no change
|
|
751
|
+
savedToken = change._id;
|
|
752
|
+
},
|
|
753
|
+
onError: (error) => console.error("the watcher stopped", error),
|
|
754
|
+
backoff: { initialDelay: ms(250), maxDelay: seconds(30) },
|
|
755
|
+
logger,
|
|
756
|
+
signal,
|
|
757
|
+
});
|
|
758
|
+
await watch.ready; // the stream is open
|
|
759
|
+
// watch.resumeToken is the token of the last change that onChange took
|
|
760
|
+
await watch.stop();
|
|
761
|
+
```
|
|
762
|
+
|
|
763
|
+
- **The raw event.** `onChange` gets the event of the driver with no change: `ns`, `documentKey`, `fullDocument`, `fullDocumentBeforeChange` and each other field. The options `pipeline` and `streamOptions` (for example `maxAwaitTimeMS`) go to the stream.
|
|
764
|
+
- **The image before the change.** Set `fullDocumentBeforeChange: "whenAvailable"`. A collection gives the image only with `changeStreamPreAndPostImages: { enabled: true }` (MongoDB 6 or later).
|
|
765
|
+
- **One event at a time.** The events go in the order of the stream. The watcher reads the next event after `onChange` resolves.
|
|
766
|
+
- **The resume token** is the `_id` of the last event after its `onChange` resolved. A batch without events gives the token of the batch. The getter `resumeToken` of the handle gives the token at this time.
|
|
767
|
+
- **No lost change.** After an error of the stream, the watcher waits for the backoff. Then it opens the stream again with `startAfter` and the token. Thus it loses no change and gives no change twice.
|
|
768
|
+
- **An error of `onChange`** has the same backoff. The token stays at the change before, so the same change comes again.
|
|
769
|
+
- **The backoff** starts at `initialDelay` and grows by `backoffMultiplier` to at most `maxDelay`, with `jitter`. A change or a batch without events sets it back to `initialDelay`. The defaults are `ms(250)`, 2, `seconds(30)` and the full jitter, as for `watchStorageChanges`.
|
|
770
|
+
- **The errors that stop the watcher.** The watcher stops on these errors. The error goes to `onError` and to the logger, and `ready` rejects when the stream was not open:
|
|
771
|
+
- An `invalidate` event, for example after `dropDatabase`. The error has the event in its field `change`, and `resumeToken` is the token of that event. A new watcher with `startAfter` starts after it.
|
|
772
|
+
- A token that the server does not know any more: the codes 260, 280 and 286 (`ChangeStreamHistoryLost`).
|
|
773
|
+
- Before the first open: a refused access, a single server (code 40573), or a wrong option.
|
|
774
|
+
- **The stop.** `stop()` or an abort of `signal` stops the watcher. It closes the stream and cancels the timer of the backoff. An `onChange` that runs finishes, and no change comes after it.
|
|
775
|
+
- **The tests.** The rules of the resume, the backoff and the stop run against a fake stream in each test run. The tests against a real server run in CI, on a single-node replica set. One of these tests makes the stream fail with a failpoint, and examines the order of the changes.
|
|
776
|
+
|
|
777
|
+
### The logger of the watchers and of the materializer
|
|
778
|
+
|
|
779
|
+
`watchDatabaseChanges`, `watchStorageChanges` and `ViewMaterializer` take the option `logger` (roadmap CLI-88). The type `Logger` of `@procedures/client` has one method: `log(level, message, fields)`.
|
|
780
|
+
|
|
781
|
+
- The message tells what occurred. The fields hold the context: `component`, the database or the collection, the operation type, `resumeToken` (true when the watcher has a token), the error, and `attempt` and `delayMs` of the backoff.
|
|
782
|
+
- `watchStorageChanges` and `ViewMaterializer` keep the option `log(level, text)`, with the context in the text. It is deprecated: use `logger`. When you give both, `logger` wins.
|
|
783
|
+
- Without `logger` and without `log`, each line goes to the console as before, with the prefix `[storage-hints]` or `[materializer]`. `watchDatabaseChanges` writes the message and the fields with the prefix `[database-changes]`.
|
|
784
|
+
|
|
738
785
|
### The guard of a pipeline: `scopePipeline`
|
|
739
786
|
|
|
740
787
|
`scopePipeline(pipeline, scope)` gives a pipeline that reads only the documents of a filter, on each collection that it reads. `MongoDocumentStorage.aggregate()` uses it with the filter of its partition. An app can use it for its own aggregations over shared collections:
|