Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Multiple Driver Fallback Support #357

Open
1 task done
dalezak opened this issue Dec 13, 2023 · 2 comments
Open
1 task done

Multiple Driver Fallback Support #357

dalezak opened this issue Dec 13, 2023 · 2 comments

Comments

@dalezak
Copy link

dalezak commented Dec 13, 2023

Describe the feature

Is it possible to define multiple drivers with fallback logic?

For example use IndexDB if supported, else fallback to use LocalStorage.

This is something that LocalForage supports, which is great to have this "use best option" functionality.

localForage.config({
  driver: [
    this.unstorage.INDEXEDDB,
    this.unstorage.WEBSQL,
    this.unstorage.LOCALSTORAGE
  ]
});

Is this currently possible with unstorage? If not could this be implemented with a custom driver?

Additional information

  • Would you be willing to help implement this feature?
@dalezak
Copy link
Author

dalezak commented Dec 14, 2023

One idea is a simple try-catch, try using indexedDbDriver, if it fails then fallback to localStorageDriver.

try {
  this.unstorage = createStorage({
    driver: indexedDbDriver({
      storeName: "app"
    })
  });
}
catch (error) {
  this.unstorage = createStorage({
    driver: localStorageDriver()
  });
}

Will this work? Is there a better option?

@eligao
Copy link

eligao commented Jan 12, 2024

I suggest implementing a few common policies like read-through/write-through etc.
see: https://www.prisma.io/dataguide/managing-databases/introduction-database-caching
I've recently implemented a TieredStorage driver in my own project, it's under tests and I'll create a PR when it's ready.

@pi0 pi0 added the discussion label May 1, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants