Closed aptly-io closed 1 month ago
This might be a recipe for others when a str as doc_id:
class _Document(dict):
def __init__(self, value: Mapping, doc_id: str):
super().__init__(value)
self.doc_id = doc_id
class _Table(Table):
document_class = _Document
document_id_class = str
def _get_next_id(self):
raise NotImplementedError("doc_id should be self provided")
class _TinyDB(TinyDB):
table_class = _Table
class Model:
def __init__(self):
self._db = _TinyDB("db.json")
There are some issue with table.py though when using _Document ...
I think your solution with a subclass is the best solution for your use-case (modulo the bug you found with the incorrect isinstance
check).
When inserting documents and knowing a certain document property value (e.g. house-id) is unique, is there a way to tell tinydb to use that house-id as document ID?
Just found https://github.com/msiemens/tinydb/issues/351
The
document_id_class
is a class variable, it's for all tinydb'sTable
(looks not ideal at first sight).Would this approach make sense: When inserting a new Mapping, first make it a
Document(mapping, mapping.house-id)
(where the doc_id equal to the house-id). HoweverDocument
needs anint
typedoc_it
. Should one also subclassDocument
to customize thedoc_id
's type and change theTable
'sdocument_class
? When creating aTable
sub-class, one also has to create TinyDB sub-class to modify its class variabletable_class
?Is there a more elegant approach that I'm overlooking?