Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft: Credentials component - replaces localstorage with indexed db #224

Draft
wants to merge 3 commits into
base: master
Choose a base branch
from
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
62 changes: 61 additions & 1 deletion src/containers/Credentials.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,12 @@ export function Credentials(props) {
}
setCredentials(usernamesToRender);
}

useEffect(() => {
saveCredentialsToIndexedDB( [{credential_id: 'mongodb:ext:16',credential_title: "Charles Leclerc"},{credential_id: 'mongodb:ext:03',credential_title: "Daniel Ricciardo"}], 'Credentials');
const dataInIDB = getCredentialsFromIDB('Credentials');
console.log('ln63: ', dataInIDB);

matchCredentialIds(credentials_ids);
}, [])

Expand Down Expand Up @@ -96,6 +100,40 @@ function getUsernamesFromLS(name, cleanupTime) {
return ls ? ls : { credentials: [], expiration: new Date().getTime() + cleanupTime };
}

const getCredentialsFromIDB = (OSname) => {
console.log('inside getCredentialsFromIDB')

let credentialsInIDB = [];
let db;

const request = window.indexedDB.open('ASAB|Credentials', 1);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmms, you need to create the CredentialsService and open the database during init-time, then keep it open.
Opening/closing is what makes your implementation slow.

// if indexedDB does not exist yet, onupgradeneeded is triggered
request.onupgradeneeded = (e) => {
console.info('Database created');
db = e.target.result;
db.createObjectStore(OSname, {keyPath: "credential_id"});
};
request.onerror = (e) => {
console.error(e);
};
request.onsuccess = (e) => {
db = e.target.result;
db.onerror = e => console.error("Database error: " + e.target.error);
const tx = db.transaction([OSname], 'readonly').objectStore(OSname);
const requestCursor = tx.openCursor();
requestCursor.onsuccess = e => {
const cursor = e.target.result
if (cursor) {
console.log('ln 145 cursor.key: ', cursor.key, 'cursor.value.credential_title: ', cursor.value.credential_title)
credentialsInIDB.push({ [cursor.key] : cursor.value.credential_title })
cursor.continue()
}
};
console.log('ln149 creentialsInIDB: ', credentialsInIDB)
return credentialsInIDB
};
}

function saveUsernamesToLS(data, credentials_ids, cleanupTime) {
if (localStorage) {
let dataInLS = getUsernamesFromLS('Credentials', cleanupTime);
Expand Down Expand Up @@ -123,4 +161,26 @@ function saveUsernamesToLS(data, credentials_ids, cleanupTime) {
localStorage.setItem('Credentials', JSON.stringify(dataInLS));
return dataToLS;
}
}

const saveCredentialsToIndexedDB = (data, OSname) => {
// data is array of objects in this format [{ credential_id: 'mongodb:ext:007', credential_title: "James" }, ... ]
const request = window.indexedDB.open('ASAB|Credentials', 1);
let db
request.onupgradeneeded = (e) => {
console.info('Database created');
db = e.target.result;
db.createObjectStore(OSname, {keyPath: "credential_id"});
};
request.onsuccess = (e) => {
console.log('saveCredentialsToIndexedDB ln 189 reguest.onsuccess')
db = e.target.result
db.onerror = e => console.error("Database error: " + e.target.error);
const tx = db.transaction([OSname], 'readwrite')
const credsIDB = tx.objectStore(OSname);
data.map((item, i) => {
credsIDB.add(item);
console.log('this is iteration no.: ', i)
})
}
}