nedb icon indicating copy to clipboard operation
nedb copied to clipboard

syncing databases between two instances of the same app

Open selganor74 opened this issue 4 years ago • 2 comments

We developed an application using electron and nedb running in the renderer process. This app uses nedb to allow users to work offline, by downloading a copy of information provided by a backend. Until now everything worked fine, but now a new requirement came ... the users wants a way to use several instances of the app at the same time (until now we limited the instances to one)... at this point we are a bit in trouble because nedb is running in the renderer process, storing data in data files. Opening an new browser window will create a new renderer process instance, and since nedb keeps all of its data in memory, an update from an instance won't be recognized by the other and don't even want to imagine what can happen when updating from both (or more) sides ...

The wise way is to move all the database handling to a separate node process, and communicate via ipc with the renderer process, but this solution moves us one step away from making the app full web (at the time we developed this app HTML5 and PWA were not an hype) ....

To avoid blocking our way to remove electron from the dependencies (while keeping nedb 😄 ) a path could be to find a way to keep the database in sync between the two (or more) instances of the application.

To follow the latter path I need to know when an instance completes a modification (to put database access of the other instances in a wait state) and when the modification is effectively written to disk (to force a loadDatabase() and free the other instances). For the first I can instrument my code to record every change having success, but is it enough to say that the file has been modified and is ready to be reloaded by the other instances ?

selganor74 avatar Jul 15 '20 21:07 selganor74

+1

mqc507 avatar Sep 20 '20 02:09 mqc507

I solved this problem recreate database like below

db = new Datastore();

respecu avatar Nov 19 '20 02:11 respecu