Files
Chromy/chromy/handlers/delete_collection.py
T

37 lines
1.1 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
from chromy.command_inputs import DeleteCollectionInput, DeleteRecordsInput
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
def handle_delete_collection(command: DeleteCollectionInput) -> int:
delete_collection(command.collection)
print(f"Deleted collection '{command.collection}'.")
2026-04-21 17:42:37 +02:00
return 0
2026-04-21 21:26:40 +02:00
def handle_delete_records(command: DeleteRecordsInput) -> int:
where = _parse_where_clause(command.where)
deleted = delete_data(command.collection, where)
2026-04-21 21:26:40 +02:00
condition, value = next(iter(where.items()))
print(
f"Deleted {deleted} record(s) from collection '{command.collection}' "
2026-04-21 21:26:40 +02:00
f"where {condition}={value}."
)
return 0