ngx-indexed-db icon indicating copy to clipboard operation
ngx-indexed-db copied to clipboard

Prevent race condition on creation of database

Open coolweb opened this issue 5 years ago • 9 comments

The creation of the database is done in your forroot that call the ngx indexed db service constructor which create the db in asynchronous action. If some code try to access the database, the database might not be created. A solution is to create a separate method to create the database with async return like promise and call this method in an APP_INITILAZER provider in your module.

coolweb avatar Feb 14 '20 09:02 coolweb

I am not sure if this would make a big difference, currently, the DB is created on the module initialization. What would change creating a method and calling it? I think to check if the DB is ready will still have to be done.

assuncaocharles avatar Feb 15 '20 11:02 assuncaocharles

It make a difference, in our case we want in some scenario delete the database on app starting. If your code is in a method which return a promise and that method is called into an APP_INITIALIZER provider in your module, it'll not create race condition. For the moment while our app is starting, your code start which create the db without store, our code do some stuff and then the rest of your code continue and try to create stores...

coolweb avatar Feb 17 '20 09:02 coolweb

@coolweb we´r facing the same issue

WhatzzUp avatar Feb 21 '20 11:02 WhatzzUp

Alright it makes sense, so I would think about passing a callback into the configuration to be called when it's done.. What do you think?

assuncaocharles avatar Feb 21 '20 11:02 assuncaocharles

A callback'll resolve the problem but I think the standard way in angular is to use the APP_INITIALIZER factory when we need to initialize async stuffs, in the forroot you initialize sync stuff like config values. I use APP_INITIALIZER in some apps for this kind of scenario, some info here https://www.tektutorialshub.com/angular/angular-how-to-use-app-initializer/

coolweb avatar Feb 21 '20 12:02 coolweb

Just FYI since this is still open, to my knowledge, you can't use APP_INITIALIZER when the module is loaded lazily (and there is no MODULE_INITIALIZER equivalent yet), so a callback would be more universal

evilstiefel avatar Jun 04 '21 09:06 evilstiefel

Can this be related to #295

jonlighthill avatar Feb 25 '22 00:02 jonlighthill

@all-contributors please add @coolweb for maintenance

assuncaocharles avatar May 29 '22 00:05 assuncaocharles

@assuncaocharles

I've put up a pull request to add @coolweb! :tada:

allcontributors[bot] avatar May 29 '22 00:05 allcontributors[bot]