Search code examples
indexeddbdexie

Can we use set objectstore name dynamically?


I am using Dixie.js in my application, below is the piece of code to create a DB & a object store

import Dexie from "dexie";

const db = new Dexie("sampledb");
db.version(1).stores({ tasks: "++id" });

Instead of hard codeed table/objectstore name task, can we use some variable's value like below

const sessionId = "SEEE-123456";
db.version(1).stores({ sessionId: "++id" });

Could not find any option in their official documentation. I will appreciated for any help/input.

Thanks!


Solution

  • In modern javascript you can declare objects with dynamic property names:

    const tableName = "yourDynamicTableName";
    db.version(1).stores({ [tableName]: "++id" });