mirror of
https://github.com/usebruno/bruno.git
synced 2025-06-24 05:51:22 +02:00
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 { openDB } from 'idb';
|
||||||
import actions from './actions';
|
import actions from './actions';
|
||||||
|
|
||||||
const useIdb = () => {
|
const useIdb = (dispatch) => {
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
let dbName = `grafnode`;
|
let dbName = `grafnode`;
|
||||||
let connection = openDB(dbName, 1, {
|
let connection = openDB(dbName, 1, {
|
||||||
upgrade(db, oldVersion, newVersion, transaction) {
|
upgrade(db, oldVersion, newVersion, transaction) {
|
||||||
switch(oldVersion) {
|
switch(oldVersion) {
|
||||||
case 0:
|
case 0:
|
||||||
const collectionStore = db.createObjectStore('collections', { keyPath: 'id' });
|
const collectionStore = db.createObjectStore('collection', { keyPath: 'id' });
|
||||||
collectionStore.createIndex('transactionIdIndex', 'transaction_id');
|
collectionStore.createIndex('transactionIdIndex', 'transaction_id');
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user