From be9998568cf375b95b3244c192495b4e471f5d51 Mon Sep 17 00:00:00 2001 From: Anoop M D Date: Sat, 29 Jan 2022 23:53:22 +0530 Subject: [PATCH] feat: useIdb hook --- packages/grafnode-run/package.json | 1 + .../src/providers/Store/actions.js | 4 ++- .../src/providers/Store/useIdb.js | 27 +++++++++++++++++++ 3 files changed, 31 insertions(+), 1 deletion(-) create mode 100644 packages/grafnode-run/src/providers/Store/useIdb.js diff --git a/packages/grafnode-run/package.json b/packages/grafnode-run/package.json index 957059662..9ef704922 100644 --- a/packages/grafnode-run/package.json +++ b/packages/grafnode-run/package.json @@ -36,6 +36,7 @@ "graphiql": "^1.5.9", "graphql": "^16.2.0", "graphql-request": "^3.7.0", + "idb": "^7.0.0", "immer": "^9.0.7", "lodash": "^4.17.21", "markdown-it": "^12.2.0", diff --git a/packages/grafnode-run/src/providers/Store/actions.js b/packages/grafnode-run/src/providers/Store/actions.js index 16b51c6e5..ed70de908 100644 --- a/packages/grafnode-run/src/providers/Store/actions.js +++ b/packages/grafnode-run/src/providers/Store/actions.js @@ -11,6 +11,7 @@ const SENDING_REQUEST = "SENDING_REQUEST"; const ADD_REQUEST = "ADD_REQUEST"; const ADD_NEW_HTTP_REQUEST = "ADD_NEW_HTTP_REQUEST"; const ADD_NEW_GQL_REQUEST = "ADD_NEW_GQL_REQUEST"; +const IDB_CONNECTION_READY = "IDB_CONNECTION_READY"; export default { SIDEBAR_COLLECTION_CLICK, @@ -25,5 +26,6 @@ export default { SENDING_REQUEST, ADD_REQUEST, ADD_NEW_HTTP_REQUEST, - ADD_NEW_GQL_REQUEST + ADD_NEW_GQL_REQUEST, + IDB_CONNECTION_READY }; diff --git a/packages/grafnode-run/src/providers/Store/useIdb.js b/packages/grafnode-run/src/providers/Store/useIdb.js new file mode 100644 index 000000000..c7dec75a5 --- /dev/null +++ b/packages/grafnode-run/src/providers/Store/useIdb.js @@ -0,0 +1,27 @@ +import { useEffect } from 'react'; +import { openDB } from 'idb'; +import actions from './actions'; + +const useIdb = () => { + 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' }); + collectionStore.createIndex('transactionIdIndex', 'transaction_id'); + } + } + }); + + connection.then(() => { + dispatch({ + type: actions.IDB_CONNECTION_READY, + connection: connection + }); + }); + }, []); +}; + +export default useIdb; \ No newline at end of file