@procedures/mongo 0.2.2 → 0.3.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.
Files changed (53) hide show
  1. package/CHANGELOG.md +54 -0
  2. package/README.md +74 -3
  3. package/dist/.tsbuildinfo +1 -1
  4. package/dist/index.d.ts +2 -2
  5. package/dist/index.d.ts.map +1 -1
  6. package/dist/index.js.map +1 -1
  7. package/dist/module.d.ts.map +1 -1
  8. package/dist/module.js +9 -0
  9. package/dist/module.js.map +1 -1
  10. package/dist/procedures/documents.aggregate.d.ts +5 -0
  11. package/dist/procedures/documents.aggregate.d.ts.map +1 -1
  12. package/dist/procedures/documents.aggregate.js +36 -3
  13. package/dist/procedures/documents.aggregate.js.map +1 -1
  14. package/dist/procedures/documents.bulk.d.ts +42 -0
  15. package/dist/procedures/documents.bulk.d.ts.map +1 -0
  16. package/dist/procedures/documents.bulk.js +159 -0
  17. package/dist/procedures/documents.bulk.js.map +1 -0
  18. package/dist/procedures/documents.distinct.d.ts +22 -0
  19. package/dist/procedures/documents.distinct.d.ts.map +1 -0
  20. package/dist/procedures/documents.distinct.js +33 -0
  21. package/dist/procedures/documents.distinct.js.map +1 -0
  22. package/dist/procedures/documents.explain.d.ts +41 -0
  23. package/dist/procedures/documents.explain.d.ts.map +1 -0
  24. package/dist/procedures/documents.explain.js +111 -0
  25. package/dist/procedures/documents.explain.js.map +1 -0
  26. package/dist/procedures/index.d.ts +4 -0
  27. package/dist/procedures/index.d.ts.map +1 -1
  28. package/dist/procedures/index.js +5 -0
  29. package/dist/procedures/index.js.map +1 -1
  30. package/dist/procedures/watch.d.ts +59 -0
  31. package/dist/procedures/watch.d.ts.map +1 -0
  32. package/dist/procedures/watch.js +149 -0
  33. package/dist/procedures/watch.js.map +1 -0
  34. package/dist/procedures.types.d.ts +24 -3
  35. package/dist/procedures.types.d.ts.map +1 -1
  36. package/dist/storage/mongo-storage.d.ts +1 -0
  37. package/dist/storage/mongo-storage.d.ts.map +1 -1
  38. package/dist/storage/mongo-storage.js +4 -0
  39. package/dist/storage/mongo-storage.js.map +1 -1
  40. package/dist/types.d.ts +20 -2
  41. package/dist/types.d.ts.map +1 -1
  42. package/package.json +4 -4
  43. package/src/index.ts +10 -0
  44. package/src/module.ts +9 -0
  45. package/src/procedures/documents.aggregate.ts +34 -3
  46. package/src/procedures/documents.bulk.ts +192 -0
  47. package/src/procedures/documents.distinct.ts +57 -0
  48. package/src/procedures/documents.explain.ts +144 -0
  49. package/src/procedures/index.ts +6 -0
  50. package/src/procedures/watch.ts +184 -0
  51. package/src/procedures.types.ts +25 -0
  52. package/src/storage/mongo-storage.ts +5 -0
  53. package/src/types.ts +22 -2
package/CHANGELOG.md CHANGED
@@ -1,5 +1,59 @@
1
1
  # @procedures/mongo
2
2
 
