The DataStore class takes two parameters in its constructor: dbName (the name of the IndexedDB database) and storeName (the name of the object store within the database).
The init method is used to initialize the IndexedDB database and create the object store if it doesn't exist. It uses the openDB function from the idb library to open the database and upgrade it if necessary.
The isIndexedDBSupported method checks if the browser supports IndexedDB by checking for the presence of the indexedDB property in the window object.
The getItem, setItem, removeItem, clear, key, and length methods are implemented to provide an API similar to the localStorage API.
If IndexedDB is supported, these methods interact with the IndexedDB database using the idb library's methods (get, put, delete, clear, getAllKeys).
If IndexedDB is not supported, the methods fallback to using the corresponding localStorage methods.
The async/await syntax is used to handle asynchronous operations with IndexedDB.
To use this class in your web app, you need to import it and create an instance of DataStore with the desired database name and store name. Make sure to call the init method to initialize the database before using other methods.
Note: Make sure to install the idb library using npm install idb or include it via a script tag in your HTML file.