Files
Chromy/chromy/handlers/delete_collection.py
T

36 lines
1.0 KiB
Python
Raw Normal View History

from __future__ import annotations
2026-04-21 17:42:37 +02:00
2026-04-22 15:47:46 +02:00
from chromy.chroma_functions import delete_collection, delete_data
2026-04-21 21:26:40 +02:00
def _parse_where_clause(where_clause: str) -> dict[str, str]:
condition, separator, value = where_clause.partition("=")
if separator == "":
2026-04-22 17:03:01 +02:00
raise ValueError("Invalid --where value. Expected <condition>=<value>.")
2026-04-21 21:26:40 +02:00
condition = condition.strip()
value = value.strip()
if not condition or not value:
2026-04-22 17:03:01 +02:00
raise ValueError("Invalid --where value. Expected <condition>=<value>.")
2026-04-21 21:26:40 +02:00
return {condition: value}
2026-04-21 17:42:37 +02:00
2026-04-22 22:14:26 +02:00
def handle_delete_collection(collection: str) -> int:
delete_collection(collection)
print(f"Deleted collection '{collection}'.")
2026-04-21 17:42:37 +02:00
return 0
2026-04-21 21:26:40 +02:00
2026-04-22 22:14:26 +02:00
def handle_delete_records(collection: str, where_clause: str) -> int:
where = _parse_where_clause(where_clause)
deleted = delete_data(collection, where)
2026-04-21 21:26:40 +02:00
condition, value = next(iter(where.items()))
print(
2026-04-22 22:14:26 +02:00
f"Deleted {deleted} record(s) from collection '{collection}' "
2026-04-21 21:26:40 +02:00
f"where {condition}={value}."
)
return 0