Files
myprojplanet_vite/src/services/idb.js
Surya Paolo a51bc5a8a2 - aggiornamenti guida RIS, FAQ
- Editor HTML aggiunto CSS e Script
- Statistiche
- CRISBalanceBar
- Inizio Sync... (ma disattivato)
2025-12-02 22:16:24 +01:00

69 lines
1.8 KiB
JavaScript

// src/services/idb.js (nuovo, usa storage.js esistente)
import { idbKeyval } from '@/storage';
class SyncIDB {
constructor() {
this.tables = ['resps', 'workers', 'groups', 'mygroups', 'products', 'cart', 'orderscart'];
}
// Sostituisci intera tabella
async replaceTable(tableName, records) {
await idbKeyval.clearalldata(tableName);
for (const record of records) {
await idbKeyval.setdata(tableName, record);
}
}
// Merge records modificati
async mergeRecords(tableName, records) {
for (const record of records) {
if (record.deleted === true) {
// Trova BOMID e cancella
const existing = await this.findByMongoId(tableName, record._id);
if (existing) {
await idbKeyval.deletedata(tableName, existing.BOMID);
}
} else {
// Controlla se esiste già
const existing = await this.findByMongoId(tableName, record._id);
if (existing) {
// Aggiorna (mantieni BOMID)
record.BOMID = existing.BOMID;
await idbKeyval.setdata(tableName, record);
} else {
// Inserisci nuovo
await idbKeyval.setdata(tableName, record);
}
}
}
}
// Trova record per _id MongoDB
async findByMongoId(tableName, mongoId) {
const allRecords = await idbKeyval.getalldata(tableName);
return allRecords.find(r => r._id === mongoId);
}
// Ottieni tutti i record
async getTable(tableName) {
return await idbKeyval.getalldata(tableName);
}
// Timestamp ultima sync
async getLastSync(tableName) {
const meta = await idbKeyval.getdata('metadata', `${tableName}_lastSync`);
return meta?.value || 0;
}
async setLastSync(tableName, timestamp) {
await idbKeyval.setdata('metadata', {
BOMID: `${tableName}_lastSync`,
value: timestamp
});
}
}
export default new SyncIDB();