2022-01-29 19:23:22 +01:00
|
|
|
import { useEffect } from 'react';
|
|
|
|
import { openDB } from 'idb';
|
|
|
|
import actions from './actions';
|
|
|
|
|
2022-01-30 19:27:24 +01:00
|
|
|
const useIdb = (dispatch) => {
|
2022-01-29 19:23:22 +01:00
|
|
|
useEffect(() => {
|
|
|
|
let dbName = `grafnode`;
|
|
|
|
let connection = openDB(dbName, 1, {
|
|
|
|
upgrade(db, oldVersion, newVersion, transaction) {
|
|
|
|
switch(oldVersion) {
|
|
|
|
case 0:
|
2022-01-30 19:27:24 +01:00
|
|
|
const collectionStore = db.createObjectStore('collection', { keyPath: 'id' });
|
2022-01-29 19:23:22 +01:00
|
|
|
collectionStore.createIndex('transactionIdIndex', 'transaction_id');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
connection.then(() => {
|
|
|
|
dispatch({
|
|
|
|
type: actions.IDB_CONNECTION_READY,
|
|
|
|
connection: connection
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}, []);
|
|
|
|
};
|
|
|
|
|
|
|
|
export default useIdb;
|