Closed cdwilson closed 2 years ago
I agree with @cdwilson ,
You can choose the type of the doc_id by setting doc_id_class
, but it get converted to string anyway.
Why not let you choose the type of doc_id you actually want to write?
Also even when working with the json format, the moment you choose to use something other than the standard lib json u probably can use other types as key as well.
For example orjson has a nonstringkey option wich allows it to parse things other than string.
Anyhow, this seems more of a job for the storage than for the Table class.
Short note: I'm moving this to a discussion as I feel that the discussions board is a better place to, well, discuss this topic 🙂
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:https://github.com/msiemens/tinydb/blob/967dbf9197b2943eb9bd91650f90bd9ab497bc12/tinydb/table.py#L727-L736
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: