2022-03-13 16:57:59 +01:00
|
|
|
import isArray from 'lodash/isArray';
|
|
|
|
|
2022-03-10 04:31:05 +01:00
|
|
|
export const saveCollectionToIdb = (connection, collection) => {
|
2022-01-30 19:27:24 +01:00
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
connection
|
|
|
|
.then((db) => {
|
|
|
|
let tx = db.transaction(`collection`, 'readwrite');
|
|
|
|
let collectionStore = tx.objectStore('collection');
|
|
|
|
|
2022-03-13 16:57:59 +01:00
|
|
|
if(isArray(collection)) {
|
|
|
|
for(let c of collection) {
|
|
|
|
collectionStore.put(c);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
collectionStore.put(collection);
|
|
|
|
}
|
2022-01-30 19:27:24 +01:00
|
|
|
|
|
|
|
resolve();
|
|
|
|
})
|
|
|
|
.catch((err) => reject(err));
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2022-03-13 13:13:21 +01:00
|
|
|
export const getCollectionsFromIdb = (connection) => {
|
2022-01-30 19:27:24 +01:00
|
|
|
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));
|
|
|
|
});
|
|
|
|
};
|