msiemens / tinydb

TinyDB is a lightweight document oriented database optimized for your happiness :)
https://tinydb.readthedocs.org
MIT License
6.75k stars 536 forks source link

Need 'prettified' version of the JSON db #180

Closed NGenetzky closed 6 years ago

NGenetzky commented 6 years ago

I currently find my self using a simple function to prettify the json database before I commit it to a git repository. I do this for two reasons: 1. readability 2. diff-ability .

Is there currently a built in way to do this? I didn't see anything on the 'TinyDB' class. Would you consider adding a small function that makes it easier?

I also took a glance at 'storage.py' and it appears that it would be pretty easy to utilize the self.kwargs of JSONStorage to use dumps(data, sort_keys=True, indent='\t') . Do you think that this would cause a major performance hit?

NGenetzky commented 6 years ago

Thanks for creating a great python package. I am excited to use it in my current project.

msiemens commented 6 years ago

Hey, you can pass your own parameters to JSONStorage which are passed down to dumps. So you can use this to get a formatted JSON file:

db = TinyDB('db.json', sort_keys=True, indent=4, separators=(',', ': '))

(Just for reference: this has been implemented in #61)

msiemens commented 6 years ago

Feel free to re-open if it doesn't work as expected.

answerquest commented 6 years ago

This is wonderful. Please include a mention of these options in the docs? Could mention it on getting-started.html or in API doc - database

answerquest commented 6 years ago

Hi @msiemens , this code is working well, but in sorting since the id's are as strings, it is sorting them as string. So, they are coming in this order : 1, 10, 100, 102, ... 179, 18, 180, ... 199, 2, 20, 200... . This is not much problem from operations point of view so I'm ok with it, but wanted to share FYI.

msiemens commented 6 years ago

This is wonderful. Please include a mention of these options in the docs? Could mention it on getting-started.html or in API doc - database

I've added this to the docs in 0ae404967d7dc467a3dad40d2481a87a598fd9ce 🙃

MostHated commented 4 years ago

Sorry if this is a silly question, but I was wondering, it doesn't seem like there would be, but is there any sort of performance impact that might come down the road if the database starts to get sizable when using the formatted version due to all the extra whitespace?

NGenetzky commented 4 years ago

@MostHated , Yes extra processing (formatting) will most certainly affect performance. If that starts to be a concern I would recommend you leave the default separators and simply do the formatting as a separate script/aciton.