-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathIndexedDBManager.js
More file actions
149 lines (118 loc) · 5.12 KB
/
Copy pathIndexedDBManager.js
File metadata and controls
149 lines (118 loc) · 5.12 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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
/*
MIT License
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
The Software is provided "as is", without warranty of any kind, express or implied, including but not limited to the warranties of merchantability, fitness for a particular purpose and noninfringement. In no event shall the authors or copyright holders be liable for any claim, damages or other liability, whether in an action of contract, tort or otherwise, arising from, out of or in connection with the Software or the use or other dealings in the Software.
To use the IndexedDBManager class, please include the above license notice in all copies or substantial portions of the software. The IndexedDBManager class provides a convenient way to manage data in IndexedDB databases, but please note that it is provided "as is" without any warranty, and the author is not liable for any damages or other liability arising from the use or distribution of the software.
*/
class IndexedDBManager {
constructor(databaseName, storeName) {
this.databaseName = databaseName;
this.storeName = storeName;
this.db = null;
this.open = false;
}
static async indexedDBManagerBuilder(databaseName, storeName){
const manager = new IndexedDBManager(databaseName, storeName);
await manager.openDatabase();
return manager;
}
async openDatabase() {
return new Promise((resolve, reject) => {
const request = window.indexedDB.open(this.databaseName);
request.onerror = () => {
reject(new Error(`Failed to open database ${this.databaseName}`));
};
request.onsuccess = () => {
this.db = request.result;
this.open = true;
resolve(this.db);
};
request.onupgradeneeded = (event) => {
this.db = event.target.result;
if (!this.db.objectStoreNames.contains(this.storeName)) {
this.db.createObjectStore(this.storeName, { keyPath: "id" , autoIncrement: true });
}
};
});
}
async addRecord(record) {
const transaction = this.db.transaction([this.storeName], "readwrite");
const objectStore = transaction.objectStore(this.storeName);
const request = objectStore.add(record);
return new Promise((resolve, reject) => {
request.onerror = () => {
reject(new Error(`Failed to add record ${record.id}`));
};
request.onsuccess = () => {
record.id = request.result; // Update the record's ID with the auto-generated ID
resolve(record.id);
};
});
}
async getRecord(id) {
const transaction = this.db.transaction([this.storeName], "readonly");
const objectStore = transaction.objectStore(this.storeName);
const request = objectStore.get(id);
return new Promise((resolve, reject) => {
request.onerror = () => {
reject(new Error(`Failed to get record ${id}`));
};
request.onsuccess = () => {
resolve(request.result);
};
});
}
async getAllRecords() {
const transaction = this.db.transaction([this.storeName], "readonly");
const objectStore = transaction.objectStore(this.storeName);
const request = objectStore.getAll();
return new Promise((resolve, reject) => {
request.onerror = () => {
reject(new Error(`Failed to get all records`));
};
request.onsuccess = () => {
resolve(request.result);
};
});
}
async updateRecord(record) {
const transaction = this.db.transaction([this.storeName], "readwrite");
const objectStore = transaction.objectStore(this.storeName);
const request = objectStore.put(record);
return new Promise((resolve, reject) => {
request.onerror = () => {
reject(new Error(`Failed to update record ${record.id}`));
};
request.onsuccess = () => {
resolve(record.id);
};
});
}
async deleteRecord(id) {
const transaction = this.db.transaction([this.storeName], "readwrite");
const objectStore = transaction.objectStore(this.storeName);
const request = objectStore.delete(id);
return new Promise((resolve, reject) => {
request.onerror = () => {
reject(new Error(`Failed to delete record ${id}`));
};
request.onsuccess = () => {
resolve(id);
};
});
}
async clearStore() {
const transaction = this.db.transaction([this.storeName], "readwrite");
const objectStore = transaction.objectStore(this.storeName);
const request = objectStore.clear();
return new Promise((resolve, reject) => {
request.onerror = () => {
reject(new Error(`Failed to clear store ${this.storeName}`));
};
request.onsuccess = () => {
resolve();
};
})
}
}