-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnotes-class.js
More file actions
68 lines (53 loc) · 1.85 KB
/
notes-class.js
File metadata and controls
68 lines (53 loc) · 1.85 KB
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
class Notes{
dbName = 'Notes';
dbVersion = 1;
reverse = false; //Reverse order of display notes
connect(){
// connect to indexedDB, create object store(table) as notes
return new Promise((resolve, reject)=> {
const request = indexedDB.open(this.dbName, this.dbVersion);
request.onupgradeneeded = ()=>{
let db = request.result;
if(!db.objectStoreNames.contains('notes')){
db.createObjectStore('notes', {keyPath: 'id', autoIncrement:true});
}
}
request.onsuccess = ()=> {
resolve(request.result);
}
request.onerror = () =>{
reject(request.error.message);
}
})};
async accessStore(mode){
// access to notes Object store (table) with select specific mode('readwrite' or 'readonly').
let connect = await this.connect();
let tx = connect.transaction('notes', mode);
return tx.objectStore('notes');
}
async add(note){
// add new note
let accessDB = await this.accessStore('readwrite');
accessDB.add(note);
}
async update(note){
// update exist note
let accessDB = await this.accessStore('readwrite');
accessDB.put(note);
}
async all(){
// show all notes on webpage
let accessDB = await this.accessStore('readonly');
return accessDB.openCursor(null, this.reverse ? 'prev' : 'next' ); // will return all rows in table as object
}
async delete(noteId){
//delete exist note
let accessDB = await this.accessStore('readwrite');
accessDB.delete(noteId);
}
async clear(){
// clear all notes on webpage
let accessDB = await this.accessStore('readwrite');
accessDB.clear()
}
}