Dilip8756's picture
Upload 100 files
58c1398 verified
/**
* db.js
* API-based wrapper for Aadhaar History (Server-side SQLite)
*/
const dbService = {
async init() {
// No client-side initialization needed for API, but keeping the signature
return Promise.resolve();
},
async getAll() {
try {
const response = await fetch('/api/history');
if (!response.ok) throw new Error('API Error: ' + response.status);
const history = await response.json();
return history;
} catch (e) {
console.error('Failed to fetch history:', e);
return [];
}
},
async save(item) {
try {
const response = await fetch('/api/history', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify(item)
});
if (!response.ok) throw new Error('API Error: ' + response.status);
return await response.json();
} catch (e) {
console.error('Failed to save history:', e);
throw e;
}
},
async delete(id) {
try {
const response = await fetch(`/api/history?id=${encodeURIComponent(id)}`, {
method: 'DELETE'
});
if (!response.ok) throw new Error('API Error: ' + response.status);
return await response.json();
} catch (e) {
console.error('Failed to delete history:', e);
throw e;
}
},
async clear() {
// Clearing all history not implemented in API yet for safety,
// but could be added if needed.
console.warn('Clear all not supported via API currently.');
return Promise.resolve();
},
async getLimit(limit = 50) {
const all = await this.getAll();
return all.slice(0, limit);
},
async enforceLimit(limit = 50) {
// Enforce limit is handled by the server-side API automatically
return Promise.resolve();
}
};
window.dbService = dbService;