hexdb 2.4.1__tar.gz → 2.4.3__tar.gz

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.
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: hexdb
3
- Version: 2.4.1
3
+ Version: 2.4.3
4
4
  Summary: Client for HexDB, the hexagonal document database.
5
5
  Author: Dream In Hex
6
6
  License-Expression: Apache-2.0
@@ -29,7 +29,7 @@ from typing import Any, BinaryIO, Callable, Dict, Iterator, List, Optional, Sequ
29
29
  from .connection_uri import ConnectionUri, is_connection_uri, parse_connection_uri
30
30
 
31
31
  __all__ = ["HexDB", "HexDBError", "Tessellation", "Stream", "Files", "ConnectionUri", "parse_connection_uri"]
32
- __version__ = "2.4.1"
32
+ __version__ = "2.4.3"
33
33
 
34
34
  Json = Any
35
35
  Document = Dict[str, Any]
@@ -206,6 +206,20 @@ class HexDB:
206
206
  """Operations across tessellations, all or nothing."""
207
207
  return self.request("POST", "/transactions", {"operations": operations}, _idem(idempotency_key))
208
208
 
209
+ def deletes(self, **query: Any) -> Dict[str, Any]:
210
+ """The delete history, newest first: ``by``, ``tessellation``, ``kind``, ``since``,
211
+ ``until``, ``limit``, ``offset`` (your own deletes, or everyone's with the audit permission)."""
212
+ params = "&".join(f"{k}={_quote(str(v))}" for k, v in query.items() if v is not None)
213
+ return self.request("GET", f"/deletes?{params}")
214
+
215
+ def restore_deletes(self, batch: str, *, skip_existing: bool = False) -> Dict[str, Any]:
216
+ """Put back everything a delete batch removed, in one step."""
217
+ return self.request("POST", f"/deletes/{_quote(batch)}/restore?skip_existing={'true' if skip_existing else 'false'}")
218
+
219
+ def purge_deletes(self, batch: str) -> Dict[str, Any]:
220
+ """Delete what a delete batch left in the trash, for good."""
221
+ return self.request("DELETE", f"/deletes/{_quote(batch)}")
222
+
209
223
  def graphql(self, query: str, variables: Optional[Dict[str, Any]] = None) -> Dict[str, Any]:
210
224
  """Run a GraphQL query or mutation; raises HexDBError on GraphQL errors."""
211
225
  result = self.request("POST", "/graphql", {"query": query, "variables": variables or {}})
@@ -331,8 +345,27 @@ class Tessellation:
331
345
  detokenize permission; audited). One-way and unknown tokens map to None."""
332
346
  return self.db.request("POST", f"/tessellations/{_quote(self.name)}/detokenize", {"tokens": list(tokens)})["values"]
333
347
 
334
- def update_where(self, filter: Dict[str, Any], update: Dict[str, Any]) -> Dict[str, int]:
335
- return self.db.request("POST", f"{self.path}/_update", {"filter": filter, "update": update})
348
+ def update_where(self, filter: Dict[str, Any], update: Dict[str, Any], *, all: bool = False) -> Dict[str, int]:
349
+ """Merge ``update`` into every document matching ``filter``. An empty filter
350
+ (every document) is refused unless ``all=True``."""
351
+ return self.db.request("POST", f"{self.path}/_update", {"filter": filter, "update": update, "all": all})
352
+
353
+ def delete_where(
354
+ self,
355
+ filter: Dict[str, Any],
356
+ *,
357
+ all: bool = False,
358
+ dry_run: bool = False,
359
+ expect: Optional[int] = None,
360
+ idempotency_key: Optional[str] = None,
361
+ ) -> Dict[str, Any]:
362
+ """Delete every document matching ``filter``, and what belongs to them through
363
+ parent links, as one atomic delete batch that ``db.restore_deletes(batch)`` puts
364
+ back. An empty filter is refused unless ``all=True``. ``dry_run`` only reports
365
+ what would go; ``expect`` refuses (409 count_changed) unless exactly that many
366
+ documents and files would go."""
367
+ body = {"filter": filter, "all": all, "dry_run": dry_run, "expect": expect}
368
+ return self.db.request("POST", f"{self.path}/_delete", body, _idem(idempotency_key))
336
369
 
337
370
 
338
371
  class Stream:
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: hexdb
3
- Version: 2.4.1
3
+ Version: 2.4.3
4
4
  Summary: Client for HexDB, the hexagonal document database.
5
5
  Author: Dream In Hex
6
6
  License-Expression: Apache-2.0
@@ -4,7 +4,7 @@ build-backend = "setuptools.build_meta"
4
4
 
5
5
  [project]
6
6
  name = "hexdb"
7
- version = "2.4.1"
7
+ version = "2.4.3"
8
8
  description = "Client for HexDB, the hexagonal document database."
9
9
  readme = "README.md"
10
10
  requires-python = ">=3.9"
@@ -53,6 +53,28 @@ class ClientTest(unittest.TestCase):
53
53
  self.assertEqual(result["ids"][0], ids[0])
54
54
  self.assertEqual(orders.update_where({"status": "void"}, {"refunded": True})["modified"], 1)
55
55
 
56
+ def test_deletes(self):
57
+ name = f"{self.prefix}_carts"
58
+ carts = self.db.tessellation(name)
59
+ carts.insert_many([{"open": True}, {"open": True}, {"open": False}])
60
+ with self.assertRaises(HexDBError) as caught:
61
+ carts.delete_where({})
62
+ self.assertEqual(caught.exception.code, "filter_required")
63
+ with self.assertRaises(HexDBError):
64
+ carts.update_where({}, {"seen": True})
65
+ self.assertEqual(carts.update_where({}, {"seen": True}, all=True)["modified"], 3)
66
+ preview = carts.delete_where({"open": True}, dry_run=True)
67
+ self.assertEqual(preview["total"], 2)
68
+ with self.assertRaises(HexDBError) as caught:
69
+ carts.delete_where({"open": True}, expect=5)
70
+ self.assertEqual(caught.exception.code, "count_changed")
71
+ done = carts.delete_where({"open": True}, expect=preview["total"])
72
+ self.assertEqual(carts.count(), 1)
73
+ self.assertEqual(self.db.deletes(tessellation=name)["batches"][0]["batch"], done["batch"])
74
+ self.assertEqual(self.db.restore_deletes(done["batch"])["restored"][name], 2)
75
+ self.assertEqual(carts.count(), 3)
76
+ self.assertEqual(self.db.purge_deletes(done["batch"])["purged"], 0)
77
+
56
78
  def test_transactions_and_errors(self):
57
79
  ledger = f"{self.prefix}_ledger"
58
80
  op = [{"op": "insert", "tessellation": ledger, "data": {"amount": 5}}]
File without changes
File without changes
File without changes
File without changes