-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdb.ts
More file actions
230 lines (201 loc) · 8.27 KB
/
db.ts
File metadata and controls
230 lines (201 loc) · 8.27 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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
const DB_NAME = 'MedlyLocalVault';
const STORE_LOGS = 'SymptomLogs';
const STORE_PROFILE = 'UserProfile';
const STORE_MEDS = 'Medications';
const STORE_REPORTS = 'Reports';
const DB_VERSION = 3;
export const openDB = (): Promise<IDBDatabase> => {
return new Promise((resolve, reject) => {
const request = indexedDB.open(DB_NAME, DB_VERSION);
request.onupgradeneeded = (event: any) => {
const db = request.result;
if (!db.objectStoreNames.contains(STORE_LOGS)) {
db.createObjectStore(STORE_LOGS, { keyPath: 'id' });
}
if (!db.objectStoreNames.contains(STORE_PROFILE)) {
db.createObjectStore(STORE_PROFILE, { keyPath: 'id' });
}
if (!db.objectStoreNames.contains(STORE_MEDS)) {
db.createObjectStore(STORE_MEDS, { keyPath: 'id' });
}
if (!db.objectStoreNames.contains(STORE_REPORTS)) {
db.createObjectStore(STORE_REPORTS, { keyPath: 'id' });
}
};
request.onsuccess = () => resolve(request.result);
request.onerror = () => reject(request.error);
});
};
export const saveLogToDB = async (log: any) => {
const db = await openDB();
return new Promise((resolve, reject) => {
const transaction = db.transaction(STORE_LOGS, 'readwrite');
const store = transaction.objectStore(STORE_LOGS);
const request = store.put(log);
request.onsuccess = () => resolve(request.result);
request.onerror = () => reject(request.error);
});
};
export const getAllLogsFromDB = async (): Promise<any[]> => {
const db = await openDB();
return new Promise((resolve, reject) => {
const transaction = db.transaction(STORE_LOGS, 'readonly');
const store = transaction.objectStore(STORE_LOGS);
const request = store.getAll();
request.onsuccess = () => resolve(request.result);
request.onerror = () => reject(request.error);
});
};
export const deleteLogFromDB = async (id: string) => {
const db = await openDB();
return new Promise((resolve, reject) => {
const transaction = db.transaction(STORE_LOGS, 'readwrite');
const store = transaction.objectStore(STORE_LOGS);
const request = store.delete(id);
request.onsuccess = () => resolve(request.result);
request.onerror = () => reject(request.error);
});
};
export const getProfileFromDB = async (): Promise<any> => {
const db = await openDB();
return new Promise((resolve, reject) => {
const transaction = db.transaction(STORE_PROFILE, 'readonly');
const store = transaction.objectStore(STORE_PROFILE);
const request = store.get('main');
request.onsuccess = () => resolve(request.result);
request.onerror = () => reject(request.error);
});
};
export const saveProfileToDB = async (profile: any) => {
const db = await openDB();
return new Promise((resolve, reject) => {
const transaction = db.transaction(STORE_PROFILE, 'readwrite');
const store = transaction.objectStore(STORE_PROFILE);
const request = store.put({ ...profile, id: 'main' });
request.onsuccess = () => resolve(request.result);
request.onerror = () => reject(request.error);
});
};
export const getAllMedsFromDB = async (): Promise<any[]> => {
const db = await openDB();
return new Promise((resolve, reject) => {
const transaction = db.transaction(STORE_MEDS, 'readonly');
const store = transaction.objectStore(STORE_MEDS);
const request = store.getAll();
request.onsuccess = () => resolve(request.result);
request.onerror = () => reject(request.error);
});
};
export const saveMedToDB = async (med: any) => {
const db = await openDB();
return new Promise((resolve, reject) => {
const transaction = db.transaction(STORE_MEDS, 'readwrite');
const store = transaction.objectStore(STORE_MEDS);
const request = store.put(med);
request.onsuccess = () => resolve(request.result);
request.onerror = () => reject(request.error);
});
};
export const deleteMedFromDB = async (id: string) => {
const db = await openDB();
return new Promise((resolve, reject) => {
const transaction = db.transaction(STORE_MEDS, 'readwrite');
const store = transaction.objectStore(STORE_MEDS);
const request = store.delete(id);
request.onsuccess = () => resolve(request.result);
request.onerror = () => reject(request.error);
});
};
export const getAllReportsFromDB = async (): Promise<any[]> => {
const db = await openDB();
return new Promise((resolve, reject) => {
const transaction = db.transaction(STORE_REPORTS, 'readonly');
const store = transaction.objectStore(STORE_REPORTS);
const request = store.getAll();
request.onsuccess = () => resolve(request.result);
request.onerror = () => reject(request.error);
});
};
export const saveReportToDB = async (report: any) => {
const db = await openDB();
return new Promise((resolve, reject) => {
const transaction = db.transaction(STORE_REPORTS, 'readwrite');
const store = transaction.objectStore(STORE_REPORTS);
const request = store.put(report);
request.onsuccess = () => resolve(request.result);
request.onerror = () => reject(request.error);
});
};
export const deleteReportFromDB = async (id: string) => {
const db = await openDB();
return new Promise((resolve, reject) => {
const transaction = db.transaction(STORE_REPORTS, 'readwrite');
const store = transaction.objectStore(STORE_REPORTS);
const request = store.delete(id);
request.onsuccess = () => resolve(request.result);
request.onerror = () => reject(request.error);
});
};
export const exportAllData = async (): Promise<string> => {
const [logs, profile, medications, reports] = await Promise.all([
getAllLogsFromDB(),
getProfileFromDB(),
getAllMedsFromDB(),
getAllReportsFromDB()
]);
const exportData = {
version: 1,
exportedAt: new Date().toISOString(),
logs,
profile: profile || null,
medications,
reports
};
return JSON.stringify(exportData, null, 2);
};
export const importAllData = async (jsonString: string): Promise<void> => {
const data = JSON.parse(jsonString);
if (data.version !== 1) {
throw new Error('Unsupported export version');
}
const db = await openDB();
return new Promise((resolve, reject) => {
const transaction = db.transaction([STORE_LOGS, STORE_PROFILE, STORE_MEDS, STORE_REPORTS], 'readwrite');
transaction.oncomplete = () => resolve();
transaction.onerror = () => reject(transaction.error);
if (data.logs && Array.isArray(data.logs)) {
const logsStore = transaction.objectStore(STORE_LOGS);
data.logs.forEach((log: any) => {
logsStore.put(log);
});
}
if (data.profile) {
const profileStore = transaction.objectStore(STORE_PROFILE);
profileStore.put({ ...data.profile, id: 'main' });
}
if (data.medications && Array.isArray(data.medications)) {
const medsStore = transaction.objectStore(STORE_MEDS);
data.medications.forEach((med: any) => {
medsStore.put(med);
});
}
if (data.reports && Array.isArray(data.reports)) {
const reportsStore = transaction.objectStore(STORE_REPORTS);
data.reports.forEach((report: any) => {
reportsStore.put(report);
});
}
});
};
export const deleteAllData = async (): Promise<void> => {
const db = await openDB();
return new Promise((resolve, reject) => {
const transaction = db.transaction([STORE_LOGS, STORE_PROFILE, STORE_MEDS, STORE_REPORTS], 'readwrite');
transaction.oncomplete = () => resolve();
transaction.onerror = () => reject(transaction.error);
transaction.objectStore(STORE_LOGS).clear();
transaction.objectStore(STORE_PROFILE).clear();
transaction.objectStore(STORE_MEDS).clear();
transaction.objectStore(STORE_REPORTS).clear();
});
};