-
Notifications
You must be signed in to change notification settings - Fork 0
/
db.js
79 lines (65 loc) · 2.23 KB
/
db.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
const dbOpenedEvent = new Event("databaseLoaded");
let dbLoaded = false;
let db;
let dbRequest = indexedDB.open('myDatabase', 1);
let myBooks;
dbRequest.onupgradeneeded = function(e){
// Set the db variable to our database so we can use it!
db = e.target.result;
// Create an object store named notes. Object stores in databases are where data are stored.
let notes = db.createObjectStore('books', {keypath: 'isbn13'});
}
dbRequest.onsuccess = function(e){
db = e.target.result;
loadDatabase();
}
dbRequest.onerror = function(e){
alert('error opening database' + e.target.errorCode);
}
function addSavedBook(db, bookInfo, bookID){
// Start a database transaction and get the book object store
const tx = db.transaction(['books'], 'readwrite');
const store = tx.objectStore('books');
// Add the book to the object store
const book = {info: bookInfo, created: Date.now()};
store.add(book, bookID);
// Wait for the database transaction to complete
tx.oncomplete = function(){
console.log('stored book!');
}
tx.onerror = function(e){
alert('error storing book ' + e.target.errorCode);
}
}
function loadDatabase(){
console.log('trying to load database');
// Start a database transaction and get the book object store
const tx = db.transaction(['books'], 'readonly');
const store = tx.objectStore('books');
const books = store.getAll();
books.onerror = (event) => {
// Handle errors!
};
books.onsuccess = (event) => {
myBooks = books.result;
document.dispatchEvent(dbOpenedEvent);
dbLoaded = true;
};
}
function updateSavedBook(bookToUpdate, bookID, onSave){
console.log("saving database");
// Start a database transaction and get the book object store
const tx = db.transaction(['books'], 'readwrite');
const store = tx.objectStore('books');
const request = store.put(bookToUpdate, bookID);
request.onsuccess = function(e){
// Update page with new database
console.log("Updated Database");
document.dispatchEvent(dbOpenedEvent);
onSave();
console.log("hmm");
};
request.onerror = function(e){
console.log('Error adding: '+e);
};
}