Offline databse

#1

Hi,
I am developing sales tools app , this app uses our company’s existing website database which is in MongoDB. For online access I am using WEB API calls to get product data etc. Now I was wondering as this app will be used by sales person mostly on road. So they wont have internet connectivity all the time. I was wondering what will be the best approach to make this app available offline. Which database should I use for the device and how to syn device local database with remote database.

Any suggestions.
Thanks

#2

Check out ionics built in Storage: https://ionicframework.com/docs/storage/

If the online database will always be the “source of truth” then you can make an api call when online and merge it into a local copy in the storage. This would be something similar to the settings demo here except with an api call: https://github.com/ionic-team/ionic-starter-super/blob/master/src/providers/settings/settings.ts

Its going to be a lot trickier if the guy in the field is able to edit whats in the local storage and expects these changes to all be synced back to the online database once he has connection again.

#3

Hi,
Thanks for your reply , The only thing I am struggling to know is how I am going to retrieve a particular product data.
The web API call will return all products as JSON , then I will store this JSON data, on local device, under a Key say ProductInfo. This ProductInfo will contain all products, and if I want to retrieve a product name “Calvin” then how I am going to do this. If I follow the github example then the following code snippet will return all products not one particular product that I am looking for.

 getValue(key: string) {
    return this.storage.get(this.SETTINGS_KEY)
      .then(settings => {
        return settings[key];
      });
  }

I am not worry about user changing data on local device as it will be one way sync, from serve to the device.

Thanks

#4

How you retrieve a particular product depends on the structure of the JSON the API returns. If the names are the keys then it would be something like this:

# API returns object like this { 'Alice': { 'color': 'red', 'monitoring': true }, 'Bob': { 'color': 'blue' }};

getValue(key: string) {
    return this.storage.get(this.PRODUCT_INFO_KEY)
      .then(productInfo=> {
        return productInfo[key];
      });
  }

getValue('Alice');
# this returns { 'color': 'red', 'monitoring': true }

If the product name is nested deeper within the object or if the API returns an array of objects then you’ll have to iterate through and look for certain key/value pairs.