feat: idb get and save collection methods

This commit is contained in:
Anoop M D 2022-01-30 23:57:24 +05:30
parent be9998568c
commit 42fa05bc0f
2 changed files with 35 additions and 2 deletions

View File

@ -0,0 +1,33 @@
export const saveCollectionToIdb = (connection, domain, collection) => {
return new Promise((resolve, reject) => {
connection
.then((db) => {
let tx = db.transaction(`collection`, 'readwrite');
let collectionStore = tx.objectStore('collection');
collectionStore.put(collection);
resolve();
})
.catch((err) => reject(err));
});
};
export const getCollectionsFromIdb = (connection, domain) => {
return new Promise((resolve, reject) => {
connection
.then((db) => {
let tx = db.transaction('collection');
let collectionStore = tx.objectStore('collection');
return collectionStore.getAll();
})
.then((collections) => {
if(!Array.isArray(collections)) {
return new Error('IDB Corrupted');
}
return resolve(collections);
})
.catch((err) => reject(err));
});
};

View File

@ -2,14 +2,14 @@ import { useEffect } from 'react';
import { openDB } from 'idb';
import actions from './actions';
const useIdb = () => {
const useIdb = (dispatch) => {
useEffect(() => {
let dbName = `grafnode`;
let connection = openDB(dbName, 1, {
upgrade(db, oldVersion, newVersion, transaction) {
switch(oldVersion) {
case 0:
const collectionStore = db.createObjectStore('collections', { keyPath: 'id' });
const collectionStore = db.createObjectStore('collection', { keyPath: 'id' });
collectionStore.createIndex('transactionIdIndex', 'transaction_id');
}
}