-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathappIndexedDBOperator.js
More file actions
153 lines (130 loc) · 4.82 KB
/
appIndexedDBOperator.js
File metadata and controls
153 lines (130 loc) · 4.82 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
150
151
152
153
class IndexedDBOperator {
constructor(durability = "relaxed") {
this.dbPromise = undefined;
this.storeConfig = {};
this.durability = durability;
}
async getDb() {
if (!this.dbPromise) {
this.dbPromise = new Promise((resolve, reject) => {
const request = indexedDB.open('HelpViewer', 1);
request.onerror = () => reject(request.error);
request.onsuccess = () => {
Object.entries(this.storeConfig).forEach(([storeName, config]) => {
const suffixName = storeName.charAt(0).toUpperCase() + storeName.slice(1);
this[`add${suffixName}`] = (data) => this.add(storeName, data);
this[`get${suffixName}`] = (id) => this.get(storeName, id);
this[`update${suffixName}`] = (id, updates) => this.update(storeName, id, updates);
this[`delete${suffixName}`] = (id) => this.delete(storeName, id);
});
return resolve(request.result);
};
request.onupgradeneeded = (event) => {
const db = event.target.result;
Object.entries(this.storeConfig).forEach(([storeName, config]) => {
if (!db.objectStoreNames.contains(storeName)) {
const store = db.createObjectStore(storeName, { keyPath: 'id', autoIncrement: true });
config.indexes?.forEach(({ name, key, unique }) => {
store.createIndex(name, key, { unique });
});
}
});
};
});
}
return this.dbPromise;
}
async _execute(operation) {
try {
return await operation();
} catch (error) {
throw new Error(`DB operation failed: ${error.message}`);
}
}
_request(store, method, ...args) {
log('E IndexedDB operation request: ', [store, method?.name, [...args]]);
return new Promise((resolve, reject) => {
const request = store[method](...args);
request.onsuccess = () => resolve(request.result);
request.onerror = () => reject(request.error);
});
}
async _transaction(storeNames, mode, callback) {
const db = await this.getDb();
const tx = db.transaction(storeNames, mode, { durability: this.durability });
const stores = Array.isArray(storeNames)
? Object.fromEntries(storeNames.map(name => [name, tx.objectStore(name)]))
: { [storeNames]: tx.objectStore(storeNames) };
const result = await callback(stores);
return new Promise((resolve, reject) => {
tx.oncomplete = () => resolve(result);
tx.onerror = () => reject(tx.error);
tx.onabort = () => reject(tx.error || new Error('Transaction aborted'));
});
}
async add(storeName, record) {
return this._transaction(storeName, 'readwrite', store =>
this._request(store[storeName], 'add', record)
);
}
async get(storeName, id) {
return this._transaction(storeName, 'readonly', store =>
this._request(store[storeName], 'get', id)
);
}
async getAll(storeName) {
return this._transaction(storeName, 'readonly', store =>
this._request(store[storeName], 'getAll')
);
}
async update(storeName, id, updates) {
return this._transaction(storeName, 'readwrite', async store => {
const existing = await this._request(store[storeName], 'get', id);
if (!existing) throw new Error(`Record ${id} not found`);
const updated = { ...existing, ...updates };
await this._request(store[storeName], 'put', updated);
return updated;
});
}
async delete(storeName, id) {
return this._transaction(storeName, 'readwrite', store =>
this._request(store[storeName], 'delete', id)
);
}
async count(storeName) {
const db = await this.getDb();
const tx = db.transaction(storeName, 'readonly');
const store = tx.objectStore(storeName);
return this._request(store.count());
}
async getByIndex(storeName, indexName, value) {
return this._transaction(storeName, 'readonly', store =>
this._request(store[storeName].index(indexName), 'get', value)
);
}
async getAllByIndex(storeName, indexName, value) {
return this._transaction(storeName, 'readonly', store =>
this._request(store[storeName].index(indexName), 'getAll', value)
);
}
async getDbStats() {
const stats = {};
for (const storeName of Object.keys(this.storeConfig)) {
stats[storeName] = await this.count(storeName);
}
return stats;
}
async clearStore(storeName) {
return this._execute(() =>
this._transaction(storeName, 'readwrite', (store, resolve, reject) => {
const request = store.clear();
request.onsuccess = () => resolve(true);
request.onerror = () => reject(request.error);
})
);
}
async clearAllData() {
const promises = Object.keys(this.storeConfig).map(store => this.clearStore(store));
return Promise.all(promises);
}
}