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

Usage with localForage/IndexedDB/Dexie #53

Open
NickVishwamitraJTY opened this issue Jan 25, 2022 · 2 comments
Open

Usage with localForage/IndexedDB/Dexie #53

NickVishwamitraJTY opened this issue Jan 25, 2022 · 2 comments

Comments

@NickVishwamitraJTY
Copy link

Thanks for creating this library. Saved me a ton of time and is super simple. I am however getting errors as I am using up all the provided space in local storage. Read up that using localforage or Dexie can get around this. Are we able to integrate these into recoil-persist?

@polemius
Copy link
Owner

Hi, you could pass the storage to recoilPersist function:

import { recoilPersist } from 'recoil-persist'

const { persistAtom } = recoilPersist({
  storage: localStorage, // configurate which stroage will be used to store the data
})

I can see that localForage has the same API as localStorage so it should just work.
But if storage has some different API then you could create a proxy for the storage:

const customStorage = () => {
  return {
    setItem: (key, value) => {
      // handle setItem
    },
    getItem: (key) => {
      // handle getItem
      // this function should return something
    },
    clear: () => {
      // clear the whole db
    },
  }
}

const { persistAtom } = recoilPersist({ storage: customStorage() })

Another example how you could encode values before putting it in localStorage

@kusare
Copy link

kusare commented May 24, 2022

It moved! Thank you.

import localforage from "localforage";

localforage.config({
  driver: localforage.WEBSQL, // Force WebSQL; same as using setDriver()
  name: "myApp",
  version: 1.0,
  size: 4980736, // Size of database, in bytes. WebSQL-only for now.
  storeName: "keyvaluepairs", // Should be alphanumeric, with underscores.
  description: "some description",
});

const customStorage = () => {
  return {
    setItem: (key: any, value: any) => {
      // handle setItem
      localforage.setItem(key, value);
      // if err is non-null, we got an error
    },
    getItem: (key: any) => {
      // handle getItem
      // this function should return something
      const a = localforage.getItem(key, function (err, value) {
        // if err is non-null, we got an error. otherwise, value is the value
      });
      return a;
    },
    clear: () => {
      // clear the whole db
    },
  };
};

                                                                                                
const { persistAtom } = recoilPersist({
  key: "cssMsgStates", // this key is using to store data in local storage
  // @ts-ignoree
  storage: customStorage(),
});

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants