Added optionnal online storage and cross-client sync
This commit is contained in:
@@ -49,6 +49,11 @@ import { DAY, HOUR, localDateKey } from './time.js';
|
||||
* days: import('dexie').Table<DayRecord, string>,
|
||||
* entries: import('dexie').Table<EntryRecord, number>,
|
||||
* settings: import('dexie').Table<SettingsRecord, string>,
|
||||
* syncState: import('dexie').Table<any, string>,
|
||||
* syncObjects: import('dexie').Table<any, string>,
|
||||
* syncVersions: import('dexie').Table<any, string>,
|
||||
* syncOutbox: import('dexie').Table<any, string>,
|
||||
* syncConflicts: import('dexie').Table<any, string>,
|
||||
* }} TimeLogDB
|
||||
*/
|
||||
|
||||
@@ -67,6 +72,38 @@ db.version(2).stores({
|
||||
settings: 'key',
|
||||
});
|
||||
|
||||
db.version(3).stores({
|
||||
syncState: 'key', syncObjects: 'key', syncVersions: 'id, key',
|
||||
syncOutbox: 'id', syncConflicts: 'key',
|
||||
});
|
||||
|
||||
// Journal every local mutation in its native IndexedDB transaction, including
|
||||
// direct writes by editing, automation, import and purge. The durable source
|
||||
// rows + last captured values are the outbox until revisions are materialised.
|
||||
db.use({
|
||||
stack: 'dbcore', name: 'sync-dirty-journal',
|
||||
create(core) {
|
||||
return {
|
||||
...core,
|
||||
transaction(stores, mode, options) {
|
||||
const names = mode === 'readwrite' && stores.some(s => ['days', 'entries', 'settings'].includes(s))
|
||||
? [...new Set([...stores, 'syncState'])] : stores;
|
||||
return core.transaction(names, mode, options);
|
||||
},
|
||||
table(name) {
|
||||
const table = core.table(name);
|
||||
if (!['days', 'entries', 'settings'].includes(name)) return table;
|
||||
return { ...table, async mutate(req) {
|
||||
const result = await table.mutate(req);
|
||||
const marker = await core.table('syncState').mutate({ type: 'put', trans: req.trans, values: [{ key: 'dirty', value: true }] });
|
||||
if (marker.numFailures) throw marker.failures[0];
|
||||
return result;
|
||||
} };
|
||||
},
|
||||
};
|
||||
},
|
||||
});
|
||||
|
||||
/* ------------------------------------------------------------------ settings */
|
||||
|
||||
export async function readSettings() {
|
||||
|
||||
Reference in New Issue
Block a user