Ionic Storage - Create the DB

Using this API https://github.com/ionic-team/ionic-storage

when we create a database we call the following method

export const createStore = (name:string) => {
    storage = new Storage({
       name,
        driverOrder: [Drivers.IndexedDB, Drivers.LocalStorage]
    });
    storage.create();
}

So we create it and then we set it a value

createStore(“providers”)
setDB(“providers”, JSON.stringify(providers))

Once it has already been created and a value has been stored. When the application is run again to read or write to the database. Is it necessary to recreate it? I mean, use the statement createStore

I think yes, at app launch

The name might be confusing - I think of it as “connecting” and under the hood when the db does not exist It is created

The Localforage doc also says you can have multiple storages pointing to same instance so maybe too many creates wont even harm the underlyibg data in the db

1 Like

So I shouldn’t create many databases but just one and use different keys?

I think you need to keep the datbase name in the create function the same and assure the create function runs as soon as possible to not to create issues with read and writes

And then yes, use as many keys as you like

1 Like