diff --git a/packages/grafnode-run/src/providers/Store/idb.js b/packages/grafnode-run/src/providers/Store/idb.js new file mode 100644 index 00000000..cfdd9f68 --- /dev/null +++ b/packages/grafnode-run/src/providers/Store/idb.js @@ -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)); + }); +}; \ No newline at end of file diff --git a/packages/grafnode-run/src/providers/Store/useIdb.js b/packages/grafnode-run/src/providers/Store/useIdb.js index c7dec75a..690134ad 100644 --- a/packages/grafnode-run/src/providers/Store/useIdb.js +++ b/packages/grafnode-run/src/providers/Store/useIdb.js @@ -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'); } }