3
+ ## 0.3.0
4
+
5
+ ### Minor Changes
6
+
7
+ - c799cdd: `HybridStorage` cannot lose data (roadmap R2). A sync gets the ids of the remote items from the new `getEntries()` of a storage (the collection procedure `collections.<name>.entries`), or else from `keyOf`. A remote item without a key stops the sync with `MissingKeysError` before any change: before, such a list emptied the local cache. A write-through that the remote cannot take waits in the queue and throws `PendingWriteError`, and a timer sends the queue. A write that the remote refuses is undone in the local cache. A full queue refuses a write with `QueueFullError`: before, it dropped the oldest operation. A second try of a write replaces its queued operation. A write-through sends the older queued operations first. The reads wait for the first sync.
8
+ - 3aa6495: Four procedures (roadmap R44). `mongo.documents.distinct` gives the distinct values of a field. `mongo.documents.bulk` sends `insertOne`, `updateOne`, `updateMany`, `deleteOne` and `deleteMany` operations in one request, in order by default. An empty filter of an update or a delete needs `confirm: true`. `mongo.documents.explain` gives the plan of a find or an aggregation, and refuses `$out` and `$merge`.
9
+
10
+ `mongo.watch` gives the changes of a collection or a database as a stream without an end. The stream closes when the reader stops or the call aborts. A change stream needs a replica set. The type `BulkOperation` also has `BulkUpdateMany` and `BulkDeleteMany`.
11
+ - 89d8a4a: Three procedures only read now (CAP-1, CAP-2, CAP-3, roadmap R7), so a read-only session (`mark mcp --read-only`) serves them. `db.query` refuses a statement that changes data (`PRAGMA query_only`), and the file keeps its bytes. `mongo.documents.aggregate` refuses `$out` and `$merge`, also in the pipelines of `$facet`, `$lookup` and `$unionWith`. `logs.query` gives no entries for a missing file or table, and does not make them.
12
+
13
+ ### Patch Changes
14
+
15
+ - Updated dependencies [ddea529]
16
+ - Updated dependencies [b2a3e3a]
17
+ - Updated dependencies [a8c47f1]
18
+ - Updated dependencies [381ce3a]
19
+ - Updated dependencies [0c627f8]
20
+ - Updated dependencies [ae7f05f]
21
+ - Updated dependencies [7873940]
22
+ - Updated dependencies [a70d445]
23
+ - Updated dependencies [bc70357]
24
+ - Updated dependencies [07cc998]
25
+ - Updated dependencies [a6b01a9]
26
+ - Updated dependencies [c799cdd]
27
+ - Updated dependencies [540f8d9]
28
+ - Updated dependencies [b6daaec]
29
+ - Updated dependencies [5b9264e]
30
+ - Updated dependencies [956e88b]
31
+ - Updated dependencies [ff62281]
32
+ - Updated dependencies [d81d0ed]
33
+ - Updated dependencies [aa67cc7]
34
+ - Updated dependencies [061fa22]
35
+ - Updated dependencies [cfb9d69]
36
+ - Updated dependencies [43f7de5]
37
+ - Updated dependencies [b07bb50]
38
+ - Updated dependencies [4a3bfa1]
39
+ - Updated dependencies [bd0b626]
40
+ - Updated dependencies [a6be873]
41
+ - Updated dependencies [fb30718]
42
+ - Updated dependencies [c5528e4]
43
+ - Updated dependencies [fee5d9c]
44
+ - Updated dependencies [21f7743]
45
+ - Updated dependencies [31a0b46]
46
+ - @procedures/client@0.3.0
47
+ - @procedures/collections@0.3.0
48
+
49
+ ## 0.2.3
50
+
51
+ ### Patch Changes
52
+
53
+ - Updated dependencies [15e48cf]
54
+ - @procedures/client@0.2.3
55
+ - @procedures/collections@0.2.3
56
+
3
57
  ## 0.2.2
4
58
 
5
59
  ### Patch Changes
package/README.md CHANGED
@@ -13,11 +13,12 @@ MongoDB client wrapper with client procedures for local or RPC access. Provides
13
13
  - **Local usage**: Direct MongoDB connection with native driver
14
14
  - **RPC usage**: Remote procedure calls via `client.call()`
15
15
 
16
- The package follows the client procedure pattern, with 16 procedures in 4 categories. Its register entry (`import "@procedures/mongo/register"`) registers them:
16
+ The package follows the client procedure pattern, with 20 procedures in 5 categories. Its register entry (`import "@procedures/mongo/register"`) registers them:
17
17
  - **Database**: `ping`, `info`
18
18
  - **Collections**: `list`, `create`, `drop`, `stats`
19
- - **Documents**: `find`, `get`, `insert`, `update`, `delete`, `count`, `aggregate`
19
+ - **Documents**: `find`, `get`, `insert`, `update`, `delete`, `count`, `aggregate`, `distinct`, `bulk`, `explain`
20
20
  - **Indexes**: `list`, `create`, `drop`
21
+ - **Change streams**: `mongo.watch`
21
22
 
22
23
  ## Architecture
23
24
 
@@ -735,6 +736,52 @@ interface AggregateOutput {
735
736
  }
