Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Urgent - Cannot add additional object store #332

Open
reengineeringdevops opened this issue Dec 20, 2024 · 0 comments
Open

Urgent - Cannot add additional object store #332

reengineeringdevops opened this issue Dec 20, 2024 · 0 comments

Comments

@reengineeringdevops
Copy link

reengineeringdevops commented Dec 20, 2024

To add an additional object store, it is required to increase the database version. Therefore, I suggest the following fix

`async function openDatabase(databaseInfo: DatabaseInfo) {
if (databaseInfo.version === null) {
databaseInfo.version = undefined;
}
databaseInfo.keyPath ??= 'id';

try {

    const storeName = databaseInfo.storeName ?? databaseInfo.databaseName;

    const currentVersionConnection = await openDB(databaseInfo.databaseName);

    if (currentVersionConnection != null &&
        !currentVersionConnection.objectStoreNames.contains(storeName))
    {
        databaseInfo.version =  currentVersionConnection.version + 1;

        currentVersionConnection.close();

        const database = await openDB(
            databaseInfo.databaseName,
            databaseInfo.version, {
            upgrade(db) {
                const storeName = databaseInfo.storeName ?? databaseInfo.databaseName;
                if (!db.objectStoreNames.contains(storeName)) {
                    db.createObjectStore(
                        databaseInfo.storeName ?? databaseInfo.databaseName, {
                        keyPath: databaseInfo.keyPath,
                    });
                }
            }
        });

        return database;
    }

    return currentVersionConnection;
    
} catch (e) {
    console.error(e);
    return null;
}

}`

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant