Get key of value just saved in Dexie

Viewed 51

I am saving a value to my Dexie (IndexedDB) database, using put(), and I immediately need to know what the key of that new entry is. How do I go about doing that?

The below is my code. I wasn't able to find a solution searching with Google. Thanks!

 let newDog = {
          armbandNumber: this.armbandMax,
          dogCallName: this.callName,
          dogRegName: this.regName,
          dogOwnerKey: this.dogOwner.value,
          dogOwner: this.dogOwner.label,
          dogCoOwner: this.dogCoOwner,
          dogHandler: this.dogHandler.value,
          dogBday: this.dogBday,
          dogBreed: this.dogBreed,
          dogHeight: this.dogHeight,
          dogVariety: this.dogVariety,
          dogSire: this.dogSire,
          dogDam: this.dogDam,
          dogBreeder: this.dogBreeder,
          akcNum: this.akcNum,
          bhaNum: this.bhaNum,
          dogGender: this.dogGenderModel,
          dateDogAdded: Date.now(),
          barnHuntLevel: this.bhLevelModel,
          barnHuntClassAB: this.bhLevelModelAB,
          dogHeightDivision: this.heightDivision,
          scentWorkLevel: this.swLevelModel,
          scentWorkClassAB: this.swLevelModelAB,
        }
        pawTapDB.dogTable.put(newDog) 
1 Answers

https://dexie.org/docs/Table/Table.put() says:

If the operation succeeds then the returned Promise resolves to the key under which the object was stored in the Table.

So do:

const key = await pawTapDB.dogTable.put(newDog);
Related