forked from extern/bruno
feat: idb get and save collection methods
This commit is contained in:
parent
be9998568c
commit
42fa05bc0f
33
packages/grafnode-run/src/providers/Store/idb.js
Normal file
33
packages/grafnode-run/src/providers/Store/idb.js
Normal 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));
|
||||
});
|
||||
};
|
@ -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');
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user