import { athenaConfigOptions } from '../athena-config.js';

const request = indexedDB.open(
    athenaConfigOptions.indexedDatabaseName,
    1
);

request.onerror = () => console.error('Error opening database');

// Initiating database
let database;
let objectStore;

// Create a promise to handle the asynchronous operation
const promiseDatabase = new Promise((resolve, reject) => {
    request.onupgradeneeded = (event) => {
        database = event.target.result;

        objectStore = database.createObjectStore(athenaConfigOptions.indexedObjectStore, {
            keyPath: 'id',
            autoIncrement: false,
        });
    }

    request.onsuccess = event => resolve(event.target.result);
    request.onerror = event => reject(event.target.error);
});

export default promiseDatabase;
