Hello there all.
I am trying to figure out how to go about organizing this and how I should go about inserting / updating. The way my system will primarily work is the first thing it does is run a check to see if a particular ID string exists anywhere, if not it moves on with my application, the ID string gets verified by an outside API and if it comes back good I want to make a record in the DB in which I am capturing the users username, the ID number being used, the name of the thing the ID number is associated with, and the current date / time.
What I am wanting is for the username to essentially be primary key of the entry, but the user might have multiple entries of ID numbers that would get added over time along with the name of the item it goes with. So what I am wanting to do keep it how I have it at first which checks if the ID exists, while the ID might not exist, the username might, so when it gets to the point of inseting the new ID I want it to check if the username exists, if not it makes a new entry and adds the data, but if a username does exists, use that entry and simple add a new line for the new ID number and associated name.
What would be the recommended way to go about structuring my function to achieve this? Here is the general idea of how it would be structured, but I am not quite sure how to go about achieving this properly with insert / updating into a specific record based on the username.
db.insert({
'username': myusername,
packages: [{
'idnum': 123, 'package': package1,
'idnum': 456, 'package': package2,
'idnum': 789, 'package': package3,
}],
'date': str(datetime.now())
})
I realize that the actual code would look something more like this.
db.insert({
'username': username,
packages: [{'idnum': idnum, 'package': package}],
'date': str(datetime.now())
})
How might I go about doing an insert to an already existing entry like this, then simply adding a new entry to "packages" with a new line containing an idnum and package? With SQL or similar I believe it would end up being something like INSERT INTO packages WHERE username = $username (or close to that), I just am not sure how to achieve this using TinyDB (or using python in general, I have only been using it for 2-3 days, so definitely still learning).
Thanks!