Replies: 4 comments 2 replies
-
I agree with @cdwilson , You can choose the type of the doc_id by setting Anyhow, this seems more of a job for the storage than for the Table class. |
Beta Was this translation helpful? Give feedback.
This comment has been minimized.
This comment has been minimized.
-
Honestly, the main reason was that when I implemented custom document ID types ( A complicating factor regarding this topic is that the interface between TinyDB and storage implementations is under-specified. For a long time the only real contract between TinyDB and storage implementations was that the storage received a All that being said, a way forward regarding this would be to drop the conversion of document IDs to string when storing data as you proposed and also at least documenting the data that storages may receive from TinyDB better. This still wouldn't solve the issue of different storage implementations supporting different data types. But maybe we could mention this in the user documentation and note that when using non-primitive types (anything not representable in JSON) one may need to use the |
Beta Was this translation helpful? Give feedback.
-
@msiemens thanks for the additional context on this! (sorry for the delayed response, been super busy the last few weeks)
Agreed, this occurred to me as well. BTW, this isn't something that I urgently have a need for now, but just wanted to pose the question in case you think it's worth considering for a future version of TinyDB. I was playing around with the I'll keep thinking through some of the additional notes you mentioned and reply back once I've had a chance to think through this a bit more. |
Beta Was this translation helpful? Give feedback.
-
As someone who is brand new to TinyDB, my naive assumption was that when writing to storage, the
doc_id
would be passed to the storage class keeping thedocument_id_class
type intact, and it would be the responsibility of each storage class to convert it into a type that is compatible for that storage type.However, I was surprised to find that the
Table
class preemptively converts thedoc_id
to a string before passing the table to the storage class for writing:tinydb/tinydb/table.py
Lines 727 to 736 in 967dbf9
I'm curious if there is a reason why this conversion needs to happen in the
Table._update_table()
method, or whether the conversion could be delegated to the storage class instead?For example, the
YAMLStorage
class described in Write a Custom Storage supports writing a YAML document directly to storage with integer document IDs. Making the following change to theTable._update_table()
method enables theYAMLStorage
storage class to store a YAML document with integer document IDs:Beta Was this translation helpful? Give feedback.
All reactions