bruno/renderer/providers/App/useIdb.js
2022-03-18 01:56:25 +05:30

30 lines
826 B
JavaScript

import { useEffect } from 'react';
import { openDB } from 'idb';
import { idbConnectionReady } from 'providers/ReduxStore/slices/app'
import { useDispatch } from 'react-redux';
const useIdb = () => {
const dispatch = useDispatch();
useEffect(() => {
let dbName = `grafnode`;
let connection = openDB(dbName, 1, {
upgrade(db, oldVersion, newVersion, transaction) {
switch(oldVersion) {
case 0:
const collectionStore = db.createObjectStore('collection', { keyPath: 'uid' });
collectionStore.createIndex('transactionIdIndex', 'transaction_id');
}
}
});
connection
.then(() => {
window.__idb = connection;
dispatch(idbConnectionReady());
})
.catch((err) => console.log(err));
}, []);
};
export default useIdb;