-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdb.js
More file actions
107 lines (103 loc) · 3.6 KB
/
db.js
File metadata and controls
107 lines (103 loc) · 3.6 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
// db.js
const DB_NAME = 'uvcs';
const DB_VERSION = 3;
export function openDb() {
return new Promise((resolve, reject) => {
const req = indexedDB.open(DB_NAME, DB_VERSION);
req.onupgradeneeded = () => {
const db = req.result;
if (!db.objectStoreNames.contains('visits')) {
const v = db.createObjectStore('visits', { keyPath: 'id' });
v.createIndex('by_ts', 'tsStart');
v.createIndex('by_tab', 'tabId');
}
if (!db.objectStoreNames.contains('edges')) {
const e = db.createObjectStore('edges', { keyPath: 'id' });
e.createIndex('by_from', 'fromVisitId');
e.createIndex('by_to', 'toVisitId');
}
// textual snapshots (HTML stored as text)
if (!db.objectStoreNames.contains('snapshots_html')) {
const s = db.createObjectStore('snapshots_html', { keyPath: 'id' });
s.createIndex('by_visit', 'visitId');
}
// visual snapshots (JPEG stored as dataURL string)
if (!db.objectStoreNames.contains('snapshots_img')) {
const s2 = db.createObjectStore('snapshots_img', { keyPath: 'id' });
s2.createIndex('by_visit', 'visitId');
}
if (!db.objectStoreNames.contains('settings')) {
db.createObjectStore('settings', { keyPath: 'key' });
}
};
req.onsuccess = () => resolve(req.result);
req.onerror = () => reject(req.error);
});
}
// Generic helpers
export async function txGet(store, key) {
const db = await openDb();
return new Promise((resolve, reject) => {
const t = db.transaction(store, 'readonly');
const r = t.objectStore(store).get(key);
r.onsuccess = () => resolve(r.result || null);
r.onerror = () => reject(r.error);
});
}
export async function txGetAll(store) {
const db = await openDb();
return new Promise((resolve, reject) => {
const t = db.transaction(store, 'readonly');
const r = t.objectStore(store).getAll();
r.onsuccess = () => resolve(r.result || []);
r.onerror = () => reject(r.error);
});
}
export async function txPut(store, value) {
const db = await openDb();
return new Promise((resolve, reject) => {
const t = db.transaction(store, 'readwrite');
t.objectStore(store).put(value);
t.oncomplete = () => resolve();
t.onerror = () => reject(t.error);
});
}
export async function txDel(store, key) {
const db = await openDb();
return new Promise((resolve, reject) => {
const t = db.transaction(store, 'readwrite');
t.objectStore(store).delete(key);
t.oncomplete = () => resolve();
t.onerror = () => reject(t.error);
});
}
export async function txClear(store) {
const db = await openDb();
return new Promise((resolve, reject) => {
const t = db.transaction(store, 'readwrite');
t.objectStore(store).clear();
t.oncomplete = () => resolve();
t.onerror = () => reject(t.error);
});
}
// Query helpers for snapshots (by visitId)
export async function getHtmlSnapByVisit(visitId) {
const db = await openDb();
return new Promise((resolve, reject) => {
const t = db.transaction('snapshots_html', 'readonly');
const idx = t.objectStore('snapshots_html').index('by_visit');
const r = idx.get(visitId);
r.onsuccess = () => resolve(r.result || null);
r.onerror = () => reject(r.error);
});
}
export async function getImgSnapByVisit(visitId) {
const db = await openDb();
return new Promise((resolve, reject) => {
const t = db.transaction('snapshots_img', 'readonly');
const idx = t.objectStore('snapshots_img').index('by_visit');
const r = idx.get(visitId);
r.onsuccess = () => resolve(r.result || null);
r.onerror = () => reject(r.error);
});
}