736
737
  ```
737
738
 
739
+ ##### `mongo.documents.distinct`
740
+
741
+ The procedure gives the distinct values of one field in the documents that match `query`. The default `query` matches all documents. An array field gives each of its elements. Use dots for an embedded field, for example `address.city`.
742
+
743
+ ```typescript
744
+ const { values } = await client.exec(
745
+ ["mongo", "documents", "distinct"],
746
+ { collection: "users", field: "country", query: { status: "active" } }
747
+ );
748
+ ```
749
+
750
+ ##### `mongo.documents.bulk`
751
+
752
+ The procedure sends several writes in one request. The operations have the form of the `bulkWrite` of the driver: `insertOne`, `updateOne`, `updateMany`, `deleteOne` and `deleteMany`. By default, the operations run in order, and the first failure stops the rest. With `ordered: false`, the server tries each operation, and the error tells which ones failed.
753
+
754
+ The rules of `update` and `delete` apply to each operation. An empty filter of an update or a delete needs `confirm: true` on the call. An unknown operation or field is an error.
755
+
756
+ ```typescript
757
+ const result = await client.exec(["mongo", "documents", "bulk"], {
758
+ collection: "users",
759
+ operations: [
760
+ { insertOne: { document: { name: "Di" } } },
761
+ { updateOne: { filter: { name: "Ann" }, update: { $set: { status: "active" } } } },
762
+ { updateMany: { filter: { status: "new" }, update: { $set: { status: "active" } } } },
763
+ { deleteOne: { filter: { name: "Bob" } } },
764
+ { deleteMany: { filter: { status: "deleted" } } },
765
+ ],
766
+ });
767
+ // result: { ok, insertedCount, matchedCount, modifiedCount, deletedCount, upsertedCount, insertedIds, upsertedIds }
768
+ ```
769
+
770
+ The ids in `insertedIds` and `upsertedIds` are text, and their keys are the indexes of the operations.
771
+
772
+ ##### `mongo.documents.explain`
773
+
774
+ The procedure gives the plan of a find or of an aggregation pipeline. With `pipeline`, it explains an aggregation. Without `pipeline`, it explains a find with `query`, `projection`, `sort` and `limit`. The verbosity `queryPlanner` (the default) gives the plan only. With the verbosities `executionStats` and `allPlansExecution`, the server also does the plan and gives its statistics. As `documents.aggregate` does, the procedure refuses `$out` and `$merge`.
775
+
776
+ ```typescript
777
+ const { operation, plan } = await client.exec(["mongo", "documents", "explain"], {
778
+ collection: "users",
779
+ query: { email: "ann@example.com" },
780
+ verbosity: "executionStats",
781
+ });
782
+ console.log(plan.queryPlanner.winningPlan);
783
+ ```
784
+
738
785
  #### Index Procedures
739
786
 
740
787
  ##### `mongo.indexes.list`
@@ -856,6 +903,26 @@ interface DropIndexOutput {
856
903
  }
857
904
  ```
858
905
 
906
+ #### Change Streams
907
+
908
+ ##### `mongo.watch`
909
+
910
+ The procedure gives the changes of a collection as a stream. Without `collection`, it gives the changes of each collection of the database. The stream has no end (`metadata.stream: { unbounded: true }`). It stops when the reader stops or the call aborts, and the procedure then closes the change stream.
911
+
912
+ A change stream needs a replica set or a sharded cluster. A standalone server refuses it, and the reader gets that error.
913
+
914
+ ```typescript
915
+ for await (const change of client.execStream(["mongo", "watch"], {
916
+ collection: "orders",
917
+ pipeline: [{ $match: { operationType: { $in: ["insert", "update"] } } }],
918
+ fullDocument: "updateLookup",
919
+ })) {
920
+ console.log(change.operationType, change.documentKey, change.resumeToken);
921
+ }
922
+ ```
923
+
924
+ Each item has the `operationType`, the namespace `ns`, the `documentKey` and the `resumeToken`. An insert gives the `fullDocument`, and an update gives the `updateDescription`. With `fullDocument: "updateLookup"`, an update also gives the current document. To continue after a stop, give the last `resumeToken` as `resumeAfter`. The `pipeline` accepts only the stages of a change stream: `$match`, `$project`, `$addFields`, `$set`, `$unset`, `$replaceRoot`, `$replaceWith` and `$redact`.
925
+
859
926
  ### Type Definitions
860
927
 
861
928
  #### Core Types
@@ -1278,9 +1345,13 @@ client-mongo/
1278
1345
  │ │ ├── documents.delete.ts
1279
1346
  │ │ ├── documents.count.ts
1280
1347
  │ │ ├── documents.aggregate.ts
1348
+ │ │ ├── documents.distinct.ts
1349
+ │ │ ├── documents.bulk.ts
1350
+ │ │ ├── documents.explain.ts
1281
1351
  │ │ ├── indexes.list.ts
1282
1352
  │ │ ├── indexes.create.ts
1283
- │ │ └── indexes.drop.ts
1353
+ │ │ ├── indexes.drop.ts
1354
+ │ │ └── watch.ts # mongo.watch
1284
1355
  ├── dist/ # Compiled output
1285
1356
  ├── package.json
1286
1357
  ├── tsconfig.json