0

I want to fetch my data from multiple DB in an indexed DB

here is my database in indexedDB

const DBUser = {
  name: "userDB",
  version: 2,
  objectStoresMeta: [
    {
      store: "cData",
      storeConfig: { keyPath: "id", autoIncrement: true },
      storeSchema: [
        { name: "userId", keypath: "userId", options: { unique: false } },
        { name: "eData", keypath: "ecgData", options: { unique: false } },
        { name: "date", keypath: "date", options: { unique: false } },
      ],
    },
    {
      store: "oData",
      storeConfig: { keyPath: "id", autoIncrement: true },
      storeSchema: [
        { name: "userId", keypath: "userId", options: { unique: false } },
        { name: "oData", keypath: "ppgData", options: { unique: false } },
      ],
    }
]}

I can use getAll function to fetch my data from one of the above databases. for example cData or oData with this code:

const { getAll } = useIndexedDB(cData);
  useEffect(() => {
    getAll().then(dataFromDB => {
      setData(dataFromDB );
    });

  }, []);

When I put multiple getAll, obviously the error popped up and said Identifier 'getAll' has already been declared.

But I want to fetch my data from both cData and oData in one component. I can't have multiple getAll and without getAll, I can't fetch the data. So what is the alternative way? I don't find anything on the internet.

hermi
  • 51
  • 5
  • 1
    Does this answer your question? [ES6/ES2015 object destructuring and changing target variable](https://stackoverflow.com/questions/34904523/es6-es2015-object-destructuring-and-changing-target-variable) – Sergey Sosunov Jan 20 '23 at 15:47
  • @SergeySosunov No. My question is about indexedDB and how to use multiple getAll in components. But thank you for paying attention. – hermi Jan 20 '23 at 16:03
  • 1
    Basically i was trying to tell that you can have multiple getAll in same component, you just need to rename them a bit during destructuring. So `const {getAll: getAllCData} = useIndexedDB(cData);` and on the next line `const {getAll: getAllOData} = useIndexedDB(oData);`. But anyway, sorry for taking your time. – Sergey Sosunov Jan 20 '23 at 16:08
  • No worries. In worst case - you can leave out the destructuring itself, like `const cDataDb = useIndexedDB(cData);` and use `cDataDb.getAll()` instead of destructuring. (But I'd recommend renaming, as I did in previous comment) – Sergey Sosunov Jan 20 '23 at 16:20
  • Oh. You're right. I was worried that I can't use multiple getAll. – hermi Jan 20 '23 at 16:24

1 Answers1

0

I can just simply do this:

const { getAll : getAllC } = useIndexedDB(cData);
  useEffect(() => {
    getAll().then(dataFromDB => {
      setData(dataFromDB );
    });

  }, []);


const { getAll : getAllo } = useIndexedDB(oData);
  useEffect(() => {
    getAll().then(dataFromDB => {
      setData(dataFromDB );
    });

  }, []);
hermi
  • 51
  • 5