T017
This commit is contained in:
42
packages/storage/src/db.ts
Normal file
42
packages/storage/src/db.ts
Normal file
@@ -0,0 +1,42 @@
|
|||||||
|
/**
|
||||||
|
* IndexedDB schema v1 for GlowTrack
|
||||||
|
* Exports: openDb(name = 'glowtrack', version = 1)
|
||||||
|
*/
|
||||||
|
export async function openDb(name = 'glowtrack', version = 1): Promise<IDBDatabase> {
|
||||||
|
return await new Promise((resolve, reject) => {
|
||||||
|
const req = indexedDB.open(name, version);
|
||||||
|
|
||||||
|
req.onupgradeneeded = (ev) => {
|
||||||
|
const db = req.result;
|
||||||
|
// v1 stores
|
||||||
|
// settings: no keyPath; we will store a singleton record with a manual key
|
||||||
|
if (!db.objectStoreNames.contains('settings')) {
|
||||||
|
db.createObjectStore('settings');
|
||||||
|
}
|
||||||
|
|
||||||
|
// habits: keyPath 'id', index by_type
|
||||||
|
if (!db.objectStoreNames.contains('habits')) {
|
||||||
|
const s = db.createObjectStore('habits', { keyPath: 'id' });
|
||||||
|
s.createIndex('by_type', 'type', { unique: false });
|
||||||
|
}
|
||||||
|
|
||||||
|
// days: keyPath 'date'
|
||||||
|
if (!db.objectStoreNames.contains('days')) {
|
||||||
|
db.createObjectStore('days', { keyPath: 'date' });
|
||||||
|
}
|
||||||
|
|
||||||
|
// entries: keyPath 'id', indexes by_date, by_habit
|
||||||
|
if (!db.objectStoreNames.contains('entries')) {
|
||||||
|
const e = db.createObjectStore('entries', { keyPath: 'id' });
|
||||||
|
e.createIndex('by_date', 'date', { unique: false });
|
||||||
|
e.createIndex('by_habit', 'habitId', { unique: false });
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
req.onsuccess = () => resolve(req.result);
|
||||||
|
req.onerror = () => reject(req.error);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
// Convenience types (re-exported from models in other tasks) to avoid hard dependency
|
||||||
|
export type OpenDbFn = (name?: string, version?: number) => Promise<IDBDatabase>;
|
||||||
@@ -116,7 +116,7 @@ From data-model.md → model creation tasks [P]
|
|||||||
- Export interfaces: WellbeingGrid, GridSettings, ExportSettings, DayTile, Mood, HabitEntry, HabitDefinition
|
- Export interfaces: WellbeingGrid, GridSettings, ExportSettings, DayTile, Mood, HabitEntry, HabitDefinition
|
||||||
- Dependencies: T009-T015 (tests exist), T003
|
- Dependencies: T009-T015 (tests exist), T003
|
||||||
|
|
||||||
- [ ] T017 [P] Implement IndexedDB schema v1
|
- [X] T017 [P] Implement IndexedDB schema v1
|
||||||
- Create /home/jawz/Development/Projects/GlowTrack/packages/storage/src/db.ts with openDb(name='glowtrack', version=1)
|
- Create /home/jawz/Development/Projects/GlowTrack/packages/storage/src/db.ts with openDb(name='glowtrack', version=1)
|
||||||
- Create stores: settings (key 'singleton'), habits (keyPath 'id', index by_type), days (keyPath 'date'), entries (keyPath 'id', indexes by_date, by_habit)
|
- Create stores: settings (key 'singleton'), habits (keyPath 'id', index by_type), days (keyPath 'date'), entries (keyPath 'id', indexes by_date, by_habit)
|
||||||
- Dependencies: T016, T010
|
- Dependencies: T016, T010
|
||||||
|
|||||||
Reference in New Issue
Block a user