-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdatabase.js
More file actions
325 lines (272 loc) · 11.4 KB
/
database.js
File metadata and controls
325 lines (272 loc) · 11.4 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
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
// IndexedDB Database Management Module
class TLNotesDB {
constructor() {
this.db = null;
this.DB_NAME = 'TLNotesDB';
this.DB_VERSION = 1;
}
async init() {
return new Promise((resolve, reject) => {
const request = indexedDB.open(this.DB_NAME, this.DB_VERSION);
request.onerror = () => {
console.error('Database error:', request.error);
reject(request.error);
};
request.onsuccess = () => {
this.db = request.result;
console.log('Database initialized successfully');
resolve(this.db);
};
request.onupgradeneeded = (event) => {
this.db = event.target.result;
console.log('Upgrading database...');
// Create notes store
if (!this.db.objectStoreNames.contains('notes')) {
const notesStore = this.db.createObjectStore('notes', { keyPath: 'id' });
notesStore.createIndex('created', 'created', { unique: false });
notesStore.createIndex('type', 'type', { unique: false });
notesStore.createIndex('edited', 'edited', { unique: false });
console.log('Notes store created');
}
// Create trash store
if (!this.db.objectStoreNames.contains('trash')) {
const trashStore = this.db.createObjectStore('trash', { keyPath: 'id' });
trashStore.createIndex('trashedAt', 'trashedAt', { unique: false });
console.log('Trash store created');
}
// Create settings store
if (!this.db.objectStoreNames.contains('settings')) {
const settingsStore = this.db.createObjectStore('settings', { keyPath: 'key' });
console.log('Settings store created');
}
};
});
}
// Notes operations
async getAllNotes() {
return new Promise((resolve, reject) => {
const transaction = this.db.transaction(['notes'], 'readonly');
const store = transaction.objectStore('notes');
const request = store.getAll();
request.onsuccess = () => resolve(request.result || []);
request.onerror = () => reject(request.error);
});
}
async saveNote(note) {
return new Promise((resolve, reject) => {
const transaction = this.db.transaction(['notes'], 'readwrite');
const store = transaction.objectStore('notes');
const request = store.put(note);
request.onsuccess = () => resolve(request.result);
request.onerror = () => reject(request.error);
});
}
async deleteNote(id) {
return new Promise((resolve, reject) => {
const transaction = this.db.transaction(['notes'], 'readwrite');
const store = transaction.objectStore('notes');
const request = store.delete(id);
request.onsuccess = () => resolve(request.result);
request.onerror = () => reject(request.error);
});
}
async getNote(id) {
return new Promise((resolve, reject) => {
const transaction = this.db.transaction(['notes'], 'readonly');
const store = transaction.objectStore('notes');
const request = store.get(id);
request.onsuccess = () => resolve(request.result);
request.onerror = () => reject(request.error);
});
}
// Trash operations
async getAllTrash() {
return new Promise((resolve, reject) => {
const transaction = this.db.transaction(['trash'], 'readonly');
const store = transaction.objectStore('trash');
const request = store.getAll();
request.onsuccess = () => resolve(request.result || []);
request.onerror = () => reject(request.error);
});
}
async moveToTrash(note) {
note.trashedAt = Date.now();
return new Promise((resolve, reject) => {
const transaction = this.db.transaction(['trash', 'notes'], 'readwrite');
const trashStore = transaction.objectStore('trash');
const notesStore = transaction.objectStore('notes');
const addToTrash = trashStore.put(note);
const removeFromNotes = notesStore.delete(note.id);
transaction.oncomplete = () => resolve();
transaction.onerror = () => reject(transaction.error);
});
}
async restoreFromTrash(id) {
return new Promise((resolve, reject) => {
const transaction = this.db.transaction(['trash', 'notes'], 'readwrite');
const trashStore = transaction.objectStore('trash');
const notesStore = transaction.objectStore('notes');
const getFromTrash = trashStore.get(id);
getFromTrash.onsuccess = () => {
const note = getFromTrash.result;
if (note) {
delete note.trashedAt;
notesStore.put(note);
trashStore.delete(id);
}
};
transaction.oncomplete = () => resolve();
transaction.onerror = () => reject(transaction.error);
});
}
async deleteFromTrash(id) {
return new Promise((resolve, reject) => {
const transaction = this.db.transaction(['trash'], 'readwrite');
const store = transaction.objectStore('trash');
const request = store.delete(id);
request.onsuccess = () => resolve(request.result);
request.onerror = () => reject(request.error);
});
}
async emptyTrash() {
return new Promise((resolve, reject) => {
const transaction = this.db.transaction(['trash'], 'readwrite');
const store = transaction.objectStore('trash');
const request = store.clear();
request.onsuccess = () => resolve(request.result);
request.onerror = () => reject(request.error);
});
}
async restoreAllFromTrash() {
return new Promise(async (resolve, reject) => {
try {
const trashItems = await this.getAllTrash();
const transaction = this.db.transaction(['trash', 'notes'], 'readwrite');
const trashStore = transaction.objectStore('trash');
const notesStore = transaction.objectStore('notes');
trashItems.forEach(note => {
delete note.trashedAt;
notesStore.put(note);
trashStore.delete(note.id);
});
transaction.oncomplete = () => resolve();
transaction.onerror = () => reject(transaction.error);
} catch (error) {
reject(error);
}
});
}
// Settings operations
async getSetting(key) {
return new Promise((resolve, reject) => {
const transaction = this.db.transaction(['settings'], 'readonly');
const store = transaction.objectStore('settings');
const request = store.get(key);
request.onsuccess = () => resolve(request.result?.value);
request.onerror = () => reject(request.error);
});
}
async setSetting(key, value) {
return new Promise((resolve, reject) => {
const transaction = this.db.transaction(['settings'], 'readwrite');
const store = transaction.objectStore('settings');
const request = store.put({ key, value });
request.onsuccess = () => resolve(request.result);
request.onerror = () => reject(request.error);
});
}
// Data management operations
async exportData() {
try {
const notes = await this.getAllNotes();
const trash = await this.getAllTrash();
return {
notes,
trash,
exportDate: new Date().toISOString(),
version: this.DB_VERSION
};
} catch (error) {
throw new Error('Failed to export data: ' + error.message);
}
}
async importData(data) {
return new Promise((resolve, reject) => {
const transaction = this.db.transaction(['notes', 'trash'], 'readwrite');
const notesStore = transaction.objectStore('notes');
const trashStore = transaction.objectStore('trash');
// Clear existing data
notesStore.clear();
trashStore.clear();
// Import notes
if (data.notes && Array.isArray(data.notes)) {
data.notes.forEach(note => notesStore.put(note));
}
// Import trash
if (data.trash && Array.isArray(data.trash)) {
data.trash.forEach(note => trashStore.put(note));
}
transaction.oncomplete = () => resolve();
transaction.onerror = () => reject(transaction.error);
});
}
async clearAllData() {
return new Promise((resolve, reject) => {
const transaction = this.db.transaction(['notes', 'trash', 'settings'], 'readwrite');
const notesStore = transaction.objectStore('notes');
const trashStore = transaction.objectStore('trash');
const settingsStore = transaction.objectStore('settings');
notesStore.clear();
trashStore.clear();
settingsStore.clear();
transaction.oncomplete = () => resolve();
transaction.onerror = () => reject(transaction.error);
});
}
// Database statistics
async getStats() {
try {
const notes = await this.getAllNotes();
const trash = await this.getAllTrash();
const stats = {
totalNotes: notes.length,
trashCount: trash.length,
notesByType: {},
oldestNote: null,
newestNote: null
};
// Calculate notes by type
notes.forEach(note => {
stats.notesByType[note.type] = (stats.notesByType[note.type] || 0) + 1;
});
// Find oldest and newest notes
if (notes.length > 0) {
const sortedByCreated = notes.sort((a, b) => a.created - b.created);
stats.oldestNote = sortedByCreated[0].created;
stats.newestNote = sortedByCreated[sortedByCreated.length - 1].created;
}
return stats;
} catch (error) {
throw new Error('Failed to get stats: ' + error.message);
}
}
// Calculate approximate database size
async getDatabaseSize() {
try {
const data = await this.exportData();
const dataString = JSON.stringify(data);
const sizeInBytes = new Blob([dataString]).size;
if (sizeInBytes < 1024) {
return sizeInBytes + ' B';
} else if (sizeInBytes < 1024 * 1024) {
return Math.round(sizeInBytes / 1024 * 100) / 100 + ' KB';
} else {
return Math.round(sizeInBytes / (1024 * 1024) * 100) / 100 + ' MB';
}
} catch (error) {
return 'Unknown';
}
}
}
// Create global database instance
const database = new TLNotesDB();