Synchronizing MongoDB server data to an IndexedDB

2020-05-23 04:29发布

问题:

I'm trying to evaluate using IndexedDB to solve the offline issue. It would be populated with data currently stored in a MongoDB database (as is).

Once data is stored in IndexedDB, it may be changed on the MongoDB server and I need to propagate those changes. Is there any existing framework or Library to do somehting like this for Mongo. I already know about CouchDB/PouchDB and am not exploring those two.

回答1:

I haven't worked with IndexDB, but the design problem isn't that uncommon. My understanding of your app is that when the client makes the connection to MongoDB, you pull a set of documents down for local storage and disconnect. The client then can do things locally (not connected to the data server), and then push up the changes.

The way I see it you've got to handle two general cases:

  1. when the MongoDB server is updated and breaks continuity with the client, the client will have to
    1. poll for the data (timer?) or
    2. keep a websocket open to let notifications free-flow over the pipe
  2. when the user needs to push changed data back up the pipe
    1. you can reconnect asynchronously, check for state changes, (resolving conflicts according to your business rules)
    2. have a server side (light) interface for handling conflicts (depending on complexity of your app, comparing time stamps of state changes in MongoDB to IndexedDB updates should suffice)