import Dexie from 'dexie' export const db = new Dexie('turnpike') db.version(1).stores({ session: 'id, token, user', meta: 'key', event: 'id', attendees: 'id, name, ticket_type, checked_in, deleted_at', departments: 'id, name, deleted_at', volunteers: 'id, name, department_id, checked_in, attendee_id, deleted_at', shifts: 'id, department_id, day, deleted_at', volunteer_shifts: '[volunteer_id+shift_id], volunteer_id, shift_id', outbox: '++id, table, op, synced_at', }) db.version(2).stores({ session: 'id, token, user', meta: 'key', event: 'id', attendees: 'id, name, ticket_type, checked_in, volunteer_token, deleted_at', departments: 'id, name, deleted_at', volunteers: 'id, name, department_id, checked_in, attendee_id, deleted_at', shifts: 'id, department_id, day, position, deleted_at', volunteer_shifts: '[volunteer_id+shift_id], volunteer_id, shift_id', outbox: '++id, table, op, synced_at', }) db.version(3).stores({ session: 'id, token, user', meta: 'key', event: 'id', attendees: 'id, name, ticket_type, checked_in, volunteer_token, deleted_at', participants: 'id, email, preferred_name, updated_at, deleted_at', tickets: 'id, participant_id, code, source, checked_in_at, updated_at, deleted_at', departments: 'id, name, deleted_at', volunteers: 'id, name, department_id, checked_in, attendee_id, participant_id, deleted_at', shifts: 'id, department_id, day, position, deleted_at', volunteer_shifts: '[volunteer_id+shift_id], volunteer_id, shift_id', outbox: '++id, table, op, synced_at', }) db.version(4).stores({ attendees: null, outbox: null, volunteers: 'id, name, department_id, checked_in, participant_id, deleted_at', }) db.version(5).stores({ volunteers: 'id, participant_id, department_id, deleted_at', participants: 'id, email, preferred_name, email_confirmed, updated_at, deleted_at', }) db.version(6).stores({}).upgrade(async tx => { await tx.table('session').clear() await tx.table('meta').clear() await tx.table('participants').clear() await tx.table('tickets').clear() await tx.table('departments').clear() await tx.table('volunteers').clear() await tx.table('shifts').clear() await tx.table('volunteer_shifts').clear() }) export async function getLastSync() { const m = await db.meta.get('last_sync') return m?.value ?? '' } export async function setLastSync(ts) { await db.meta.put({ key: 'last_sync', value: ts }) } export async function getSession() { return db.session.get(1) } export async function saveSession(token, user) { await db.session.put({ id: 1, token, user }) } export async function clearSession() { await db.session.clear() await db.meta.clear() }