msiemens . Hi. Look my example below is a little silly, but i think gets the point across. But my normal use case for TinyDB is more like the func add_game_timer2. I am generally adding/updating/reading records that I consider the records to be unique to one of the dicts attrs.
So if I could for example use add_game_timer func, and pass it a key param, it would just seem to me this would be the wider use case. As I mentioned I am guessing at that assertion. But regardless if the key was an optional kwarg or there was a new method, it would not need to impact the use case were you dont care about duplicates.
I am not sure if my example of how I check for duplicates is the moist effecifient way or not. But it seemed to make sense to use the get method as I am assuming I will only either find one or no records, then if found use doc_id to do the update.
Again I understand its still pretty trivial to do the test. Just seems to me that for when you are doing something very quick, this notation is neat and easy, also easy to remember. Also i realise many other ways to code this functionality under the hood. But given backward compatibility, hopefully something like this approch would avoid that.
I also see you may have a problem having a method called insert having the ability to do updates. Maybe that maybe a good reason for a new method(stub at least) with a more appropriate name. On the other hand, you might be ok with it.
Look, I have gone on a little bit here. The reason I have is because I really like TinyDB. I know the intention is not to be a full fledge relational database. But I do not think its not a stretch to think that often users will store records that they consider unquie by on of their dicts attrs.
from tinydb import TinyDB, Query
_tdb = TinyDB('example.dat')
def add_game_timer(timer_dict):
doc = _tdb.insert(timer_dict)
print('doc inserted, doc_id', doc)
def add_game_timer2(timer_dict):
tbl = _tdb.table('Table2')
q = Query()
doc = tbl.get(q.name == timer_dict['name'])
if not doc:
id = tbl.insert(timer_dict)
print('doc inserted=', id)
else:
tbl.update(timer_dict, doc_ids=[doc.doc_id])
print('doc ID, updated=', doc.doc_id)
t1 = dict(name='kvk', start=0, finish=10)
t2 = dict(name='ww', start=40, finish=10)
t3 = dict(name='kvk', start=20, finish=100)
add_game_timer(t1)
add_game_timer(t2)
add_game_timer(t3) # will be duplicated
add_game_timer2(t1)
add_game_timer2(t2)
add_game_timer2(t3) # t1, will be updated