Search code examples
javascriptindexeddbdexie

Specify different unique key in Dexie database schema?


My basic dexie database scheme is something like this.

const db = new Dexie('MyDatabase');

// Declare tables, IDs and indexes
db.version(1).stores({
    myrecords: 'record_id'
});

I want to use my record_id as a unique key. In indexeddb I can do this like the below

 var myrecordsObjectStore = db.createObjectStore('myrecords' , {
                                keyPath: 'record_id'
                            });

Solution

  • Should work using & prefix for unique as noted in docs

    db.version(1).stores({
        myrecords: '&record_id'
    });
    

    See Dexie Quick Reference