Edoruin's picture
Force DB upgrade to v2, add robust error handling and asset cache-busting
75007fd
/**
* IndexedDB Database Service using Dexie.js
* Migrated from Angular db.service.ts
*/
console.log('db.js loaded');
const db = new Dexie('MiPescaDB');
// Define database schema - Increment version to 2 to handle boolean to numeric key transition
db.version(2).stores({
captures: 'id, timestamp, synced',
species: 'id, commonName, category'
});
console.log('Dexie DB schema defined (v2)');
// Database operations
const dbService = {
/**
* Add a new capture to the database
*/
async addCapture(capture) {
return await db.captures.add(capture);
},
/**
* Get all captures
*/
async getAllCaptures() {
return await db.captures.toArray();
},
/**
* Get unsynced captures
*/
async getUnsyncedCaptures() {
return await db.captures.where('synced').equals(0).toArray();
},
/**
* Update sync status of a capture
*/
async updateSyncStatus(id, synced) {
return await db.captures.update(id, {
synced: synced ? 1 : 0,
syncedAt: new Date()
});
},
/**
* Save species list to database
*/
async saveSpecies(speciesList) {
return await db.species.bulkPut(speciesList);
},
/**
* Get all species
*/
async getAllSpecies() {
return await db.species.toArray();
},
/**
* Clear all data (for testing)
*/
async clearAll() {
await db.captures.clear();
await db.species.clear();
}
};