Irrelon / ForerunnerDB

A JavaScript database with mongo-like query language, data-binding support, runs in browsers and hybrid mobile apps as a client-side DB or on the server via Node.js!
http://www.irrelon.com
721 stars 72 forks source link

How to add a custom localforage to forerunnerdb? #225

Open regisdetoni opened 7 years ago

regisdetoni commented 7 years ago

I'm will try to use this plugin to persist on IOS and break the size limitations of browser storage. https://github.com/thgreasi/localForage-cordovaSQLiteDriver Yet, don't know how to add this to forerunnerdb.

Irrelon commented 7 years ago

Hi there.

According to the documentation on that page, you just need to load the sql lite driver after loading localforage. Since localforage gets wrapped in ForerunnerDB's code you can load the driver after loading ForerunnerDB's js file. The main complexity is that we don't expose libraries to the window by default because that could create global naming conflicts. Therefore in order to access localforage from ForerunnerDB you need to know where it is located, which is here:

ForerunnerDB.shared.modules.Persist.prototype.localforage

From their documentation you just need to do something like this (notice I have replaced loading localforage.js with ForerunnerDB instead):

<script src="cordova.js"></script>

<script src="lib/ForerunnerDB/dist/fdb-all.min.js"></script>
<script src="lib/localForage-cordovaSQLiteDriver/dist/localforage-cordovasqlitedriver.js"></script>
<script>
// Expose localforage to the global scope - I wouldn't do it this way but it's up to you :)
window.localforage = ForerunnerDB.shared.modules.Persist.prototype.localforage;

localforage.defineDriver(window.cordovaSQLiteDriver).then(function() {
    return localforage.setDriver([
        // Try setting cordovaSQLiteDriver if available,
      window.cordovaSQLiteDriver._driver,
      // otherwise use one of the default localforage drivers as a fallback.
      // This should allow you to transparently do your tests in a browser
      localforage.INDEXEDDB,
      localforage.WEBSQL,
      localforage.LOCALSTORAGE
    ]);
}).then(function() {
  // this should alert "cordovaSQLiteDriver" when in an emulator or a device
  alert(localforage.driver());
  // set a value;
  return localforage.setItem('testPromiseKey', 'testPromiseValue');
}).then(function() {
  return localforage.getItem('testPromiseKey');
}).then(function(value) {
  alert(value);
}).catch(function(err) {
  alert(err);
});
</script>

I haven't tested this code but it should work just fine!

regisdetoni commented 7 years ago

this is what is was missing

ForerunnerDB.shared.modules.Persist.prototype.localforage

maybe should doc this 👍

thanks

Irrelon commented 7 years ago

Awesome. I'll add it to the docs!