From 42fa05bc0fe1816068412bb94efd4b04ed16841b Mon Sep 17 00:00:00 2001 From: Anoop M D Date: Sun, 30 Jan 2022 23:57:24 +0530 Subject: [PATCH] feat: idb get and save collection methods --- .../grafnode-run/src/providers/Store/idb.js | 33 +++++++++++++++++++ .../src/providers/Store/useIdb.js | 4 +-- 2 files changed, 35 insertions(+), 2 deletions(-) create mode 100644 packages/grafnode-run/src/providers/Store/idb.js 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'); } }